blob: ee13f29467f2f09494fef880e54a89d901c1792d [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();
Joe Onorato41a12d22009-10-31 18:30:00 -0400405 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800406 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700407
Daniel Sandlerff02d492013-08-05 02:12:05 -0400408 mStats = new Stats(this);
409
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700410 mAppWidgetManager = AppWidgetManager.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700411
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700412 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
413 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700414
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700415 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
416 // this also ensures that any synchronous binding below doesn't re-trigger another
417 // LauncherModel load.
418 mPaused = false;
419
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800420 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200421 android.os.Debug.startMethodTracing(
422 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800423 }
424
Adam Cohen53805212013-10-01 10:39:23 -0700425
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800426 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800427 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700428
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800429 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100430 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800431
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800432 registerContentObservers();
433
Joe Onorato7c312c12009-08-13 21:36:53 -0700434 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700435
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800436 mSavedState = savedInstanceState;
437 restoreState(mSavedState);
438
Winson Chunga12a2502010-12-20 14:41:35 -0800439 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700440 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200441 mAppsCustomizeContent.onPackagesUpdated(
442 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chung785d2eb2011-04-14 16:08:02 -0700443 }
Winson Chunga12a2502010-12-20 14:41:35 -0800444
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800445 if (PROFILE_STARTUP) {
446 android.os.Debug.stopMethodTracing();
447 }
448
449 if (!mRestoring) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700450 if (sPausedFromUserAction) {
451 // If the user leaves launcher, then we should just load items asynchronously when
452 // they return.
453 mModel.startLoader(true, -1);
454 } else {
455 // We only load the page synchronously if the user rotates (or triggers a
456 // configuration change) while launcher is in the foreground
457 mModel.startLoader(true, mWorkspace.getCurrentPage());
458 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800459 }
460
461 // For handling default keys
462 mDefaultKeySsb = new SpannableStringBuilder();
463 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800464
465 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
466 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800467
Adam Cohenf9426d52012-06-04 17:26:21 -0700468 updateGlobalIcons();
469
470 // On large interfaces, we want the screen to auto-rotate based on the current orientation
471 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700472
473 showFirstRunCling();
Adam Cohenf9426d52012-06-04 17:26:21 -0700474 }
475
Winson Chung4a2afa32012-07-19 14:53:05 -0700476 protected void onUserLeaveHint() {
477 super.onUserLeaveHint();
478 sPausedFromUserAction = true;
479 }
480
Winson Chung98ca0f72013-07-29 12:58:51 -0700481 /** To be overriden by subclasses to hint to Launcher that we have custom content */
482 protected boolean hasCustomContentToLeft() {
483 return false;
484 }
485
Dave Hawkeya8881582013-09-17 15:55:33 -0600486 /**
487 * To be overridden by subclasses to create the custom content and call
488 * {@link #addToCustomContentPage}. This will only be invoked if
489 * {@link #hasCustomContentToLeft()} is {@code true}.
490 */
491 protected void addCustomContentToLeft() {
492 }
493
494 /**
495 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
496 * ensure the custom content page is added or removed if necessary.
497 */
498 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600499 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600500 // Not bound yet, wait for bindScreens to be called.
501 return;
502 }
503
504 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
505 // Create the custom content page and call the subclass to populate it.
506 mWorkspace.createCustomContentPage();
507 addCustomContentToLeft();
508 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
509 mWorkspace.removeCustomContentPage();
510 }
511 }
512
Adam Cohenf9426d52012-06-04 17:26:21 -0700513 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700514 boolean searchVisible = false;
515 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800516 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700517 int coi = getCurrentOrientationIndexForGlobalIcons();
518 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
519 sAppMarketIcon[coi] == null) {
Winson Chungd64a6662013-09-30 11:06:59 -0700520 if (!DISABLE_MARKET_BUTTON) {
521 updateAppMarketIcon();
522 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700523 searchVisible = updateGlobalSearchIcon();
524 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700525 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700526 if (sGlobalSearchIcon[coi] != null) {
527 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700528 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700529 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700530 if (sVoiceSearchIcon[coi] != null) {
531 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700532 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700533 }
Winson Chungd64a6662013-09-30 11:06:59 -0700534 if (!DISABLE_MARKET_BUTTON && sAppMarketIcon[coi] != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -0700535 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800536 }
Winson Chungadf0c182012-08-23 14:59:07 -0700537 if (mSearchDropTargetBar != null) {
538 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
539 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800540 }
Romain Guycbb89e42009-06-08 15:52:54 -0700541
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800542 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700543 if (sLocaleConfiguration == null) {
544 new AsyncTask<Void, Void, LocaleConfiguration>() {
545 @Override
546 protected LocaleConfiguration doInBackground(Void... unused) {
547 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
548 readConfiguration(Launcher.this, localeConfiguration);
549 return localeConfiguration;
550 }
551
552 @Override
553 protected void onPostExecute(LocaleConfiguration result) {
554 sLocaleConfiguration = result;
555 checkForLocaleChange(); // recursive, but now with a locale configuration
556 }
557 }.execute();
558 return;
559 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700560
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800561 final Configuration configuration = getResources().getConfiguration();
562
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700563 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800564 final String locale = configuration.locale.toString();
565
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700566 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800567 final int mcc = configuration.mcc;
568
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700569 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800570 final int mnc = configuration.mnc;
571
Romain Guy84f296c2009-11-04 15:00:44 -0800572 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800573
Romain Guy84f296c2009-11-04 15:00:44 -0800574 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700575 sLocaleConfiguration.locale = locale;
576 sLocaleConfiguration.mcc = mcc;
577 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700578
Joe Onorato0589f0f2010-02-08 13:44:00 -0800579 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700580
581 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
582 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700583 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700584 public void run() {
585 writeConfiguration(Launcher.this, localeConfiguration);
586 }
587 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700588 }
589 }
590
591 private static class LocaleConfiguration {
592 public String locale;
593 public int mcc = -1;
594 public int mnc = -1;
595 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700596
Romain Guy98d01652009-06-30 16:21:04 -0700597 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
598 DataInputStream in = null;
599 try {
600 in = new DataInputStream(context.openFileInput(PREFERENCES));
601 configuration.locale = in.readUTF();
602 configuration.mcc = in.readInt();
603 configuration.mnc = in.readInt();
604 } catch (FileNotFoundException e) {
605 // Ignore
606 } catch (IOException e) {
607 // Ignore
608 } finally {
609 if (in != null) {
610 try {
611 in.close();
612 } catch (IOException e) {
613 // Ignore
614 }
615 }
616 }
617 }
618
619 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
620 DataOutputStream out = null;
621 try {
622 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
623 out.writeUTF(configuration.locale);
624 out.writeInt(configuration.mcc);
625 out.writeInt(configuration.mnc);
626 out.flush();
627 } catch (FileNotFoundException e) {
628 // Ignore
629 } catch (IOException e) {
630 //noinspection ResultOfMethodCallIgnored
631 context.getFileStreamPath(PREFERENCES).delete();
632 } finally {
633 if (out != null) {
634 try {
635 out.close();
636 } catch (IOException e) {
637 // Ignore
638 }
639 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800640 }
641 }
642
Anton Hanssona2f665f2013-09-26 12:18:32 +0100643 public Stats getStats() {
644 return mStats;
645 }
646
Bjorn Bringertc459e522013-06-07 19:36:01 +0100647 public LayoutInflater getInflater() {
648 return mInflater;
649 }
650
Adam Cohen716b51e2011-06-30 12:09:54 -0700651 public DragLayer getDragLayer() {
652 return mDragLayer;
653 }
654
Winson Chung36a62fe2012-05-06 18:04:42 -0700655 boolean isDraggingEnabled() {
656 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
657 // that is subsequently removed from the workspace in startBinding().
658 return !mModel.isLoadingWorkspace();
659 }
660
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800661 static int getScreen() {
662 synchronized (sLock) {
663 return sScreen;
664 }
665 }
666
667 static void setScreen(int screen) {
668 synchronized (sLock) {
669 sScreen = screen;
670 }
671 }
672
Winson Chung557d6ed2011-07-08 15:34:52 -0700673 /**
674 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
675 * a configuration step, this allows the proper animations to run after other transitions.
676 */
677 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700678 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800679 switch (args.requestCode) {
680 case REQUEST_PICK_APPLICATION:
Adam Cohendcd297f2013-06-18 13:13:40 -0700681 completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700682 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800683 break;
684 case REQUEST_PICK_SHORTCUT:
685 processShortcut(args.intent);
686 break;
687 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700688 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700689 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700690 result = true;
691 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800692 case REQUEST_CREATE_APPWIDGET:
693 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700694 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700695 result = true;
696 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800697 }
Michael Jurka27614d22012-04-02 06:40:22 -0700698 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
699 // if you turned the screen off and then back while in All Apps, Launcher would not
700 // return to the workspace. Clearing mAddInfo.container here fixes this issue
701 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700702 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800703 }
704
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800705 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700706 protected void onActivityResult(
707 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700708 // Reset the startActivity waiting flag
709 mWaitingForResult = false;
710
Michael Jurka8b805b12012-04-18 14:23:14 -0700711 if (requestCode == REQUEST_BIND_APPWIDGET) {
712 int appWidgetId = data != null ?
713 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
714 if (resultCode == RESULT_CANCELED) {
715 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
716 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700717 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700718 }
719 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200720 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
721 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
722 mWorkspace.exitOverviewMode(false);
723 }
724 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700725 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200726
Winson Chung557d6ed2011-07-08 15:34:52 -0700727 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800728 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
729 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700730
Adam Cohened66b2b2012-01-23 17:28:51 -0800731 // We have special handling for widgets
732 if (isWidgetDrop) {
733 int appWidgetId = data != null ?
734 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700735 if (appWidgetId < 0) {
736 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
737 "widget configuration activity.");
738 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen00074722013-10-11 17:46:09 -0700739 mWorkspace.stripEmptyScreens();
Winson Chung5aaab772012-04-27 15:24:02 -0700740 } else {
741 completeTwoStageWidgetDrop(resultCode, appWidgetId);
742 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800743 return;
744 }
745
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800746 // The pattern used here is that a user PICKs a specific application,
747 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700748
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800749 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
750 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700751 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800752 final PendingAddArguments args = new PendingAddArguments();
753 args.requestCode = requestCode;
754 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700755 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700756 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700757 args.cellX = mPendingAddInfo.cellX;
758 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800759 if (isWorkspaceLocked()) {
760 sPendingAddList.add(args);
761 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700762 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800763 }
Adam Cohen00074722013-10-11 17:46:09 -0700764 } else if (resultCode == RESULT_CANCELED) {
765 mWorkspace.stripEmptyScreens();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800766 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800767 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700768 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800769 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
770 null);
771 }
772
773 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700774 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700775 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800776 Runnable onCompleteRunnable = null;
777 int animationType = 0;
778
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700779 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800780 if (resultCode == RESULT_OK) {
781 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
782 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700783 mPendingAddWidgetInfo);
784 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800785 onCompleteRunnable = new Runnable() {
786 @Override
787 public void run() {
788 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700789 mPendingAddInfo.screenId, layout, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800790 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
791 null);
792 }
793 };
794 } else if (resultCode == RESULT_CANCELED) {
795 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
796 onCompleteRunnable = new Runnable() {
797 @Override
798 public void run() {
799 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
800 null);
801 }
802 };
803 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700804 if (mDragLayer.getAnimatedView() != null) {
805 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
806 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
807 animationType, boundWidget, true);
808 } else {
809 // The animated view may be null in the case of a rotation during widget configuration
810 onCompleteRunnable.run();
811 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800812 }
813
814 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700815 protected void onStop() {
816 super.onStop();
817 FirstFrameAnimatorHelper.setIsVisible(false);
818 }
819
820 @Override
821 protected void onStart() {
822 super.onStart();
823 FirstFrameAnimatorHelper.setIsVisible(true);
824 }
825
826 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800827 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200828 long startTime = 0;
829 if (DEBUG_RESUME_TIME) {
830 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400831 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200832 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800833 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700834
Winson Chung4a2afa32012-07-19 14:53:05 -0700835 // Restore the previous launcher state
836 if (mOnResumeState == State.WORKSPACE) {
837 showWorkspace(false);
838 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chung82963d52013-10-09 11:20:57 -0700839 showAllApps(false, AppsCustomizePagedView.ContentType.Applications, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700840 }
841 mOnResumeState = State.NONE;
842
Craig Mautner360310b2012-10-26 15:13:08 -0700843 // Background was set to gradient in onPause(), restore to black if in all apps.
844 setWorkspaceBackground(mState == State.WORKSPACE);
845
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800846 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700847 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700848 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400849 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700850 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400851 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700852 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800853 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700854 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200855 // We might have postponed some bind calls until onResume (see waitUntilResume) --
856 // execute them here
857 long startTimeCallbacks = 0;
858 if (DEBUG_RESUME_TIME) {
859 startTimeCallbacks = System.currentTimeMillis();
860 }
861
862 if (mAppsCustomizeContent != null) {
863 mAppsCustomizeContent.setBulkBind(true);
864 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700865 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
866 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200867 }
868 if (mAppsCustomizeContent != null) {
869 mAppsCustomizeContent.setBulkBind(false);
870 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700871 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200872 if (DEBUG_RESUME_TIME) {
873 Log.d(TAG, "Time spent processing callbacks in onResume: " +
874 (System.currentTimeMillis() - startTimeCallbacks));
875 }
Michael Jurka447bf842013-05-15 14:52:15 +0200876 }
Michael Jurka54554252013-08-01 12:52:23 +0200877 if (mOnResumeCallbacks.size() > 0) {
878 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
879 mOnResumeCallbacks.get(i).run();
880 }
881 mOnResumeCallbacks.clear();
882 }
Winson Chunge4e50662012-01-23 14:45:13 -0800883
884 // Reset the pressed state of icons that were locked in the press state while activities
885 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800886 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800887 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800888 mWaitingForResume.setStayPressed(false);
889 }
Winson Chunge4e50662012-01-23 14:45:13 -0800890 if (mAppsCustomizeContent != null) {
891 // Resets the previous all apps icon press state
892 mAppsCustomizeContent.resetDrawableState();
893 }
Winson Chung82963d52013-10-09 11:20:57 -0700894
Adam Cohen06dff352012-06-01 17:17:08 -0700895 // It is possible that widgets can receive updates while launcher is not in the foreground.
896 // Consequently, the widgets will be inflated in the orientation of the foreground activity
897 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
898 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700899 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700900
Winson Chung780fe592013-09-26 14:48:44 -0700901 // Process any items that were added while Launcher was away.
902 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
903
Winson Chung5841efa2013-09-30 18:06:44 -0700904 // Update the voice search button proxy
905 updateVoiceButtonProxyVisible(false);
906
Adam Cohenf9426d52012-06-04 17:26:21 -0700907 // Again, as with the above scenario, it's possible that one or more of the global icons
908 // were updated in the wrong orientation.
909 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200910 if (DEBUG_RESUME_TIME) {
911 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
912 }
Adam Cohen6fecd412013-10-02 17:41:50 -0700913
914 if (mWorkspace.getCustomContentCallbacks() != null) {
915 // If we are resuming and the custom content is the current page, we call onShow().
916 // It is also poassible that onShow will instead be called slightly after first layout
917 // if PagedView#setRestorePage was set to the custom content page in onCreate().
918 if (mWorkspace.isOnOrMovingToCustomContent()) {
919 mWorkspace.getCustomContentCallbacks().onShow();
920 }
921 }
Adam Cohenedaaa302013-10-01 17:33:27 -0700922 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -0700923 mWorkspace.onResume();
Adam Cohen06dff352012-06-01 17:17:08 -0700924 }
925
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800926 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700927 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700928 // Ensure that items added to Launcher are queued until Launcher returns
929 InstallShortcutReceiver.enableInstallQueue();
930
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700931 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700932 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800933 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700934 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700935
936 // We call onHide() aggressively. The custom content callbacks should be able to
937 // debounce excess onHide calls.
938 if (mWorkspace.getCustomContentCallbacks() != null) {
939 mWorkspace.getCustomContentCallbacks().onHide();
940 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700941 }
Romain Guycbb89e42009-06-08 15:52:54 -0700942
Adam Cohen66a01fd2013-06-11 12:48:00 -0700943 protected void onFinishBindingItems() {
Adam Cohen82398362013-09-29 12:57:34 -0700944 if (mWorkspace != null && hasCustomContentToLeft() && mWorkspace.hasCustomContent()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600945 addCustomContentToLeft();
946 }
Adam Cohen66a01fd2013-06-11 12:48:00 -0700947 }
948
Adam Cohenbffe7452013-07-22 18:21:45 -0700949 QSBScroller mQsbScroller = new QSBScroller() {
950 int scrollY = 0;
951
952 @Override
953 public void setScrollY(int scroll) {
954 scrollY = scroll;
955
956 if (mWorkspace.isOnOrMovingToCustomContent()) {
957 mSearchDropTargetBar.setTranslationY(- scrollY);
Mark Brophyd7dc6812013-09-20 17:27:32 +0100958 getQsbBar().setTranslationY(-scrollY);
Adam Cohenbffe7452013-07-22 18:21:45 -0700959 }
960 }
961 };
962
963 public void resetQSBScroll() {
964 mSearchDropTargetBar.animate().translationY(0).start();
Mark Brophyd7dc6812013-09-20 17:27:32 +0100965 getQsbBar().animate().translationY(0).start();
Adam Cohenbffe7452013-07-22 18:21:45 -0700966 }
967
968 public interface CustomContentCallbacks {
969 // Custom content is completely shown
970 public void onShow();
971
972 // Custom content is completely hidden
973 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -0700974
975 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
976 public void onScrollProgressChanged(float progress);
Adam Cohenbffe7452013-07-22 18:21:45 -0700977 }
978
Adam Cohen30bacb22013-08-29 14:25:25 -0700979 protected void startSettings() {
980 }
981
Adam Cohenbffe7452013-07-22 18:21:45 -0700982 public interface QSBScroller {
983 public void setScrollY(int scrollY);
984 }
985
Winson Chung98ca0f72013-07-29 12:58:51 -0700986 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -0700987 CustomContentCallbacks callbacks, String description) {
988 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohenbffe7452013-07-22 18:21:45 -0700989 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -0700990 }
991
Adam Cohenedb40762013-07-18 16:45:45 -0700992 // The custom content needs to offset its content to account for the QSB
993 public int getTopOffsetForCustomContent() {
994 return mWorkspace.getPaddingTop();
995 }
996
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700997 @Override
998 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400999 // Flag the loader to stop early before switching
1000 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -07001001 if (mAppsCustomizeContent != null) {
1002 mAppsCustomizeContent.surrender();
1003 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001004 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001005 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001006
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001007 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001008 @Override
1009 public void onWindowFocusChanged(boolean hasFocus) {
1010 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001011 mHasFocus = hasFocus;
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001012 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001013
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001014 private boolean acceptFilter() {
1015 final InputMethodManager inputManager = (InputMethodManager)
1016 getSystemService(Context.INPUT_METHOD_SERVICE);
1017 return !inputManager.isFullscreenMode();
1018 }
1019
1020 @Override
1021 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001022 final int uniChar = event.getUnicodeChar();
1023 final boolean handled = super.onKeyDown(keyCode, event);
1024 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1025 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001026 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1027 keyCode, event);
1028 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001029 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001030 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001031 // showSearchDialog()
1032 // If there are multiple keystrokes before the search dialog takes focus,
1033 // onSearchRequested() will be called for every keystroke,
1034 // but it is idempotent, so it's fine.
1035 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001036 }
1037 }
1038
Joe Onorato8a9625e2010-01-28 15:55:35 -08001039 // Eat the long press event so the keyboard doesn't come up.
1040 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1041 return true;
1042 }
1043
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001044 return handled;
1045 }
1046
Karl Rosaen138a0412009-04-23 19:00:21 -07001047 private String getTypedText() {
1048 return mDefaultKeySsb.toString();
1049 }
1050
1051 private void clearTypedText() {
1052 mDefaultKeySsb.clear();
1053 mDefaultKeySsb.clearSpans();
1054 Selection.setSelection(mDefaultKeySsb, 0);
1055 }
1056
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001057 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001058 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1059 * State
1060 */
1061 private static State intToState(int stateOrdinal) {
1062 State state = State.WORKSPACE;
1063 final State[] stateValues = State.values();
1064 for (int i = 0; i < stateValues.length; i++) {
1065 if (stateValues[i].ordinal() == stateOrdinal) {
1066 state = stateValues[i];
1067 break;
1068 }
1069 }
1070 return state;
1071 }
1072
1073 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001074 * Restores the previous state, if it exists.
1075 *
1076 * @param savedState The previous state.
1077 */
1078 private void restoreState(Bundle savedState) {
1079 if (savedState == null) {
1080 return;
1081 }
1082
Michael Jurka883f55b2010-10-21 15:47:14 -07001083 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001084 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001085 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001086 }
1087
Adam Cohen21cd0022013-10-09 18:57:02 -07001088 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1089 PagedView.INVALID_RESTORE_PAGE);
1090 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001091 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001092 }
1093
Winson Chung3d503fb2011-07-13 17:25:49 -07001094 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001095 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001096
Winson Chung3d503fb2011-07-13 17:25:49 -07001097 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1098 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001099 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001100 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1101 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001102 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1103 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1104 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -07001105 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001106 mRestoring = true;
1107 }
1108
1109 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1110 if (renameFolder) {
1111 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001112 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001113 mRestoring = true;
1114 }
Winson Chunga12a2502010-12-20 14:41:35 -08001115
Winson Chung785d2eb2011-04-14 16:08:02 -07001116 // Restore the AppsCustomize tab
1117 if (mAppsCustomizeTabHost != null) {
1118 String curTab = savedState.getString("apps_customize_currentTab");
1119 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001120 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001121 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001122 mAppsCustomizeContent.loadAssociatedPages(
1123 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001124 }
1125
Winson Chung5afbf7b2011-07-25 11:53:08 -07001126 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1127 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001128 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001129 }
1130
1131 /**
1132 * Finds all the views we need and configure them properly.
1133 */
1134 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001135 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001136
Craig Mautner360310b2012-10-26 15:13:08 -07001137 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001138 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1139 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -07001140
John Spurlock77e1f472013-09-11 10:09:51 -04001141 mLauncherView.setSystemUiVisibility(
1142 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001143 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001144
Winson Chung4c98d922011-05-31 16:50:48 -07001145 // Setup the drag layer
1146 mDragLayer.setup(this, dragController);
1147
Winson Chung3d503fb2011-07-13 17:25:49 -07001148 // Setup the hotseat
1149 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1150 if (mHotseat != null) {
1151 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001152 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001153 }
1154
Adam Cohenf358a4b2013-07-23 16:47:31 -07001155 mOverviewPanel = findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001156 View widgetButton = findViewById(R.id.widget_button);
1157 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001158 @Override
1159 public void onClick(View arg0) {
Winson Chung82963d52013-10-09 11:20:57 -07001160 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001161 }
1162 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001163 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1164
1165 View wallpaperButton = findViewById(R.id.wallpaper_button);
1166 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001167 @Override
1168 public void onClick(View arg0) {
1169 startWallpaper();
1170 }
1171 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001172 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1173
1174 View settingsButton = findViewById(R.id.settings_button);
1175 settingsButton.setOnClickListener(new OnClickListener() {
Adam Cohen30bacb22013-08-29 14:25:25 -07001176 @Override
1177 public void onClick(View arg0) {
1178 startSettings();
1179 }
1180 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001181 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
Adam Cohendbdff6b2013-09-18 19:09:15 -07001182 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001183
Winson Chung4c98d922011-05-31 16:50:48 -07001184 // Setup the workspace
1185 mWorkspace.setHapticFeedbackEnabled(false);
1186 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001187 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001188 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001189
Winson Chungf0ea4d32011-06-06 14:27:16 -07001190 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001191 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001192
Winson Chungf0ea4d32011-06-06 14:27:16 -07001193 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001194 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001195 mAppsCustomizeContent = (AppsCustomizePagedView)
1196 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1197 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001198
Winson Chung3d503fb2011-07-13 17:25:49 -07001199 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001200 dragController.setDragScoller(mWorkspace);
1201 dragController.setScrollView(mDragLayer);
1202 dragController.setMoveTarget(mWorkspace);
1203 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001204 if (mSearchDropTargetBar != null) {
1205 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001206 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001207
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001208 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001209 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001210 mWeightWatcher = new WeightWatcher(this);
1211 mWeightWatcher.setAlpha(0.5f);
1212 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001213 new FrameLayout.LayoutParams(
1214 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001215 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001216 Gravity.BOTTOM)
1217 );
Adam Cohen39a06042013-07-19 14:30:12 -07001218
1219 boolean show = shouldShowWeightWatcher();
1220 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001221 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001222 }
1223
1224 /**
1225 * Creates a view representing a shortcut.
1226 *
1227 * @param info The data structure describing the shortcut.
1228 *
1229 * @return A View inflated from R.layout.application.
1230 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001231 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001232 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001233 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001234 }
1235
1236 /**
1237 * Creates a view representing a shortcut inflated from the specified resource.
1238 *
1239 * @param layoutResId The id of the XML layout used to create the shortcut.
1240 * @param parent The group the shortcut belongs to.
1241 * @param info The data structure describing the shortcut.
1242 *
1243 * @return A View inflated from layoutResId.
1244 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001245 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001246 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1247 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001248 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001249 return favorite;
1250 }
1251
1252 /**
1253 * Add an application shortcut to the workspace.
1254 *
1255 * @param data The intent describing the application.
1256 * @param cellInfo The position on screen where to create the shortcut.
1257 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001258 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001259 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001260 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001261
Adam Cohenfbba09b2011-07-18 21:43:05 -07001262 // First we check if we already know the exact location where we want to add this item.
1263 if (cellX >= 0 && cellY >= 0) {
1264 cellXY[0] = cellX;
1265 cellXY[1] = cellY;
1266 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001267 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001268 return;
1269 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001270
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001271 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001272
Romain Guy73b979d2009-06-09 12:57:21 -07001273 if (info != null) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07001274 info.setActivity(this, data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001275 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001276 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001277 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001278 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001279 } else {
1280 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001281 }
1282 }
Romain Guycbb89e42009-06-08 15:52:54 -07001283
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001284 /**
1285 * Add a shortcut to the workspace.
1286 *
1287 * @param data The intent describing the shortcut.
1288 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001289 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001290 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001291 int cellY) {
1292 int[] cellXY = mTmpAddItemCellCoordinates;
1293 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001294 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001295
Michael Jurkad3ef3062010-11-23 16:23:58 -08001296 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001297
Adam Cohen558baaf2011-08-15 15:22:57 -07001298 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001299 if (info == null) {
1300 return;
1301 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001302 final View view = createShortcut(info);
1303
Adam Cohenfbba09b2011-07-18 21:43:05 -07001304 // First we check if we already know the exact location where we want to add this item.
1305 if (cellX >= 0 && cellY >= 0) {
1306 cellXY[0] = cellX;
1307 cellXY[1] = cellY;
1308 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001309
1310 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001311 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001312 true, null,null)) {
1313 return;
1314 }
1315 DragObject dragObject = new DragObject();
1316 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001317 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1318 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001319 return;
1320 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001321 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001322 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001323 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001324 foundCellSpan = (result != null);
1325 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001326 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001327 }
1328
1329 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001330 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001331 return;
1332 }
1333
Adam Cohendcd297f2013-06-18 13:13:40 -07001334 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001335
1336 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001337 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001338 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001339 }
1340 }
1341
Adam Cohen2f093b62012-04-30 18:59:53 -07001342 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1343 int minHeight) {
1344 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001345 // We want to account for the extra amount of padding that we are adding to the widget
1346 // to ensure that it gets the full amount of space that it has requested
1347 int requiredWidth = minWidth + padding.left + padding.right;
1348 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001349 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001350 }
1351
Adam Cohen2f093b62012-04-30 18:59:53 -07001352 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1353 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001354 }
1355
Adam Cohen2f093b62012-04-30 18:59:53 -07001356 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1357 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001358 }
1359
Adam Cohen2f093b62012-04-30 18:59:53 -07001360 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1361 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001362 }
1363
Adam Cohen2f093b62012-04-30 18:59:53 -07001364 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1365 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001366 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001367 }
1368
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001369 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001370 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001371 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001372 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001373 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001374 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001375 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001376 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1377 if (appWidgetInfo == null) {
1378 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1379 }
Romain Guycbb89e42009-06-08 15:52:54 -07001380
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001381 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001382 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001383
Adam Cohen2f093b62012-04-30 18:59:53 -07001384 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1385 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001386
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001387 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001388 // We have saved the position to which the widget was dragged-- this really only matters
1389 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001390 int[] cellXY = mTmpAddItemCellCoordinates;
1391 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001392 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001393 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001394 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1395 cellXY[0] = mPendingAddInfo.cellX;
1396 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001397 spanXY[0] = mPendingAddInfo.spanX;
1398 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001399 foundCellSpan = true;
1400 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001401 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001402 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001403 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1404 spanXY[1], cellXY, finalSpan);
1405 spanXY[0] = finalSpan[0];
1406 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001407 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001408 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001409 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001410 }
1411
Michael Jurka0280c3b2010-09-17 15:00:07 -07001412 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001413 if (appWidgetId != -1) {
1414 // Deleting an app widget ID is a void call but writes to disk before returning
1415 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001416 new Thread("deleteAppWidgetId") {
1417 public void run() {
1418 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1419 }
1420 }.start();
1421 }
Winson Chung93eef082012-03-23 15:59:27 -07001422 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001423 return;
1424 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001425
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001426 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001427 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1428 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001429 launcherInfo.spanX = spanXY[0];
1430 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001431 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1432 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001433
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001434 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001435 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001436
1437 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001438 if (hostView == null) {
1439 // Perform actual inflation because we're live
1440 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1441 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1442 } else {
1443 // The AppWidgetHostView has already been inflated and instantiated
1444 launcherInfo.hostView = hostView;
1445 }
Romain Guycbb89e42009-06-08 15:52:54 -07001446
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001447 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001448 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001449 launcherInfo.notifyWidgetSizeChanged(this);
1450
Adam Cohendcd297f2013-06-18 13:13:40 -07001451 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001452 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001453
1454 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001455 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001456 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001457 }
Romain Guycbb89e42009-06-08 15:52:54 -07001458
Adam Cohended9f8d2010-11-03 13:25:16 -07001459 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1460 @Override
1461 public void onReceive(Context context, Intent intent) {
1462 final String action = intent.getAction();
1463 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1464 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001465 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001466 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001467
Winson Chungc100e8e2011-08-09 16:02:43 -07001468 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001469 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001470 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001471 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001472 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001473 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1474 mUserPresent = true;
1475 updateRunning();
1476 }
1477 }
1478 };
1479
1480 @Override
1481 public void onAttachedToWindow() {
1482 super.onAttachedToWindow();
1483
1484 // Listen for broadcasts related to user-presence
1485 final IntentFilter filter = new IntentFilter();
1486 filter.addAction(Intent.ACTION_SCREEN_OFF);
1487 filter.addAction(Intent.ACTION_USER_PRESENT);
1488 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001489 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001490 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001491 mVisible = true;
1492 }
1493
1494 @Override
1495 public void onDetachedFromWindow() {
1496 super.onDetachedFromWindow();
1497 mVisible = false;
1498
Adam Cohend113e0c2010-11-11 10:48:05 -08001499 if (mAttached) {
1500 unregisterReceiver(mReceiver);
1501 mAttached = false;
1502 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001503 updateRunning();
1504 }
1505
1506 public void onWindowVisibilityChanged(int visibility) {
1507 mVisible = visibility == View.VISIBLE;
1508 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001509 // The following code used to be in onResume, but it turns out onResume is called when
1510 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1511 // is a more appropriate event to handle
1512 if (mVisible) {
1513 mAppsCustomizeTabHost.onWindowVisible();
1514 if (!mWorkspaceLoading) {
1515 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001516 // We want to let Launcher draw itself at least once before we force it to build
1517 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001518 // apps is nice and speedy.
1519 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001520 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001521 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001522 if (mStarted) return;
1523 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001524 // We delay the layer building a bit in order to give
1525 // other message processing a time to run. In particular
1526 // this avoids a delay in hiding the IME if it was
1527 // currently shown, because doing that may involve
1528 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001529 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001530 final ViewTreeObserver.OnDrawListener listener = this;
1531 mWorkspace.post(new Runnable() {
1532 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001533 if (mWorkspace != null &&
1534 mWorkspace.getViewTreeObserver() != null) {
1535 mWorkspace.getViewTreeObserver().
1536 removeOnDrawListener(listener);
1537 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001538 }
1539 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001540 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001541 }
1542 });
1543 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001544 // When Launcher comes back to foreground, a different Activity might be responsible for
1545 // the app market intent, so refresh the icon
Winson Chungd64a6662013-09-30 11:06:59 -07001546 if (!DISABLE_MARKET_BUTTON) {
1547 updateAppMarketIcon();
1548 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001549 clearTypedText();
1550 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001551 }
1552
1553 private void sendAdvanceMessage(long delay) {
1554 mHandler.removeMessages(ADVANCE_MSG);
1555 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1556 mHandler.sendMessageDelayed(msg, delay);
1557 mAutoAdvanceSentTime = System.currentTimeMillis();
1558 }
1559
1560 private void updateRunning() {
1561 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1562 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1563 mAutoAdvanceRunning = autoAdvanceRunning;
1564 if (autoAdvanceRunning) {
1565 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1566 sendAdvanceMessage(delay);
1567 } else {
1568 if (!mWidgetsToAdvance.isEmpty()) {
1569 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1570 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1571 }
1572 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001573 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001574 }
1575 }
1576 }
1577
1578 private final Handler mHandler = new Handler() {
1579 @Override
1580 public void handleMessage(Message msg) {
1581 if (msg.what == ADVANCE_MSG) {
1582 int i = 0;
1583 for (View key: mWidgetsToAdvance.keySet()) {
1584 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1585 final int delay = mAdvanceStagger * i;
1586 if (v instanceof Advanceable) {
1587 postDelayed(new Runnable() {
1588 public void run() {
1589 ((Advanceable) v).advance();
1590 }
1591 }, delay);
1592 }
1593 i++;
1594 }
1595 sendAdvanceMessage(mAdvanceInterval);
1596 }
1597 }
1598 };
1599
1600 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001601 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001602 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1603 if (v instanceof Advanceable) {
1604 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001605 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001606 updateRunning();
1607 }
1608 }
1609
1610 void removeWidgetToAutoAdvance(View hostView) {
1611 if (mWidgetsToAdvance.containsKey(hostView)) {
1612 mWidgetsToAdvance.remove(hostView);
1613 updateRunning();
1614 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001615 }
1616
Joe Onorato9c1289c2009-08-17 11:03:03 -04001617 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001618 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001619 launcherInfo.hostView = null;
1620 }
1621
Winson Chung93eef082012-03-23 15:59:27 -07001622 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1623 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1624 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001625 }
1626
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001627 public LauncherAppWidgetHost getAppWidgetHost() {
1628 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001629 }
Romain Guycbb89e42009-06-08 15:52:54 -07001630
Winson Chunga9abd0e2010-10-27 17:18:37 -07001631 public LauncherModel getModel() {
1632 return mModel;
1633 }
1634
Bjorn Bringertc459e522013-06-07 19:36:01 +01001635 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001636 getWindow().closeAllPanels();
1637
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001638 // Whatever we were doing is hereby canceled.
1639 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001640 }
1641
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001642 @Override
1643 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001644 long startTime = 0;
1645 if (DEBUG_RESUME_TIME) {
1646 startTime = System.currentTimeMillis();
1647 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001648 super.onNewIntent(intent);
1649
1650 // Close the menu
1651 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001652 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001653 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001654
Adam Cohened307df2013-10-02 09:37:31 -07001655 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1656 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1657 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001658
Adam Cohen6fecd412013-10-02 17:41:50 -07001659 if (mWorkspace == null) {
1660 // Can be cases where mWorkspace is null, this prevents a NPE
1661 return;
1662 }
1663 Folder openFolder = mWorkspace.getOpenFolder();
1664 // In all these cases, only animate if we're already on home
1665 mWorkspace.exitWidgetResizeMode();
1666 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1667 openFolder == null) {
1668 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001669 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001670
Adam Cohen6fecd412013-10-02 17:41:50 -07001671 closeFolder();
1672 exitSpringLoadedDragMode();
1673
1674 // If we are already on home, then just animate back to the workspace,
1675 // otherwise, just wait until onResume to set the state back to Workspace
1676 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001677 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001678 } else {
1679 mOnResumeState = State.WORKSPACE;
1680 }
1681
1682 final View v = getWindow().peekDecorView();
1683 if (v != null && v.getWindowToken() != null) {
1684 InputMethodManager imm = (InputMethodManager)getSystemService(
1685 INPUT_METHOD_SERVICE);
1686 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1687 }
1688
1689 // Reset the apps customize page
1690 if (mAppsCustomizeTabHost != null) {
1691 mAppsCustomizeTabHost.reset();
1692 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001693 }
Adam Cohened307df2013-10-02 09:37:31 -07001694
Michael Jurka447bf842013-05-15 14:52:15 +02001695 if (DEBUG_RESUME_TIME) {
1696 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1697 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001698 }
1699
1700 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001701 public void onRestoreInstanceState(Bundle state) {
1702 super.onRestoreInstanceState(state);
1703 for (int page: mSynchronouslyBoundPages) {
1704 mWorkspace.restoreInstanceStateForChild(page);
1705 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001706 }
1707
1708 @Override
1709 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001710 if (mWorkspace.getChildCount() > 0) {
1711 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getRestorePage());
1712 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001713 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001714
Michael Jurka883f55b2010-10-21 15:47:14 -07001715 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001716 // We close any open folder since it will not be re-opened, and we need to make sure
1717 // this state is reflected.
1718 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001719
Adam Cohendcd297f2013-06-18 13:13:40 -07001720 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001721 mWaitingForResult) {
1722 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001723 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001724 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1725 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001726 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1727 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1728 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001729 }
1730
1731 if (mFolderInfo != null && mWaitingForResult) {
1732 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1733 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1734 }
Michael Jurka946ad472010-07-09 18:05:18 -07001735
Winson Chung785d2eb2011-04-14 16:08:02 -07001736 // Save the current AppsCustomize tab
1737 if (mAppsCustomizeTabHost != null) {
1738 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1739 if (currentTabTag != null) {
1740 outState.putString("apps_customize_currentTab", currentTabTag);
1741 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001742 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1743 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001744 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001745 }
1746
1747 @Override
1748 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001749 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001750
Winson Chunge7a03942011-08-05 15:05:12 -07001751 // Remove all pending runnables
1752 mHandler.removeMessages(ADVANCE_MSG);
1753 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001754 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001755
Winson Chungcd2b0142011-06-08 16:02:26 -07001756 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001757 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001758 mModel.stopLoader();
1759 app.setLauncher(null);
1760
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001761 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001762 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001763 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001764 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001765 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001766 mAppWidgetHost = null;
1767
1768 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001769
1770 TextKeyListener.getInstance().release();
1771
Winson Chung81b52252012-08-27 15:34:29 -07001772 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1773 // to prevent leaking Launcher activities on orientation change.
1774 if (mModel != null) {
1775 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1776 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001777
1778 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001779 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001780
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001781 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001782 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1783 mWorkspace.removeAllViews();
1784 mWorkspace = null;
1785 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001786
Michael Jurka2ecf9952012-06-18 12:52:28 -07001787 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001788 }
1789
Adam Cohena9cf38f2011-05-02 15:36:58 -07001790 public DragController getDragController() {
1791 return mDragController;
1792 }
1793
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001794 @Override
1795 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001796 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001797 super.startActivityForResult(intent, requestCode);
1798 }
1799
Winson Chung70d72102011-08-12 11:24:35 -07001800 /**
1801 * Indicates that we want global search for this activity by setting the globalSearch
1802 * argument for {@link #startSearch} to true.
1803 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001804 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001805 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001806 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001807
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001808 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001809
Karl Rosaen138a0412009-04-23 19:00:21 -07001810 if (initialQuery == null) {
1811 // Use any text typed in the launcher as the initial query
1812 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001813 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001814 if (appSearchData == null) {
1815 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001816 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001817 }
Winson Chungadf0c182012-08-23 14:59:07 -07001818 Rect sourceBounds = new Rect();
1819 if (mSearchDropTargetBar != null) {
1820 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1821 }
Romain Guycbb89e42009-06-08 15:52:54 -07001822
Bjorn Bringertc459e522013-06-07 19:36:01 +01001823 startSearch(initialQuery, selectInitialQuery,
1824 appSearchData, sourceBounds);
1825 }
1826
1827 public void startSearch(String initialQuery,
1828 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001829 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001830 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001831 }
1832
1833 /**
1834 * Starts the global search activity. This code is a copied from SearchManager
1835 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001836 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001837 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001838 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001839 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1840 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1841 if (globalSearchActivity == null) {
1842 Log.w(TAG, "No global search activity found.");
1843 return;
1844 }
1845 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1846 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1847 intent.setComponent(globalSearchActivity);
1848 // Make sure that we have a Bundle to put source in
1849 if (appSearchData == null) {
1850 appSearchData = new Bundle();
1851 } else {
1852 appSearchData = new Bundle(appSearchData);
1853 }
1854 // Set source to package name of app that starts global search, if not set already.
1855 if (!appSearchData.containsKey("source")) {
1856 appSearchData.putString("source", getPackageName());
1857 }
1858 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1859 if (!TextUtils.isEmpty(initialQuery)) {
1860 intent.putExtra(SearchManager.QUERY, initialQuery);
1861 }
1862 if (selectInitialQuery) {
1863 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1864 }
1865 intent.setSourceBounds(sourceBounds);
1866 try {
1867 startActivity(intent);
1868 } catch (ActivityNotFoundException ex) {
1869 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1870 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001871 }
1872
Winson Chung70d72102011-08-12 11:24:35 -07001873 @Override
Winson Chung70d72102011-08-12 11:24:35 -07001874 public boolean onPrepareOptionsMenu(Menu menu) {
1875 super.onPrepareOptionsMenu(menu);
Michael Jurkab94f3f82013-09-11 18:08:54 +02001876 if (!mWorkspace.isInOverviewMode()) {
1877 mWorkspace.enterOverviewMode();
Winson Chung70d72102011-08-12 11:24:35 -07001878 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02001879 return false;
Winson Chung70d72102011-08-12 11:24:35 -07001880 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001881
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001882 @Override
1883 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001884 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001885 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001886 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001887 }
1888
Joe Onorato9c1289c2009-08-17 11:03:03 -04001889 public boolean isWorkspaceLocked() {
1890 return mWorkspaceLoading || mWaitingForResult;
1891 }
1892
Michael Jurka0280c3b2010-09-17 15:00:07 -07001893 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001894 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001895 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001896 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1897 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001898 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001899 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001900 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001901
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001902 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1903 AppWidgetProviderInfo appWidgetInfo) {
1904 if (appWidgetInfo.configure != null) {
1905 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001906
Bjorn Bringert7984c942009-12-09 15:38:25 +00001907 // Launch over to configure widget, if needed
1908 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001909 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001910 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Michael Jurkaa805e1a2013-08-22 15:00:33 +02001911 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001912 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001913 // Otherwise just add it
Adam Cohendcd297f2013-06-18 13:13:40 -07001914 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001915 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001916 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001917 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001918 }
1919 }
Romain Guycbb89e42009-06-08 15:52:54 -07001920
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001921 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07001922 // Close any folders that may be open.
1923 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001924 mWorkspace.moveToCustomContentScreen(animate);
1925 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001926 /**
1927 * Process a shortcut drop.
1928 *
1929 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07001930 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001931 * @param cell The cell it should be added to, optional
1932 * @param position The location on the screen where it was dropped, optional
1933 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001934 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07001935 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001936 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001937 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001938 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001939 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001940
1941 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001942 mPendingAddInfo.cellX = cell[0];
1943 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001944 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001945
1946 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1947 createShortcutIntent.setComponent(componentName);
1948 processShortcut(createShortcutIntent);
1949 }
1950
Adam Cohenfbba09b2011-07-18 21:43:05 -07001951 /**
1952 * Process a widget drop.
1953 *
1954 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07001955 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001956 * @param cell The cell it should be added to, optional
1957 * @param position The location on the screen where it was dropped, optional
1958 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001959 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08001960 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001961 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001962 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001963 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001964 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001965 mPendingAddInfo.minSpanX = info.minSpanX;
1966 mPendingAddInfo.minSpanY = info.minSpanY;
1967
Adam Cohenfbba09b2011-07-18 21:43:05 -07001968 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001969 mPendingAddInfo.cellX = cell[0];
1970 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001971 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001972 if (span != null) {
1973 mPendingAddInfo.spanX = span[0];
1974 mPendingAddInfo.spanY = span[1];
1975 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001976
Adam Cohened66b2b2012-01-23 17:28:51 -08001977 AppWidgetHostView hostView = info.boundWidget;
1978 int appWidgetId;
1979 if (hostView != null) {
1980 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001981 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001982 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001983 // In this case, we either need to start an activity to get permission to bind
1984 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001985 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07001986 Bundle options = info.bindOptions;
1987
1988 boolean success = false;
1989 if (options != null) {
1990 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1991 info.componentName, options);
1992 } else {
1993 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1994 info.componentName);
1995 }
1996 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001997 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001998 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001999 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002000 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2001 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2002 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07002003 // TODO: we need to make sure that this accounts for the options bundle.
2004 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002005 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2006 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002007 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002008 }
2009
Joe Onoratodeb98af2010-02-19 14:59:39 -08002010 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002011 // Handle case where user selected "Applications"
2012 String applicationName = getResources().getString(R.string.group_applications);
2013 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07002014
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002015 if (applicationName != null && applicationName.equals(shortcutName)) {
2016 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2017 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07002018
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002019 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2020 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07002021 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002022 Utilities.startActivityForResultSafely(this, pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002023 } else {
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002024 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002025 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002026 }
2027
Winson Chung24ab2f12010-09-16 14:10:47 -07002028 void processWallpaper(Intent intent) {
2029 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2030 }
2031
Adam Cohendcd297f2013-06-18 13:13:40 -07002032 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002033 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002034 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002035 folderInfo.title = getText(R.string.folder_name);
2036
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002037 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002038 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002039 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002040 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002041
2042 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002043 FolderIcon newFolder =
2044 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002045 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002046 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002047 // Force measure the new folder icon
2048 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2049 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002050 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002051 }
Romain Guycbb89e42009-06-08 15:52:54 -07002052
Joe Onorato9c1289c2009-08-17 11:03:03 -04002053 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002054 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002055 }
2056
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002057 protected void startWallpaper() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002058 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002059 pickWallpaper.setComponent(getWallpaperPickerComponent());
Michael Jurka104c4562013-07-08 18:03:46 -07002060 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002061 }
2062
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002063 protected ComponentName getWallpaperPickerComponent() {
2064 return new ComponentName(getPackageName(), WallpaperPickerActivity.class.getName());
2065 }
2066
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002067 /**
2068 * Registers various content observers. The current implementation registers
2069 * only a favorites observer to keep track of the favorites applications.
2070 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002071 private void registerContentObservers() {
2072 ContentResolver resolver = getContentResolver();
2073 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2074 true, mWidgetObserver);
2075 }
2076
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002077 @Override
2078 public boolean dispatchKeyEvent(KeyEvent event) {
2079 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2080 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002081 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002082 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002083 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002084 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002085 dumpState();
2086 return true;
2087 }
2088 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002089 }
2090 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2091 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002092 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002093 return true;
2094 }
2095 }
2096
2097 return super.dispatchKeyEvent(event);
2098 }
2099
Joe Onorato88ec0992009-11-19 13:16:06 -08002100 @Override
2101 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002102 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002103 if (mAppsCustomizeContent.getContentType() ==
2104 AppsCustomizePagedView.ContentType.Applications) {
2105 showWorkspace(true);
2106 } else {
2107 showOverviewMode(true);
2108 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002109 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002110 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002111 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002112 Folder openFolder = mWorkspace.getOpenFolder();
2113 if (openFolder.isEditingName()) {
2114 openFolder.dismissEditingName();
2115 } else {
2116 closeFolder();
2117 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002118 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002119 mWorkspace.exitWidgetResizeMode();
2120
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002121 // Back button is a no-op here, but give at least some feedback for the button press
2122 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002123 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002124 }
2125
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002126 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002127 * Re-listen when widgets are reset.
2128 */
2129 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002130 if (mAppWidgetHost != null) {
2131 mAppWidgetHost.startListening();
2132 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002133 }
2134
2135 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002136 * Launches the intent referred by the clicked shortcut.
2137 *
2138 * @param v The view representing the clicked shortcut.
2139 */
2140 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002141 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2142 // view has detached (it's possible for this to happen if the view is removed mid touch).
2143 if (v.getWindowToken() == null) {
2144 return;
2145 }
2146
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002147 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002148 return;
2149 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002150
Adam Cohen1697b792013-09-17 19:08:21 -07002151 if (v instanceof Workspace) {
2152 if (mWorkspace.isInOverviewMode()) {
2153 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002154 }
2155 return;
2156 }
2157
2158 if (v instanceof CellLayout) {
2159 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002160 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002161 }
2162 }
2163
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002164 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002165 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002166 // Open shortcut
Daniel Sandlerff02d492013-08-05 02:12:05 -04002167 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2168 final Intent intent = shortcut.intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002169
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002170 // Check for special shortcuts
2171 if (intent.getComponent() != null) {
2172 final String shortcutClass = intent.getComponent().getClassName();
2173
2174 if (shortcutClass.equals(WidgetAdder.class.getName())) {
Winson Chung82963d52013-10-09 11:20:57 -07002175 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002176 return;
2177 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2178 MemoryDumpActivity.startDump(this);
2179 return;
Adam Cohen39a06042013-07-19 14:30:12 -07002180 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2181 toggleShowWeightWatcher();
2182 return;
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002183 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002184 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002185
2186 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002187 int[] pos = new int[2];
2188 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002189 intent.setSourceBounds(new Rect(pos[0], pos[1],
2190 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002191
2192 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002193
Daniel Sandlerff02d492013-08-05 02:12:05 -04002194 mStats.recordLaunch(intent, shortcut);
2195
Michael Jurkaddd62e92011-02-16 17:49:14 -08002196 if (success && v instanceof BubbleTextView) {
2197 mWaitingForResume = (BubbleTextView) v;
2198 mWaitingForResume.setStayPressed(true);
2199 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002200 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002201 if (v instanceof FolderIcon) {
2202 FolderIcon fi = (FolderIcon) v;
2203 handleFolderClick(fi);
2204 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002205 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002206 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002207 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002208 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002209 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002210 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002211 }
2212 }
2213
Michael Jurka0e260592010-06-30 17:07:39 -07002214 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002215 return false;
2216 }
2217
Michael Jurkaaf442092010-06-10 17:01:57 -07002218 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002219 * Event handler for the search button
2220 *
2221 * @param v The view that was clicked.
2222 */
2223 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002224 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2225
Amith Yamasania135ba82011-08-09 17:42:01 -07002226 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002227 }
2228
2229 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002230 * Event handler for the voice button
2231 *
2232 * @param v The view that was clicked.
2233 */
2234 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002235 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002236
Bjorn Bringertc459e522013-06-07 19:36:01 +01002237 startVoice();
2238 }
2239
2240 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002241 try {
2242 final SearchManager searchManager =
2243 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2244 ComponentName activityName = searchManager.getGlobalSearchActivity();
2245 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002246 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002247 if (activityName != null) {
2248 intent.setPackage(activityName.getPackageName());
2249 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002250 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002251 } catch (ActivityNotFoundException e) {
2252 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002253 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002254 startActivitySafely(null, intent, "onClickVoiceButton");
2255 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002256 }
2257
2258 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002259 * Event handler for the "grid" button that appears on the home screen, which
2260 * enters all apps mode.
2261 *
2262 * @param v The view that was clicked.
2263 */
2264 public void onClickAllAppsButton(View v) {
Winson Chung82963d52013-10-09 11:20:57 -07002265 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, true);
Michael Jurka5130e402011-10-13 04:55:35 -07002266 }
2267
2268 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002269 // Provide the same haptic feedback that the system offers for virtual keys.
2270 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002271 }
2272
Adam Cohen61f560d2013-09-30 15:58:20 -07002273 public void performHapticFeedbackOnTouchDown(View v) {
2274 // Provide the same haptic feedback that the system offers for virtual keys.
2275 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2276 }
2277
2278 public View.OnTouchListener getHapticFeedbackTouchListener() {
2279 if (mHapticFeedbackTouchListener == null) {
2280 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2281 @Override
2282 public boolean onTouch(View v, MotionEvent event) {
2283 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2284 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2285 }
2286 return false;
2287 }
2288 };
2289 }
2290 return mHapticFeedbackTouchListener;
2291 }
2292
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002293 public void onClickAppMarketButton(View v) {
Winson Chungd64a6662013-09-30 11:06:59 -07002294 if (!DISABLE_MARKET_BUTTON) {
2295 if (mAppMarketIntent != null) {
2296 startActivitySafely(v, mAppMarketIntent, "app market");
2297 } else {
2298 Log.e(TAG, "Invalid app market intent.");
2299 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002300 }
2301 }
2302
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002303 /**
2304 * Called when the user stops interacting with the launcher.
2305 * This implies that the user is now on the homescreen and is not doing housekeeping.
2306 */
2307 protected void onInteractionEnd() {}
2308
2309 /**
2310 * Called when the user starts interacting with the launcher.
2311 * The possible interactions are:
2312 * - open all apps
2313 * - reorder an app shortcut, or a widget
2314 * - open the overview mode.
2315 * This is a good time to stop doing things that only make sense
2316 * when the user is on the homescreen and not doing housekeeping.
2317 */
2318 protected void onInteractionBegin() {}
2319
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002320 void startApplicationDetailsActivity(ComponentName componentName) {
2321 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002322 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2323 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002324 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002325 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002326 }
2327
Michael Jurka1e2f4652013-07-08 18:03:46 -07002328 // returns true if the activity was started
2329 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002330 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002331 // System applications cannot be installed. For now, show a toast explaining that.
2332 // We may give them the option of disabling apps this way.
2333 int messageId = R.string.uninstall_system_app_text;
2334 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002335 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002336 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002337 String packageName = componentName.getPackageName();
2338 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002339 Intent intent = new Intent(
2340 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002341 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2342 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002343 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002344 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002345 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002346 }
2347
Michael Jurka86a720e2012-05-09 11:23:23 -07002348 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002349 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002350
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002351 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002352 // Only launch using the new animation if the shortcut has not opted out (this is a
2353 // private contract between launcher and may be ignored in the future).
2354 boolean useLaunchAnimation = (v != null) &&
2355 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2356 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002357 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2358 v.getMeasuredWidth(), v.getMeasuredHeight());
2359
2360 startActivity(intent, opts.toBundle());
2361 } else {
2362 startActivity(intent);
2363 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002364 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002365 } catch (SecurityException e) {
2366 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002367 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002368 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002369 "or use the exported attribute for this activity. "
2370 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002371 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002372 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002373 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002374
Michael Jurka86a720e2012-05-09 11:23:23 -07002375 boolean startActivitySafely(View v, Intent intent, Object tag) {
2376 boolean success = false;
2377 try {
2378 success = startActivity(v, intent, tag);
2379 } catch (ActivityNotFoundException e) {
2380 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2381 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2382 }
2383 return success;
2384 }
2385
Adam Cohena9cf38f2011-05-02 15:36:58 -07002386 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002387 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002388 Folder openFolder = mWorkspace.getFolderForTag(info);
2389
2390 // If the folder info reports that the associated folder is open, then verify that
2391 // it is actually opened. There have been a few instances where this gets out of sync.
2392 if (info.opened && openFolder == null) {
2393 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002394 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002395 info.opened = false;
2396 }
2397
Adam Cohenfb91f302012-06-11 15:45:18 -07002398 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002399 // Close any open folder
2400 closeFolder();
2401 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002402 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002403 } else {
2404 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002405 int folderScreen;
2406 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002407 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002408 // .. and close it
2409 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002410 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002411 // Close any folder open on the current screen
2412 closeFolder();
2413 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002414 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002415 }
2416 }
2417 }
2418 }
2419
Adam Cohen268c4752012-06-06 17:47:33 -07002420 /**
2421 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2422 * in the DragLayer in the exact absolute location of the original FolderIcon.
2423 */
2424 private void copyFolderIconToImage(FolderIcon fi) {
2425 final int width = fi.getMeasuredWidth();
2426 final int height = fi.getMeasuredHeight();
2427
2428 // Lazy load ImageView, Bitmap and Canvas
2429 if (mFolderIconImageView == null) {
2430 mFolderIconImageView = new ImageView(this);
2431 }
2432 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2433 mFolderIconBitmap.getHeight() != height) {
2434 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2435 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2436 }
2437
2438 DragLayer.LayoutParams lp;
2439 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2440 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2441 } else {
2442 lp = new DragLayer.LayoutParams(width, height);
2443 }
2444
Adam Cohen307fe232012-08-16 17:55:58 -07002445 // The layout from which the folder is being opened may be scaled, adjust the starting
2446 // view size by this scale factor.
2447 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002448 lp.customPosition = true;
2449 lp.x = mRectForFolderAnimation.left;
2450 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002451 lp.width = (int) (scale * width);
2452 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002453
2454 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2455 fi.draw(mFolderIconCanvas);
2456 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002457 if (fi.getFolder() != null) {
2458 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2459 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002460 }
Adam Cohen268c4752012-06-06 17:47:33 -07002461 // Just in case this image view is still in the drag layer from a previous animation,
2462 // we remove it and re-add it.
2463 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2464 mDragLayer.removeView(mFolderIconImageView);
2465 }
2466 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002467 if (fi.getFolder() != null) {
2468 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002469 }
Adam Cohen268c4752012-06-06 17:47:33 -07002470 }
2471
Adam Cohen2801caf2011-05-13 20:57:39 -07002472 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002473 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002474 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2475 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2476 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2477
Adam Cohenc51934b2011-07-26 21:07:43 -07002478 FolderInfo info = (FolderInfo) fi.getTag();
2479 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2480 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002481 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2482 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002483 }
2484
Adam Cohen268c4752012-06-06 17:47:33 -07002485 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2486 copyFolderIconToImage(fi);
2487 fi.setVisibility(View.INVISIBLE);
2488
Michael Jurka2ecf9952012-06-18 12:52:28 -07002489 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002490 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002491 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2492 oa.start();
2493 }
2494
Adam Cohen268c4752012-06-06 17:47:33 -07002495 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002496 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002497 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2498 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2499 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2500
Adam Cohen268c4752012-06-06 17:47:33 -07002501 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002502
Adam Cohen268c4752012-06-06 17:47:33 -07002503 // We remove and re-draw the FolderIcon in-case it has changed
2504 mDragLayer.removeView(mFolderIconImageView);
2505 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002506 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002507 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002508 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002509 oa.addListener(new AnimatorListenerAdapter() {
2510 @Override
2511 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002512 if (cl != null) {
2513 cl.clearFolderLeaveBehind();
2514 // Remove the ImageView copy of the FolderIcon and make the original visible.
2515 mDragLayer.removeView(mFolderIconImageView);
2516 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002517 }
2518 }
2519 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002520 oa.start();
2521 }
2522
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002523 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002524 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002525 * is animated relative to the specified View. If the View is null, no animation
2526 * is played.
2527 *
2528 * @param folderInfo The FolderInfo describing the folder to open.
2529 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002530 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002531 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002532 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002533
Adam Cohena9cf38f2011-05-02 15:36:58 -07002534 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002535
Adam Cohen4554ee12011-08-03 16:13:21 -07002536 // Just verify that the folder hasn't already been added to the DragLayer.
2537 // There was a one-off crash where the folder had a parent already.
2538 if (folder.getParent() == null) {
2539 mDragLayer.addView(folder);
2540 mDragController.addDropTarget((DropTarget) folder);
2541 } else {
2542 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2543 folder.getParent() + ").");
2544 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002545 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002546 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002547
2548 // Notify the accessibility manager that this folder "window" has appeared and occluded
2549 // the workspace items
2550 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2551 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002552 }
2553
2554 public void closeFolder() {
2555 Folder folder = mWorkspace.getOpenFolder();
2556 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002557 if (folder.isEditingName()) {
2558 folder.dismissEditingName();
2559 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002560 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002561
2562 // Dismiss the folder cling
2563 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002564 }
2565 }
2566
2567 void closeFolder(Folder folder) {
2568 folder.getInfo().opened = false;
2569
2570 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2571 if (parent != null) {
2572 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2573 shrinkAndFadeInFolderIcon(fi);
2574 }
2575 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002576
2577 // Notify the accessibility manager that this folder "window" has disappeard and no
2578 // longer occludeds the workspace items
2579 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002580 }
2581
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002582 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002583 if (!isDraggingEnabled()) return false;
2584 if (isWorkspaceLocked()) return false;
2585 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002586
Adam Cohen1697b792013-09-17 19:08:21 -07002587 if (v instanceof Workspace) {
2588 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07002589 if (mWorkspace.enterOverviewMode()) {
2590 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2591 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2592 return true;
2593 } else {
2594 return false;
2595 }
Adam Cohen1697b792013-09-17 19:08:21 -07002596 }
Adam Cohen1697b792013-09-17 19:08:21 -07002597 }
2598
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002599 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002600 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002601 }
2602
Michael Jurka0280c3b2010-09-17 15:00:07 -07002603 resetAddInfo();
2604 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002605 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002606 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002607 return true;
2608 }
2609
Winson Chung3d503fb2011-07-13 17:25:49 -07002610 // The hotseat touch handling does not go through Workspace, and we always allow long press
2611 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002612 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002613 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2614 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002615 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002616 // User long pressed on empty space
2617 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2618 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2619 // Disabling reordering until we sort out some issues.
2620 if (mWorkspace.isInOverviewMode()) {
2621 mWorkspace.startReordering(v);
2622 } else {
2623 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07002624 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002625 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002626 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002627 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002628 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002629 }
2630 }
2631 }
2632 return true;
2633 }
2634
Winson Chung3d503fb2011-07-13 17:25:49 -07002635 boolean isHotseatLayout(View layout) {
2636 return mHotseat != null && layout != null &&
2637 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2638 }
2639 Hotseat getHotseat() {
2640 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002641 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002642 View getOverviewPanel() {
2643 return mOverviewPanel;
2644 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002645 SearchDropTargetBar getSearchBar() {
2646 return mSearchDropTargetBar;
2647 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002648
Winson Chung3d503fb2011-07-13 17:25:49 -07002649 /**
2650 * Returns the CellLayout of the specified container at the specified screen.
2651 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002652 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002653 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2654 if (mHotseat != null) {
2655 return mHotseat.getLayout();
2656 } else {
2657 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002658 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002659 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002660 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002661 }
2662 }
2663
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002664 Workspace getWorkspace() {
2665 return mWorkspace;
2666 }
2667
Daniel Sandler843e8602010-06-07 14:59:01 -04002668 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002669 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002670 }
2671
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002672 /**
2673 * Helper method for the cameraZoomIn/cameraZoomOut animations
2674 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002675 * @param scaleFactor The scale factor used for the zoom
2676 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002677 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002678 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002679 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002680 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002681
Craig Mautner360310b2012-10-26 15:13:08 -07002682 private void setWorkspaceBackground(boolean workspace) {
2683 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002684 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002685 }
2686
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002687 void updateWallpaperVisibility(boolean visible) {
2688 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2689 int curflags = getWindow().getAttributes().flags
2690 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2691 if (wpflags != curflags) {
2692 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2693 }
Craig Mautner360310b2012-10-26 15:13:08 -07002694 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002695 }
2696
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002697 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2698 if (v instanceof LauncherTransitionable) {
2699 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2700 }
2701 }
2702
Michael Jurkabed61d22012-02-14 22:51:29 -08002703 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2704 if (v instanceof LauncherTransitionable) {
2705 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2706 }
Winson Chung70442722012-02-10 15:43:22 -08002707
2708 // Update the workspace transition step as well
2709 dispatchOnLauncherTransitionStep(v, 0f);
2710 }
2711
2712 private void dispatchOnLauncherTransitionStep(View v, float t) {
2713 if (v instanceof LauncherTransitionable) {
2714 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2715 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002716 }
2717
2718 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2719 if (v instanceof LauncherTransitionable) {
2720 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2721 }
Winson Chung70442722012-02-10 15:43:22 -08002722
2723 // Update the workspace transition step as well
2724 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002725 }
2726
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002727 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002728 * Things to test when changing the following seven functions.
2729 * - Home from workspace
2730 * - from center screen
2731 * - from other screens
2732 * - Home from all apps
2733 * - from center screen
2734 * - from other screens
2735 * - Back from all apps
2736 * - from center screen
2737 * - from other screens
2738 * - Launch app from workspace and quit
2739 * - with back
2740 * - with home
2741 * - Launch app from all apps and quit
2742 * - with back
2743 * - with home
2744 * - Go to a screen that's not the default, then all
2745 * apps, and launch and app, and go back
2746 * - with back
2747 * -with home
2748 * - On workspace, long press power and go back
2749 * - with back
2750 * - with home
2751 * - On all apps, long press power and go back
2752 * - with back
2753 * - with home
2754 * - On workspace, power off
2755 * - On all apps, power off
2756 * - Launch an app and turn off the screen while in that app
2757 * - Go back with home key
2758 * - Go back with back key TODO: make this not go to workspace
2759 * - From all apps
2760 * - From workspace
2761 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2762 * - From all apps
2763 * - From the center workspace
2764 * - From another workspace
2765 */
2766
2767 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002768 * Zoom the camera out from the workspace to reveal 'toView'.
2769 * Assumes that the view to show is anchored at either the very top or very bottom
2770 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002771 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002772 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07002773 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
2774 showAppsCustomizeHelper(animated, springLoaded, contentType);
2775 }
2776 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
2777 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002778 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002779 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002780 mStateAnimation.cancel();
2781 mStateAnimation = null;
2782 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002783 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002784
Winson Chungf0ea4d32011-06-06 14:27:16 -07002785 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2786 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2787 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002788 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002789 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002790 final int startDelay =
2791 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002792
Michael Jurkab3e22d92011-10-31 15:58:33 -07002793 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002794
Michael Jurkad74c9842011-07-10 12:44:21 -07002795 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002796 Animator workspaceAnim =
2797 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Winson Chungc58497e2013-09-03 17:48:37 -07002798 if (!AppsCustomizePagedView.DISABLE_ALL_APPS) {
2799 // Set the content type for the all apps space
2800 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
2801 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002802
2803 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002804 toView.setScaleX(scale);
2805 toView.setScaleY(scale);
2806 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2807 scaleAnim.
2808 scaleX(1f).scaleY(1f).
2809 setDuration(duration).
2810 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002811
Winson Chungf0ea4d32011-06-06 14:27:16 -07002812 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002813 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002814 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002815 .ofFloat(toView, "alpha", 0f, 1f)
2816 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002817 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002818 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2819 @Override
2820 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002821 if (animation == null) {
2822 throw new RuntimeException("animation is null");
2823 }
Winson Chung70442722012-02-10 15:43:22 -08002824 float t = (Float) animation.getAnimatedValue();
2825 dispatchOnLauncherTransitionStep(fromView, t);
2826 dispatchOnLauncherTransitionStep(toView, t);
2827 }
2828 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002829
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002830 // toView should appear right at the end of the workspace shrink
2831 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002832 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002833 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002834 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002835
2836 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002837 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002838 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002839 // Prepare the position
2840 toView.setTranslationX(0.0f);
2841 toView.setTranslationY(0.0f);
2842 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002843 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002844 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002845 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002846 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002847 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2848 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002849
Winson Chungc7d2b602012-05-16 17:02:20 -07002850 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002851 if (mSearchDropTargetBar != null) {
2852 mSearchDropTargetBar.hideSearchBar(false);
2853 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002854 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002855 });
2856
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002857 if (workspaceAnim != null) {
2858 mStateAnimation.play(workspaceAnim);
2859 }
Michael Jurka1899a362011-11-03 13:50:45 -07002860
2861 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002862
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002863 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2864 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002865
2866 // If any of the objects being animated haven't been measured/laid out
2867 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002868 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002869 (mWorkspace.getMeasuredWidth() == 0) ||
2870 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002871 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002872 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002873
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002874 final AnimatorSet stateAnimation = mStateAnimation;
2875 final Runnable startAnimRunnable = new Runnable() {
2876 public void run() {
2877 // Check that mStateAnimation hasn't changed while
2878 // we waited for a layout/draw pass
2879 if (mStateAnimation != stateAnimation)
2880 return;
2881 setPivotsForZoom(toView, scale);
2882 dispatchOnLauncherTransitionStart(fromView, animated, false);
2883 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002884 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002885 }
2886 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002887 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002888 final ViewTreeObserver observer = toView.getViewTreeObserver();
2889 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2890 public void onGlobalLayout() {
2891 startAnimRunnable.run();
2892 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2893 }
2894 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002895 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002896 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002897 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002898 } else {
2899 toView.setTranslationX(0.0f);
2900 toView.setTranslationY(0.0f);
2901 toView.setScaleX(1.0f);
2902 toView.setScaleY(1.0f);
2903 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002904 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002905
Daniel Sandlere4f98912013-06-25 15:13:26 -04002906 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002907 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002908 if (mSearchDropTargetBar != null) {
2909 mSearchDropTargetBar.hideSearchBar(false);
2910 }
Michael Jurkaabded662011-03-04 12:06:57 -08002911 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002912 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002913 dispatchOnLauncherTransitionStart(fromView, animated, false);
2914 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002915 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002916 dispatchOnLauncherTransitionStart(toView, animated, false);
2917 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002918 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002919 }
2920
2921 /**
2922 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002923 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002924 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002925 */
Adam Cohened307df2013-10-02 09:37:31 -07002926 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08002927 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002928
Michael Jurkab3e22d92011-10-31 15:58:33 -07002929 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002930 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002931 mStateAnimation.cancel();
2932 mStateAnimation = null;
2933 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002934 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002935
Winson Chungf0ea4d32011-06-06 14:27:16 -07002936 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002937 final int fadeOutDuration =
2938 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002939 final float scaleFactor = (float)
2940 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2941 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002942 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002943 Animator workspaceAnim = null;
Adam Cohened307df2013-10-02 09:37:31 -07002944 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002945 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2946 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07002947 toState, animated, stagger, -1);
2948 } else if (toState == Workspace.State.SPRING_LOADED ||
2949 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002950 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07002951 toState, animated);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002952 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002953
Michael Jurkab3e22d92011-10-31 15:58:33 -07002954 setPivotsForZoom(fromView, scaleFactor);
Winson Chung4afe9b32011-07-27 17:46:20 -07002955 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002956 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002957 final LauncherViewPropertyAnimator scaleAnim =
2958 new LauncherViewPropertyAnimator(fromView);
2959 scaleAnim.
2960 scaleX(scaleFactor).scaleY(scaleFactor).
2961 setDuration(duration).
2962 setInterpolator(new Workspace.ZoomInInterpolator());
2963
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002964 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002965 .ofFloat(fromView, "alpha", 1f, 0f)
2966 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002967 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002968 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2969 @Override
2970 public void onAnimationUpdate(ValueAnimator animation) {
2971 float t = 1f - (Float) animation.getAnimatedValue();
2972 dispatchOnLauncherTransitionStep(fromView, t);
2973 dispatchOnLauncherTransitionStep(toView, t);
2974 }
2975 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002976
Michael Jurka2ecf9952012-06-18 12:52:28 -07002977 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002978
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002979 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2980 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Chet Haasebc2f0822012-10-26 17:59:53 -07002981 mAppsCustomizeContent.pauseScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08002982
2983 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002984 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002985 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002986 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002987 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2988 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002989 if (onCompleteRunnable != null) {
2990 onCompleteRunnable.run();
2991 }
Chet Haasebc2f0822012-10-26 17:59:53 -07002992 mAppsCustomizeContent.updateCurrentPageScroll();
2993 mAppsCustomizeContent.resumeScrolling();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002994 }
2995 });
2996
Winson Chungf0ea4d32011-06-06 14:27:16 -07002997 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002998 if (workspaceAnim != null) {
2999 mStateAnimation.play(workspaceAnim);
3000 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003001 dispatchOnLauncherTransitionStart(fromView, animated, true);
3002 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003003 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003004 } else {
3005 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003006 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003007 dispatchOnLauncherTransitionStart(fromView, animated, true);
3008 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003009 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003010 dispatchOnLauncherTransitionStart(toView, animated, true);
3011 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003012 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003013 }
3014
Michael Jurkae326f182011-11-21 14:05:46 -08003015 @Override
3016 public void onTrimMemory(int level) {
3017 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07003018 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08003019 mAppsCustomizeTabHost.onTrimMemory();
3020 }
3021 }
3022
Adam Cohened307df2013-10-02 09:37:31 -07003023 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003024 showWorkspace(animated, null);
3025 }
3026
Adam Cohened307df2013-10-02 09:37:31 -07003027 protected void showWorkspace() {
3028 showWorkspace(true);
3029 }
3030
Adam Cohened66b2b2012-01-23 17:28:51 -08003031 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohened307df2013-10-02 09:37:31 -07003032 if (mWorkspace.isInOverviewMode()) {
3033 mWorkspace.exitOverviewMode(animated);
3034 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003035 if (mState != State.WORKSPACE) {
Winson Chung2d75f122013-09-23 16:53:31 -07003036 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003037 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003038 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003039
Winson Chungc7d2b602012-05-16 17:02:20 -07003040 // Show the search bar (only animate if we were showing the drop target bar in spring
3041 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003042 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003043 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003044 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003045
Michael Jurkab3e22d92011-10-31 15:58:33 -07003046 // Set focus to the AppsCustomize button
3047 if (mAllAppsButton != null) {
3048 mAllAppsButton.requestFocus();
3049 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003050 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003051
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003052 // Change the state *after* we've called all the transition code
3053 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003054
Winson Chung5fb63472011-02-02 17:03:37 -08003055 // Resume the auto-advance of widgets
3056 mUserPresent = true;
3057 updateRunning();
3058
alanv1d4fde62012-10-17 13:15:47 -07003059 // Send an accessibility event to announce the context change
3060 getWindow().getDecorView()
3061 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003062
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003063 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003064 }
3065
Adam Cohened307df2013-10-02 09:37:31 -07003066 void showOverviewMode(boolean animated) {
3067 mWorkspace.setVisibility(View.VISIBLE);
3068 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3069 mState = State.WORKSPACE;
3070 onWorkspaceShown(animated);
3071 }
3072
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003073 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003074 }
3075
Winson Chung82963d52013-10-09 11:20:57 -07003076 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3077 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003078 if (mState != State.WORKSPACE) return;
3079
Winson Chung82963d52013-10-09 11:20:57 -07003080 if (resetPageToZero) {
3081 mAppsCustomizeTabHost.reset();
3082 }
Winson Chungc58497e2013-09-03 17:48:37 -07003083 showAppsCustomizeHelper(animated, false, contentType);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003084 mAppsCustomizeTabHost.requestFocus();
3085
Michael Jurkab3e22d92011-10-31 15:58:33 -07003086 // Change the state *after* we've called all the transition code
3087 mState = State.APPS_CUSTOMIZE;
3088
3089 // Pause the auto-advance of widgets until we are out of AllApps
3090 mUserPresent = false;
3091 updateRunning();
3092 closeFolder();
3093
3094 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003095 getWindow().getDecorView()
3096 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003097 }
3098
Adam Cohen7777d962011-08-18 18:58:38 -07003099 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003100 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003101 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003102 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003103 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003104 }
Adam Cohen7777d962011-08-18 18:58:38 -07003105
Adam Cohened66b2b2012-01-23 17:28:51 -08003106 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
3107 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003108 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3109
Winson Chunge7a03942011-08-05 15:05:12 -07003110 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003111 @Override
3112 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003113 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003114 // Before we show workspace, hide all apps again because
3115 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3116 // clean up our state transition functions
3117 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003118 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003119 } else {
3120 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003121 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003122 }
3123 }, (extendedDelay ?
3124 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
3125 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
3126 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003127
Michael Jurkad3ef3062010-11-23 16:23:58 -08003128 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003129 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003130 final boolean animated = true;
3131 final boolean springLoaded = true;
3132 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003133 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003134 }
3135 // Otherwise, we are not in spring loaded mode, so don't do anything.
3136 }
3137
Michael Jurkab3e22d92011-10-31 15:58:33 -07003138 void lockAllApps() {
3139 // TODO
3140 }
3141
3142 void unlockAllApps() {
3143 // TODO
3144 }
3145
Winson Chungf0ea4d32011-06-06 14:27:16 -07003146 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003147 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003148 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003149 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003150 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003151 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003152 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003153 int duration = 0;
3154 if (mSearchDropTargetBar != null) {
3155 duration = mSearchDropTargetBar.getTransitionInDuration();
3156 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003157 mHotseat.animate().alpha(1f).setDuration(duration);
3158 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003159 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003160 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003161 }
3162 }
3163 }
3164
3165 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003166 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003167 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003168 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003169 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003170 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003171 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003172 int duration = 0;
3173 if (mSearchDropTargetBar != null) {
3174 duration = mSearchDropTargetBar.getTransitionOutDuration();
3175 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003176 mHotseat.animate().alpha(0f).setDuration(duration);
3177 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003178 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003179 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003180 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003181 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003182 }
3183
Patrick Dubroy5f445422011-02-18 14:35:21 -08003184 /**
3185 * Add an item from all apps or customize onto the given workspace screen.
3186 * If layout is null, add to the current screen.
3187 */
3188 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003189 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003190 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003191 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003192 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003193
Winson Chungdff8ebb2011-09-08 17:25:31 -07003194 /** Maps the current orientation to an index for referencing orientation correct global icons */
3195 private int getCurrentOrientationIndexForGlobalIcons() {
3196 // default - 0, landscape - 1
3197 switch (getResources().getConfiguration().orientation) {
3198 case Configuration.ORIENTATION_LANDSCAPE:
3199 return 1;
3200 default:
3201 return 0;
3202 }
3203 }
3204
Michael Jurkaae65ee32012-04-30 11:45:54 -07003205 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003206 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003207 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003208 // Look for the toolbar icon specified in the activity meta-data
3209 Bundle metaData = packageManager.getActivityInfo(
3210 activityName, PackageManager.GET_META_DATA).metaData;
3211 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003212 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003213 if (iconResId != 0) {
3214 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003215 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003216 }
3217 }
3218 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003219 // This can happen if the activity defines an invalid drawable
3220 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3221 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003222 } catch (Resources.NotFoundException nfe) {
3223 // This can happen if the activity defines an invalid drawable
3224 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3225 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003226 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003227 return null;
3228 }
3229
3230 // if successful in getting icon, return it; otherwise, set button to use default drawable
3231 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003232 int buttonId, ComponentName activityName, int fallbackDrawableId,
3233 String toolbarResourceName) {
3234 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003235 Resources r = getResources();
3236 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3237 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003238
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003239 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003240 // If we were unable to find the icon via the meta-data, use a generic one
3241 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003242 toolbarIcon = r.getDrawable(fallbackDrawableId);
3243 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003244 if (button != null) {
3245 button.setCompoundDrawables(toolbarIcon, null, null, null);
3246 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003247 return null;
3248 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003249 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003250 if (button != null) {
3251 button.setCompoundDrawables(toolbarIcon, null, null, null);
3252 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003253 return toolbarIcon.getConstantState();
3254 }
3255 }
3256
3257 // if successful in getting icon, return it; otherwise, set button to use default drawable
3258 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003259 int buttonId, ComponentName activityName, int fallbackDrawableId,
3260 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003261 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003262 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003263
Michael Jurka19e0fc52011-07-22 18:00:21 -07003264 if (button != null) {
3265 // If we were unable to find the icon via the meta-data, use a
3266 // generic one
3267 if (toolbarIcon == null) {
3268 button.setImageResource(fallbackDrawableId);
3269 } else {
3270 button.setImageDrawable(toolbarIcon);
3271 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003272 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003273
3274 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3275
Michael Jurka0423dcf2010-10-05 14:56:18 -07003276 }
3277
Winson Chung1b884092012-06-08 17:13:02 -07003278 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003279 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003280 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003281 }
3282
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003283 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003284 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003285 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003286 }
3287
Winson Chungbb185bd2011-11-21 12:31:42 -08003288 private void invalidatePressedFocusedStates(View container, View button) {
3289 if (container instanceof HolographicLinearLayout) {
3290 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3291 layout.invalidatePressedFocusedStates();
3292 } else if (button instanceof HolographicImageView) {
3293 HolographicImageView view = (HolographicImageView) button;
3294 view.invalidatePressedFocusedStates();
3295 }
3296 }
3297
Cristina Stancu476493b2013-08-07 17:20:14 +01003298 public View getQsbBar() {
3299 if (mQsbBar == null) {
Winson Chung54e65132013-09-27 11:44:58 -07003300 mQsbBar = mInflater.inflate(R.layout.search_bar, mSearchDropTargetBar, false);
3301 mSearchDropTargetBar.addView(mQsbBar);
Cristina Stancu476493b2013-08-07 17:20:14 +01003302 }
3303 return mQsbBar;
3304 }
3305
3306 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003307 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003308 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003309 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003310 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003311
Winson Chung1cad91e2011-05-25 17:41:01 -07003312 final SearchManager searchManager =
3313 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3314 ComponentName activityName = searchManager.getGlobalSearchActivity();
3315 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003316 int coi = getCurrentOrientationIndexForGlobalIcons();
3317 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003318 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3319 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3320 if (sGlobalSearchIcon[coi] == null) {
3321 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3322 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3323 TOOLBAR_ICON_METADATA_NAME);
3324 }
3325
Winson Chungc51db6a2011-10-05 11:44:49 -07003326 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3327 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003328 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003329 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003330 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003331 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003332 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3333 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003334 if (searchButton != null) searchButton.setVisibility(View.GONE);
3335 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003336 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003337 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003338 }
3339 }
3340
Cristina Stancu476493b2013-08-07 17:20:14 +01003341 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003342 final View searchButtonContainer = findViewById(R.id.search_button_container);
3343 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003344 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003345 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003346 }
3347
Cristina Stancu476493b2013-08-07 17:20:14 +01003348 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003349 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003350 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003351
Winson Chungc51db6a2011-10-05 11:44:49 -07003352 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003353 final SearchManager searchManager =
3354 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3355 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3356
3357 ComponentName activityName = null;
3358 if (globalSearchActivity != null) {
3359 // Check if the global search activity handles voice search
3360 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3361 intent.setPackage(globalSearchActivity.getPackageName());
3362 activityName = intent.resolveActivity(getPackageManager());
3363 }
3364
3365 if (activityName == null) {
3366 // Fallback: check if an activity other than the global search activity
3367 // resolves this
3368 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3369 activityName = intent.resolveActivity(getPackageManager());
3370 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003371 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003372 int coi = getCurrentOrientationIndexForGlobalIcons();
3373 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003374 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3375 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3376 if (sVoiceSearchIcon[coi] == null) {
3377 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3378 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3379 TOOLBAR_ICON_METADATA_NAME);
3380 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003381 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003382 voiceButton.setVisibility(View.VISIBLE);
Winson Chung5841efa2013-09-30 18:06:44 -07003383 updateVoiceButtonProxyVisible(false);
Winson Chungbb185bd2011-11-21 12:31:42 -08003384 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003385 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003386 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003387 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003388 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003389 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003390 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003391 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003392 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003393
Cristina Stancu476493b2013-08-07 17:20:14 +01003394 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003395 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3396 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003397 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003398 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003399 }
3400
Winson Chung5841efa2013-09-30 18:06:44 -07003401 public void updateVoiceButtonProxyVisible(boolean forceDisableVoiceButtonProxy) {
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003402 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3403 if (voiceButtonProxy != null) {
Winson Chung5841efa2013-09-30 18:06:44 -07003404 boolean visible = !forceDisableVoiceButtonProxy &&
3405 mWorkspace.shouldVoiceButtonProxyBeVisible();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003406 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003407 voiceButtonProxy.bringToFront();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003408 }
3409 }
Winson Chung5841efa2013-09-30 18:06:44 -07003410
3411 /**
3412 * This is an overrid eot disable the voice button proxy. If disabled is true, then the voice button proxy
3413 * will be hidden regardless of what shouldVoiceButtonProxyBeVisible() returns.
3414 */
3415 public void disableVoiceButtonProxy(boolean disabled) {
3416 updateVoiceButtonProxyVisible(disabled);
3417 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003418 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003419 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003420 */
3421 private void updateAppMarketIcon() {
Winson Chungd64a6662013-09-30 11:06:59 -07003422 if (!DISABLE_MARKET_BUTTON) {
3423 final View marketButton = findViewById(R.id.market_button);
3424 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3425 // Find the app market activity by resolving an intent.
3426 // (If multiple app markets are installed, it will return the ResolverActivity.)
3427 ComponentName activityName = intent.resolveActivity(getPackageManager());
3428 if (activityName != null) {
3429 int coi = getCurrentOrientationIndexForGlobalIcons();
3430 mAppMarketIntent = intent;
3431 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
3432 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3433 TOOLBAR_ICON_METADATA_NAME);
3434 marketButton.setVisibility(View.VISIBLE);
3435 } else {
3436 // We should hide and disable the view so that we don't try and restore the visibility
3437 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3438 marketButton.setVisibility(View.GONE);
3439 marketButton.setEnabled(false);
3440 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003441 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003442 }
3443
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003444 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungd64a6662013-09-30 11:06:59 -07003445 if (!DISABLE_MARKET_BUTTON) {
3446 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3447 Resources r = getResources();
3448 Drawable marketIconDrawable = d.newDrawable(r);
3449 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3450 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3451 marketIconDrawable.setBounds(0, 0, w, h);
Winson Chung1b884092012-06-08 17:13:02 -07003452
Winson Chungd64a6662013-09-30 11:06:59 -07003453 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
3454 }
Michael Jurka4ef207b2010-11-29 17:05:45 -08003455 }
3456
Michael Jurkad7c28052012-04-27 15:43:36 -07003457 @Override
3458 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003459 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003460 final List<CharSequence> text = event.getText();
3461 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003462 // Populate event with a fake title based on the current state.
3463 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen4b378872013-10-04 18:58:04 -07003464 text.add(mAppsCustomizeTabHost.getCurrentTabView().getContentDescription());
alanv1d4fde62012-10-17 13:15:47 -07003465 } else {
3466 text.add(getString(R.string.all_apps_home_button_label));
3467 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003468 return result;
3469 }
3470
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003471 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003472 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003473 */
3474 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3475 @Override
3476 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003477 closeSystemDialogs();
3478 }
3479 }
3480
3481 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003482 * Receives notifications whenever the appwidgets are reset.
3483 */
3484 private class AppWidgetResetObserver extends ContentObserver {
3485 public AppWidgetResetObserver() {
3486 super(new Handler());
3487 }
3488
3489 @Override
3490 public void onChange(boolean selfChange) {
3491 onAppWidgetReset();
3492 }
3493 }
3494
3495 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003496 * If the activity is currently paused, signal that we need to run the passed Runnable
3497 * in onResume.
3498 *
3499 * This needs to be called from incoming places where resources might have been loaded
3500 * while we are paused. That is becaues the Configuration might be wrong
3501 * when we're not running, and if it comes back to what it was when we
3502 * were paused, we are not restarted.
3503 *
3504 * Implementation of the method from LauncherModel.Callbacks.
3505 *
3506 * @return true if we are currently paused. The caller might be able to
3507 * skip some work in that case since we will come back again.
3508 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003509 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003510 if (mPaused) {
3511 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003512 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003513 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003514 }
3515 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003516 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003517 return true;
3518 } else {
3519 return false;
3520 }
3521 }
3522
Michael Jurkac402cd92013-05-20 15:49:32 +02003523 private boolean waitUntilResume(Runnable run) {
3524 return waitUntilResume(run, false);
3525 }
3526
Michael Jurka1e2f4652013-07-08 18:03:46 -07003527 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003528 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003529 }
3530
Michael Jurka7607c2f2013-04-03 14:33:19 -07003531 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003532 * If the activity is currently paused, signal that we need to re-run the loader
3533 * in onResume.
3534 *
3535 * This needs to be called from incoming places where resources might have been loaded
3536 * while we are paused. That is becaues the Configuration might be wrong
3537 * when we're not running, and if it comes back to what it was when we
3538 * were paused, we are not restarted.
3539 *
3540 * Implementation of the method from LauncherModel.Callbacks.
3541 *
3542 * @return true if we are currently paused. The caller might be able to
3543 * skip some work in that case since we will come back again.
3544 */
3545 public boolean setLoadOnResume() {
3546 if (mPaused) {
3547 Log.i(TAG, "setLoadOnResume");
3548 mOnResumeNeedsLoad = true;
3549 return true;
3550 } else {
3551 return false;
3552 }
3553 }
3554
3555 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003556 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003557 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003558 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003559 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003560 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003561 } else {
3562 return SCREEN_COUNT / 2;
3563 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003564 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003565
Joe Onorato9c1289c2009-08-17 11:03:03 -04003566 /**
3567 * Refreshes the shortcuts shown on the workspace.
3568 *
3569 * Implementation of the method from LauncherModel.Callbacks.
3570 */
3571 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003572 // If we're starting binding all over again, clear any bind calls we'd postponed in
3573 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3574 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003575 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003576
Winson Chungd64d1762013-08-20 14:37:16 -07003577 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003578 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003579 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003580
Michael Jurka05bf6442011-11-30 20:28:53 -08003581 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003582 if (mHotseat != null) {
3583 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003584 }
3585 }
3586
Adam Cohendcd297f2013-06-18 13:13:40 -07003587 @Override
3588 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003589 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003590
Adam Cohen5084cba2013-09-03 12:01:16 -07003591 // If there are no screens, we need to have an empty screen
3592 if (orderedScreenIds.size() == 0) {
3593 mWorkspace.addExtraEmptyScreen();
3594 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003595
3596 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003597 // setCurrentPage() so ensure that all pages are added before calling this).
3598 // The actual content of the custom page will be added during onFinishBindingItems().
Winson Chung0e6a7132013-08-23 12:55:10 -07003599 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
3600 mWorkspace.createCustomContentPage();
3601 }
Winson Chung64359a52013-07-08 17:17:08 -07003602 }
3603
3604 @Override
3605 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003606 int count = orderedScreenIds.size();
3607 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003608 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003609 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003610 }
3611
Adam Cohen39a06042013-07-19 14:30:12 -07003612 private boolean shouldShowWeightWatcher() {
3613 String spKey = LauncherAppState.getSharedPreferencesKey();
3614 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003615 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003616
3617 return show;
3618 }
3619
3620 private void toggleShowWeightWatcher() {
3621 String spKey = LauncherAppState.getSharedPreferencesKey();
3622 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3623 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3624
3625 show = !show;
3626
3627 SharedPreferences.Editor editor = sp.edit();
3628 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3629 editor.commit();
3630
3631 if (mWeightWatcher != null) {
3632 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3633 }
3634 }
3635
Winson Chungd64d1762013-08-20 14:37:16 -07003636 public void bindAppsAdded(final ArrayList<Long> newScreens,
3637 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003638 final ArrayList<ItemInfo> addAnimated,
3639 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003640 Runnable r = new Runnable() {
3641 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003642 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003643 }
3644 };
3645 if (waitUntilResume(r)) {
3646 return;
3647 }
3648
Winson Chungd64d1762013-08-20 14:37:16 -07003649 // Add the new screens
3650 bindAddScreens(newScreens);
3651
3652 // We add the items without animation on non-visible pages, and with
3653 // animations on the new page (which we will try and snap to).
3654 if (!addNotAnimated.isEmpty()) {
3655 bindItems(addNotAnimated, 0,
3656 addNotAnimated.size(), false);
3657 }
3658 if (!addAnimated.isEmpty()) {
3659 bindItems(addAnimated, 0,
3660 addAnimated.size(), true);
3661 }
Winson Chungc58497e2013-09-03 17:48:37 -07003662
Winson Chung87412982013-10-03 18:34:14 -07003663 // Remove the extra empty screen
3664 mWorkspace.removeExtraEmptyScreen();
3665
Winson Chungc58497e2013-09-03 17:48:37 -07003666 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
3667 addedApps != null && mAppsCustomizeContent != null) {
3668 mAppsCustomizeContent.addApps(addedApps);
3669 }
Winson Chungd64d1762013-08-20 14:37:16 -07003670 }
3671
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003672 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003673 * Bind the items start-end from the list.
3674 *
3675 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003676 */
Winson Chung64359a52013-07-08 17:17:08 -07003677 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3678 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003679 Runnable r = new Runnable() {
3680 public void run() {
3681 bindItems(shortcuts, start, end, forceAnimateIcons);
3682 }
3683 };
3684 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003685 return;
3686 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003687
Winson Chungf0c6ae02012-03-21 16:10:31 -07003688 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003689 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3690 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003691 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003692 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003693 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003694 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003695 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003696
3697 // Short circuit if we are loading dock items for a configuration which has no dock
3698 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3699 mHotseat == null) {
3700 continue;
3701 }
3702
Joe Onorato9c1289c2009-08-17 11:03:03 -04003703 switch (item.itemType) {
3704 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3705 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003706 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003707 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003708
Winson Chung64359a52013-07-08 17:17:08 -07003709 /*
3710 * TODO: FIX collision case
3711 */
Winson Chungce376632013-07-11 16:12:41 -07003712 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3713 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3714 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3715 throw new RuntimeException("OCCUPIED");
3716 }
Winson Chung64359a52013-07-08 17:17:08 -07003717 }
3718
Adam Cohendcd297f2013-06-18 13:13:40 -07003719 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3720 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003721 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003722 // Animate all the applications up now
3723 shortcut.setAlpha(0f);
3724 shortcut.setScaleX(0f);
3725 shortcut.setScaleY(0f);
3726 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003727 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003728 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003729 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003730 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003731 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003732 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003733 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003734 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3735 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003736 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003737 default:
3738 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003739 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003740 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003741
Winson Chung997a9232013-07-24 15:33:46 -07003742 if (animateIcons) {
3743 // Animate to the correct page
3744 if (newShortcutsScreenId > -1) {
3745 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003746 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003747 final Runnable startBounceAnimRunnable = new Runnable() {
3748 public void run() {
3749 anim.playTogether(bounceAnims);
3750 anim.start();
3751 }
3752 };
Winson Chung997a9232013-07-24 15:33:46 -07003753 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003754 // We post the animation slightly delayed to prevent slowdowns
3755 // when we are loading right after we return to launcher.
3756 mWorkspace.postDelayed(new Runnable() {
3757 public void run() {
3758 mWorkspace.snapToPage(newScreenIndex);
Winson Chungb2323832013-10-03 15:22:09 -07003759 mWorkspace.postDelayed(startBounceAnimRunnable,
3760 NEW_APPS_ANIMATION_DELAY);
Winson Chung94d67682013-09-25 16:29:40 -07003761 }
3762 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003763 } else {
3764 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003765 }
3766 }
Winson Chung64359a52013-07-08 17:17:08 -07003767 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003768 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003769 }
3770
Joe Onorato9c1289c2009-08-17 11:03:03 -04003771 /**
3772 * Implementation of the method from LauncherModel.Callbacks.
3773 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003774 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003775 Runnable r = new Runnable() {
3776 public void run() {
3777 bindFolders(folders);
3778 }
3779 };
3780 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003781 return;
3782 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003783 sFolders.clear();
3784 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003785 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003786
3787 /**
3788 * Add the views for a widget to the workspace.
3789 *
3790 * Implementation of the method from LauncherModel.Callbacks.
3791 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003792 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003793 Runnable r = new Runnable() {
3794 public void run() {
3795 bindAppWidget(item);
3796 }
3797 };
3798 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003799 return;
3800 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003801
Daniel Sandler843e8602010-06-07 14:59:01 -04003802 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3803 if (DEBUG_WIDGETS) {
3804 Log.d(TAG, "bindAppWidget: " + item);
3805 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003806 final Workspace workspace = mWorkspace;
3807
3808 final int appWidgetId = item.appWidgetId;
3809 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003810 if (DEBUG_WIDGETS) {
3811 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3812 }
3813
Joe Onorato9c1289c2009-08-17 11:03:03 -04003814 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3815
Joe Onorato9c1289c2009-08-17 11:03:03 -04003816 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003817 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003818
Adam Cohendcd297f2013-06-18 13:13:40 -07003819 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003820 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003821 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3822
Joe Onorato9c1289c2009-08-17 11:03:03 -04003823 workspace.requestLayout();
3824
Daniel Sandler843e8602010-06-07 14:59:01 -04003825 if (DEBUG_WIDGETS) {
3826 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3827 + (SystemClock.uptimeMillis()-start) + "ms");
3828 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003829 }
3830
Adam Cohen1462de32012-07-24 22:34:36 -07003831 public void onPageBoundSynchronously(int page) {
3832 mSynchronouslyBoundPages.add(page);
3833 }
3834
Joe Onorato9c1289c2009-08-17 11:03:03 -04003835 /**
3836 * Callback saying that there aren't any more items to bind.
3837 *
3838 * Implementation of the method from LauncherModel.Callbacks.
3839 */
Adam Cohene25af792013-06-06 23:08:25 -07003840 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07003841 Runnable r = new Runnable() {
3842 public void run() {
3843 finishBindingItems(upgradePath);
3844 }
3845 };
3846 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003847 return;
3848 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003849 if (mSavedState != null) {
3850 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003851 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003852 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003853 mSavedState = null;
3854 }
3855
Adam Cohen1462de32012-07-24 22:34:36 -07003856 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003857
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003858 // If we received the result of any pending adds while the loader was running (e.g. the
3859 // widget configuration forced an orientation change), process them now.
3860 for (int i = 0; i < sPendingAddList.size(); i++) {
3861 completeAdd(sPendingAddList.get(i));
3862 }
3863 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003864
Winson Chungc51db6a2011-10-05 11:44:49 -07003865 // Update the market app icon as necessary (the other icons will be managed in response to
3866 // package changes in bindSearchablesChanged()
Winson Chungd64a6662013-09-30 11:06:59 -07003867 if (!DISABLE_MARKET_BUTTON) {
3868 updateAppMarketIcon();
3869 }
Michael Jurkac1f5d262011-09-30 19:32:27 -07003870
Winson Chungf0c6ae02012-03-21 16:10:31 -07003871 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003872 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07003873 mWorkspace.getUniqueComponents(true, null);
3874 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07003875 }
Adam Cohen99894d92013-06-14 11:22:59 -07003876
Adam Cohen66a01fd2013-06-11 12:48:00 -07003877 mWorkspace.post(new Runnable() {
3878 @Override
3879 public void run() {
3880 onFinishBindingItems();
3881 }
3882 });
Winson Chungf0c6ae02012-03-21 16:10:31 -07003883 }
3884
Winson Chunga0b7e862013-09-05 16:03:15 -07003885 public boolean isAllAppsButtonRank(int rank) {
3886 if (mHotseat != null) {
3887 return mHotseat.isAllAppsButtonRank(rank);
3888 }
3889 return false;
3890 }
3891
Winson Chunga2413752012-04-03 14:22:34 -07003892 private boolean canRunNewAppsAnimation() {
3893 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3894 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3895 }
3896
Winson Chungc9168342013-06-26 14:54:55 -07003897 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
3898 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
3899 PropertyValuesHolder.ofFloat("alpha", 1f),
3900 PropertyValuesHolder.ofFloat("scaleX", 1f),
3901 PropertyValuesHolder.ofFloat("scaleY", 1f));
3902 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3903 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3904 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3905 return bounceAnim;
3906 }
3907
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003908 @Override
3909 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003910 boolean searchVisible = updateGlobalSearchIcon();
3911 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003912 if (mSearchDropTargetBar != null) {
3913 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3914 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003915 }
3916
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003917 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003918 * Add the icons for all apps.
3919 *
3920 * Implementation of the method from LauncherModel.Callbacks.
3921 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003922 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungc58497e2013-09-03 17:48:37 -07003923 if (AppsCustomizePagedView.DISABLE_ALL_APPS) {
3924 if (mIntentsOnWorkspaceFromUpgradePath != null) {
3925 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
3926 getHotseat().addAllAppsFolder(mIconCache, apps,
3927 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
3928 }
3929 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07003930 }
Winson Chungc58497e2013-09-03 17:48:37 -07003931 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07003932 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07003933 mAppsCustomizeContent.setApps(apps);
3934 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003935 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003936 }
3937
3938 /**
3939 * A package was updated.
3940 *
3941 * Implementation of the method from LauncherModel.Callbacks.
3942 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003943 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003944 Runnable r = new Runnable() {
3945 public void run() {
3946 bindAppsUpdated(apps);
3947 }
3948 };
3949 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003950 return;
3951 }
3952
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003953 if (mWorkspace != null) {
3954 mWorkspace.updateShortcuts(apps);
3955 }
Winson Chungc58497e2013-09-03 17:48:37 -07003956
3957 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
3958 mAppsCustomizeContent != null) {
3959 mAppsCustomizeContent.updateApps(apps);
3960 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003961 }
3962
3963 /**
Winson Chung83892cc2013-05-01 16:53:33 -07003964 * A package was uninstalled. We take both the super set of packageNames
3965 * in addition to specific applications to remove, the reason being that
3966 * this can be called when a package is updated as well. In that scenario,
3967 * we only remove specific components from the workspace, where as
3968 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003969 *
3970 * Implementation of the method from LauncherModel.Callbacks.
3971 */
Winson Chung83892cc2013-05-01 16:53:33 -07003972 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003973 final ArrayList<AppInfo> appInfos,
Winson Chung64359a52013-07-08 17:17:08 -07003974 final boolean packageRemoved) {
Winson Chungd64d1762013-08-20 14:37:16 -07003975 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003976 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07003977 bindComponentsRemoved(packageNames, appInfos, packageRemoved);
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 Chung64359a52013-07-08 17:17:08 -07003984 if (packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07003985 mWorkspace.removeItemsByPackageName(packageNames);
3986 } else {
3987 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04003988 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003989
Winson Chunga1820962011-10-03 16:31:06 -07003990 // Notify the drag controller
Winson Chung83892cc2013-05-01 16:53:33 -07003991 mDragController.onAppsRemoved(appInfos, this);
Winson Chungc58497e2013-09-03 17:48:37 -07003992
3993 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
3994 mAppsCustomizeContent != null) {
3995 mAppsCustomizeContent.removeApps(appInfos);
3996 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003997 }
Joe Onoratobe386092009-11-17 17:32:16 -08003998
3999 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004000 * A number of packages were updated.
4001 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004002 private ArrayList<Object> mWidgetsAndShortcuts;
4003 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004004 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004005 bindPackagesUpdated(mWidgetsAndShortcuts);
4006 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004007 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004008 };
4009
4010 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4011 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4012 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004013 return;
4014 }
4015
Winson Chung64359a52013-07-08 17:17:08 -07004016 // Update the widgets pane
Winson Chungc58497e2013-09-03 17:48:37 -07004017 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
4018 mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004019 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004020 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004021 }
4022
Winson Chung400438b2011-01-16 17:53:48 -08004023 private int mapConfigurationOriActivityInfoOri(int configOri) {
4024 final Display d = getWindowManager().getDefaultDisplay();
4025 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4026 switch (d.getRotation()) {
4027 case Surface.ROTATION_0:
4028 case Surface.ROTATION_180:
4029 // We are currently in the same basic orientation as the natural orientation
4030 naturalOri = configOri;
4031 break;
4032 case Surface.ROTATION_90:
4033 case Surface.ROTATION_270:
4034 // We are currently in the other basic orientation to the natural orientation
4035 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4036 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4037 break;
4038 }
4039
4040 int[] oriMap = {
4041 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4042 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4043 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4044 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4045 };
4046 // Since the map starts at portrait, we need to offset if this device's natural orientation
4047 // is landscape.
4048 int indexOffset = 0;
4049 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4050 indexOffset = 1;
4051 }
4052 return oriMap[(d.getRotation() + indexOffset) % 4];
4053 }
Adam Cohen446e9402011-09-15 18:21:21 -07004054
Winson Chung4b919f82012-05-01 10:44:08 -07004055 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08004056 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07004057 getResources().getBoolean(R.bool.allow_rotation);
4058 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08004059 }
Winson Chung4b919f82012-05-01 10:44:08 -07004060 public void lockScreenOrientation() {
4061 if (isRotationEnabled()) {
4062 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4063 .getConfiguration().orientation));
4064 }
4065 }
4066 public void unlockScreenOrientation(boolean immediate) {
4067 if (isRotationEnabled()) {
4068 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004069 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004070 } else {
4071 mHandler.postDelayed(new Runnable() {
4072 public void run() {
4073 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4074 }
4075 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004076 }
Winson Chung4b919f82012-05-01 10:44:08 -07004077 }
Winson Chung400438b2011-01-16 17:53:48 -08004078 }
4079
Winson Chung82f55532011-08-09 14:14:23 -07004080 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07004081 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07004082 if (DISABLE_CLINGS) {
4083 return false;
4084 }
4085
Winson Chung3a6e7f32013-10-09 15:50:52 -07004086 // For now, limit only to phones
4087 LauncherAppState app = LauncherAppState.getInstance();
4088 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
4089 if (grid.isTablet()) {
4090 return false;
4091 }
4092
Brett Chabot2a9e2282011-08-23 15:03:13 -07004093 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07004094 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07004095
Casey Burkhardt0d9cbae2013-10-04 14:13:15 -07004096 // Disable clings for accessibility when explore by touch is enabled
4097 final AccessibilityManager a11yManager = (AccessibilityManager) getSystemService(
4098 ACCESSIBILITY_SERVICE);
4099 if (a11yManager.isTouchExplorationEnabled()) {
4100 return false;
4101 }
4102
Michael Jurkae233a8b2013-03-19 13:49:20 +01004103 // Restricted secondary users (child mode) will potentially have very few apps
4104 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04004105// boolean supportsLimitedUsers =
4106// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
4107// Account[] accounts = AccountManager.get(this).getAccounts();
4108// if (supportsLimitedUsers && accounts.length == 0) {
4109// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
4110// Bundle restrictions = um.getUserRestrictions();
4111// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
4112// return false;
4113// }
4114// }
Winson Chung7d7541e2011-09-16 20:14:36 -07004115 return true;
Winson Chung82f55532011-08-09 14:14:23 -07004116 }
Michael Jurka22143132012-10-04 17:42:38 +02004117
Winson Chungfa545132013-09-26 17:45:32 -07004118 private Cling initCling(int clingId, int scrimId, boolean animate,
Winson Chungaf40f202013-09-18 18:26:31 -07004119 boolean dimNavBarVisibilty) {
Winson Chungfa545132013-09-26 17:45:32 -07004120 Cling cling = (Cling) findViewById(clingId);
4121 View scrim = null;
4122 if (scrimId > 0) {
Sandeep Siddharthaa6125ef2013-09-26 20:30:40 -07004123 scrim = findViewById(R.id.cling_scrim);
Winson Chungfa545132013-09-26 17:45:32 -07004124 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004125 if (cling != null) {
Winson Chungfa545132013-09-26 17:45:32 -07004126 cling.init(this, scrim);
Winson Chungaf40f202013-09-18 18:26:31 -07004127 cling.show(animate, SHOW_CLING_DURATION);
4128
4129 if (dimNavBarVisibilty) {
4130 cling.setSystemUiVisibility(cling.getSystemUiVisibility() |
4131 View.SYSTEM_UI_FLAG_LOW_PROFILE);
Winson Chung7d7541e2011-09-16 20:14:36 -07004132 }
4133 }
4134 return cling;
4135 }
Michael Jurka22143132012-10-04 17:42:38 +02004136
Winson Chungaf40f202013-09-18 18:26:31 -07004137 private void dismissCling(final Cling cling, final Runnable postAnimationCb,
4138 final String flag, int duration, boolean restoreNavBarVisibilty) {
Winson Chung7819abd2012-11-29 14:29:38 -08004139 // To catch cases where siblings of top-level views are made invisible, just check whether
4140 // the cling is directly set to GONE before dismissing it.
4141 if (cling != null && cling.getVisibility() != View.GONE) {
Winson Chungaf40f202013-09-18 18:26:31 -07004142 final Runnable cleanUpClingCb = new Runnable() {
4143 public void run() {
Winson Chung82f55532011-08-09 14:14:23 -07004144 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08004145 // We should update the shared preferences on a background thread
4146 new Thread("dismissClingThread") {
4147 public void run() {
4148 SharedPreferences.Editor editor = mSharedPrefs.edit();
4149 editor.putBoolean(flag, true);
4150 editor.commit();
4151 }
4152 }.start();
Winson Chungaf40f202013-09-18 18:26:31 -07004153 if (postAnimationCb != null) {
4154 postAnimationCb.run();
4155 }
4156 }
4157 };
4158 if (duration <= 0) {
4159 cleanUpClingCb.run();
4160 } else {
4161 cling.hide(duration, cleanUpClingCb);
4162 }
Michael Jurka22143132012-10-04 17:42:38 +02004163 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chungaf40f202013-09-18 18:26:31 -07004164
4165 if (restoreNavBarVisibilty) {
4166 cling.setSystemUiVisibility(cling.getSystemUiVisibility() &
4167 ~View.SYSTEM_UI_FLAG_LOW_PROFILE);
4168 }
Winson Chung82f55532011-08-09 14:14:23 -07004169 }
4170 }
Michael Jurka22143132012-10-04 17:42:38 +02004171
Winson Chung9d9d74f2011-09-19 11:49:12 -07004172 private void removeCling(int id) {
4173 final View cling = findViewById(id);
4174 if (cling != null) {
4175 final ViewGroup parent = (ViewGroup) cling.getParent();
4176 parent.post(new Runnable() {
4177 @Override
4178 public void run() {
4179 parent.removeView(cling);
4180 }
4181 });
Michael Jurka22143132012-10-04 17:42:38 +02004182 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004183 }
4184 }
Michael Jurka974c3862012-05-22 22:00:31 -07004185
4186 private boolean skipCustomClingIfNoAccounts() {
4187 Cling cling = (Cling) findViewById(R.id.workspace_cling);
4188 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
4189 if (customCling) {
4190 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04004191 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07004192 Account[] accounts = am.getAccountsByType("com.google");
4193 return accounts.length == 0;
4194 }
4195 return false;
4196 }
4197
Winson Chung82e5c982013-10-09 12:04:50 -07004198 public void updateCustomContentHintVisibility() {
4199 Cling cling = (Cling) findViewById(R.id.first_run_cling);
4200 String ccHintStr = getFirstRunCustomContentHint();
4201
4202 if (mWorkspace.hasCustomContent()) {
4203 // Show the custom content hint if ccHintStr is not empty
4204 if (cling != null) {
4205 setCustomContentHintVisibility(cling, ccHintStr, true, true);
4206 }
4207 } else {
4208 // Hide the custom content hint
4209 if (cling != null) {
4210 setCustomContentHintVisibility(cling, ccHintStr, false, true);
4211 }
4212 }
4213 }
4214
4215 private void setCustomContentHintVisibility(Cling cling, String ccHintStr, boolean visible,
4216 boolean animate) {
4217 final TextView ccHint = (TextView) cling.findViewById(R.id.custom_content_hint);
4218 if (ccHint != null) {
4219 if (visible && !ccHintStr.isEmpty()) {
4220 ccHint.setText(ccHintStr);
4221 ccHint.setVisibility(View.VISIBLE);
4222 if (animate) {
4223 ccHint.setAlpha(0f);
4224 ccHint.animate().alpha(1f)
4225 .setDuration(SHOW_CLING_DURATION)
4226 .start();
4227 } else {
4228 ccHint.setAlpha(1f);
4229 }
4230 } else {
4231 if (animate) {
4232 ccHint.animate().alpha(0f)
4233 .setDuration(SHOW_CLING_DURATION)
4234 .setListener(new AnimatorListenerAdapter() {
4235 @Override
4236 public void onAnimationEnd(Animator animation) {
4237 ccHint.setVisibility(View.GONE);
4238 }
4239 })
4240 .start();
4241 } else {
4242 ccHint.setAlpha(0f);
4243 ccHint.setVisibility(View.GONE);
4244 }
4245 }
4246 }
4247 }
4248
Winson Chungaf40f202013-09-18 18:26:31 -07004249 public void showFirstRunCling() {
Winson Chung46353de2012-02-16 14:05:10 -08004250 if (isClingsEnabled() &&
Winson Chungaf40f202013-09-18 18:26:31 -07004251 !mSharedPrefs.getBoolean(Cling.FIRST_RUN_CLING_DISMISSED_KEY, false) &&
Michael Jurka974c3862012-05-22 22:00:31 -07004252 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004253 // If we're not using the default workspace layout, replace workspace cling
4254 // with a custom workspace cling (usually specified in an overlay)
4255 // For now, only do this on tablets
Winson Chunge6eabff2013-09-24 11:01:23 -07004256 if (!DISABLE_CUSTOM_CLINGS) {
4257 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
4258 getResources().getBoolean(R.bool.config_useCustomClings)) {
4259 // Use a custom cling
4260 View cling = findViewById(R.id.workspace_cling);
4261 ViewGroup clingParent = (ViewGroup) cling.getParent();
4262 int clingIndex = clingParent.indexOfChild(cling);
4263 clingParent.removeViewAt(clingIndex);
4264 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4265 clingParent.addView(customCling, clingIndex);
4266 customCling.setId(R.id.workspace_cling);
4267 }
4268 }
4269 Cling cling = (Cling) findViewById(R.id.first_run_cling);
4270 if (cling != null) {
4271 String sbHintStr = getFirstRunClingSearchBarHint();
4272 String ccHintStr = getFirstRunCustomContentHint();
4273 if (!sbHintStr.isEmpty()) {
4274 TextView sbHint = (TextView) cling.findViewById(R.id.search_bar_hint);
4275 sbHint.setText(sbHintStr);
4276 sbHint.setVisibility(View.VISIBLE);
4277 }
Winson Chung82e5c982013-10-09 12:04:50 -07004278 setCustomContentHintVisibility(cling, ccHintStr, true, false);
Michael Jurka45355c42012-10-08 13:21:35 +02004279 }
Winson Chungfa545132013-09-26 17:45:32 -07004280 initCling(R.id.first_run_cling, 0, false, true);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004281 } else {
Winson Chungaf40f202013-09-18 18:26:31 -07004282 removeCling(R.id.first_run_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004283 }
4284 }
Winson Chungaf40f202013-09-18 18:26:31 -07004285
Winson Chunge6eabff2013-09-24 11:01:23 -07004286 protected String getFirstRunClingSearchBarHint() {
4287 return "";
4288 }
4289 protected String getFirstRunCustomContentHint() {
4290 return "";
4291 }
Winson Chung3a6e7f32013-10-09 15:50:52 -07004292 protected int getFirstRunFocusedHotseatAppDrawableId() {
4293 return -1;
4294 }
4295 protected ComponentName getFirstRunFocusedHotseatAppComponentName() {
4296 return null;
4297 }
4298 protected int getFirstRunFocusedHotseatAppRank() {
4299 return -1;
4300 }
4301 protected String getFirstRunFocusedHotseatAppBubbleTitle() {
4302 return "";
4303 }
4304 protected String getFirstRunFocusedHotseatAppBubbleDescription() {
4305 return "";
4306 }
Winson Chunge6eabff2013-09-24 11:01:23 -07004307
Winson Chungaf40f202013-09-18 18:26:31 -07004308 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004309 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004310 if (isClingsEnabled() &&
Winson Chungaf40f202013-09-18 18:26:31 -07004311 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false)) {
Winson Chung3a6e7f32013-10-09 15:50:52 -07004312 Cling c = initCling(R.id.workspace_cling, 0, false, true);
4313
4314 // Set the focused hotseat app if there is one
4315 c.setFocusedHotseatApp(getFirstRunFocusedHotseatAppDrawableId(),
4316 getFirstRunFocusedHotseatAppRank(),
4317 getFirstRunFocusedHotseatAppComponentName(),
4318 getFirstRunFocusedHotseatAppBubbleTitle(),
4319 getFirstRunFocusedHotseatAppBubbleDescription());
Winson Chung9d9d74f2011-09-19 11:49:12 -07004320 } else {
Winson Chungaf40f202013-09-18 18:26:31 -07004321 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004322 }
4323 }
4324 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004325 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004326 if (isClingsEnabled() &&
4327 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
Winson Chungfa545132013-09-26 17:45:32 -07004328 Cling cling = initCling(R.id.folder_cling, R.id.cling_scrim,
4329 true, true);
Winson Chungaf40f202013-09-18 18:26:31 -07004330 return cling;
Winson Chung9d9d74f2011-09-19 11:49:12 -07004331 } else {
4332 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004333 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004334 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004335 }
Michael Jurka104c4562013-07-08 18:03:46 -07004336 protected SharedPreferences getSharedPrefs() {
4337 return mSharedPrefs;
4338 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004339 public boolean isFolderClingVisible() {
4340 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004341 if (cling != null) {
4342 return cling.getVisibility() == View.VISIBLE;
4343 }
4344 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004345 }
Winson Chungaf40f202013-09-18 18:26:31 -07004346 public void dismissFirstRunCling(View v) {
4347 Cling cling = (Cling) findViewById(R.id.first_run_cling);
4348 Runnable cb = new Runnable() {
4349 public void run() {
4350 // Show the workspace cling next
4351 showFirstRunWorkspaceCling();
4352 }
4353 };
4354 dismissCling(cling, cb, Cling.FIRST_RUN_CLING_DISMISSED_KEY,
4355 DISMISS_CLING_DURATION, false);
Winson Chung3a6e7f32013-10-09 15:50:52 -07004356
4357 // Fade out the search bar for the workspace cling coming up
4358 mSearchDropTargetBar.hideSearchBar(true);
Winson Chungaf40f202013-09-18 18:26:31 -07004359 }
Winson Chung82f55532011-08-09 14:14:23 -07004360 public void dismissWorkspaceCling(View v) {
4361 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chungaf40f202013-09-18 18:26:31 -07004362 Runnable cb = null;
4363 if (v == null) {
4364 cb = new Runnable() {
4365 public void run() {
4366 mWorkspace.enterOverviewMode();
4367 }
4368 };
4369 }
4370 dismissCling(cling, cb, Cling.WORKSPACE_CLING_DISMISSED_KEY,
4371 DISMISS_CLING_DURATION, true);
Winson Chung3a6e7f32013-10-09 15:50:52 -07004372
4373 // Fade in the search bar
4374 mSearchDropTargetBar.showSearchBar(true);
Winson Chung7d7541e2011-09-16 20:14:36 -07004375 }
4376 public void dismissFolderCling(View v) {
4377 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chungaf40f202013-09-18 18:26:31 -07004378 dismissCling(cling, null, Cling.FOLDER_CLING_DISMISSED_KEY,
4379 DISMISS_CLING_DURATION, true);
Winson Chung82f55532011-08-09 14:14:23 -07004380 }
4381
Winson Chung80baf5a2010-08-09 16:03:15 -07004382 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004383 * Prints out out state for debugging.
4384 */
4385 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004386 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004387 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004388 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4389 Log.d(TAG, "mRestoring=" + mRestoring);
4390 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4391 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004392 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004393 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004394
Winson Chung785d2eb2011-04-14 16:08:02 -07004395 if (mAppsCustomizeContent != null) {
4396 mAppsCustomizeContent.dumpState();
4397 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004398 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004399 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004400
4401 @Override
4402 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4403 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004404 synchronized (sDumpLogs) {
4405 writer.println(" ");
4406 writer.println("Debug logs: ");
4407 for (int i = 0; i < sDumpLogs.size(); i++) {
4408 writer.println(" " + sDumpLogs.get(i));
4409 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004410 }
4411 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004412
4413 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004414 if (DEBUG_DUMP_LOG) {
4415 synchronized (sDumpLogs) {
4416 Log.d(TAG, "");
4417 Log.d(TAG, "*********************");
4418 Log.d(TAG, "Launcher debug logs: ");
4419 for (int i = 0; i < sDumpLogs.size(); i++) {
4420 Log.d(TAG, " " + sDumpLogs.get(i));
4421 }
4422 Log.d(TAG, "*********************");
4423 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004424 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004425 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004426 }
4427
4428 public static void addDumpLog(String tag, String log, boolean debugLog) {
4429 if (debugLog) {
4430 Log.d(tag, log);
4431 }
Winson Chungede41292013-09-19 16:27:36 -07004432 if (DEBUG_DUMP_LOG) {
4433 sDateStamp.setTime(System.currentTimeMillis());
4434 synchronized (sDumpLogs) {
4435 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log);
Adam Cohen4caf2982013-08-20 18:54:31 -07004436 }
Winson Chungede41292013-09-19 16:27:36 -07004437 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004438 }
4439
Winson Chungede41292013-09-19 16:27:36 -07004440 public void dumpLogsToLocalData() {
4441 if (DEBUG_DUMP_LOG) {
4442 new Thread("DumpLogsToLocalData") {
4443 @Override
4444 public void run() {
4445 boolean success = false;
4446 sDateStamp.setTime(sRunStart);
4447 String FILENAME = sDateStamp.getMonth() + "-"
4448 + sDateStamp.getDay() + "_"
4449 + sDateStamp.getHours() + "-"
4450 + sDateStamp.getMinutes() + "_"
4451 + sDateStamp.getSeconds() + ".txt";
4452
4453 FileOutputStream fos = null;
4454 File outFile = null;
4455 try {
4456 outFile = new File(getFilesDir(), FILENAME);
4457 outFile.createNewFile();
4458 fos = new FileOutputStream(outFile);
4459 } catch (Exception e) {
4460 e.printStackTrace();
4461 }
4462 if (fos != null) {
4463 PrintWriter writer = new PrintWriter(fos);
4464
4465 writer.println(" ");
4466 writer.println("Debug logs: ");
4467 synchronized (sDumpLogs) {
4468 for (int i = 0; i < sDumpLogs.size(); i++) {
4469 writer.println(" " + sDumpLogs.get(i));
4470 }
4471 }
4472 writer.close();
4473 }
4474 try {
4475 if (fos != null) {
4476 fos.close();
4477 success = true;
4478 }
4479 } catch (IOException e) {
4480 e.printStackTrace();
4481 }
4482 }
4483 }.start();
Adam Cohen4caf2982013-08-20 18:54:31 -07004484 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004485 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004486}
Michael Jurka2763be32011-02-24 11:19:57 -08004487
Michael Jurkaabded662011-03-04 12:06:57 -08004488interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004489 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004490 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004491 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004492 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004493 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004494}