blob: 985e0968dc690b6f296952aebff2426798943d73 [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;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700200
Sunny Goyal47328fd2016-05-25 18:56:41 -0700201 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700202
203 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700204 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700205 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700206
Winson Chung8ae41982017-11-10 11:42:13 -0800207 // UI and state for the overview panel
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800208 private View mOverviewPanel;
Winson Chung8ae41982017-11-10 11:42:13 -0800209
Adam Cohen091440a2015-03-18 14:16:05 -0700210 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400211
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700212 private OnResumeCallback mOnResumeCallback;
213
Sunny Goyal527c7d32015-08-28 15:19:36 -0700214 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700215
Joe Onorato9c1289c2009-08-17 11:03:03 -0400216 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800217 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800218 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700219 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400220
Jon Miranda2d89ea82017-05-04 11:47:53 -0700221 private ObjectAnimator mScrimAnimator;
Jon Miranda7fda2852017-07-19 16:07:01 -0700222 private boolean mShouldFadeInScrim;
Jon Miranda2d89ea82017-05-04 11:47:53 -0700223
Tony Wickham010d2552017-01-20 08:15:28 -0800224 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700225
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800226 private int mSynchronouslyBoundPage = PagedView.INVALID_PAGE;
Adam Cohen1462de32012-07-24 22:34:36 -0700227
Winson Chung46353de2012-02-16 14:05:10 -0800228 // We only want to get the SharedPreferences once since it does an FS stat each time we get
229 // it from the context.
230 private SharedPreferences mSharedPrefs;
231
Sunny Goyal2100c782016-08-22 16:00:03 -0700232 // Activity result which needs to be processed after workspace has loaded.
233 private ActivityResultInfo mPendingActivityResult;
234 /**
235 * Holds extra information required to handle a result from an external call, like
236 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
237 */
238 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800239
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700240 public ViewGroupFocusHelper mFocusHandler;
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700241 private boolean mAppLaunchSuccess;
242
Sunny Goyal623eddd2018-03-02 12:24:41 -0800243 private RotationHelper mRotationHelper;
Sunny Goyal7779d622015-06-11 16:18:39 -0700244
Winson Chungedb0d6b2018-03-21 17:32:55 -0700245 // Used to keep track of the swipe up state
246 private SharedPreferences.OnSharedPreferenceChangeListener mSharedPrefsListener =
247 (sharedPreferences, s) -> {
248 mDragLayer.setup(mDragController);
249 };
250
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800251 @Override
252 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700253 if (DEBUG_STRICT_MODE) {
254 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
255 .detectDiskReads()
256 .detectDiskWrites()
257 .detectNetwork() // or .detectAll() for all detectable problems
258 .penaltyLog()
259 .build());
260 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
261 .detectLeakedSqlLiteObjects()
262 .detectLeakedClosableObjects()
263 .penaltyLog()
264 .penaltyDeath()
265 .build());
266 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700267 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700268
Mario Bertschler27288382017-05-24 15:35:09 -0700269 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
270 wallpaperColorInfo.setOnThemeChangeListener(this);
Mario Bertschlera6936942017-05-31 14:48:19 -0700271 overrideTheme(wallpaperColorInfo.isDark(), wallpaperColorInfo.supportsDarkText());
Mario Bertschler27288382017-05-24 15:35:09 -0700272
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800273 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700274 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400275
Sunny Goyal87f784c2017-01-11 10:48:34 -0800276 LauncherAppState app = LauncherAppState.getInstance(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800277 mOldConfig = new Configuration(getResources().getConfiguration());
Sunny Goyal0e7724c2018-02-22 13:22:21 -0800278 mModel = app.setLauncher(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800279 initDeviceProfile(app.getInvariantDeviceProfile());
Winson Chung6e1c0d32013-10-25 15:24:24 -0700280
Sunny Goyalf7258242015-10-19 16:59:07 -0700281 mSharedPrefs = Utilities.getPrefs(this);
Winson Chungedb0d6b2018-03-21 17:32:55 -0700282 mSharedPrefs.registerOnSharedPreferenceChangeListener(mSharedPrefsListener);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800283 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700284 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700285
Joe Onorato41a12d22009-10-31 18:30:00 -0400286 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700287 mAllAppsController = new AllAppsTransitionController(this);
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800288 mStateManager = new LauncherStateManager(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700289
Sunny Goyalffe83f12014-08-14 17:39:34 -0700290 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700291
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700292 mAppWidgetHost = new LauncherAppWidgetHost(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700293 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700294
Sunny Goyal60820d72017-05-09 12:40:11 -0700295 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700296
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800297 setupViews();
Tony Wickham010d2552017-01-20 08:15:28 -0800298 mPopupDataProvider = new PopupDataProvider(this);
299
Sunny Goyal623eddd2018-03-02 12:24:41 -0800300 mRotationHelper = new RotationHelper(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800301
Sunny Goyald3864fa2017-11-14 15:06:36 -0800302 boolean internalStateHandled = InternalStateHandler.handleCreate(this, getIntent());
303 if (internalStateHandled) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800304 if (savedInstanceState != null) {
305 // InternalStateHandler has already set the appropriate state.
306 // We dont need to do anything.
307 savedInstanceState.remove(RUNTIME_STATE);
308 }
309 }
310 restoreState(savedInstanceState);
Winson Chungb63b44c2017-11-10 17:54:44 -0800311
Sunny Goyal2100c782016-08-22 16:00:03 -0700312 // We only load the page synchronously if the user rotates (or triggers a
313 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700314 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
315 if (savedInstanceState != null) {
316 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
317 }
Sunny Goyald3864fa2017-11-14 15:06:36 -0800318
Sunny Goyalfe770c92016-09-27 14:38:58 -0700319 if (!mModel.startLoader(currentScreen)) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800320 if (!internalStateHandled) {
321 // If we are not binding synchronously, show a fade in animation when
322 // the first page bind completes.
323 mDragLayer.setAlpha(0);
324 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700325 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700326 // Pages bound synchronously.
327 mWorkspace.setCurrentPage(currentScreen);
328
Sunny Goyal2100c782016-08-22 16:00:03 -0700329 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800330 }
331
332 // For handling default keys
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800333 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800334
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800335 setContentView(mLauncherView);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800336 getRootView().dispatchInsets();
Mario Bertschlera6936942017-05-31 14:48:19 -0700337
Jon Miranda7fda2852017-07-19 16:07:01 -0700338 // Listen for broadcasts
339 IntentFilter filter = new IntentFilter();
340 filter.addAction(Intent.ACTION_SCREEN_OFF);
341 filter.addAction(Intent.ACTION_USER_PRESENT); // When the device wakes up + keyguard is gone
342 registerReceiver(mReceiver, filter);
343 mShouldFadeInScrim = true;
Sunny Goyala616d2b2017-06-12 13:54:01 -0700344
Sunny Goyal8392c822017-06-20 10:03:56 -0700345 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
346 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700347
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800348 mAppTransitionManager = LauncherAppTransitionManager.newInstance(this);
Jon Miranda7853bdb2018-01-22 17:32:30 -0800349
Sunny Goyal14b32402017-07-31 10:03:28 -0700350 if (mLauncherCallbacks != null) {
351 mLauncherCallbacks.onCreate(savedInstanceState);
352 }
Sunny Goyal623eddd2018-03-02 12:24:41 -0800353 mRotationHelper.initialize();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700354
355 TraceHelper.endSection("Launcher-onCreate");
Adam Cohen9211d422014-10-07 18:14:53 -0700356 }
357
Sunny Goyal7779d622015-06-11 16:18:39 -0700358 @Override
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800359 public void onConfigurationChanged(Configuration newConfig) {
360 int diff = newConfig.diff(mOldConfig);
361 if ((diff & (CONFIG_ORIENTATION | CONFIG_SCREEN_SIZE)) != 0) {
362 mUserEventDispatcher = null;
363 initDeviceProfile(mDeviceProfile.inv);
Sunny Goyal2bba1902018-02-27 12:20:50 -0800364 FileLog.d(TAG, "Config changed, my orientation=" +
365 getResources().getConfiguration().orientation +
366 ", new orientation=" + newConfig.orientation +
367 ", old orientation=" + mOldConfig.orientation +
368 ", isTransposed=" + mDeviceProfile.isVerticalBarLayout());
Sunny Goyalfde55052018-02-01 14:46:13 -0800369 dispatchDeviceProfileChanged();
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800370
371 getRootView().dispatchInsets();
372 getStateManager().reapplyState();
373
Sunny Goyal7185dd62018-03-14 17:51:49 -0700374 // Recreate touch controllers
375 mDragLayer.setup(mDragController);
376
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800377 // TODO: We can probably avoid rebind when only screen size changed.
Sunny Goyal605bcf32018-03-02 15:16:12 -0800378 rebindModel();
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800379 }
380
381 mOldConfig.setTo(newConfig);
Tracy Zhoua706f002018-03-28 13:55:19 -0700382 UiFactory.onLauncherStateOrResumeChanged(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800383 super.onConfigurationChanged(newConfig);
384 }
385
Sunny Goyal605bcf32018-03-02 15:16:12 -0800386 @Override
387 public void rebindModel() {
388 int currentPage = mWorkspace.getNextPage();
389 if (mModel.startLoader(currentPage)) {
390 mWorkspace.setCurrentPage(currentPage);
391 setWorkspaceLoading(true);
392 }
393 }
394
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800395 private void initDeviceProfile(InvariantDeviceProfile idp) {
396 // Load configuration-specific DeviceProfile
Sunny Goyalf633ef52018-03-13 09:57:05 -0700397 setDeviceProfile(idp.getDeviceProfile(this));
Sunny Goyal605bcf32018-03-02 15:16:12 -0800398 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout(), true);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800399 }
400
Sunny Goyal623eddd2018-03-02 12:24:41 -0800401 public RotationHelper getRotationHelper() {
402 return mRotationHelper;
403 }
404
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800405 @Override
Mario Bertschler27288382017-05-24 15:35:09 -0700406 public void onThemeChanged() {
407 recreate();
408 }
409
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700410 public LauncherStateManager getStateManager() {
411 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700412 }
413
Mario Bertschlera6936942017-05-31 14:48:19 -0700414 protected void overrideTheme(boolean isDark, boolean supportsDarkText) {
Mario Bertschler27288382017-05-24 15:35:09 -0700415 if (isDark) {
416 setTheme(R.style.LauncherThemeDark);
Mario Bertschlera6936942017-05-31 14:48:19 -0700417 } else if (supportsDarkText) {
418 setTheme(R.style.LauncherThemeDarkText);
Mario Bertschler27288382017-05-24 15:35:09 -0700419 }
420 }
421
422 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700423 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800424 return mLauncherView.findViewById(id);
425 }
426
427 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700428 public void onAppWidgetHostReset() {
429 if (mAppWidgetHost != null) {
430 mAppWidgetHost.startListening();
431 }
432 }
433
Adam Cohen9211d422014-10-07 18:14:53 -0700434 private LauncherCallbacks mLauncherCallbacks;
435
Sunny Goyal32554d12015-12-03 15:31:25 -0800436 /**
437 * Call this after onCreate to set or clear overlay.
438 */
439 public void setLauncherOverlay(LauncherOverlay overlay) {
440 if (overlay != null) {
441 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
442 }
443 mWorkspace.setLauncherOverlay(overlay);
444 }
445
Adam Cohen9211d422014-10-07 18:14:53 -0700446 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
447 mLauncherCallbacks = callbacks;
448 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700449 }
450
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700451 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700452 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700453 if (mLauncherCallbacks != null) {
454 mLauncherCallbacks.onLauncherProviderChange();
455 }
456 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700457
Hyunyoung Song3f471442015-04-08 19:01:34 -0700458 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700459 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
460 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700461 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700462 }
463
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000464 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700465 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
466 // This cast is safe as long as the id < 0x00FFFFFF
467 // Since we jail all the dynamically generated views, there should be no clashes
468 // with any other views.
469 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000470 }
471
Tony Wickham010d2552017-01-20 08:15:28 -0800472 public PopupDataProvider getPopupDataProvider() {
473 return mPopupDataProvider;
474 }
475
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700476 @Override
477 public BadgeInfo getBadgeInfoForItem(ItemInfo info) {
478 return mPopupDataProvider.getBadgeInfoForItem(info);
479 }
480
481 @Override
482 public void invalidateParent(ItemInfo info) {
483 FolderIconPreviewVerifier verifier = new FolderIconPreviewVerifier(getDeviceProfile().inv);
484 if (verifier.isItemInPreview(info.rank) && (info.container >= 0)) {
485 View folderIcon = getWorkspace().getHomescreenIconByItemId(info.container);
486 if (folderIcon != null) {
487 folderIcon.invalidate();
488 }
489 }
490 }
491
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000492 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700493 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
494 * a configuration step, this allows the proper animations to run after other transitions.
495 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700496 private long completeAdd(
497 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
498 long screenId = info.screenId;
499 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700500 // When the screen id represents an actual screen (as opposed to a rank) we make sure
501 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700502 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700503 }
Adam Cohendb364c32014-05-20 14:23:40 -0700504
Sunny Goyal2100c782016-08-22 16:00:03 -0700505 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800506 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700507 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700508 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800509 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700510 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700511 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700512 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700513 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700514 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700515 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700516 int widgetId = appWidgetId;
517 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700518 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700519 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700520 // Since the view was just bound, also launch the configure activity if needed
521 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
522 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800523 if (provider != null) {
524 new WidgetAddFlowHandler(provider)
525 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700526 }
527 }
528 break;
529 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800530 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700531
Adam Cohendb364c32014-05-20 14:23:40 -0700532 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800533 }
534
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800535 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700536 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700537 if (isWorkspaceLoading()) {
538 // process the result once the workspace has loaded.
539 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
540 return;
541 }
542 mPendingActivityResult = null;
543
Winson Chunge341d302013-08-16 14:31:00 -0700544 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700545 final PendingRequestArgs requestArgs = mPendingRequestArgs;
546 setWaitingForResult(null);
547 if (requestArgs == null) {
548 return;
549 }
550
551 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700552
Adam Cohenad4e15c2013-10-17 16:21:35 -0700553 Runnable exitSpringLoaded = new Runnable() {
554 @Override
555 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700556 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700557 }
558 };
559
Michael Jurka8b805b12012-04-18 14:23:14 -0700560 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700561 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700562 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700563 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
564 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700565 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700566 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700567 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700568 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700569 addAppWidgetImpl(
570 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800571 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700572 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700573 }
574 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200575 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700576 if (resultCode == RESULT_OK && isInState(OVERVIEW)) {
Tony Wickhame3054412015-09-09 09:05:25 -0700577 // User could have free-scrolled between pages before picking a wallpaper; make sure
578 // we move to the closest one now.
579 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700580 mStateManager.goToState(NORMAL, false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200581 }
582 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700583 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200584
Adam Cohened66b2b2012-01-23 17:28:51 -0800585 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700586 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700587
Adam Cohened66b2b2012-01-23 17:28:51 -0800588 // We have special handling for widgets
589 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800590 final int appWidgetId;
591 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
592 : -1;
593 if (widgetId < 0) {
594 appWidgetId = pendingAddWidgetId;
595 } else {
596 appWidgetId = widgetId;
597 }
598
Adam Cohenad4e15c2013-10-17 16:21:35 -0700599 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800600 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700601 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
602 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700603 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700604 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700605 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700606 @Override
607 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700608 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700609 }
610 };
Adam Cohendb364c32014-05-20 14:23:40 -0700611
Sunny Goyal2100c782016-08-22 16:00:03 -0700612 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
613 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
614 } else {
615 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
616 // When the screen id represents an actual screen (as opposed to a rank)
617 // we make sure that the drop page actually exists.
618 requestArgs.screenId =
619 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700620 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700621 final CellLayout dropLayout =
622 mWorkspace.getScreenWithId(requestArgs.screenId);
623
624 dropLayout.setDropPending(true);
625 final Runnable onComplete = new Runnable() {
626 @Override
627 public void run() {
628 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
629 dropLayout.setDropPending(false);
630 }
631 };
632 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
633 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700634 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800635 return;
636 }
637
Sunny Goyald478c832016-04-01 12:04:16 -0700638 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
639 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700640 if (resultCode == RESULT_OK) {
641 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700642 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700643 }
644 // Leave the widget in the pending state if the user canceled the configure.
645 return;
646 }
647
Sunny Goyalaad90582015-07-09 14:22:50 -0700648 if (requestCode == REQUEST_CREATE_SHORTCUT) {
649 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700650 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
651 completeAdd(requestCode, data, -1, requestArgs);
652 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
653 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
654
Sunny Goyalaad90582015-07-09 14:22:50 -0700655 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700656 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
657 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800658 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800659 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800660 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800661 }
662
663 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700664 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800665 final int requestCode, final int resultCode, final Intent data) {
666 handleActivityResult(requestCode, resultCode, data);
667 if (mLauncherCallbacks != null) {
668 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
669 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800670 }
671
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700672 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700673 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600674 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700675 PendingRequestArgs pendingArgs = mPendingRequestArgs;
676 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
677 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
678 setWaitingForResult(null);
679
Sunny Goyal28c6b962015-10-12 11:42:05 -0700680 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700681 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700682 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700683 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700684 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700685 Intent intent = pendingArgs.getPendingIntent();
686
Sunny Goyal28c6b962015-10-12 11:42:05 -0700687 if (grantResults.length > 0
688 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700689 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700690 } else {
691 // TODO: Show a snack bar with link to settings
692 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700693 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700694 }
695 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600696 if (mLauncherCallbacks != null) {
697 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
698 grantResults);
699 }
700 }
701
Adam Cohendb364c32014-05-20 14:23:40 -0700702 /**
703 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
704 *
705 * @param screenId the screen id to check
706 * @return the new screen, or screenId if it exists
707 */
708 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800709 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700710 if (dropLayout == null) {
711 // it's possible that the add screen was removed because it was
712 // empty and a re-bind occurred
713 mWorkspace.addExtraEmptyScreen();
714 return mWorkspace.commitExtraEmptyScreen();
715 } else {
716 return screenId;
717 }
718 }
719
Sunny Goyal2100c782016-08-22 16:00:03 -0700720 @Thunk void completeTwoStageWidgetDrop(
721 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
722 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800723 Runnable onCompleteRunnable = null;
724 int animationType = 0;
725
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700726 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800727 if (resultCode == RESULT_OK) {
728 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
729 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800730 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700731 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800732 onCompleteRunnable = new Runnable() {
733 @Override
734 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700735 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700736 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800737 }
738 };
739 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800740 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800741 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800742 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700743 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700744 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700745 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
746 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700747 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700748 // The animated view may be null in the case of a rotation during widget configuration
749 onCompleteRunnable.run();
750 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800751 }
752
753 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700754 protected void onStop() {
755 super.onStop();
756 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700757
758 if (mLauncherCallbacks != null) {
759 mLauncherCallbacks.onStop();
760 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800761 mAppWidgetHost.setListenIfResumed(false);
Tony Wickham010d2552017-01-20 08:15:28 -0800762
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700763 if (!mAppLaunchSuccess) {
764 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -0800765 mStateManager.getState().containerType, -1);
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700766 }
Tony Wickham010d2552017-01-20 08:15:28 -0800767 NotificationListener.removeNotificationsChangedListener();
Sunny Goyalaf161fd2018-02-05 16:34:57 -0800768 getStateManager().moveToRestState();
Winson Chungedb0d6b2018-03-21 17:32:55 -0700769
Michael Jurkacd496d72013-04-11 11:32:45 -0700770 }
771
772 @Override
773 protected void onStart() {
774 super.onStart();
775 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700776
777 if (mLauncherCallbacks != null) {
778 mLauncherCallbacks.onStart();
779 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800780 mAppWidgetHost.setListenIfResumed(true);
Sunny Goyalcac002d2018-01-04 09:51:45 -0800781 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
Jon Miranda2d89ea82017-05-04 11:47:53 -0700782
Jon Mirandade43a712018-01-18 11:35:10 -0800783 if (mShouldFadeInScrim && mLauncherView.getBackground() != null) {
Jon Miranda2d89ea82017-05-04 11:47:53 -0700784 if (mScrimAnimator != null) {
785 mScrimAnimator.cancel();
786 }
Jon Mirandade43a712018-01-18 11:35:10 -0800787 mLauncherView.getBackground().setAlpha(0);
788 mScrimAnimator = ObjectAnimator.ofInt(mLauncherView.getBackground(),
Jon Miranda2d89ea82017-05-04 11:47:53 -0700789 LauncherAnimUtils.DRAWABLE_ALPHA, 0, 255);
790 mScrimAnimator.addListener(new AnimatorListenerAdapter() {
791 @Override
792 public void onAnimationEnd(Animator animation) {
793 mScrimAnimator = null;
794 }
795 });
796 mScrimAnimator.setDuration(600);
797 mScrimAnimator.setStartDelay(getWindow().getTransitionBackgroundFadeDuration());
798 mScrimAnimator.start();
799 }
Jon Miranda7fda2852017-07-19 16:07:01 -0700800 mShouldFadeInScrim = false;
Winson Chung62a70be2018-02-23 15:10:05 -0800801 UiFactory.onStart(this);
Michael Jurkacd496d72013-04-11 11:32:45 -0700802 }
803
804 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800805 protected void onResume() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700806 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800807 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700808 TraceHelper.partitionSection("ON_RESUME", "superCall");
809
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700810 mAppLaunchSuccess = false;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700811 getUserEventDispatcher().resetElapsedSessionMillis();
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700812 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700813 // Process any items that were added while Launcher was away.
814 InstallShortcutReceiver.disableAndFlushInstallQueue(
815 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700816
Sunny Goyala474a9b2017-05-04 16:47:11 -0700817 // Refresh shortcuts if the permission changed.
818 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700819
Sunny Goyal7ede6112017-12-05 15:11:21 -0800820 DiscoveryBounce.showIfNeeded(this);
Adam Cohen9211d422014-10-07 18:14:53 -0700821 if (mLauncherCallbacks != null) {
822 mLauncherCallbacks.onResume();
823 }
Tracy Zhoua706f002018-03-28 13:55:19 -0700824 UiFactory.onLauncherStateOrResumeChanged(this);
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
Tracy Zhoua706f002018-03-28 13:55:19 -0700844 protected void onUserLeaveHint() {
845 super.onUserLeaveHint();
846 UiFactory.onLauncherStateOrResumeChanged(this);
847 }
848
849 @Override
Winson Chungd7823942018-02-16 03:23:47 +0000850 public void onWindowFocusChanged(boolean hasFocus) {
851 super.onWindowFocusChanged(hasFocus);
852 mStateManager.onWindowFocusChanged();
853 }
854
Adam Cohenc2d6e892014-10-16 09:49:24 -0700855 public interface LauncherOverlay {
856
857 /**
858 * Touch interaction leading to overscroll has begun
859 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700860 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700861
862 /**
863 * Touch interaction related to overscroll has ended
864 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700865 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700866
867 /**
868 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
869 * screen (or in the case of RTL, the rightmost screen).
870 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700871 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700872
873 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800874 * Called when the launcher is ready to use the overlay
875 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700876 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700877 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700878 }
879
880 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700881 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700882 }
883
884 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
885
Sunny Goyalc86df472016-02-25 09:19:38 -0800886 public void onScrollChanged(float progress) {
887 if (mWorkspace != null) {
888 mWorkspace.onOverlayScrollChanged(progress);
889 }
890 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700891 }
892
Sunny Goyal16764582017-11-06 16:00:34 -0800893 public boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -0700894 if (mLauncherCallbacks != null) {
895 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700896 } else {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800897 // On O and above we there is always some setting present settings (add icon to
898 // home screen or icon badging). On earlier APIs we will have the allow rotation
899 // setting, on devices with a locked orientation,
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700900 return Utilities.ATLEAST_OREO || !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -0700901 }
Jorim Jaggid017f882014-01-14 17:08:48 -0800902 }
903
Sunny Goyalf9403d92017-10-18 10:55:56 -0700904 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -0700905 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700906 }
907
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800908 /**
909 * Restores the previous state, if it exists.
910 *
911 * @param savedState The previous state.
912 */
913 private void restoreState(Bundle savedState) {
914 if (savedState == null) {
915 return;
916 }
917
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700918 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -0700919 LauncherState[] stateValues = LauncherState.values();
920 LauncherState state = stateValues[stateOrdinal];
Sunny Goyalbc683e92017-12-06 10:25:07 -0800921 if (!state.disableRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700922 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800923 }
924
Sunny Goyal2100c782016-08-22 16:00:03 -0700925 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
926 if (requestArgs != null) {
927 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800928 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700929
930 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700931
932 SparseArray<Parcelable> widgetsState =
933 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
934 if (widgetsState != null) {
935 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
936 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800937 }
938
939 /**
940 * Finds all the views we need and configure them properly.
941 */
942 private void setupViews() {
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800943 mDragLayer = findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700944 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -0700945 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700946 mWorkspace.initParentViews(mDragLayer);
Sunny Goyal16764582017-11-06 16:00:34 -0800947 mOverviewPanel = findViewById(R.id.overview_panel);
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800948 mHotseat = findViewById(R.id.hotseat);
Craig Mautner360310b2012-10-26 15:13:08 -0700949
Sunny Goyal784f9c32016-03-23 16:56:54 -0700950 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
951 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
952 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800953
Winson Chung4c98d922011-05-31 16:50:48 -0700954 // Setup the drag layer
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700955 mDragLayer.setup(mDragController);
Winson Chung4c98d922011-05-31 16:50:48 -0700956
Hyunyoung Song645764e2016-06-06 14:19:02 -0700957 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -0700958 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
959 // default state, otherwise we will update to the wrong offsets in RTL
960 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700961 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700962 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700963
Tony Wickham34d2c912015-09-11 09:27:58 -0700964 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700965 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700966
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700967 // Setup Apps
968 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -0700969
Winson Chung3d503fb2011-07-13 17:25:49 -0700970 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -0700971 mDragController.setMoveTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700972 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -0400973
Sunny Goyal7185dd62018-03-14 17:51:49 -0700974 mAllAppsController.setupViews(mAppsView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800975 }
976
977 /**
978 * Creates a view representing a shortcut.
979 *
980 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800981 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800982 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700983 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800984 }
985
986 /**
987 * Creates a view representing a shortcut inflated from the specified resource.
988 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800989 * @param parent The group the shortcut belongs to.
990 * @param info The data structure describing the shortcut.
991 *
992 * @return A View inflated from layoutResId.
993 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700994 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -0800995 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
996 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800997 favorite.applyFromShortcutInfo(info);
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800998 favorite.setOnClickListener(ItemClickHandler.INSTANCE);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700999 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001000 return favorite;
1001 }
1002
1003 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -07001004 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001005 *
1006 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001007 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001008 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001009 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001010 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
1011 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001012 return;
1013 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001014
Jon Mirandac476d6e2017-05-04 16:30:01 -07001015 int[] cellXY = mTmpAddItemCellCoordinates;
1016 CellLayout layout = getCellLayout(container, screenId);
1017
Sunny Goyal782f0c92017-01-19 10:27:54 -08001018 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001019 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001020 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
1021 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001022 }
1023
1024 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001025 // Legacy shortcuts are only supported for primary profile.
1026 info = Process.myUserHandle().equals(args.user)
1027 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001028
Sunny Goyalb57645f2017-03-20 13:57:28 -07001029 if (info == null) {
1030 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1031 return;
1032 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001033 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001034 // The app is trying to add a shortcut without sufficient permissions
1035 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1036 return;
1037 }
1038 }
1039
Jon Mirandac476d6e2017-05-04 16:30:01 -07001040 if (container < 0) {
1041 // Adding a shortcut to the Workspace.
1042 final View view = createShortcut(info);
1043 boolean foundCellSpan = false;
1044 // First we check if we already know the exact location where we want to add this item.
1045 if (cellX >= 0 && cellY >= 0) {
1046 cellXY[0] = cellX;
1047 cellXY[1] = cellY;
1048 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001049
Jon Mirandac476d6e2017-05-04 16:30:01 -07001050 // If appropriate, either create a folder or add to an existing folder
1051 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001052 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001053 return;
1054 }
1055 DragObject dragObject = new DragObject();
1056 dragObject.dragInfo = info;
1057 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1058 true)) {
1059 return;
1060 }
1061 } else {
1062 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1063 }
1064
1065 if (!foundCellSpan) {
1066 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001067 return;
1068 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001069
1070 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1071 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001072 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001073 // Adding a shortcut to a Folder.
Sunny Goyale29897f2017-07-20 10:09:42 -07001074 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001075 if (folderIcon != null) {
1076 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1077 folderInfo.add(info, args.rank, false);
1078 } else {
Sunny Goyale29897f2017-07-20 10:09:42 -07001079 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001080 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001081 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001082 }
1083
Sunny Goyale29897f2017-07-20 10:09:42 -07001084 public FolderIcon findFolderIcon(final long folderIconId) {
1085 return (FolderIcon) mWorkspace.getFirstMatch(new ItemOperator() {
1086 @Override
1087 public boolean evaluate(ItemInfo info, View view) {
1088 return info != null && info.id == folderIconId;
1089 }
1090 });
1091 }
1092
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001093 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001094 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001095 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001096 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001097 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001098 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001099 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1100
Adam Cohened66b2b2012-01-23 17:28:51 -08001101 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001102 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001103 }
Romain Guycbb89e42009-06-08 15:52:54 -07001104
Adam Cohen59400422014-03-05 18:07:04 -08001105 LauncherAppWidgetInfo launcherInfo;
1106 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001107 launcherInfo.spanX = itemInfo.spanX;
1108 launcherInfo.spanY = itemInfo.spanY;
1109 launcherInfo.minSpanX = itemInfo.minSpanX;
1110 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001111 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001112
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001113 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001114 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001115
Sunny Goyal2100c782016-08-22 16:00:03 -07001116 if (hostView == null) {
1117 // Perform actual inflation because we're live
1118 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001119 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001120 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301121 prepareAppWidget(hostView, launcherInfo);
1122 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001123 }
Romain Guycbb89e42009-06-08 15:52:54 -07001124
Sunny Goyald5462aa2016-11-22 16:52:39 +05301125 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001126 hostView.setTag(item);
1127 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001128 hostView.setFocusable(true);
1129 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001130 }
1131
Adam Cohended9f8d2010-11-03 13:25:16 -07001132 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1133 @Override
1134 public void onReceive(Context context, Intent intent) {
1135 final String action = intent.getAction();
1136 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001137 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001138 // processing a multi-step drop
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001139 if (mAppsView != null && mPendingRequestArgs == null) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001140 mStateManager.goToState(NORMAL);
Winson Chung785d2eb2011-04-14 16:08:02 -07001141 }
Jon Miranda7fda2852017-07-19 16:07:01 -07001142 mShouldFadeInScrim = true;
1143 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1144 // ACTION_USER_PRESENT is sent after onStart/onResume. This covers the case where
1145 // the user unlocked and the Launcher is not in the foreground.
1146 mShouldFadeInScrim = false;
Adam Cohended9f8d2010-11-03 13:25:16 -07001147 }
1148 }
1149 };
1150
Tony Wickham010d2552017-01-20 08:15:28 -08001151 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
Sunny Goyal29947f02017-12-18 13:49:44 -08001152 mWorkspace.updateIconBadges(updatedBadges);
Sunny Goyal60180b02018-02-07 12:56:30 -08001153 mAppsView.getAppsStore().updateIconBadges(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001154
Sunny Goyal29947f02017-12-18 13:49:44 -08001155 PopupContainerWithArrow popup = PopupContainerWithArrow.getOpen(Launcher.this);
1156 if (popup != null) {
1157 popup.updateNotificationHeader(updatedBadges);
Tony Wickham010d2552017-01-20 08:15:28 -08001158 }
1159 }
1160
Adam Cohended9f8d2010-11-03 13:25:16 -07001161 @Override
1162 public void onAttachedToWindow() {
1163 super.onAttachedToWindow();
1164
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001165 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyalc86df472016-02-25 09:19:38 -08001166 if (mLauncherCallbacks != null) {
1167 mLauncherCallbacks.onAttachedToWindow();
1168 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001169 }
1170
1171 @Override
1172 public void onDetachedFromWindow() {
1173 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001174
1175 if (mLauncherCallbacks != null) {
1176 mLauncherCallbacks.onDetachedFromWindow();
1177 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001178 }
1179
Sunny Goyal3bac2c22018-02-07 14:44:05 -08001180 public void onQuickstepGestureStarted(boolean isVisible) {
1181 if (mLauncherCallbacks != null) {
1182 mLauncherCallbacks.onQuickstepGestureStarted(isVisible);
1183 }
1184 }
1185
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001186 public AllAppsTransitionController getAllAppsController() {
1187 return mAllAppsController;
1188 }
1189
Sunny Goyal9d69c8d2018-03-19 13:41:31 -07001190 @Override
Winson Chung1a341002018-01-17 10:00:23 -08001191 public LauncherRootView getRootView() {
1192 return (LauncherRootView) mLauncherView;
1193 }
1194
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001195 @Override
Winson Chunga6945242014-01-08 14:04:34 -08001196 public DragLayer getDragLayer() {
1197 return mDragLayer;
1198 }
1199
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001200 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001201 return mAppsView;
1202 }
1203
Winson Chunga6945242014-01-08 14:04:34 -08001204 public Workspace getWorkspace() {
1205 return mWorkspace;
1206 }
1207
1208 public Hotseat getHotseat() {
1209 return mHotseat;
1210 }
1211
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001212 public <T extends View> T getOverviewPanel() {
Winson Chung8ae41982017-11-10 11:42:13 -08001213 return (T) mOverviewPanel;
Winson Chunga6945242014-01-08 14:04:34 -08001214 }
1215
Sunny Goyal47328fd2016-05-25 18:56:41 -07001216 public DropTargetBar getDropTargetBar() {
1217 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001218 }
1219
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001220 public LauncherAppWidgetHost getAppWidgetHost() {
1221 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001222 }
Romain Guycbb89e42009-06-08 15:52:54 -07001223
Winson Chunga9abd0e2010-10-27 17:18:37 -07001224 public LauncherModel getModel() {
1225 return mModel;
1226 }
1227
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001228 public ModelWriter getModelWriter() {
1229 return mModelWriter;
1230 }
1231
Adam Cohen79d90c52016-04-22 13:29:20 -07001232 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001233 return mSharedPrefs;
1234 }
1235
Sunny Goyalf8d56fc2018-01-31 15:18:11 -08001236 public int getOrientation() { return mOldConfig.orientation; }
Jon Miranda6bed3502017-09-19 14:43:17 -07001237
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001238 @Override
1239 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001240 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001241 super.onNewIntent(intent);
1242
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001243 boolean alreadyOnHome = hasWindowFocus() && ((intent.getFlags() &
Winson15f8b172015-08-19 11:02:39 -07001244 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1245 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001246
1247 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001248 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001249 && AbstractFloatingView.getTopOpenView(this) == null;
Winson15f8b172015-08-19 11:02:39 -07001250 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001251 boolean internalStateHandled = InternalStateHandler
Sunny Goyal8b264562018-02-03 23:00:13 -08001252 .handleNewIntent(this, intent, isStarted());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001253
Winson15f8b172015-08-19 11:02:39 -07001254 if (isActionMain) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001255 if (!internalStateHandled) {
1256 // Note: There should be at most one log per method call. This is enforced
1257 // implicitly by using if-else statements.
1258 UserEventDispatcher ued = getUserEventDispatcher();
1259 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
1260 if (topOpenView != null) {
1261 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
1262 } else if (alreadyOnHome) {
1263 Target target = newContainerTarget(mStateManager.getState().containerType);
1264 target.pageIndex = mWorkspace.getCurrentPage();
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001265 ued.logActionCommand(Action.Command.HOME_INTENT, target,
1266 newContainerTarget(ContainerType.WORKSPACE));
Sunny Goyald3864fa2017-11-14 15:06:36 -08001267 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001268
Sunny Goyald3864fa2017-11-14 15:06:36 -08001269 // In all these cases, only animate if we're already on home
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001270 AbstractFloatingView.closeAllOpenViews(this, isStarted());
Jon Mirandafeba90f2016-10-06 10:53:29 -07001271
Sunny Goyala2467272018-03-16 14:53:05 -07001272 if (!isInState(NORMAL)) {
1273 // Only change state, if not already the same. This prevents cancelling any
1274 // animations running as part of resume
1275 mStateManager.goToState(NORMAL);
1276 }
Sunny Goyald3864fa2017-11-14 15:06:36 -08001277
1278 // Reset the apps view
1279 if (!alreadyOnHome && mAppsView != null) {
Sunny Goyal7185dd62018-03-14 17:51:49 -07001280 mAppsView.reset(isStarted() /* animate */);
Sunny Goyald3864fa2017-11-14 15:06:36 -08001281 }
1282
1283 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()) {
1284 mWorkspace.post(mWorkspace::moveToDefaultScreen);
1285 }
1286 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001287
1288 final View v = getWindow().peekDecorView();
1289 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001290 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001291 }
1292
Adam Cohen9211d422014-10-07 18:14:53 -07001293 if (mLauncherCallbacks != null) {
1294 mLauncherCallbacks.onHomeIntent();
1295 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001296 }
Winson15f8b172015-08-19 11:02:39 -07001297
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001298 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001299 }
1300
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001301 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001302 public void onRestoreInstanceState(Bundle state) {
1303 super.onRestoreInstanceState(state);
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001304 mWorkspace.restoreInstanceStateForChild(mSynchronouslyBoundPage);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001305 }
1306
1307 @Override
1308 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001309 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001310 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001311
Adam Cohen21cd0022013-10-09 18:57:02 -07001312 }
Sunny Goyalea609262017-10-25 15:47:38 -07001313 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001314
1315
1316 AbstractFloatingView widgets = AbstractFloatingView
1317 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1318 if (widgets != null) {
1319 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1320 widgets.saveHierarchyState(widgetsState);
1321 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1322 } else {
1323 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1324 }
1325
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001326 // We close any open folders and shortcut containers since they will not be re-opened,
1327 // and we need to make sure this state is reflected.
1328 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001329
Sunny Goyal2100c782016-08-22 16:00:03 -07001330 if (mPendingRequestArgs != null) {
1331 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1332 }
1333 if (mPendingActivityResult != null) {
1334 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001335 }
1336
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001337 super.onSaveInstanceState(outState);
1338
Adam Cohen9211d422014-10-07 18:14:53 -07001339 if (mLauncherCallbacks != null) {
1340 mLauncherCallbacks.onSaveInstanceState(outState);
1341 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001342 }
1343
1344 @Override
1345 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001346 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001347
Sunny Goyal5cc7bbd2017-06-12 09:50:39 -07001348 unregisterReceiver(mReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001349 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001350
Winson Chungcd2b0142011-06-08 16:02:26 -07001351 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001352 // It's possible to receive onDestroy after a new Launcher activity has
1353 // been created. In this case, don't interfere with the new Launcher.
1354 if (mModel.isCurrentCallbacks(this)) {
1355 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001356 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001357 }
Sunny Goyal623eddd2018-03-02 12:24:41 -08001358 mRotationHelper.destroy();
Winson Chungedb0d6b2018-03-21 17:32:55 -07001359 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mSharedPrefsListener);
Sunny Goyal745bad92016-05-02 10:54:12 -07001360
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001361 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001362 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001363 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001364 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001365 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001366
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001367 TextKeyListener.getInstance().release();
Mario Bertschler27288382017-05-24 15:35:09 -07001368 WallpaperColorInfo.getInstance(this).setOnThemeChangeListener(null);
1369
Michael Jurka2ecf9952012-06-18 12:52:28 -07001370 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001371
Tony2917a8b2017-07-31 23:29:42 -07001372 clearPendingBinds();
1373
Adam Cohen9211d422014-10-07 18:14:53 -07001374 if (mLauncherCallbacks != null) {
1375 mLauncherCallbacks.onDestroy();
1376 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001377 }
1378
Sunny Goyalae502842016-06-17 08:43:56 -07001379 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1380 return mAccessibilityDelegate;
1381 }
1382
Adam Cohena9cf38f2011-05-02 15:36:58 -07001383 public DragController getDragController() {
1384 return mDragController;
1385 }
1386
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001387 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001388 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001389 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001390 }
1391
1392 @Override
1393 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1394 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001395 try {
1396 super.startIntentSenderForResult(intent, requestCode,
1397 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1398 } catch (IntentSender.SendIntentException e) {
1399 throw new ActivityNotFoundException();
1400 }
1401 }
1402
Winson Chung70d72102011-08-12 11:24:35 -07001403 /**
1404 * Indicates that we want global search for this activity by setting the globalSearch
1405 * argument for {@link #startSearch} to true.
1406 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001407 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001408 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001409 Bundle appSearchData, boolean globalSearch) {
1410 if (appSearchData == null) {
1411 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001412 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001413 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001414
Sunny Goyal6f28e712016-09-13 14:19:29 -07001415 if (mLauncherCallbacks == null ||
1416 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1417 // Starting search from the callbacks failed. Start the default global search.
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001418 super.startSearch(initialQuery, selectInitialQuery, appSearchData, true);
Ian Parkinson047036e2014-09-01 15:40:53 +01001419 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001420
1421 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001422 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001423 }
1424
Joe Onorato9c1289c2009-08-17 11:03:03 -04001425 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001426 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001427 }
1428
Adam Cohen517a7f52014-03-01 12:12:59 -08001429 public boolean isWorkspaceLoading() {
1430 return mWorkspaceLoading;
1431 }
1432
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001433 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001434 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001435 }
1436
Sunny Goyal04a324a2017-01-20 21:08:59 -08001437 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001438 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001439 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001440
Sunny Goyal2100c782016-08-22 16:00:03 -07001441 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001442 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001443 if (LOGD) {
1444 Log.d(TAG, "Adding widget from drop");
1445 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001446 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001447 }
1448
Sunny Goyal2100c782016-08-22 16:00:03 -07001449 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001450 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1451 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1452 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001453
Adam Cohenad4e15c2013-10-17 16:21:35 -07001454 Runnable onComplete = new Runnable() {
1455 @Override
1456 public void run() {
1457 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001458 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001459 }
1460 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001461 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001462 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001463 }
1464 }
Romain Guycbb89e42009-06-08 15:52:54 -07001465
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001466 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1467 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001468 info.container = container;
1469 info.screenId = screenId;
1470 if (cell != null) {
1471 info.cellX = cell[0];
1472 info.cellY = cell[1];
1473 }
1474 info.spanX = spanX;
1475 info.spanY = spanY;
1476
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001477 switch (info.itemType) {
1478 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1479 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001480 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001481 break;
1482 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001483 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001484 break;
1485 default:
1486 throw new IllegalStateException("Unknown item type: " + info.itemType);
1487 }
1488 }
1489
Adam Cohenfbba09b2011-07-18 21:43:05 -07001490 /**
1491 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001492 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001493 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001494 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1495 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001496 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1497 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1498 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001499 }
1500
Adam Cohenfbba09b2011-07-18 21:43:05 -07001501 /**
1502 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001503 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001504 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001505 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001506 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001507 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001508 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001509 // In the case where we've prebound the widget, we remove it from the DragLayer
1510 if (LOGD) {
1511 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1512 }
1513 getDragLayer().removeView(hostView);
1514
Adam Cohened66b2b2012-01-23 17:28:51 -08001515 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001516 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001517
1518 // Clear the boundWidget so that it doesn't get destroyed.
1519 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001520 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001521 // In this case, we either need to start an activity to get permission to bind
1522 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001523 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1524 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1525 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1526 this, info.componentName);
1527 } else {
1528 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1529 }
Adam Cohendd70d662012-10-04 16:53:44 -07001530 Bundle options = info.bindOptions;
1531
Sunny Goyalffe83f12014-08-14 17:39:34 -07001532 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1533 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001534 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001535 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001536 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001537 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001538 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001539 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001540 }
1541
Adam Cohendcd297f2013-06-18 13:13:40 -07001542 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001543 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001544 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001545 folderInfo.title = getText(R.string.folder_name);
1546
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001547 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001548 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549
1550 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001551 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301552 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001553 // Force measure the new folder icon
1554 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1555 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001556 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001557 }
Romain Guycbb89e42009-06-08 15:52:54 -07001558
Winsonc0b52fe2015-09-09 16:38:15 -07001559 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001560 * Unbinds the view for the specified item, and removes the item and all its children.
1561 *
1562 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001563 * @param itemInfo the {@link ItemInfo} for this view.
1564 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001565 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001566 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001567 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001568 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001569 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1570 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001571 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001572 } else {
1573 mWorkspace.removeWorkspaceItem(v);
1574 }
Winsonc0b52fe2015-09-09 16:38:15 -07001575 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001576 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001577 }
1578 } else if (itemInfo instanceof FolderInfo) {
1579 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001580 if (v instanceof FolderIcon) {
1581 ((FolderIcon) v).removeListeners();
1582 }
Winsonc0b52fe2015-09-09 16:38:15 -07001583 mWorkspace.removeWorkspaceItem(v);
1584 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001585 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001586 }
1587 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1588 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001589 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001590 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07001591 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001592 }
1593 } else {
1594 return false;
1595 }
1596 return true;
1597 }
1598
1599 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07001600 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07001601 */
Sunny Goyal56c73602015-09-25 12:55:01 -07001602 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07001603 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07001604 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07001605 // Deleting an app widget ID is a void call but writes to disk before returning
1606 // to the caller...
1607 new AsyncTask<Void, Void, Void>() {
1608 public Void doInBackground(Void ... args) {
1609 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
1610 return null;
1611 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07001612 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07001613 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001614 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001615 }
1616
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001617 @Override
1618 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001619 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001620 }
1621
Joe Onorato88ec0992009-11-19 13:16:06 -08001622 @Override
1623 public void onBackPressed() {
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08001624 if (finishAutoCancelActionMode()) {
1625 return;
1626 }
Adam Cohen9211d422014-10-07 18:14:53 -07001627 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1628 return;
1629 }
1630
Sunny Goyal45478022015-06-08 16:52:41 -07001631 if (mDragController.isDragging()) {
1632 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001633 return;
1634 }
1635
Jon Mirandafeba90f2016-10-06 10:53:29 -07001636 // Note: There should be at most one log per method call. This is enforced implicitly
1637 // by using if-else statements.
1638 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001639 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
1640 if (topView != null) {
Sunny Goyal37920962017-09-28 13:43:24 -07001641 topView.onBackPressed();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001642 } else if (!isInState(NORMAL)) {
Sunny Goyal49bcf342018-01-11 13:59:53 -08001643 LauncherState lastState = mStateManager.getLastState();
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001644 ued.logActionCommand(Action.Command.BACK, mStateManager.getState().containerType,
1645 lastState.containerType);
Sunny Goyal49bcf342018-01-11 13:59:53 -08001646 mStateManager.goToState(lastState);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001647 } else {
1648 // Back button is a no-op here, but give at least some feedback for the button press
1649 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001650 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001651 }
1652
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001653 /**
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001654 * Event handler for the wallpaper picker button that appears after a long press
1655 * on the home screen.
1656 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001657 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07001658 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07001659 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
1660 return;
1661 }
1662
Tony Wickham785f7a52015-08-31 17:28:32 -07001663 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
1664 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal2100c782016-08-22 16:00:03 -07001665 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001666 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001667 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001668
1669 String pickerPackage = getString(R.string.wallpaper_picker_package);
Tony85efb4b2017-06-02 13:06:10 -07001670 boolean hasTargetPackage = !TextUtils.isEmpty(pickerPackage);
1671 if (hasTargetPackage) {
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001672 intent.setPackage(pickerPackage);
1673 }
1674
Sunny Goyala5ace712017-11-15 17:12:51 -08001675 final Bundle launchOptions;
1676 if (v != null) {
1677 intent.setSourceBounds(getViewBounds(v));
1678 // If there is no target package, use the default intent chooser animation
Jon Miranda17940f02018-01-18 14:46:34 -08001679 launchOptions = hasTargetPackage
Tony Wickham005df0b2018-02-13 11:28:12 -08001680 ? getActivityLaunchOptionsAsBundle(v, isInMultiWindowModeCompat())
Jon Miranda17940f02018-01-18 14:46:34 -08001681 : null;
Sunny Goyala5ace712017-11-15 17:12:51 -08001682 } else {
1683 launchOptions = null;
1684 }
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001685 try {
Sunny Goyala5ace712017-11-15 17:12:51 -08001686 startActivityForResult(intent, REQUEST_PICK_WALLPAPER, launchOptions);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001687 } catch (ActivityNotFoundException e) {
1688 setWaitingForResult(null);
1689 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1690 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001691 }
1692
Sunny Goyalfe770c92016-09-27 14:38:58 -07001693 @TargetApi(Build.VERSION_CODES.M)
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001694 @Override
Tony Wickham005df0b2018-02-13 11:28:12 -08001695 public ActivityOptions getActivityLaunchOptions(View v, boolean useDefaultLaunchOptions) {
Jon Miranda17940f02018-01-18 14:46:34 -08001696 return useDefaultLaunchOptions
Jon Miranda54441f52018-01-24 15:38:25 -08001697 ? mAppTransitionManager.getDefaultActivityLaunchOptions(this, v)
1698 : mAppTransitionManager.getActivityLaunchOptions(this, v);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001699 }
1700
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001701 @TargetApi(Build.VERSION_CODES.M)
1702 @Override
1703 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
1704 // Due to legacy reasons, direct call shortcuts require Launchers to have the
1705 // corresponding permission. Show the appropriate permission prompt if that
1706 // is the case.
1707 if (intent.getComponent() == null
1708 && Intent.ACTION_CALL.equals(intent.getAction())
1709 && checkSelfPermission(android.Manifest.permission.CALL_PHONE) !=
1710 PackageManager.PERMISSION_GRANTED) {
1711
1712 setWaitingForResult(PendingRequestArgs
1713 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
1714 requestPermissions(new String[]{android.Manifest.permission.CALL_PHONE},
1715 REQUEST_PERMISSION_CALL_PHONE);
1716 return true;
1717 } else {
1718 return false;
1719 }
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001720 }
1721
Sunny Goyala7ce1662016-05-31 15:01:35 -07001722 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001723 mAppLaunchSuccess = super.startActivitySafely(v, intent, item);
1724 if (mAppLaunchSuccess && v instanceof BubbleTextView) {
1725 // This is set to the view that launched the activity that navigated the user away
1726 // from launcher. Since there is no callback for when the activity has finished
1727 // launching, enable the press state and keep this reference to reset the press
1728 // state when we return to launcher.
1729 BubbleTextView btv = (BubbleTextView) v;
1730 btv.setStayPressed(true);
1731 setOnResumeCallback(btv);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001732 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07001733 return mAppLaunchSuccess;
Michael Jurka86a720e2012-05-09 11:23:23 -07001734 }
1735
Winson Chung3d503fb2011-07-13 17:25:49 -07001736 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001737 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07001738 return mHotseat != null && layout != null &&
1739 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
1740 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001741
Winson Chung3d503fb2011-07-13 17:25:49 -07001742 /**
1743 * Returns the CellLayout of the specified container at the specified screen.
1744 */
Sunny Goyal92820592015-03-02 11:31:35 -08001745 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001746 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1747 if (mHotseat != null) {
1748 return mHotseat.getLayout();
1749 } else {
1750 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08001751 }
Winson Chung3d503fb2011-07-13 17:25:49 -07001752 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08001753 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08001754 }
1755 }
1756
Michael Jurkae326f182011-11-21 14:05:46 -08001757 @Override
1758 public void onTrimMemory(int level) {
1759 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07001760 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
1761 // The widget preview db can result in holding onto over
1762 // 3MB of memory for caching which isn't necessary.
1763 SQLiteDatabase.releaseMemory();
1764
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08001765 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07001766 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08001767 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01001768 if (mLauncherCallbacks != null) {
1769 mLauncherCallbacks.onTrimMemory(level);
1770 }
Winson Chung62a70be2018-02-23 15:10:05 -08001771 UiFactory.onTrimMemory(this, level);
Michael Jurkae326f182011-11-21 14:05:46 -08001772 }
1773
Michael Jurkad7c28052012-04-27 15:43:36 -07001774 @Override
1775 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07001776 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07001777 final List<CharSequence> text = event.getText();
1778 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07001779 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001780 // TODO: When can workspace be null?
1781 text.add(mWorkspace == null
1782 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07001783 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07001784 return result;
1785 }
1786
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001787 public void setOnResumeCallback(OnResumeCallback callback) {
1788 if (mOnResumeCallback != null) {
1789 mOnResumeCallback.onLauncherResume();
1790 }
1791 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07001792 }
1793
Michael Jurka7607c2f2013-04-03 14:33:19 -07001794 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001795 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001796 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07001797 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001798 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07001799 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001800 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07001801 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07001802 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07001803 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001804 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001805
Joe Onorato9c1289c2009-08-17 11:03:03 -04001806 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07001807 * Clear any pending bind callbacks. This is called when is loader is planning to
1808 * perform a full rebind from scratch.
1809 */
1810 @Override
1811 public void clearPendingBinds() {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001812 if (mPendingExecutor != null) {
1813 mPendingExecutor.markCompleted();
1814 mPendingExecutor = null;
1815 }
1816 }
1817
1818 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001819 * Refreshes the shortcuts shown on the workspace.
1820 *
1821 * Implementation of the method from LauncherModel.Callbacks.
1822 */
1823 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001824 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001825 // Floating panels (except the full widget sheet) are associated with individual icons. If
1826 // we are starting a fresh bind, close all such panels as all the icons are about
1827 // to go away.
1828 AbstractFloatingView.closeOpenViews(this, true,
Sunny Goyal7ede6112017-12-05 15:11:21 -08001829 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_REBIND_SAFE);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08001830
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001831 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08001832
Winson Chungd64d1762013-08-20 14:37:16 -07001833 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07001834 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07001835 mWorkspace.removeAllWorkspaceScreens();
Sunny Goyalc11fac32018-02-27 19:20:15 -08001836 mAppWidgetHost.clearViews();
Winson Chungd64d1762013-08-20 14:37:16 -07001837
Winson Chung3d503fb2011-07-13 17:25:49 -07001838 if (mHotseat != null) {
1839 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001840 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001841 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001842 }
1843
Adam Cohendcd297f2013-06-18 13:13:40 -07001844 @Override
1845 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001846 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001847 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
1848 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001849 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
1850 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001851 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001852 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
1853 // If there are no screens, we need to have an empty screen
1854 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07001855 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001856 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07001857
Winsonc7d2e832016-07-28 12:24:55 -07001858 // After we have added all the screens, if the wallpaper was locked to the default state,
1859 // then notify to indicate that it can be released and a proper wallpaper offset can be
1860 // computed before the next layout
1861 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07001862 }
1863
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001864 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001865 int count = orderedScreenIds.size();
1866 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001867 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001868 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001869 // No need to bind the first screen, as its always bound.
1870 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
1871 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001872 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001873 }
1874
Sunny Goyalb23980c2017-08-17 07:45:25 -07001875 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08001876 public void bindAppsAdded(ArrayList<Long> newScreens, ArrayList<ItemInfo> addNotAnimated,
1877 ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07001878 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08001879 if (newScreens != null) {
1880 bindAddScreens(newScreens);
1881 }
Winson Chungd64d1762013-08-20 14:37:16 -07001882
1883 // We add the items without animation on non-visible pages, and with
1884 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08001885 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001886 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001887 }
Adam Cohen76a47a12014-02-05 11:47:43 -08001888 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001889 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07001890 }
Winson Chungc58497e2013-09-03 17:48:37 -07001891
Winson Chung87412982013-10-03 18:34:14 -07001892 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07001893 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001894 }
1895
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001896 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001897 * Bind the items start-end from the list.
1898 *
1899 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001900 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07001901 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07001902 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001903 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07001904 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001905 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07001906 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001907 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001908 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07001909 int end = items.size();
1910 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001911 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07001912
1913 // Short circuit if we are loading dock items for a configuration which has no dock
1914 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
1915 mHotseat == null) {
1916 continue;
1917 }
1918
Sunny Goyal639e9062015-08-19 19:17:06 -07001919 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001920 switch (item.itemType) {
1921 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1922 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08001923 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001924 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07001925 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001926 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001927 }
1928 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07001929 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001930 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001931 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001932 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001933 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07001934 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1935 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07001936 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07001937 if (view == null) {
1938 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001939 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08001940 break;
1941 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001942 default:
1943 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001944 }
Sunny Goyal639e9062015-08-19 19:17:06 -07001945
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001946 /*
1947 * Remove colliding items.
1948 */
1949 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1950 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
1951 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
1952 View v = cl.getChildAt(item.cellX, item.cellY);
1953 Object tag = v.getTag();
1954 String desc = "Collision while binding workspace item: " + item
1955 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08001956 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001957 throw (new RuntimeException(desc));
1958 } else {
1959 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001960 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001961 continue;
1962 }
1963 }
1964 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05301965 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07001966 if (animateIcons) {
1967 // Animate all the applications up now
1968 view.setAlpha(0f);
1969 view.setScaleX(0f);
1970 view.setScaleY(0f);
1971 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08001972 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07001973 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001974 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07001975
Winson Chung997a9232013-07-24 15:33:46 -07001976 if (animateIcons) {
1977 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08001978 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07001979 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08001980 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07001981 final Runnable startBounceAnimRunnable = new Runnable() {
1982 public void run() {
1983 anim.playTogether(bounceAnims);
1984 anim.start();
1985 }
1986 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08001987 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07001988 // We post the animation slightly delayed to prevent slowdowns
1989 // when we are loading right after we return to launcher.
1990 mWorkspace.postDelayed(new Runnable() {
1991 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00001992 if (mWorkspace != null) {
1993 mWorkspace.snapToPage(newScreenIndex);
1994 mWorkspace.postDelayed(startBounceAnimRunnable,
1995 NEW_APPS_ANIMATION_DELAY);
1996 }
Winson Chung94d67682013-09-25 16:29:40 -07001997 }
1998 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07001999 } else {
2000 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07002001 }
2002 }
Winson Chung64359a52013-07-08 17:17:08 -07002003 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002004 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002005 }
2006
Joe Onorato9c1289c2009-08-17 11:03:03 -04002007 /**
2008 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002009 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07002010 public void bindAppWidget(LauncherAppWidgetInfo item) {
2011 View view = inflateAppWidget(item);
2012 if (view != null) {
2013 mWorkspace.addInScreen(view, item);
2014 mWorkspace.requestLayout();
2015 }
2016 }
2017
2018 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002019 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302020 PendingAppWidgetHostView view =
2021 new PendingAppWidgetHostView(this, item, mIconCache, true);
2022 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002023 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002024 }
2025
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002026 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002027
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002028 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08002029
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002030 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2031 // If the provider is not ready, bind as a pending widget.
2032 appWidgetInfo = null;
2033 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2034 // The widget id is not valid. Try to find the widget based on the provider info.
2035 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
2036 } else {
2037 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
2038 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002039
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002040 // If the provider is ready, but the width is not yet restored, try to restore it.
2041 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
2042 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07002043 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002044 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
2045 + " belongs to component " + item.providerName
2046 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002047 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002048 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002049 }
Sunny Goyalff572272014-07-23 13:58:07 -07002050
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002051 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002052 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07002053 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2054 // Id has not been allocated yet. Allocate a new id.
2055 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
2056 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07002057
Sunny Goyald478c832016-04-01 12:04:16 -07002058 // Also try to bind the widget. If the bind fails, the user will be shown
2059 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08002060 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07002061 pendingInfo.spanX = item.spanX;
2062 pendingInfo.spanY = item.spanY;
2063 pendingInfo.minSpanX = item.minSpanX;
2064 pendingInfo.minSpanY = item.minSpanY;
2065 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07002066
2067 boolean isDirectConfig =
2068 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2069 if (isDirectConfig && item.bindOptions != null) {
2070 Bundle newOptions = item.bindOptions.getExtras();
2071 if (options != null) {
2072 newOptions.putAll(options);
2073 }
2074 options = newOptions;
2075 }
Sunny Goyald478c832016-04-01 12:04:16 -07002076 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2077 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002078
Sunny Goyal86df1382016-08-10 15:03:22 -07002079 // We tried to bind once. If we were not able to bind, we would need to
2080 // go through the permission dialog, which means we cannot skip the config
2081 // activity.
2082 item.bindOptions = null;
2083 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2084
Sunny Goyald478c832016-04-01 12:04:16 -07002085 // Bind succeeded
2086 if (success) {
2087 // If the widget has a configure activity, it is still needs to set it up,
2088 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002089 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002090 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2091 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002092 }
Sunny Goyald478c832016-04-01 12:04:16 -07002093
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002094 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002095 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002096 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002097 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002098 // The widget was marked as UI not ready, but there is no configure activity to
2099 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002100 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002101 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002102 }
Sunny Goyalff572272014-07-23 13:58:07 -07002103 }
2104
Sunny Goyald5462aa2016-11-22 16:52:39 +05302105 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002106 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002107 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002108 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002109 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07002110 deleteWidgetInfo(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002111 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002112 }
2113
Sunny Goyal233ee962015-08-03 13:05:01 -07002114 item.minSpanX = appWidgetInfo.minSpanX;
2115 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302116 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002117 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302118 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002119 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302120 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002121
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002122 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002123 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002124 }
2125
Sunny Goyalff572272014-07-23 13:58:07 -07002126 /**
2127 * Restores a pending widget.
2128 *
2129 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002130 */
Sunny Goyald478c832016-04-01 12:04:16 -07002131 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002132 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2133 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2134 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002135 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002136 }
2137
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002138 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002139 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002140 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2141 info.pendingItemInfo = null;
2142 }
Sunny Goyalff572272014-07-23 13:58:07 -07002143
Sunny Goyalba47faa2017-10-04 16:50:57 -07002144 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002145 view.reInflate();
Sunny Goyalba47faa2017-10-04 16:50:57 -07002146 }
2147
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002148 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002149 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002150 }
2151
Adam Cohen1462de32012-07-24 22:34:36 -07002152 public void onPageBoundSynchronously(int page) {
Sunny Goyalf4aceab2017-12-06 16:16:31 -08002153 mSynchronouslyBoundPage = page;
Adam Cohen1462de32012-07-24 22:34:36 -07002154 }
2155
Sunny Goyal527c7d32015-08-28 15:19:36 -07002156 @Override
2157 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2158 if (mPendingExecutor != null) {
2159 mPendingExecutor.markCompleted();
2160 }
2161 mPendingExecutor = executor;
Sunny Goyal60180b02018-02-07 12:56:30 -08002162 if (!isInState(ALL_APPS)) {
2163 mAppsView.getAppsStore().setDeferUpdates(true);
2164 mPendingExecutor.execute(() -> mAppsView.getAppsStore().setDeferUpdates(false));
2165 }
2166
Sunny Goyal527c7d32015-08-28 15:19:36 -07002167 executor.attachTo(this);
2168 }
2169
2170 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2171 if (mPendingExecutor == executor) {
2172 mPendingExecutor = null;
2173 }
2174 }
2175
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002176 @Override
2177 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002178 if (mDragLayer.getAlpha() < 1) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002179 mDragLayer.animate().alpha(1).withEndAction(
2180 executor == null ? null : executor::onLoadAnimationCompleted).start();
2181 } else if (executor != null) {
2182 executor.onLoadAnimationCompleted();
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002183 }
2184 }
2185
Joe Onorato9c1289c2009-08-17 11:03:03 -04002186 /**
2187 * Callback saying that there aren't any more items to bind.
2188 *
2189 * Implementation of the method from LauncherModel.Callbacks.
2190 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002191 public void finishBindingItems() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002192 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002193 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002194
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002195 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002196
Sunny Goyal2100c782016-08-22 16:00:03 -07002197 if (mPendingActivityResult != null) {
2198 handleActivityResult(mPendingActivityResult.requestCode,
2199 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2200 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002201 }
2202
Sunny Goyala474a9b2017-05-04 16:47:11 -07002203 InstallShortcutReceiver.disableAndFlushInstallQueue(
2204 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002205
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002206 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002207 }
2208
Winson Chunga2413752012-04-03 14:22:34 -07002209 private boolean canRunNewAppsAnimation() {
2210 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002211 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002212 }
2213
Winson Chungc9168342013-06-26 14:54:55 -07002214 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002215 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07002216 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
2217 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002218 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002219 return bounceAnim;
2220 }
2221
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002222 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002223 * Add the icons for all apps.
2224 *
2225 * Implementation of the method from LauncherModel.Callbacks.
2226 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002227 public void bindAllApplications(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002228 mAppsView.getAppsStore().setApps(apps);
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002229
Adam Cohen9211d422014-10-07 18:14:53 -07002230 if (mLauncherCallbacks != null) {
2231 mLauncherCallbacks.bindAllApplications(apps);
2232 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002233 }
2234
2235 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002236 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
2237 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2238 */
2239 @Override
2240 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002241 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002242 }
2243
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002244 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002245 * A package was updated.
2246 *
2247 * Implementation of the method from LauncherModel.Callbacks.
2248 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002249 @Override
2250 public void bindAppsAddedOrUpdated(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002251 mAppsView.getAppsStore().addOrUpdateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002252 }
2253
Sunny Goyal4390ace2014-10-13 11:33:11 -07002254 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002255 public void bindPromiseAppProgressUpdated(PromiseAppInfo app) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002256 mAppsView.getAppsStore().updatePromiseAppProgress(app);
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002257 }
2258
2259 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002260 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002261 mWorkspace.widgetsRestored(widgets);
2262 }
2263
Joe Onorato9c1289c2009-08-17 11:03:03 -04002264 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002265 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002266 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002267 *
2268 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002269 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002270 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002271 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002272 if (!updated.isEmpty()) {
2273 mWorkspace.updateShortcuts(updated);
2274 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002275 }
2276
2277 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002278 * Update the state of a package, typically related to install state.
2279 *
2280 * Implementation of the method from LauncherModel.Callbacks.
2281 */
Sunny Goyale755d462014-07-22 13:48:29 -07002282 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002283 public void bindRestoreItemsChange(HashSet<ItemInfo> updates) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07002284 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07002285 }
2286
2287 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002288 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07002289 * in addition to specific applications to remove, the reason being that
2290 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002291 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07002292 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002293 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07002294 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002295 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002296 mWorkspace.removeItemsByMatcher(matcher);
2297 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002298 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002299
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002300 @Override
2301 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002302 mAppsView.getAppsStore().removeApps(appInfos);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002303 }
Joe Onoratobe386092009-11-17 17:32:16 -08002304
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002305 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002306 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
2307 mPopupDataProvider.setAllWidgets(allWidgets);
Tony Wickham26b17462017-03-20 17:12:24 -07002308 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
2309 if (topView != null) {
2310 topView.onWidgetsBound();
2311 }
2312 }
2313
Tony Wickham86222d22017-03-29 15:30:43 -07002314 /**
2315 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
2316 * refreshes the widgets and shortcuts associated with the given package/user
2317 */
2318 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07002319 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002320 }
2321
Winson Chung80baf5a2010-08-09 16:03:15 -07002322 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002323 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08002324 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002325 @Override
2326 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
2327 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002328
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002329 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
2330 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07002331 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002332 writer.println(prefix + " Homescreen " + i);
2333
2334 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
2335 for (int j = 0; j < layout.getChildCount(); j++) {
2336 Object tag = layout.getChildAt(j).getTag();
2337 if (tag != null) {
2338 writer.println(prefix + " " + tag.toString());
2339 }
2340 }
2341 }
2342
2343 writer.println(prefix + " Hotseat");
2344 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07002345 for (int j = 0; j < layout.getChildCount(); j++) {
2346 Object tag = layout.getChildAt(j).getTag();
2347 if (tag != null) {
2348 writer.println(prefix + " " + tag.toString());
2349 }
Adam Cohen4caf2982013-08-20 18:54:31 -07002350 }
Sunny Goyala1365452015-10-01 15:46:24 -07002351 }
2352
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002353 writer.println(prefix + "Misc:");
2354 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
2355 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
2356 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
Sunny Goyal2bba1902018-02-27 12:20:50 -08002357 writer.println(" deviceProfile isTransposed=" + getDeviceProfile().isVerticalBarLayout());
2358 writer.println(" orientation=" + getResources().getConfiguration().orientation);
2359
2360 try {
2361 FileLog.flushAll(writer);
2362 } catch (Exception e) {
2363 // Ignore
2364 }
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002365
2366 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002367
Adam Cohen9211d422014-10-07 18:14:53 -07002368 if (mLauncherCallbacks != null) {
2369 mLauncherCallbacks.dump(prefix, fd, writer, args);
2370 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002371 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002372
Sunny Goyal66b24572016-09-21 15:57:55 -07002373 @Override
2374 @TargetApi(Build.VERSION_CODES.N)
2375 public void onProvideKeyboardShortcuts(
2376 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
2377
2378 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002379 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002380 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
2381 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
2382 }
2383 View currentFocus = getCurrentFocus();
2384 if (new CustomActionsPopup(this, currentFocus).canShow()) {
2385 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
2386 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
2387 }
Tony18c4aa42017-05-10 21:23:57 -05002388 if (currentFocus.getTag() instanceof ItemInfo
2389 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002390 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
2391 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
2392 }
2393 if (!shortcutInfos.isEmpty()) {
2394 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
2395 }
2396
2397 super.onProvideKeyboardShortcuts(data, menu, deviceId);
2398 }
2399
2400 @Override
2401 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
2402 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
2403 switch (keyCode) {
2404 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002405 if (isInState(NORMAL)) {
2406 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07002407 return true;
2408 }
2409 break;
2410 case KeyEvent.KEYCODE_S: {
2411 View focusedView = getCurrentFocus();
2412 if (focusedView instanceof BubbleTextView
2413 && focusedView.getTag() instanceof ItemInfo
2414 && mAccessibilityDelegate.performAction(focusedView,
2415 (ItemInfo) focusedView.getTag(),
2416 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal00ac9202017-11-09 15:24:06 -08002417 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07002418 return true;
2419 }
2420 break;
2421 }
2422 case KeyEvent.KEYCODE_O:
2423 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
2424 return true;
2425 }
2426 break;
2427 }
2428 }
2429 return super.onKeyShortcut(keyCode, event);
2430 }
2431
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002432 @Override
2433 public boolean onKeyUp(int keyCode, KeyEvent event) {
2434 if (keyCode == KeyEvent.KEYCODE_MENU) {
2435 // KEYCODE_MENU is sent by some tests, for example
2436 // LauncherJankTests#testWidgetsContainerFling. Don't just remove its handling.
2437 if (!mDragController.isDragging() && !mWorkspace.isSwitchingState() &&
2438 isInState(NORMAL)) {
2439 // Close any open floating views.
2440 AbstractFloatingView.closeAllOpenViews(this);
2441
2442 // Setting the touch point to (-1, -1) will show the options popup in the center of
2443 // the screen.
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08002444 OptionsPopupView.show(this, -1, -1);
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002445 }
2446 return true;
2447 }
2448 return super.onKeyUp(keyCode, event);
2449 }
2450
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002451 public static Launcher getLauncher(Context context) {
2452 if (context instanceof Launcher) {
2453 return (Launcher) context;
2454 }
2455 return ((Launcher) ((ContextWrapper) context).getBaseContext());
2456 }
2457
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002458 /**
2459 * Callback for listening for onResume
2460 */
2461 public interface OnResumeCallback {
2462
2463 void onLauncherResume();
2464 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002465}