blob: 43a781d4e1765d765c204a59d8f4d1ff04e09e73 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080019import static android.content.pm.ActivityInfo.CONFIG_ORIENTATION;
20import static android.content.pm.ActivityInfo.CONFIG_SCREEN_SIZE;
Sunny Goyal623eddd2018-03-02 12:24:41 -080021
Sunny Goyal4c7f2152017-10-17 17:17:16 -070022import static com.android.launcher3.LauncherAnimUtils.SPRING_LOADED_EXIT_DELAY;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -070023import static com.android.launcher3.LauncherState.ALL_APPS;
24import static com.android.launcher3.LauncherState.NORMAL;
25import static com.android.launcher3.LauncherState.OVERVIEW;
Sunny Goyalaeb16432017-10-16 11:46:41 -070026import static com.android.launcher3.logging.LoggerUtils.newContainerTarget;
Sunny Goyal326403e2017-10-02 12:45:10 -070027
Gilles Debunnedd6c9922010-10-25 11:23:41 -070028import android.animation.Animator;
Jon Miranda2d89ea82017-05-04 11:47:53 -070029import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070030import android.animation.AnimatorSet;
Jon Miranda2d89ea82017-05-04 11:47:53 -070031import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070032import android.animation.ValueAnimator;
Adam Cohen0b395352014-06-09 22:54:36 +000033import android.annotation.TargetApi;
Tony Wickham005df0b2018-02-13 11:28:12 -080034import android.app.ActivityOptions;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080035import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070036import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080038import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080039import android.content.ComponentCallbacks2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.Context;
Andrew Sappersteinabef55a2016-06-19 12:49:00 -070041import android.content.ContextWrapper;
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;
Adam Cohen173f7112015-03-27 15:14:00 -070044import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070045import android.content.SharedPreferences;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.content.pm.PackageManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080047import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070048import android.database.sqlite.SQLiteDatabase;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070049import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020050import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051import android.os.Bundle;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070052import android.os.Parcelable;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080053import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070054import android.os.StrictMode;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070055import android.os.UserHandle;
Tony Wickham86222d22017-03-29 15:30:43 -070056import android.support.annotation.Nullable;
Michael Jurkaa33411c2012-06-14 16:18:21 -070057import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070059import android.util.Log;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070060import android.util.SparseArray;
Adam Cohen96d30a12013-07-16 18:13:21 -070061import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070062import android.view.KeyboardShortcutGroup;
63import android.view.KeyboardShortcutInfo;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080064import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -070065import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070066import android.view.View;
Adam Cohen96d30a12013-07-16 18:13:21 -070067import android.view.ViewGroup;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080068import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070069import android.view.animation.OvershootInterpolator;
Adam Cohen96d30a12013-07-16 18:13:21 -070070import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070071
Sunny Goyal651077b2014-06-30 14:15:31 -070072import com.android.launcher3.DropTarget.DragObject;
Jon Mirandac476d6e2017-05-04 16:30:01 -070073import com.android.launcher3.Workspace.ItemOperator;
Sunny Goyalae502842016-06-17 08:43:56 -070074import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070075import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070076import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal7ede6112017-12-05 15:11:21 -080077import com.android.launcher3.allapps.DiscoveryBounce;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070078import com.android.launcher3.badge.BadgeInfo;
Sunny Goyalffe83f12014-08-14 17:39:34 -070079import com.android.launcher3.compat.AppWidgetManagerCompat;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -070080import com.android.launcher3.compat.LauncherAppsCompatVO;
Tony Wickhame0c33232016-02-08 11:37:04 -080081import com.android.launcher3.config.FeatureFlags;
Vadim Tryshevfedca432015-08-19 17:55:02 -070082import com.android.launcher3.dragndrop.DragController;
83import com.android.launcher3.dragndrop.DragLayer;
84import com.android.launcher3.dragndrop.DragView;
Mario Bertschler27288382017-05-24 15:35:09 -070085import com.android.launcher3.dynamicui.WallpaperColorInfo;
Sunny Goyal26119432016-02-18 22:09:23 +000086import com.android.launcher3.folder.FolderIcon;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070087import com.android.launcher3.folder.FolderIconPreviewVerifier;
Sunny Goyal66b24572016-09-21 15:57:55 -070088import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -070089import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -070090import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -070091import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyala535ae42017-02-27 10:07:13 -080092import com.android.launcher3.model.ModelWriter;
Sunny Goyala535ae42017-02-27 10:07:13 -080093import com.android.launcher3.notification.NotificationListener;
Tony Wickham540913e2017-01-23 11:47:51 -080094import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -080095import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -070096import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyalf8088ee2017-11-10 14:52:00 -080097import com.android.launcher3.states.InternalStateHandler;
Sunny Goyal623eddd2018-03-02 12:24:41 -080098import com.android.launcher3.states.RotationHelper;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -080099import com.android.launcher3.touch.ItemClickHandler;
Sunny Goyala5ace712017-11-15 17:12:51 -0800100import com.android.launcher3.uioverrides.UiFactory;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530101import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
102import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
Sunny Goyalaeb16432017-10-16 11:46:41 -0700103import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyal2100c782016-08-22 16:00:03 -0700104import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700105import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700106import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700107import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700108import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800109import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700110import com.android.launcher3.util.PendingRequestArgs;
Sunny Goyal8392c822017-06-20 10:03:56 -0700111import com.android.launcher3.util.SystemUiController;
Mario Bertschlera6936942017-05-31 14:48:19 -0700112import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700113import com.android.launcher3.util.Thunk;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700114import com.android.launcher3.util.TraceHelper;
Sunny Goyal326403e2017-10-02 12:45:10 -0700115import com.android.launcher3.util.UiThreadHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700116import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800117import com.android.launcher3.views.OptionsPopupView;
Sunny Goyal29947f02017-12-18 13:49:44 -0800118import com.android.launcher3.widget.LauncherAppWidgetHostView;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800119import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700120import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal29947f02017-12-18 13:49:44 -0800121import com.android.launcher3.widget.PendingAppWidgetHostView;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800122import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700123import com.android.launcher3.widget.WidgetHostViewLoader;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700124import com.android.launcher3.widget.WidgetListRowEntry;
125import com.android.launcher3.widget.WidgetsFullSheet;
Sunny Goyal952e63d2017-08-16 04:59:08 -0700126import com.android.launcher3.widget.custom.CustomWidgetParser;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700127
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700128import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700129import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700130import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700131import java.util.Collection;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700132import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700133import java.util.List;
Tony Wickham010d2552017-01-20 08:15:28 -0800134import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700135
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136/**
137 * Default launcher application.
138 */
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700139public class Launcher extends BaseDraggingActivity implements LauncherExterns, LauncherModel.Callbacks,
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800140 LauncherProviderChangeListener, WallpaperColorInfo.OnThemeChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700141 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700142 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143
Winson Chunga2413752012-04-03 14:22:34 -0700144 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700145
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700147 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700148
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700149 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700150 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151
Michael Jurka8b805b12012-04-18 14:23:14 -0700152 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800153 public static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
154 public static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700155
Jon Mirandac476d6e2017-05-04 16:30:01 -0700156 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700157
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700158 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
159
Mathew Inwood876a8462013-06-14 14:12:41 +0100160 /**
161 * IntentStarter uses request codes starting with this. This must be greater than all activity
162 * request codes used internally.
163 */
164 protected static final int REQUEST_LAST = 100;
165
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 // Type: int
167 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700168 // Type: int
169 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700170 // Type: PendingRequestArgs
171 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
172 // Type: ActivityResultInfo
173 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700174 // Type: SparseArray<Parcelable>
175 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700177 private LauncherStateManager mStateManager;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700178
Adam Cohenad4e15c2013-10-17 16:21:35 -0700179 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180
Winson Chunga2413752012-04-03 14:22:34 -0700181 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700182 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
183 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
184 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700185
Jon Miranda54441f52018-01-24 15:38:25 -0800186 private LauncherAppTransitionManager mAppTransitionManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800187 private Configuration mOldConfig;
Jon Miranda54441f52018-01-24 15:38:25 -0800188
Adam Cohen091440a2015-03-18 14:16:05 -0700189 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700190 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700191 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800192 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700193
Sunny Goyalffe83f12014-08-14 17:39:34 -0700194 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700195 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700196
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700197 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700198
Sunny Goyal316490e2015-06-02 09:38:28 -0700199 @Thunk Hotseat mHotseat;
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700200 private View mDragHandleIndicator;
201 @Nullable private View mHotseatSearchBox;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700202
Sunny Goyal47328fd2016-05-25 18:56:41 -0700203 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700204
205 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700206 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700207 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700208
Winson Chung8ae41982017-11-10 11:42:13 -0800209 // UI and state for the overview panel
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800210 private View mOverviewPanel;
Winson Chung8ae41982017-11-10 11:42:13 -0800211
Adam Cohen091440a2015-03-18 14:16:05 -0700212 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400213
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700214 private OnResumeCallback mOnResumeCallback;
215
Sunny Goyal527c7d32015-08-28 15:19:36 -0700216 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700217
Joe Onorato9c1289c2009-08-17 11:03:03 -0400218 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800219 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800220 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700221 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400222
Jon Miranda2d89ea82017-05-04 11:47:53 -0700223 private ObjectAnimator mScrimAnimator;
Jon Miranda7fda2852017-07-19 16:07:01 -0700224 private boolean mShouldFadeInScrim;
Jon Miranda2d89ea82017-05-04 11:47:53 -0700225
Tony Wickham010d2552017-01-20 08:15:28 -0800226 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700227
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800228 private int mSynchronouslyBoundPage = PagedView.INVALID_PAGE;
Adam Cohen1462de32012-07-24 22:34:36 -0700229
Winson Chung46353de2012-02-16 14:05:10 -0800230 // We only want to get the SharedPreferences once since it does an FS stat each time we get
231 // it from the context.
232 private SharedPreferences mSharedPrefs;
233
Sunny Goyal2100c782016-08-22 16:00:03 -0700234 // Activity result which needs to be processed after workspace has loaded.
235 private ActivityResultInfo mPendingActivityResult;
236 /**
237 * Holds extra information required to handle a result from an external call, like
238 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
239 */
240 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800241
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700242 public ViewGroupFocusHelper mFocusHandler;
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700243 private boolean mAppLaunchSuccess;
244
Sunny Goyal623eddd2018-03-02 12:24:41 -0800245 private RotationHelper mRotationHelper;
Sunny Goyal7779d622015-06-11 16:18:39 -0700246
Winson Chungedb0d6b2018-03-21 17:32:55 -0700247 // Used to keep track of the swipe up state
248 private SharedPreferences.OnSharedPreferenceChangeListener mSharedPrefsListener =
249 (sharedPreferences, s) -> {
250 mDragLayer.setup(mDragController);
251 };
252
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800253 @Override
254 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700255 if (DEBUG_STRICT_MODE) {
256 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
257 .detectDiskReads()
258 .detectDiskWrites()
259 .detectNetwork() // or .detectAll() for all detectable problems
260 .penaltyLog()
261 .build());
262 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
263 .detectLeakedSqlLiteObjects()
264 .detectLeakedClosableObjects()
265 .penaltyLog()
266 .penaltyDeath()
267 .build());
268 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700269 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700270
Mario Bertschler27288382017-05-24 15:35:09 -0700271 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
272 wallpaperColorInfo.setOnThemeChangeListener(this);
Mario Bertschlera6936942017-05-31 14:48:19 -0700273 overrideTheme(wallpaperColorInfo.isDark(), wallpaperColorInfo.supportsDarkText());
Mario Bertschler27288382017-05-24 15:35:09 -0700274
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800275 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700276 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400277
Sunny Goyal87f784c2017-01-11 10:48:34 -0800278 LauncherAppState app = LauncherAppState.getInstance(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800279 mOldConfig = new Configuration(getResources().getConfiguration());
Sunny Goyal0e7724c2018-02-22 13:22:21 -0800280 mModel = app.setLauncher(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800281 initDeviceProfile(app.getInvariantDeviceProfile());
Winson Chung6e1c0d32013-10-25 15:24:24 -0700282
Sunny Goyalf7258242015-10-19 16:59:07 -0700283 mSharedPrefs = Utilities.getPrefs(this);
Winson Chungedb0d6b2018-03-21 17:32:55 -0700284 mSharedPrefs.registerOnSharedPreferenceChangeListener(mSharedPrefsListener);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800285 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700286 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700287
Joe Onorato41a12d22009-10-31 18:30:00 -0400288 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700289 mAllAppsController = new AllAppsTransitionController(this);
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800290 mStateManager = new LauncherStateManager(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700291
Sunny Goyalffe83f12014-08-14 17:39:34 -0700292 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700293
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700294 mAppWidgetHost = new LauncherAppWidgetHost(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700295 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700296
Sunny Goyal60820d72017-05-09 12:40:11 -0700297 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700298
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800299 setupViews();
Tony Wickham010d2552017-01-20 08:15:28 -0800300 mPopupDataProvider = new PopupDataProvider(this);
301
Sunny Goyal623eddd2018-03-02 12:24:41 -0800302 mRotationHelper = new RotationHelper(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800303
Sunny Goyald3864fa2017-11-14 15:06:36 -0800304 boolean internalStateHandled = InternalStateHandler.handleCreate(this, getIntent());
305 if (internalStateHandled) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800306 if (savedInstanceState != null) {
307 // InternalStateHandler has already set the appropriate state.
308 // We dont need to do anything.
309 savedInstanceState.remove(RUNTIME_STATE);
310 }
311 }
312 restoreState(savedInstanceState);
Winson Chungb63b44c2017-11-10 17:54:44 -0800313
Sunny Goyal2100c782016-08-22 16:00:03 -0700314 // We only load the page synchronously if the user rotates (or triggers a
315 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700316 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
317 if (savedInstanceState != null) {
318 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
319 }
Sunny Goyald3864fa2017-11-14 15:06:36 -0800320
Sunny Goyalfe770c92016-09-27 14:38:58 -0700321 if (!mModel.startLoader(currentScreen)) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800322 if (!internalStateHandled) {
323 // If we are not binding synchronously, show a fade in animation when
324 // the first page bind completes.
325 mDragLayer.setAlpha(0);
326 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700327 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700328 // Pages bound synchronously.
329 mWorkspace.setCurrentPage(currentScreen);
330
Sunny Goyal2100c782016-08-22 16:00:03 -0700331 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800332 }
333
334 // For handling default keys
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800335 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800336
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800337 setContentView(mLauncherView);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800338 getRootView().dispatchInsets();
Mario Bertschlera6936942017-05-31 14:48:19 -0700339
Jon Miranda7fda2852017-07-19 16:07:01 -0700340 // Listen for broadcasts
341 IntentFilter filter = new IntentFilter();
342 filter.addAction(Intent.ACTION_SCREEN_OFF);
343 filter.addAction(Intent.ACTION_USER_PRESENT); // When the device wakes up + keyguard is gone
344 registerReceiver(mReceiver, filter);
345 mShouldFadeInScrim = true;
Sunny Goyala616d2b2017-06-12 13:54:01 -0700346
Sunny Goyal8392c822017-06-20 10:03:56 -0700347 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
348 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700349
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800350 mAppTransitionManager = LauncherAppTransitionManager.newInstance(this);
Jon Miranda7853bdb2018-01-22 17:32:30 -0800351
Sunny Goyal14b32402017-07-31 10:03:28 -0700352 if (mLauncherCallbacks != null) {
353 mLauncherCallbacks.onCreate(savedInstanceState);
354 }
Sunny Goyal623eddd2018-03-02 12:24:41 -0800355 mRotationHelper.initialize();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700356
357 TraceHelper.endSection("Launcher-onCreate");
Adam Cohen9211d422014-10-07 18:14:53 -0700358 }
359
Sunny Goyal7779d622015-06-11 16:18:39 -0700360 @Override
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800361 public void onConfigurationChanged(Configuration newConfig) {
362 int diff = newConfig.diff(mOldConfig);
363 if ((diff & (CONFIG_ORIENTATION | CONFIG_SCREEN_SIZE)) != 0) {
364 mUserEventDispatcher = null;
365 initDeviceProfile(mDeviceProfile.inv);
Sunny Goyal2bba1902018-02-27 12:20:50 -0800366 FileLog.d(TAG, "Config changed, my orientation=" +
367 getResources().getConfiguration().orientation +
368 ", new orientation=" + newConfig.orientation +
369 ", old orientation=" + mOldConfig.orientation +
370 ", isTransposed=" + mDeviceProfile.isVerticalBarLayout());
Sunny Goyalfde55052018-02-01 14:46:13 -0800371 dispatchDeviceProfileChanged();
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800372
373 getRootView().dispatchInsets();
374 getStateManager().reapplyState();
375
Sunny Goyal7185dd62018-03-14 17:51:49 -0700376 // Recreate touch controllers
377 mDragLayer.setup(mDragController);
378
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800379 // TODO: We can probably avoid rebind when only screen size changed.
Sunny Goyal605bcf32018-03-02 15:16:12 -0800380 rebindModel();
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800381 }
382
383 mOldConfig.setTo(newConfig);
384 super.onConfigurationChanged(newConfig);
385 }
386
Sunny Goyal605bcf32018-03-02 15:16:12 -0800387 @Override
388 public void rebindModel() {
389 int currentPage = mWorkspace.getNextPage();
390 if (mModel.startLoader(currentPage)) {
391 mWorkspace.setCurrentPage(currentPage);
392 setWorkspaceLoading(true);
393 }
394 }
395
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800396 private void initDeviceProfile(InvariantDeviceProfile idp) {
397 // Load configuration-specific DeviceProfile
Sunny Goyalf633ef52018-03-13 09:57:05 -0700398 setDeviceProfile(idp.getDeviceProfile(this));
Sunny Goyal605bcf32018-03-02 15:16:12 -0800399 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout(), true);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800400 }
401
Sunny Goyal623eddd2018-03-02 12:24:41 -0800402 public RotationHelper getRotationHelper() {
403 return mRotationHelper;
404 }
405
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800406 @Override
Mario Bertschler27288382017-05-24 15:35:09 -0700407 public void onThemeChanged() {
408 recreate();
409 }
410
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700411 public LauncherStateManager getStateManager() {
412 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700413 }
414
Mario Bertschlera6936942017-05-31 14:48:19 -0700415 protected void overrideTheme(boolean isDark, boolean supportsDarkText) {
Mario Bertschler27288382017-05-24 15:35:09 -0700416 if (isDark) {
417 setTheme(R.style.LauncherThemeDark);
Mario Bertschlera6936942017-05-31 14:48:19 -0700418 } else if (supportsDarkText) {
419 setTheme(R.style.LauncherThemeDarkText);
Mario Bertschler27288382017-05-24 15:35:09 -0700420 }
421 }
422
423 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700424 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800425 return mLauncherView.findViewById(id);
426 }
427
428 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700429 public void onAppWidgetHostReset() {
430 if (mAppWidgetHost != null) {
431 mAppWidgetHost.startListening();
432 }
433 }
434
Adam Cohen9211d422014-10-07 18:14:53 -0700435 private LauncherCallbacks mLauncherCallbacks;
436
Sunny Goyal32554d12015-12-03 15:31:25 -0800437 /**
438 * Call this after onCreate to set or clear overlay.
439 */
440 public void setLauncherOverlay(LauncherOverlay overlay) {
441 if (overlay != null) {
442 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
443 }
444 mWorkspace.setLauncherOverlay(overlay);
445 }
446
Adam Cohen9211d422014-10-07 18:14:53 -0700447 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
448 mLauncherCallbacks = callbacks;
449 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700450 }
451
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700452 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700453 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700454 if (mLauncherCallbacks != null) {
455 mLauncherCallbacks.onLauncherProviderChange();
456 }
457 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700458
Hyunyoung Song3f471442015-04-08 19:01:34 -0700459 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700460 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
461 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700462 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700463 }
464
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000465 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700466 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
467 // This cast is safe as long as the id < 0x00FFFFFF
468 // Since we jail all the dynamically generated views, there should be no clashes
469 // with any other views.
470 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000471 }
472
Tony Wickham010d2552017-01-20 08:15:28 -0800473 public PopupDataProvider getPopupDataProvider() {
474 return mPopupDataProvider;
475 }
476
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700477 @Override
478 public BadgeInfo getBadgeInfoForItem(ItemInfo info) {
479 return mPopupDataProvider.getBadgeInfoForItem(info);
480 }
481
482 @Override
483 public void invalidateParent(ItemInfo info) {
484 FolderIconPreviewVerifier verifier = new FolderIconPreviewVerifier(getDeviceProfile().inv);
485 if (verifier.isItemInPreview(info.rank) && (info.container >= 0)) {
486 View folderIcon = getWorkspace().getHomescreenIconByItemId(info.container);
487 if (folderIcon != null) {
488 folderIcon.invalidate();
489 }
490 }
491 }
492
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000493 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700494 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
495 * a configuration step, this allows the proper animations to run after other transitions.
496 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700497 private long completeAdd(
498 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
499 long screenId = info.screenId;
500 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700501 // When the screen id represents an actual screen (as opposed to a rank) we make sure
502 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700503 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700504 }
Adam Cohendb364c32014-05-20 14:23:40 -0700505
Sunny Goyal2100c782016-08-22 16:00:03 -0700506 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800507 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700508 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700509 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800510 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700511 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700512 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700513 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700514 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700515 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700516 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700517 int widgetId = appWidgetId;
518 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700519 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700520 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700521 // Since the view was just bound, also launch the configure activity if needed
522 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
523 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800524 if (provider != null) {
525 new WidgetAddFlowHandler(provider)
526 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700527 }
528 }
529 break;
530 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800531 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700532
Adam Cohendb364c32014-05-20 14:23:40 -0700533 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800534 }
535
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800536 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700537 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700538 if (isWorkspaceLoading()) {
539 // process the result once the workspace has loaded.
540 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
541 return;
542 }
543 mPendingActivityResult = null;
544
Winson Chunge341d302013-08-16 14:31:00 -0700545 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700546 final PendingRequestArgs requestArgs = mPendingRequestArgs;
547 setWaitingForResult(null);
548 if (requestArgs == null) {
549 return;
550 }
551
552 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700553
Adam Cohenad4e15c2013-10-17 16:21:35 -0700554 Runnable exitSpringLoaded = new Runnable() {
555 @Override
556 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700557 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700558 }
559 };
560
Michael Jurka8b805b12012-04-18 14:23:14 -0700561 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700562 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700563 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700564 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
565 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700566 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700567 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700568 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700569 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700570 addAppWidgetImpl(
571 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800572 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700573 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700574 }
575 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200576 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700577 if (resultCode == RESULT_OK && isInState(OVERVIEW)) {
Tony Wickhame3054412015-09-09 09:05:25 -0700578 // User could have free-scrolled between pages before picking a wallpaper; make sure
579 // we move to the closest one now.
580 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700581 mStateManager.goToState(NORMAL, false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200582 }
583 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700584 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200585
Adam Cohened66b2b2012-01-23 17:28:51 -0800586 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700587 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700588
Adam Cohened66b2b2012-01-23 17:28:51 -0800589 // We have special handling for widgets
590 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800591 final int appWidgetId;
592 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
593 : -1;
594 if (widgetId < 0) {
595 appWidgetId = pendingAddWidgetId;
596 } else {
597 appWidgetId = widgetId;
598 }
599
Adam Cohenad4e15c2013-10-17 16:21:35 -0700600 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800601 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700602 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
603 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700604 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700605 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700606 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700607 @Override
608 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700609 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700610 }
611 };
Adam Cohendb364c32014-05-20 14:23:40 -0700612
Sunny Goyal2100c782016-08-22 16:00:03 -0700613 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
614 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
615 } else {
616 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
617 // When the screen id represents an actual screen (as opposed to a rank)
618 // we make sure that the drop page actually exists.
619 requestArgs.screenId =
620 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700621 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700622 final CellLayout dropLayout =
623 mWorkspace.getScreenWithId(requestArgs.screenId);
624
625 dropLayout.setDropPending(true);
626 final Runnable onComplete = new Runnable() {
627 @Override
628 public void run() {
629 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
630 dropLayout.setDropPending(false);
631 }
632 };
633 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
634 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700635 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800636 return;
637 }
638
Sunny Goyald478c832016-04-01 12:04:16 -0700639 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
640 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700641 if (resultCode == RESULT_OK) {
642 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700643 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700644 }
645 // Leave the widget in the pending state if the user canceled the configure.
646 return;
647 }
648
Sunny Goyalaad90582015-07-09 14:22:50 -0700649 if (requestCode == REQUEST_CREATE_SHORTCUT) {
650 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700651 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
652 completeAdd(requestCode, data, -1, requestArgs);
653 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
654 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
655
Sunny Goyalaad90582015-07-09 14:22:50 -0700656 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700657 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
658 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800659 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800661 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800662 }
663
664 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700665 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800666 final int requestCode, final int resultCode, final Intent data) {
667 handleActivityResult(requestCode, resultCode, data);
668 if (mLauncherCallbacks != null) {
669 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
670 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800671 }
672
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700673 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700674 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600675 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700676 PendingRequestArgs pendingArgs = mPendingRequestArgs;
677 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
678 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
679 setWaitingForResult(null);
680
Sunny Goyal28c6b962015-10-12 11:42:05 -0700681 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700682 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700683 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700684 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700685 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700686 Intent intent = pendingArgs.getPendingIntent();
687
Sunny Goyal28c6b962015-10-12 11:42:05 -0700688 if (grantResults.length > 0
689 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700690 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700691 } else {
692 // TODO: Show a snack bar with link to settings
693 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700694 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700695 }
696 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600697 if (mLauncherCallbacks != null) {
698 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
699 grantResults);
700 }
701 }
702
Adam Cohendb364c32014-05-20 14:23:40 -0700703 /**
704 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
705 *
706 * @param screenId the screen id to check
707 * @return the new screen, or screenId if it exists
708 */
709 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800710 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700711 if (dropLayout == null) {
712 // it's possible that the add screen was removed because it was
713 // empty and a re-bind occurred
714 mWorkspace.addExtraEmptyScreen();
715 return mWorkspace.commitExtraEmptyScreen();
716 } else {
717 return screenId;
718 }
719 }
720
Sunny Goyal2100c782016-08-22 16:00:03 -0700721 @Thunk void completeTwoStageWidgetDrop(
722 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
723 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800724 Runnable onCompleteRunnable = null;
725 int animationType = 0;
726
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700727 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800728 if (resultCode == RESULT_OK) {
729 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
730 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800731 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700732 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800733 onCompleteRunnable = new Runnable() {
734 @Override
735 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700736 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700737 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800738 }
739 };
740 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800741 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800742 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800743 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700744 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700745 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700746 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
747 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700748 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700749 // The animated view may be null in the case of a rotation during widget configuration
750 onCompleteRunnable.run();
751 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800752 }
753
754 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700755 protected void onStop() {
756 super.onStop();
757 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700758
759 if (mLauncherCallbacks != null) {
760 mLauncherCallbacks.onStop();
761 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800762 mAppWidgetHost.setListenIfResumed(false);
Tony Wickham010d2552017-01-20 08:15:28 -0800763
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700764 if (!mAppLaunchSuccess) {
765 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -0800766 mStateManager.getState().containerType, -1);
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700767 }
Tony Wickham010d2552017-01-20 08:15:28 -0800768 NotificationListener.removeNotificationsChangedListener();
Sunny Goyalaf161fd2018-02-05 16:34:57 -0800769 getStateManager().moveToRestState();
Winson Chungedb0d6b2018-03-21 17:32:55 -0700770
Michael Jurkacd496d72013-04-11 11:32:45 -0700771 }
772
773 @Override
774 protected void onStart() {
775 super.onStart();
776 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700777
778 if (mLauncherCallbacks != null) {
779 mLauncherCallbacks.onStart();
780 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800781 mAppWidgetHost.setListenIfResumed(true);
Sunny Goyalcac002d2018-01-04 09:51:45 -0800782 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
Jon Miranda2d89ea82017-05-04 11:47:53 -0700783
Jon Mirandade43a712018-01-18 11:35:10 -0800784 if (mShouldFadeInScrim && mLauncherView.getBackground() != null) {
Jon Miranda2d89ea82017-05-04 11:47:53 -0700785 if (mScrimAnimator != null) {
786 mScrimAnimator.cancel();
787 }
Jon Mirandade43a712018-01-18 11:35:10 -0800788 mLauncherView.getBackground().setAlpha(0);
789 mScrimAnimator = ObjectAnimator.ofInt(mLauncherView.getBackground(),
Jon Miranda2d89ea82017-05-04 11:47:53 -0700790 LauncherAnimUtils.DRAWABLE_ALPHA, 0, 255);
791 mScrimAnimator.addListener(new AnimatorListenerAdapter() {
792 @Override
793 public void onAnimationEnd(Animator animation) {
794 mScrimAnimator = null;
795 }
796 });
797 mScrimAnimator.setDuration(600);
798 mScrimAnimator.setStartDelay(getWindow().getTransitionBackgroundFadeDuration());
799 mScrimAnimator.start();
800 }
Jon Miranda7fda2852017-07-19 16:07:01 -0700801 mShouldFadeInScrim = false;
Winson Chung62a70be2018-02-23 15:10:05 -0800802 UiFactory.onStart(this);
Michael Jurkacd496d72013-04-11 11:32:45 -0700803 }
804
805 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800806 protected void onResume() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700807 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800808 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700809 TraceHelper.partitionSection("ON_RESUME", "superCall");
810
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700811 mAppLaunchSuccess = false;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700812 getUserEventDispatcher().resetElapsedSessionMillis();
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700813 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700814 // Process any items that were added while Launcher was away.
815 InstallShortcutReceiver.disableAndFlushInstallQueue(
816 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700817
Sunny Goyala474a9b2017-05-04 16:47:11 -0700818 // Refresh shortcuts if the permission changed.
819 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700820
Sunny Goyal7ede6112017-12-05 15:11:21 -0800821 DiscoveryBounce.showIfNeeded(this);
Adam Cohen9211d422014-10-07 18:14:53 -0700822 if (mLauncherCallbacks != null) {
823 mLauncherCallbacks.onResume();
824 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800825
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700826 TraceHelper.endSection("ON_RESUME");
Adam Cohen06dff352012-06-01 17:17:08 -0700827 }
828
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800829 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700830 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700831 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700832 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700833
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700834 super.onPause();
Joe Onorato24b6fd82009-11-12 13:47:09 -0800835 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700836 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700837
Adam Cohen9211d422014-10-07 18:14:53 -0700838 if (mLauncherCallbacks != null) {
839 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700840 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700841 }
842
Winson Chungd7823942018-02-16 03:23:47 +0000843 @Override
844 public void onWindowFocusChanged(boolean hasFocus) {
845 super.onWindowFocusChanged(hasFocus);
846 mStateManager.onWindowFocusChanged();
847 }
848
Adam Cohenc2d6e892014-10-16 09:49:24 -0700849 public interface LauncherOverlay {
850
851 /**
852 * Touch interaction leading to overscroll has begun
853 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700854 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700855
856 /**
857 * Touch interaction related to overscroll has ended
858 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700859 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700860
861 /**
862 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
863 * screen (or in the case of RTL, the rightmost screen).
864 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700865 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700866
867 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800868 * Called when the launcher is ready to use the overlay
869 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700870 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700871 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700872 }
873
874 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700875 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700876 }
877
878 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
879
Sunny Goyalc86df472016-02-25 09:19:38 -0800880 public void onScrollChanged(float progress) {
881 if (mWorkspace != null) {
882 mWorkspace.onOverlayScrollChanged(progress);
883 }
884 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700885 }
886
Sunny Goyal16764582017-11-06 16:00:34 -0800887 public boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -0700888 if (mLauncherCallbacks != null) {
889 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700890 } else {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800891 // On O and above we there is always some setting present settings (add icon to
892 // home screen or icon badging). On earlier APIs we will have the allow rotation
893 // setting, on devices with a locked orientation,
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700894 return Utilities.ATLEAST_OREO || !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -0700895 }
Jorim Jaggid017f882014-01-14 17:08:48 -0800896 }
897
Sunny Goyalf9403d92017-10-18 10:55:56 -0700898 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -0700899 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700900 }
901
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800902 /**
903 * Restores the previous state, if it exists.
904 *
905 * @param savedState The previous state.
906 */
907 private void restoreState(Bundle savedState) {
908 if (savedState == null) {
909 return;
910 }
911
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700912 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -0700913 LauncherState[] stateValues = LauncherState.values();
914 LauncherState state = stateValues[stateOrdinal];
Sunny Goyalbc683e92017-12-06 10:25:07 -0800915 if (!state.disableRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700916 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800917 }
918
Sunny Goyal2100c782016-08-22 16:00:03 -0700919 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
920 if (requestArgs != null) {
921 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800922 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700923
924 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700925
926 SparseArray<Parcelable> widgetsState =
927 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
928 if (widgetsState != null) {
929 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
930 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800931 }
932
933 /**
934 * Finds all the views we need and configure them properly.
935 */
936 private void setupViews() {
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800937 mDragLayer = findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700938 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -0700939 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700940 mWorkspace.initParentViews(mDragLayer);
Sunny Goyal16764582017-11-06 16:00:34 -0800941 mOverviewPanel = findViewById(R.id.overview_panel);
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800942 mHotseat = findViewById(R.id.hotseat);
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700943 mDragHandleIndicator = findViewById(R.id.drag_indicator);
944 mHotseatSearchBox = findViewById(R.id.search_container_hotseat);
Craig Mautner360310b2012-10-26 15:13:08 -0700945
Sunny Goyal784f9c32016-03-23 16:56:54 -0700946 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
947 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
948 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800949
Winson Chung4c98d922011-05-31 16:50:48 -0700950 // Setup the drag layer
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700951 mDragLayer.setup(mDragController);
Winson Chung4c98d922011-05-31 16:50:48 -0700952
Hyunyoung Song645764e2016-06-06 14:19:02 -0700953 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -0700954 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
955 // default state, otherwise we will update to the wrong offsets in RTL
956 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700957 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700958 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700959
Tony Wickham34d2c912015-09-11 09:27:58 -0700960 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700961 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700962
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700963 // Setup Apps
964 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -0700965
Winson Chung3d503fb2011-07-13 17:25:49 -0700966 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -0700967 mDragController.setMoveTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700968 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -0400969
Sunny Goyal7185dd62018-03-14 17:51:49 -0700970 mAllAppsController.setupViews(mAppsView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800971 }
972
973 /**
974 * Creates a view representing a shortcut.
975 *
976 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800977 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800978 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700979 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800980 }
981
982 /**
983 * Creates a view representing a shortcut inflated from the specified resource.
984 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800985 * @param parent The group the shortcut belongs to.
986 * @param info The data structure describing the shortcut.
987 *
988 * @return A View inflated from layoutResId.
989 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700990 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -0800991 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
992 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800993 favorite.applyFromShortcutInfo(info);
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800994 favorite.setOnClickListener(ItemClickHandler.INSTANCE);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700995 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800996 return favorite;
997 }
998
999 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -07001000 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001001 *
1002 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001003 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001004 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001005 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001006 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
1007 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001008 return;
1009 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001010
Jon Mirandac476d6e2017-05-04 16:30:01 -07001011 int[] cellXY = mTmpAddItemCellCoordinates;
1012 CellLayout layout = getCellLayout(container, screenId);
1013
Sunny Goyal782f0c92017-01-19 10:27:54 -08001014 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001015 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001016 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
1017 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001018 }
1019
1020 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001021 // Legacy shortcuts are only supported for primary profile.
1022 info = Process.myUserHandle().equals(args.user)
1023 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001024
Sunny Goyalb57645f2017-03-20 13:57:28 -07001025 if (info == null) {
1026 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1027 return;
1028 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001029 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001030 // The app is trying to add a shortcut without sufficient permissions
1031 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1032 return;
1033 }
1034 }
1035
Jon Mirandac476d6e2017-05-04 16:30:01 -07001036 if (container < 0) {
1037 // Adding a shortcut to the Workspace.
1038 final View view = createShortcut(info);
1039 boolean foundCellSpan = false;
1040 // First we check if we already know the exact location where we want to add this item.
1041 if (cellX >= 0 && cellY >= 0) {
1042 cellXY[0] = cellX;
1043 cellXY[1] = cellY;
1044 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001045
Jon Mirandac476d6e2017-05-04 16:30:01 -07001046 // If appropriate, either create a folder or add to an existing folder
1047 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001048 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001049 return;
1050 }
1051 DragObject dragObject = new DragObject();
1052 dragObject.dragInfo = info;
1053 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1054 true)) {
1055 return;
1056 }
1057 } else {
1058 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1059 }
1060
1061 if (!foundCellSpan) {
1062 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001063 return;
1064 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001065
1066 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1067 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001068 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001069 // Adding a shortcut to a Folder.
Sunny Goyale29897f2017-07-20 10:09:42 -07001070 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001071 if (folderIcon != null) {
1072 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1073 folderInfo.add(info, args.rank, false);
1074 } else {
Sunny Goyale29897f2017-07-20 10:09:42 -07001075 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001076 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001077 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001078 }
1079
Sunny Goyale29897f2017-07-20 10:09:42 -07001080 public FolderIcon findFolderIcon(final long folderIconId) {
1081 return (FolderIcon) mWorkspace.getFirstMatch(new ItemOperator() {
1082 @Override
1083 public boolean evaluate(ItemInfo info, View view) {
1084 return info != null && info.id == folderIconId;
1085 }
1086 });
1087 }
1088
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001089 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001090 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001091 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001092 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001093 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001094 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001095 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1096
Adam Cohened66b2b2012-01-23 17:28:51 -08001097 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001098 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001099 }
Romain Guycbb89e42009-06-08 15:52:54 -07001100
Adam Cohen59400422014-03-05 18:07:04 -08001101 LauncherAppWidgetInfo launcherInfo;
1102 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001103 launcherInfo.spanX = itemInfo.spanX;
1104 launcherInfo.spanY = itemInfo.spanY;
1105 launcherInfo.minSpanX = itemInfo.minSpanX;
1106 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001107 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001108
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001109 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001110 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001111
Sunny Goyal2100c782016-08-22 16:00:03 -07001112 if (hostView == null) {
1113 // Perform actual inflation because we're live
1114 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001115 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001116 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301117 prepareAppWidget(hostView, launcherInfo);
1118 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001119 }
Romain Guycbb89e42009-06-08 15:52:54 -07001120
Sunny Goyald5462aa2016-11-22 16:52:39 +05301121 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001122 hostView.setTag(item);
1123 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001124 hostView.setFocusable(true);
1125 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001126 }
1127
Adam Cohended9f8d2010-11-03 13:25:16 -07001128 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1129 @Override
1130 public void onReceive(Context context, Intent intent) {
1131 final String action = intent.getAction();
1132 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001133 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001134 // processing a multi-step drop
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001135 if (mAppsView != null && mPendingRequestArgs == null) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001136 mStateManager.goToState(NORMAL);
Winson Chung785d2eb2011-04-14 16:08:02 -07001137 }
Jon Miranda7fda2852017-07-19 16:07:01 -07001138 mShouldFadeInScrim = true;
1139 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1140 // ACTION_USER_PRESENT is sent after onStart/onResume. This covers the case where
1141 // the user unlocked and the Launcher is not in the foreground.
1142 mShouldFadeInScrim = false;
Adam Cohended9f8d2010-11-03 13:25:16 -07001143 }
1144 }
1145 };
1146
Tony Wickham010d2552017-01-20 08:15:28 -08001147 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
Sunny Goyal29947f02017-12-18 13:49:44 -08001148 mWorkspace.updateIconBadges(updatedBadges);
Sunny Goyal60180b02018-02-07 12:56:30 -08001149 mAppsView.getAppsStore().updateIconBadges(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001150
Sunny Goyal29947f02017-12-18 13:49:44 -08001151 PopupContainerWithArrow popup = PopupContainerWithArrow.getOpen(Launcher.this);
1152 if (popup != null) {
1153 popup.updateNotificationHeader(updatedBadges);
Tony Wickham010d2552017-01-20 08:15:28 -08001154 }
1155 }
1156
Adam Cohended9f8d2010-11-03 13:25:16 -07001157 @Override
1158 public void onAttachedToWindow() {
1159 super.onAttachedToWindow();
1160
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001161 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyalc86df472016-02-25 09:19:38 -08001162 if (mLauncherCallbacks != null) {
1163 mLauncherCallbacks.onAttachedToWindow();
1164 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001165 }
1166
1167 @Override
1168 public void onDetachedFromWindow() {
1169 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001170
1171 if (mLauncherCallbacks != null) {
1172 mLauncherCallbacks.onDetachedFromWindow();
1173 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001174 }
1175
Sunny Goyal3bac2c22018-02-07 14:44:05 -08001176 public void onQuickstepGestureStarted(boolean isVisible) {
1177 if (mLauncherCallbacks != null) {
1178 mLauncherCallbacks.onQuickstepGestureStarted(isVisible);
1179 }
1180 }
1181
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001182 public AllAppsTransitionController getAllAppsController() {
1183 return mAllAppsController;
1184 }
1185
Sunny Goyal9d69c8d2018-03-19 13:41:31 -07001186 @Override
Winson Chung1a341002018-01-17 10:00:23 -08001187 public LauncherRootView getRootView() {
1188 return (LauncherRootView) mLauncherView;
1189 }
1190
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001191 @Override
Winson Chunga6945242014-01-08 14:04:34 -08001192 public DragLayer getDragLayer() {
1193 return mDragLayer;
1194 }
1195
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001196 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001197 return mAppsView;
1198 }
1199
Winson Chunga6945242014-01-08 14:04:34 -08001200 public Workspace getWorkspace() {
1201 return mWorkspace;
1202 }
1203
1204 public Hotseat getHotseat() {
1205 return mHotseat;
1206 }
1207
Sunny Goyal81b4c7b2018-03-26 12:10:31 -07001208 public View getDragHandleIndicator() {
1209 return mDragHandleIndicator;
1210 }
1211
1212 public View getHotseatSearchBox() {
1213 return mHotseatSearchBox;
1214 }
1215
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001216 public <T extends View> T getOverviewPanel() {
Winson Chung8ae41982017-11-10 11:42:13 -08001217 return (T) mOverviewPanel;
Winson Chunga6945242014-01-08 14:04:34 -08001218 }
1219
Sunny Goyal47328fd2016-05-25 18:56:41 -07001220 public DropTargetBar getDropTargetBar() {
1221 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001222 }
1223
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001224 public LauncherAppWidgetHost getAppWidgetHost() {
1225 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001226 }
Romain Guycbb89e42009-06-08 15:52:54 -07001227
Winson Chunga9abd0e2010-10-27 17:18:37 -07001228 public LauncherModel getModel() {
1229 return mModel;
1230 }
1231
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001232 public ModelWriter getModelWriter() {
1233 return mModelWriter;
1234 }
1235
Adam Cohen79d90c52016-04-22 13:29:20 -07001236 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001237 return mSharedPrefs;
1238 }
1239
Sunny Goyalf8d56fc2018-01-31 15:18:11 -08001240 public int getOrientation() { return mOldConfig.orientation; }
Jon Miranda6bed3502017-09-19 14:43:17 -07001241
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001242 @Override
1243 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001244 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001245 super.onNewIntent(intent);
1246
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001247 boolean alreadyOnHome = hasWindowFocus() && ((intent.getFlags() &
Winson15f8b172015-08-19 11:02:39 -07001248 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1249 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001250
1251 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001252 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001253 && AbstractFloatingView.getTopOpenView(this) == null;
Winson15f8b172015-08-19 11:02:39 -07001254 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001255 boolean internalStateHandled = InternalStateHandler
Sunny Goyal8b264562018-02-03 23:00:13 -08001256 .handleNewIntent(this, intent, isStarted());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001257
Winson15f8b172015-08-19 11:02:39 -07001258 if (isActionMain) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001259 if (!internalStateHandled) {
1260 // Note: There should be at most one log per method call. This is enforced
1261 // implicitly by using if-else statements.
1262 UserEventDispatcher ued = getUserEventDispatcher();
1263 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
1264 if (topOpenView != null) {
1265 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
1266 } else if (alreadyOnHome) {
1267 Target target = newContainerTarget(mStateManager.getState().containerType);
1268 target.pageIndex = mWorkspace.getCurrentPage();
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001269 ued.logActionCommand(Action.Command.HOME_INTENT, target,
1270 newContainerTarget(ContainerType.WORKSPACE));
Sunny Goyald3864fa2017-11-14 15:06:36 -08001271 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001272
Sunny Goyald3864fa2017-11-14 15:06:36 -08001273 // In all these cases, only animate if we're already on home
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001274 AbstractFloatingView.closeAllOpenViews(this, isStarted());
Jon Mirandafeba90f2016-10-06 10:53:29 -07001275
Sunny Goyala2467272018-03-16 14:53:05 -07001276 if (!isInState(NORMAL)) {
1277 // Only change state, if not already the same. This prevents cancelling any
1278 // animations running as part of resume
1279 mStateManager.goToState(NORMAL);
1280 }
Sunny Goyald3864fa2017-11-14 15:06:36 -08001281
1282 // Reset the apps view
1283 if (!alreadyOnHome && mAppsView != null) {
Sunny Goyal7185dd62018-03-14 17:51:49 -07001284 mAppsView.reset(isStarted() /* animate */);
Sunny Goyald3864fa2017-11-14 15:06:36 -08001285 }
1286
1287 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()) {
1288 mWorkspace.post(mWorkspace::moveToDefaultScreen);
1289 }
1290 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001291
1292 final View v = getWindow().peekDecorView();
1293 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001294 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001295 }
1296
Adam Cohen9211d422014-10-07 18:14:53 -07001297 if (mLauncherCallbacks != null) {
1298 mLauncherCallbacks.onHomeIntent();
1299 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001300 }
Winson15f8b172015-08-19 11:02:39 -07001301
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001302 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001303 }
1304
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001305 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001306 public void onRestoreInstanceState(Bundle state) {
1307 super.onRestoreInstanceState(state);
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001308 mWorkspace.restoreInstanceStateForChild(mSynchronouslyBoundPage);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001309 }
1310
1311 @Override
1312 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001313 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001314 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001315
Adam Cohen21cd0022013-10-09 18:57:02 -07001316 }
Sunny Goyalea609262017-10-25 15:47:38 -07001317 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001318
1319
1320 AbstractFloatingView widgets = AbstractFloatingView
1321 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1322 if (widgets != null) {
1323 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1324 widgets.saveHierarchyState(widgetsState);
1325 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1326 } else {
1327 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1328 }
1329
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001330 // We close any open folders and shortcut containers since they will not be re-opened,
1331 // and we need to make sure this state is reflected.
1332 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001333
Sunny Goyal2100c782016-08-22 16:00:03 -07001334 if (mPendingRequestArgs != null) {
1335 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1336 }
1337 if (mPendingActivityResult != null) {
1338 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001339 }
1340
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001341 super.onSaveInstanceState(outState);
1342
Adam Cohen9211d422014-10-07 18:14:53 -07001343 if (mLauncherCallbacks != null) {
1344 mLauncherCallbacks.onSaveInstanceState(outState);
1345 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001346 }
1347
1348 @Override
1349 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001350 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001351
Sunny Goyal5cc7bbd2017-06-12 09:50:39 -07001352 unregisterReceiver(mReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001353 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001354
Winson Chungcd2b0142011-06-08 16:02:26 -07001355 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001356 // It's possible to receive onDestroy after a new Launcher activity has
1357 // been created. In this case, don't interfere with the new Launcher.
1358 if (mModel.isCurrentCallbacks(this)) {
1359 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001360 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001361 }
Sunny Goyal623eddd2018-03-02 12:24:41 -08001362 mRotationHelper.destroy();
Winson Chungedb0d6b2018-03-21 17:32:55 -07001363 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mSharedPrefsListener);
Sunny Goyal745bad92016-05-02 10:54:12 -07001364
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001365 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001366 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001367 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001368 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001369 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001370
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001371 TextKeyListener.getInstance().release();
Mario Bertschler27288382017-05-24 15:35:09 -07001372 WallpaperColorInfo.getInstance(this).setOnThemeChangeListener(null);
1373
Michael Jurka2ecf9952012-06-18 12:52:28 -07001374 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001375
Tony2917a8b2017-07-31 23:29:42 -07001376 clearPendingBinds();
1377
Adam Cohen9211d422014-10-07 18:14:53 -07001378 if (mLauncherCallbacks != null) {
1379 mLauncherCallbacks.onDestroy();
1380 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001381 }
1382
Sunny Goyalae502842016-06-17 08:43:56 -07001383 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1384 return mAccessibilityDelegate;
1385 }
1386
Adam Cohena9cf38f2011-05-02 15:36:58 -07001387 public DragController getDragController() {
1388 return mDragController;
1389 }
1390
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001391 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001392 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001393 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001394 }
1395
1396 @Override
1397 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1398 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001399 try {
1400 super.startIntentSenderForResult(intent, requestCode,
1401 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1402 } catch (IntentSender.SendIntentException e) {
1403 throw new ActivityNotFoundException();
1404 }
1405 }
1406
Winson Chung70d72102011-08-12 11:24:35 -07001407 /**
1408 * Indicates that we want global search for this activity by setting the globalSearch
1409 * argument for {@link #startSearch} to true.
1410 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001411 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001412 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001413 Bundle appSearchData, boolean globalSearch) {
1414 if (appSearchData == null) {
1415 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001416 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001417 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001418
Sunny Goyal6f28e712016-09-13 14:19:29 -07001419 if (mLauncherCallbacks == null ||
1420 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1421 // Starting search from the callbacks failed. Start the default global search.
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001422 super.startSearch(initialQuery, selectInitialQuery, appSearchData, true);
Ian Parkinson047036e2014-09-01 15:40:53 +01001423 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001424
1425 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001426 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001427 }
1428
Joe Onorato9c1289c2009-08-17 11:03:03 -04001429 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001430 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001431 }
1432
Adam Cohen517a7f52014-03-01 12:12:59 -08001433 public boolean isWorkspaceLoading() {
1434 return mWorkspaceLoading;
1435 }
1436
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001437 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001438 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001439 }
1440
Sunny Goyal04a324a2017-01-20 21:08:59 -08001441 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001442 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001443 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001444
Sunny Goyal2100c782016-08-22 16:00:03 -07001445 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001446 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001447 if (LOGD) {
1448 Log.d(TAG, "Adding widget from drop");
1449 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001450 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001451 }
1452
Sunny Goyal2100c782016-08-22 16:00:03 -07001453 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001454 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1455 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1456 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001457
Adam Cohenad4e15c2013-10-17 16:21:35 -07001458 Runnable onComplete = new Runnable() {
1459 @Override
1460 public void run() {
1461 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001462 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001463 }
1464 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001465 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001466 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001467 }
1468 }
Romain Guycbb89e42009-06-08 15:52:54 -07001469
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001470 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1471 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001472 info.container = container;
1473 info.screenId = screenId;
1474 if (cell != null) {
1475 info.cellX = cell[0];
1476 info.cellY = cell[1];
1477 }
1478 info.spanX = spanX;
1479 info.spanY = spanY;
1480
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001481 switch (info.itemType) {
1482 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1483 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001484 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001485 break;
1486 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001487 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001488 break;
1489 default:
1490 throw new IllegalStateException("Unknown item type: " + info.itemType);
1491 }
1492 }
1493
Adam Cohenfbba09b2011-07-18 21:43:05 -07001494 /**
1495 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001496 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001497 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001498 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1499 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001500 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1501 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1502 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001503 }
1504
Adam Cohenfbba09b2011-07-18 21:43:05 -07001505 /**
1506 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001507 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001508 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001509 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001510 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001511 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001512 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001513 // In the case where we've prebound the widget, we remove it from the DragLayer
1514 if (LOGD) {
1515 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1516 }
1517 getDragLayer().removeView(hostView);
1518
Adam Cohened66b2b2012-01-23 17:28:51 -08001519 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001520 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001521
1522 // Clear the boundWidget so that it doesn't get destroyed.
1523 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001524 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001525 // In this case, we either need to start an activity to get permission to bind
1526 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001527 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1528 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1529 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1530 this, info.componentName);
1531 } else {
1532 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1533 }
Adam Cohendd70d662012-10-04 16:53:44 -07001534 Bundle options = info.bindOptions;
1535
Sunny Goyalffe83f12014-08-14 17:39:34 -07001536 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1537 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001538 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001539 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001540 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001541 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001542 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001543 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001544 }
1545
Adam Cohendcd297f2013-06-18 13:13:40 -07001546 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001547 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001548 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549 folderInfo.title = getText(R.string.folder_name);
1550
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001551 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001552 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001553
1554 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001555 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301556 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001557 // Force measure the new folder icon
1558 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1559 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001560 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001561 }
Romain Guycbb89e42009-06-08 15:52:54 -07001562
Winsonc0b52fe2015-09-09 16:38:15 -07001563 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001564 * Unbinds the view for the specified item, and removes the item and all its children.
1565 *
1566 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001567 * @param itemInfo the {@link ItemInfo} for this view.
1568 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001569 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001570 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001571 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001572 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001573 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1574 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001575 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001576 } else {
1577 mWorkspace.removeWorkspaceItem(v);
1578 }
Winsonc0b52fe2015-09-09 16:38:15 -07001579 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001580 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001581 }
1582 } else if (itemInfo instanceof FolderInfo) {
1583 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001584 if (v instanceof FolderIcon) {
1585 ((FolderIcon) v).removeListeners();
1586 }
Winsonc0b52fe2015-09-09 16:38:15 -07001587 mWorkspace.removeWorkspaceItem(v);
1588 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001589 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001590 }
1591 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1592 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001593 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001594 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07001595 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001596 }
1597 } else {
1598 return false;
1599 }
1600 return true;
1601 }
1602
1603 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07001604 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07001605 */
Sunny Goyal56c73602015-09-25 12:55:01 -07001606 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07001607 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07001608 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07001609 // Deleting an app widget ID is a void call but writes to disk before returning
1610 // to the caller...
1611 new AsyncTask<Void, Void, Void>() {
1612 public Void doInBackground(Void ... args) {
1613 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
1614 return null;
1615 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07001616 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07001617 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001618 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001619 }
1620
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001621 @Override
1622 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001623 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001624 }
1625
Joe Onorato88ec0992009-11-19 13:16:06 -08001626 @Override
1627 public void onBackPressed() {
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08001628 if (finishAutoCancelActionMode()) {
1629 return;
1630 }
Adam Cohen9211d422014-10-07 18:14:53 -07001631 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1632 return;
1633 }
1634
Sunny Goyal45478022015-06-08 16:52:41 -07001635 if (mDragController.isDragging()) {
1636 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001637 return;
1638 }
1639
Jon Mirandafeba90f2016-10-06 10:53:29 -07001640 // Note: There should be at most one log per method call. This is enforced implicitly
1641 // by using if-else statements.
1642 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001643 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
1644 if (topView != null) {
Sunny Goyal37920962017-09-28 13:43:24 -07001645 topView.onBackPressed();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001646 } else if (!isInState(NORMAL)) {
Sunny Goyal49bcf342018-01-11 13:59:53 -08001647 LauncherState lastState = mStateManager.getLastState();
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001648 ued.logActionCommand(Action.Command.BACK, mStateManager.getState().containerType,
1649 lastState.containerType);
Sunny Goyal49bcf342018-01-11 13:59:53 -08001650 mStateManager.goToState(lastState);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001651 } else {
1652 // Back button is a no-op here, but give at least some feedback for the button press
1653 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001654 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001655 }
1656
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001657 /**
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001658 * Event handler for the wallpaper picker button that appears after a long press
1659 * on the home screen.
1660 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001661 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07001662 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07001663 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
1664 return;
1665 }
1666
Tony Wickham785f7a52015-08-31 17:28:32 -07001667 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
1668 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal2100c782016-08-22 16:00:03 -07001669 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001670 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001671 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001672
1673 String pickerPackage = getString(R.string.wallpaper_picker_package);
Tony85efb4b2017-06-02 13:06:10 -07001674 boolean hasTargetPackage = !TextUtils.isEmpty(pickerPackage);
1675 if (hasTargetPackage) {
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001676 intent.setPackage(pickerPackage);
1677 }
1678
Sunny Goyala5ace712017-11-15 17:12:51 -08001679 final Bundle launchOptions;
1680 if (v != null) {
1681 intent.setSourceBounds(getViewBounds(v));
1682 // If there is no target package, use the default intent chooser animation
Jon Miranda17940f02018-01-18 14:46:34 -08001683 launchOptions = hasTargetPackage
Tony Wickham005df0b2018-02-13 11:28:12 -08001684 ? getActivityLaunchOptionsAsBundle(v, isInMultiWindowModeCompat())
Jon Miranda17940f02018-01-18 14:46:34 -08001685 : null;
Sunny Goyala5ace712017-11-15 17:12:51 -08001686 } else {
1687 launchOptions = null;
1688 }
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001689 try {
Sunny Goyala5ace712017-11-15 17:12:51 -08001690 startActivityForResult(intent, REQUEST_PICK_WALLPAPER, launchOptions);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001691 } catch (ActivityNotFoundException e) {
1692 setWaitingForResult(null);
1693 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1694 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001695 }
1696
Sunny Goyalfe770c92016-09-27 14:38:58 -07001697 @TargetApi(Build.VERSION_CODES.M)
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001698 @Override
Tony Wickham005df0b2018-02-13 11:28:12 -08001699 public ActivityOptions getActivityLaunchOptions(View v, boolean useDefaultLaunchOptions) {
Jon Miranda17940f02018-01-18 14:46:34 -08001700 return useDefaultLaunchOptions
Jon Miranda54441f52018-01-24 15:38:25 -08001701 ? mAppTransitionManager.getDefaultActivityLaunchOptions(this, v)
1702 : mAppTransitionManager.getActivityLaunchOptions(this, v);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001703 }
1704
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001705 @TargetApi(Build.VERSION_CODES.M)
1706 @Override
1707 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
1708 // Due to legacy reasons, direct call shortcuts require Launchers to have the
1709 // corresponding permission. Show the appropriate permission prompt if that
1710 // is the case.
1711 if (intent.getComponent() == null
1712 && Intent.ACTION_CALL.equals(intent.getAction())
1713 && checkSelfPermission(android.Manifest.permission.CALL_PHONE) !=
1714 PackageManager.PERMISSION_GRANTED) {
1715
1716 setWaitingForResult(PendingRequestArgs
1717 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
1718 requestPermissions(new String[]{android.Manifest.permission.CALL_PHONE},
1719 REQUEST_PERMISSION_CALL_PHONE);
1720 return true;
1721 } else {
1722 return false;
1723 }
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001724 }
1725
Sunny Goyala7ce1662016-05-31 15:01:35 -07001726 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001727 mAppLaunchSuccess = super.startActivitySafely(v, intent, item);
1728 if (mAppLaunchSuccess && v instanceof BubbleTextView) {
1729 // This is set to the view that launched the activity that navigated the user away
1730 // from launcher. Since there is no callback for when the activity has finished
1731 // launching, enable the press state and keep this reference to reset the press
1732 // state when we return to launcher.
1733 BubbleTextView btv = (BubbleTextView) v;
1734 btv.setStayPressed(true);
1735 setOnResumeCallback(btv);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001736 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07001737 return mAppLaunchSuccess;
Michael Jurka86a720e2012-05-09 11:23:23 -07001738 }
1739
Winson Chung3d503fb2011-07-13 17:25:49 -07001740 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001741 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07001742 return mHotseat != null && layout != null &&
1743 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
1744 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001745
Winson Chung3d503fb2011-07-13 17:25:49 -07001746 /**
1747 * Returns the CellLayout of the specified container at the specified screen.
1748 */
Sunny Goyal92820592015-03-02 11:31:35 -08001749 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001750 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1751 if (mHotseat != null) {
1752 return mHotseat.getLayout();
1753 } else {
1754 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08001755 }
Winson Chung3d503fb2011-07-13 17:25:49 -07001756 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08001757 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08001758 }
1759 }
1760
Michael Jurkae326f182011-11-21 14:05:46 -08001761 @Override
1762 public void onTrimMemory(int level) {
1763 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07001764 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
1765 // The widget preview db can result in holding onto over
1766 // 3MB of memory for caching which isn't necessary.
1767 SQLiteDatabase.releaseMemory();
1768
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08001769 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07001770 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08001771 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01001772 if (mLauncherCallbacks != null) {
1773 mLauncherCallbacks.onTrimMemory(level);
1774 }
Winson Chung62a70be2018-02-23 15:10:05 -08001775 UiFactory.onTrimMemory(this, level);
Michael Jurkae326f182011-11-21 14:05:46 -08001776 }
1777
Michael Jurkad7c28052012-04-27 15:43:36 -07001778 @Override
1779 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07001780 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07001781 final List<CharSequence> text = event.getText();
1782 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07001783 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001784 // TODO: When can workspace be null?
1785 text.add(mWorkspace == null
1786 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07001787 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07001788 return result;
1789 }
1790
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001791 public void setOnResumeCallback(OnResumeCallback callback) {
1792 if (mOnResumeCallback != null) {
1793 mOnResumeCallback.onLauncherResume();
1794 }
1795 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07001796 }
1797
Michael Jurka7607c2f2013-04-03 14:33:19 -07001798 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001799 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001800 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07001801 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001802 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07001803 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001804 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07001805 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07001806 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07001807 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001808 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001809
Joe Onorato9c1289c2009-08-17 11:03:03 -04001810 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07001811 * Clear any pending bind callbacks. This is called when is loader is planning to
1812 * perform a full rebind from scratch.
1813 */
1814 @Override
1815 public void clearPendingBinds() {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001816 if (mPendingExecutor != null) {
1817 mPendingExecutor.markCompleted();
1818 mPendingExecutor = null;
1819 }
1820 }
1821
1822 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001823 * Refreshes the shortcuts shown on the workspace.
1824 *
1825 * Implementation of the method from LauncherModel.Callbacks.
1826 */
1827 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001828 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001829 // Floating panels (except the full widget sheet) are associated with individual icons. If
1830 // we are starting a fresh bind, close all such panels as all the icons are about
1831 // to go away.
1832 AbstractFloatingView.closeOpenViews(this, true,
Sunny Goyal7ede6112017-12-05 15:11:21 -08001833 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_REBIND_SAFE);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08001834
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001835 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08001836
Winson Chungd64d1762013-08-20 14:37:16 -07001837 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07001838 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07001839 mWorkspace.removeAllWorkspaceScreens();
Sunny Goyalc11fac32018-02-27 19:20:15 -08001840 mAppWidgetHost.clearViews();
Winson Chungd64d1762013-08-20 14:37:16 -07001841
Winson Chung3d503fb2011-07-13 17:25:49 -07001842 if (mHotseat != null) {
Sunny Goyal11e96492018-03-23 17:06:00 -07001843 mHotseat.resetLayout(mDeviceProfile.isVerticalBarLayout());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001844 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001845 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001846 }
1847
Adam Cohendcd297f2013-06-18 13:13:40 -07001848 @Override
1849 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001850 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001851 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
1852 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001853 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
1854 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001855 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001856 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
1857 // If there are no screens, we need to have an empty screen
1858 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07001859 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001860 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07001861
Winsonc7d2e832016-07-28 12:24:55 -07001862 // After we have added all the screens, if the wallpaper was locked to the default state,
1863 // then notify to indicate that it can be released and a proper wallpaper offset can be
1864 // computed before the next layout
1865 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07001866 }
1867
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001868 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001869 int count = orderedScreenIds.size();
1870 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001871 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001872 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001873 // No need to bind the first screen, as its always bound.
1874 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
1875 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001876 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001877 }
1878
Sunny Goyalb23980c2017-08-17 07:45:25 -07001879 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08001880 public void bindAppsAdded(ArrayList<Long> newScreens, ArrayList<ItemInfo> addNotAnimated,
1881 ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07001882 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08001883 if (newScreens != null) {
1884 bindAddScreens(newScreens);
1885 }
Winson Chungd64d1762013-08-20 14:37:16 -07001886
1887 // We add the items without animation on non-visible pages, and with
1888 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08001889 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001890 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001891 }
Adam Cohen76a47a12014-02-05 11:47:43 -08001892 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001893 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07001894 }
Winson Chungc58497e2013-09-03 17:48:37 -07001895
Winson Chung87412982013-10-03 18:34:14 -07001896 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07001897 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001898 }
1899
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001900 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001901 * Bind the items start-end from the list.
1902 *
1903 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001904 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07001905 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07001906 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001907 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07001908 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001909 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07001910 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001911 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001912 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07001913 int end = items.size();
1914 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001915 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07001916
1917 // Short circuit if we are loading dock items for a configuration which has no dock
1918 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
1919 mHotseat == null) {
1920 continue;
1921 }
1922
Sunny Goyal639e9062015-08-19 19:17:06 -07001923 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001924 switch (item.itemType) {
1925 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1926 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08001927 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001928 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07001929 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001930 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001931 }
1932 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07001933 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001934 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001935 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001936 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001937 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07001938 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1939 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07001940 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07001941 if (view == null) {
1942 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001943 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08001944 break;
1945 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001946 default:
1947 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001948 }
Sunny Goyal639e9062015-08-19 19:17:06 -07001949
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001950 /*
1951 * Remove colliding items.
1952 */
1953 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1954 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
1955 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
1956 View v = cl.getChildAt(item.cellX, item.cellY);
1957 Object tag = v.getTag();
1958 String desc = "Collision while binding workspace item: " + item
1959 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08001960 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001961 throw (new RuntimeException(desc));
1962 } else {
1963 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001964 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001965 continue;
1966 }
1967 }
1968 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05301969 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07001970 if (animateIcons) {
1971 // Animate all the applications up now
1972 view.setAlpha(0f);
1973 view.setScaleX(0f);
1974 view.setScaleY(0f);
1975 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08001976 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07001977 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001978 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07001979
Winson Chung997a9232013-07-24 15:33:46 -07001980 if (animateIcons) {
1981 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08001982 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07001983 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08001984 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07001985 final Runnable startBounceAnimRunnable = new Runnable() {
1986 public void run() {
1987 anim.playTogether(bounceAnims);
1988 anim.start();
1989 }
1990 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08001991 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07001992 // We post the animation slightly delayed to prevent slowdowns
1993 // when we are loading right after we return to launcher.
1994 mWorkspace.postDelayed(new Runnable() {
1995 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00001996 if (mWorkspace != null) {
1997 mWorkspace.snapToPage(newScreenIndex);
1998 mWorkspace.postDelayed(startBounceAnimRunnable,
1999 NEW_APPS_ANIMATION_DELAY);
2000 }
Winson Chung94d67682013-09-25 16:29:40 -07002001 }
2002 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07002003 } else {
2004 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07002005 }
2006 }
Winson Chung64359a52013-07-08 17:17:08 -07002007 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002008 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002009 }
2010
Joe Onorato9c1289c2009-08-17 11:03:03 -04002011 /**
2012 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002013 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07002014 public void bindAppWidget(LauncherAppWidgetInfo item) {
2015 View view = inflateAppWidget(item);
2016 if (view != null) {
2017 mWorkspace.addInScreen(view, item);
2018 mWorkspace.requestLayout();
2019 }
2020 }
2021
2022 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002023 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302024 PendingAppWidgetHostView view =
2025 new PendingAppWidgetHostView(this, item, mIconCache, true);
2026 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002027 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002028 }
2029
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002030 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002031
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002032 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08002033
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002034 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2035 // If the provider is not ready, bind as a pending widget.
2036 appWidgetInfo = null;
2037 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2038 // The widget id is not valid. Try to find the widget based on the provider info.
2039 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
2040 } else {
2041 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
2042 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002043
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002044 // If the provider is ready, but the width is not yet restored, try to restore it.
2045 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
2046 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07002047 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002048 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
2049 + " belongs to component " + item.providerName
2050 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002051 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002052 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002053 }
Sunny Goyalff572272014-07-23 13:58:07 -07002054
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002055 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002056 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07002057 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2058 // Id has not been allocated yet. Allocate a new id.
2059 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
2060 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07002061
Sunny Goyald478c832016-04-01 12:04:16 -07002062 // Also try to bind the widget. If the bind fails, the user will be shown
2063 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08002064 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07002065 pendingInfo.spanX = item.spanX;
2066 pendingInfo.spanY = item.spanY;
2067 pendingInfo.minSpanX = item.minSpanX;
2068 pendingInfo.minSpanY = item.minSpanY;
2069 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07002070
2071 boolean isDirectConfig =
2072 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2073 if (isDirectConfig && item.bindOptions != null) {
2074 Bundle newOptions = item.bindOptions.getExtras();
2075 if (options != null) {
2076 newOptions.putAll(options);
2077 }
2078 options = newOptions;
2079 }
Sunny Goyald478c832016-04-01 12:04:16 -07002080 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2081 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002082
Sunny Goyal86df1382016-08-10 15:03:22 -07002083 // We tried to bind once. If we were not able to bind, we would need to
2084 // go through the permission dialog, which means we cannot skip the config
2085 // activity.
2086 item.bindOptions = null;
2087 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2088
Sunny Goyald478c832016-04-01 12:04:16 -07002089 // Bind succeeded
2090 if (success) {
2091 // If the widget has a configure activity, it is still needs to set it up,
2092 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002093 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002094 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2095 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002096 }
Sunny Goyald478c832016-04-01 12:04:16 -07002097
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002098 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002099 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002100 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002101 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002102 // The widget was marked as UI not ready, but there is no configure activity to
2103 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002104 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002105 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002106 }
Sunny Goyalff572272014-07-23 13:58:07 -07002107 }
2108
Sunny Goyald5462aa2016-11-22 16:52:39 +05302109 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002110 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002111 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002112 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002113 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07002114 deleteWidgetInfo(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002115 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002116 }
2117
Sunny Goyal233ee962015-08-03 13:05:01 -07002118 item.minSpanX = appWidgetInfo.minSpanX;
2119 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302120 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002121 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302122 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002123 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302124 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002125
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002126 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002127 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002128 }
2129
Sunny Goyalff572272014-07-23 13:58:07 -07002130 /**
2131 * Restores a pending widget.
2132 *
2133 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002134 */
Sunny Goyald478c832016-04-01 12:04:16 -07002135 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002136 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2137 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2138 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002139 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002140 }
2141
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002142 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002143 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002144 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2145 info.pendingItemInfo = null;
2146 }
Sunny Goyalff572272014-07-23 13:58:07 -07002147
Sunny Goyalba47faa2017-10-04 16:50:57 -07002148 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002149 view.reInflate();
Sunny Goyalba47faa2017-10-04 16:50:57 -07002150 }
2151
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002152 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002153 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002154 }
2155
Adam Cohen1462de32012-07-24 22:34:36 -07002156 public void onPageBoundSynchronously(int page) {
Sunny Goyalf4aceab2017-12-06 16:16:31 -08002157 mSynchronouslyBoundPage = page;
Adam Cohen1462de32012-07-24 22:34:36 -07002158 }
2159
Sunny Goyal527c7d32015-08-28 15:19:36 -07002160 @Override
2161 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2162 if (mPendingExecutor != null) {
2163 mPendingExecutor.markCompleted();
2164 }
2165 mPendingExecutor = executor;
Sunny Goyal60180b02018-02-07 12:56:30 -08002166 if (!isInState(ALL_APPS)) {
2167 mAppsView.getAppsStore().setDeferUpdates(true);
2168 mPendingExecutor.execute(() -> mAppsView.getAppsStore().setDeferUpdates(false));
2169 }
2170
Sunny Goyal527c7d32015-08-28 15:19:36 -07002171 executor.attachTo(this);
2172 }
2173
2174 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2175 if (mPendingExecutor == executor) {
2176 mPendingExecutor = null;
2177 }
2178 }
2179
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002180 @Override
2181 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002182 if (mDragLayer.getAlpha() < 1) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002183 mDragLayer.animate().alpha(1).withEndAction(
2184 executor == null ? null : executor::onLoadAnimationCompleted).start();
2185 } else if (executor != null) {
2186 executor.onLoadAnimationCompleted();
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002187 }
2188 }
2189
Joe Onorato9c1289c2009-08-17 11:03:03 -04002190 /**
2191 * Callback saying that there aren't any more items to bind.
2192 *
2193 * Implementation of the method from LauncherModel.Callbacks.
2194 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002195 public void finishBindingItems() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002196 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002197 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002198
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002199 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002200
Sunny Goyal2100c782016-08-22 16:00:03 -07002201 if (mPendingActivityResult != null) {
2202 handleActivityResult(mPendingActivityResult.requestCode,
2203 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2204 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002205 }
2206
Sunny Goyala474a9b2017-05-04 16:47:11 -07002207 InstallShortcutReceiver.disableAndFlushInstallQueue(
2208 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002209
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002210 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002211 }
2212
Winson Chunga2413752012-04-03 14:22:34 -07002213 private boolean canRunNewAppsAnimation() {
2214 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002215 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002216 }
2217
Winson Chungc9168342013-06-26 14:54:55 -07002218 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002219 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07002220 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
2221 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002222 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002223 return bounceAnim;
2224 }
2225
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002226 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002227 * Add the icons for all apps.
2228 *
2229 * Implementation of the method from LauncherModel.Callbacks.
2230 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002231 public void bindAllApplications(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002232 mAppsView.getAppsStore().setApps(apps);
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002233
Adam Cohen9211d422014-10-07 18:14:53 -07002234 if (mLauncherCallbacks != null) {
2235 mLauncherCallbacks.bindAllApplications(apps);
2236 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002237 }
2238
2239 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002240 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
2241 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2242 */
2243 @Override
2244 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002245 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002246 }
2247
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002248 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002249 * A package was updated.
2250 *
2251 * Implementation of the method from LauncherModel.Callbacks.
2252 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002253 @Override
2254 public void bindAppsAddedOrUpdated(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002255 mAppsView.getAppsStore().addOrUpdateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002256 }
2257
Sunny Goyal4390ace2014-10-13 11:33:11 -07002258 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002259 public void bindPromiseAppProgressUpdated(PromiseAppInfo app) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002260 mAppsView.getAppsStore().updatePromiseAppProgress(app);
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002261 }
2262
2263 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002264 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002265 mWorkspace.widgetsRestored(widgets);
2266 }
2267
Joe Onorato9c1289c2009-08-17 11:03:03 -04002268 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002269 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002270 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002271 *
2272 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002273 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002274 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002275 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002276 if (!updated.isEmpty()) {
2277 mWorkspace.updateShortcuts(updated);
2278 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002279 }
2280
2281 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002282 * Update the state of a package, typically related to install state.
2283 *
2284 * Implementation of the method from LauncherModel.Callbacks.
2285 */
Sunny Goyale755d462014-07-22 13:48:29 -07002286 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002287 public void bindRestoreItemsChange(HashSet<ItemInfo> updates) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07002288 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07002289 }
2290
2291 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002292 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07002293 * in addition to specific applications to remove, the reason being that
2294 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002295 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07002296 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002297 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07002298 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002299 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002300 mWorkspace.removeItemsByMatcher(matcher);
2301 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002302 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002303
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002304 @Override
2305 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002306 mAppsView.getAppsStore().removeApps(appInfos);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002307 }
Joe Onoratobe386092009-11-17 17:32:16 -08002308
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002309 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002310 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
2311 mPopupDataProvider.setAllWidgets(allWidgets);
Tony Wickham26b17462017-03-20 17:12:24 -07002312 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
2313 if (topView != null) {
2314 topView.onWidgetsBound();
2315 }
2316 }
2317
Tony Wickham86222d22017-03-29 15:30:43 -07002318 /**
2319 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
2320 * refreshes the widgets and shortcuts associated with the given package/user
2321 */
2322 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07002323 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002324 }
2325
Winson Chung80baf5a2010-08-09 16:03:15 -07002326 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002327 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08002328 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002329 @Override
2330 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
2331 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002332
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002333 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
2334 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07002335 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002336 writer.println(prefix + " Homescreen " + i);
2337
2338 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
2339 for (int j = 0; j < layout.getChildCount(); j++) {
2340 Object tag = layout.getChildAt(j).getTag();
2341 if (tag != null) {
2342 writer.println(prefix + " " + tag.toString());
2343 }
2344 }
2345 }
2346
2347 writer.println(prefix + " Hotseat");
2348 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07002349 for (int j = 0; j < layout.getChildCount(); j++) {
2350 Object tag = layout.getChildAt(j).getTag();
2351 if (tag != null) {
2352 writer.println(prefix + " " + tag.toString());
2353 }
Adam Cohen4caf2982013-08-20 18:54:31 -07002354 }
Sunny Goyala1365452015-10-01 15:46:24 -07002355 }
2356
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002357 writer.println(prefix + "Misc:");
2358 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
2359 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
2360 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
Sunny Goyal2bba1902018-02-27 12:20:50 -08002361 writer.println(" deviceProfile isTransposed=" + getDeviceProfile().isVerticalBarLayout());
2362 writer.println(" orientation=" + getResources().getConfiguration().orientation);
2363
2364 try {
2365 FileLog.flushAll(writer);
2366 } catch (Exception e) {
2367 // Ignore
2368 }
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002369
2370 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002371
Adam Cohen9211d422014-10-07 18:14:53 -07002372 if (mLauncherCallbacks != null) {
2373 mLauncherCallbacks.dump(prefix, fd, writer, args);
2374 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002375 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002376
Sunny Goyal66b24572016-09-21 15:57:55 -07002377 @Override
2378 @TargetApi(Build.VERSION_CODES.N)
2379 public void onProvideKeyboardShortcuts(
2380 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
2381
2382 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002383 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002384 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
2385 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
2386 }
2387 View currentFocus = getCurrentFocus();
2388 if (new CustomActionsPopup(this, currentFocus).canShow()) {
2389 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
2390 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
2391 }
Tony18c4aa42017-05-10 21:23:57 -05002392 if (currentFocus.getTag() instanceof ItemInfo
2393 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002394 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
2395 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
2396 }
2397 if (!shortcutInfos.isEmpty()) {
2398 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
2399 }
2400
2401 super.onProvideKeyboardShortcuts(data, menu, deviceId);
2402 }
2403
2404 @Override
2405 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
2406 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
2407 switch (keyCode) {
2408 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002409 if (isInState(NORMAL)) {
2410 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07002411 return true;
2412 }
2413 break;
2414 case KeyEvent.KEYCODE_S: {
2415 View focusedView = getCurrentFocus();
2416 if (focusedView instanceof BubbleTextView
2417 && focusedView.getTag() instanceof ItemInfo
2418 && mAccessibilityDelegate.performAction(focusedView,
2419 (ItemInfo) focusedView.getTag(),
2420 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal00ac9202017-11-09 15:24:06 -08002421 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07002422 return true;
2423 }
2424 break;
2425 }
2426 case KeyEvent.KEYCODE_O:
2427 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
2428 return true;
2429 }
2430 break;
2431 }
2432 }
2433 return super.onKeyShortcut(keyCode, event);
2434 }
2435
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002436 @Override
2437 public boolean onKeyUp(int keyCode, KeyEvent event) {
2438 if (keyCode == KeyEvent.KEYCODE_MENU) {
2439 // KEYCODE_MENU is sent by some tests, for example
2440 // LauncherJankTests#testWidgetsContainerFling. Don't just remove its handling.
2441 if (!mDragController.isDragging() && !mWorkspace.isSwitchingState() &&
2442 isInState(NORMAL)) {
2443 // Close any open floating views.
2444 AbstractFloatingView.closeAllOpenViews(this);
2445
2446 // Setting the touch point to (-1, -1) will show the options popup in the center of
2447 // the screen.
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08002448 OptionsPopupView.show(this, -1, -1);
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002449 }
2450 return true;
2451 }
2452 return super.onKeyUp(keyCode, event);
2453 }
2454
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002455 public static Launcher getLauncher(Context context) {
2456 if (context instanceof Launcher) {
2457 return (Launcher) context;
2458 }
2459 return ((Launcher) ((ContextWrapper) context).getBaseContext());
2460 }
2461
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002462 /**
2463 * Callback for listening for onResume
2464 */
2465 public interface OnResumeCallback {
2466
2467 void onLauncherResume();
2468 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002469}