blob: cc57a7bfb1eebb813a3106a25748dde7d3e8f3ff [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 Goyalcd2ac0f2022-04-14 21:21:47 -070019import static android.app.PendingIntent.FLAG_IMMUTABLE;
20import static android.app.PendingIntent.FLAG_UPDATE_CURRENT;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080021import static android.content.pm.ActivityInfo.CONFIG_ORIENTATION;
22import static android.content.pm.ActivityInfo.CONFIG_SCREEN_SIZE;
Sunny Goyal6c0c61a2020-12-08 19:19:56 -080023import static android.content.pm.ActivityInfo.CONFIG_UI_MODE;
Schneider Victor-tulias25ecae12020-09-14 14:07:38 -070024import static android.view.View.IMPORTANT_FOR_ACCESSIBILITY_NO;
Sunny Goyal8f965462020-04-23 19:00:36 -070025import static android.view.accessibility.AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED;
Vinit Nayak48b28022020-04-27 20:48:49 +000026
Pinyao Tinga74b63a2019-07-17 23:26:06 -070027import static com.android.launcher3.AbstractFloatingView.TYPE_ALL;
Brian Isganitisa85ae692021-09-23 19:56:24 -040028import static com.android.launcher3.AbstractFloatingView.TYPE_FOLDER;
Sunny Goyal30ac97d2020-06-24 23:47:46 -070029import static com.android.launcher3.AbstractFloatingView.TYPE_ICON_SURFACE;
Pinyao Tinga74b63a2019-07-17 23:26:06 -070030import static com.android.launcher3.AbstractFloatingView.TYPE_REBIND_SAFE;
Tony Wickham6a71a5b2018-08-21 11:40:23 -070031import static com.android.launcher3.AbstractFloatingView.TYPE_SNACKBAR;
Brian Isganitisa85ae692021-09-23 19:56:24 -040032import static com.android.launcher3.AbstractFloatingView.getTopOpenViewWithType;
Sunny Goyal4c7f2152017-10-17 17:17:16 -070033import static com.android.launcher3.LauncherAnimUtils.SPRING_LOADED_EXIT_DELAY;
Jon Mirandac4a99bf2021-11-04 16:53:45 -070034import static com.android.launcher3.LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -070035import static com.android.launcher3.LauncherState.ALL_APPS;
Sunny Goyal8f965462020-04-23 19:00:36 -070036import static com.android.launcher3.LauncherState.FLAG_CLOSE_POPUPS;
37import static com.android.launcher3.LauncherState.FLAG_MULTI_PAGE;
38import static com.android.launcher3.LauncherState.FLAG_NON_INTERACTIVE;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -070039import static com.android.launcher3.LauncherState.NORMAL;
Sunny Goyalf2393f12020-04-01 20:13:12 -070040import static com.android.launcher3.LauncherState.NO_OFFSET;
41import static com.android.launcher3.LauncherState.NO_SCALE;
Sunny Goyal8f965462020-04-23 19:00:36 -070042import static com.android.launcher3.LauncherState.SPRING_LOADED;
Sunny Goyalb1d7de22019-09-06 10:36:54 -070043import static com.android.launcher3.Utilities.postAsyncCallback;
Sunny Goyal384b5782021-02-09 22:50:02 -080044import static com.android.launcher3.accessibility.LauncherAccessibilityDelegate.getSupportedActions;
Thiru Ramasamy5fbac632022-01-28 15:05:36 -080045import static com.android.launcher3.logging.StatsLogManager.EventEnum;
Hyunyoung Song801f81f2020-06-19 02:58:53 -070046import static com.android.launcher3.logging.StatsLogManager.LAUNCHER_STATE_BACKGROUND;
Hyunyoung Song95786e02020-09-15 00:34:10 -070047import static com.android.launcher3.logging.StatsLogManager.LAUNCHER_STATE_HOME;
thiruramc96873c2021-02-11 15:13:47 -080048import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_ALLAPPS_ENTRY;
thiruram9f53d612021-02-24 10:54:01 -080049import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_ALLAPPS_ENTRY_WITH_DEVICE_SEARCH;
thiruramc96873c2021-02-11 15:13:47 -080050import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_ALLAPPS_EXIT;
Hyunyoung Song801f81f2020-06-19 02:58:53 -070051import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_ONRESUME;
52import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_ONSTOP;
Yogisha Dixit4ff5ee32021-05-04 14:58:34 +010053import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_WIDGET_RECONFIGURED;
Sunny Goyal60e68c92020-08-12 13:59:27 -070054import static com.android.launcher3.model.ItemInstallQueue.FLAG_ACTIVITY_PAUSED;
55import static com.android.launcher3.model.ItemInstallQueue.FLAG_DRAG_AND_DROP;
Sunny Goyal3a1eadd2019-10-23 17:44:27 -070056import static com.android.launcher3.popup.SystemShortcut.APP_INFO;
Sunny Goyal3a1eadd2019-10-23 17:44:27 -070057import static com.android.launcher3.popup.SystemShortcut.INSTALL;
58import static com.android.launcher3.popup.SystemShortcut.WIDGETS;
Sunny Goyal8f965462020-04-23 19:00:36 -070059import static com.android.launcher3.states.RotationHelper.REQUEST_LOCK;
Sunny Goyalbe8c3402019-05-22 15:17:52 -070060import static com.android.launcher3.states.RotationHelper.REQUEST_NONE;
Jon Miranda7ebe2092022-03-10 08:46:01 -080061import static com.android.launcher3.testing.TestProtocol.BAD_STATE;
Jon Mirandac4a99bf2021-11-04 16:53:45 -070062import static com.android.launcher3.util.ItemInfoMatcher.forFolderMatch;
Sunny Goyal326403e2017-10-02 12:45:10 -070063
Gilles Debunnedd6c9922010-10-25 11:23:41 -070064import android.animation.Animator;
Sunny Goyal6001ea22018-05-10 16:31:00 -070065import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070066import android.animation.AnimatorSet;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070067import android.animation.ValueAnimator;
Adam Cohen0b395352014-06-09 22:54:36 +000068import android.annotation.TargetApi;
Tony Wickhame78a70b2021-03-29 16:45:59 -070069import android.app.Notification;
70import android.app.NotificationChannel;
71import android.app.NotificationManager;
72import android.app.PendingIntent;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080073import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070074import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080076import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080077import android.content.ComponentCallbacks2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080078import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070080import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070081import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070082import android.content.SharedPreferences;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083import android.content.pm.PackageManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080084import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070085import android.database.sqlite.SQLiteDatabase;
Schneider Victor-tulias25ecae12020-09-14 14:07:38 -070086import android.graphics.Bitmap;
Tony Wickham1906cc32021-02-11 11:55:24 -080087import android.graphics.Rect;
Sunny Goyal831212e2021-05-14 14:24:09 -070088import android.graphics.RectF;
Adam Cohen0f668f32014-09-08 19:54:17 +020089import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090import android.os.Bundle;
Sunny Goyal210e1742019-10-17 12:05:38 -070091import android.os.CancellationSignal;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070092import android.os.Parcelable;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080093import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070094import android.os.StrictMode;
Riddle Hsu979da642020-09-22 21:52:40 +080095import android.os.SystemClock;
Schneider Victor-tulias927d6dc2021-08-26 11:56:47 -070096import android.os.Trace;
Jon Mirandac4a99bf2021-11-04 16:53:45 -070097import android.os.UserHandle;
Michael Jurkaa33411c2012-06-14 16:18:21 -070098import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080099import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -0700100import android.util.Log;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700101import android.util.SparseArray;
Adam Cohen96d30a12013-07-16 18:13:21 -0700102import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -0700103import android.view.KeyboardShortcutGroup;
104import android.view.KeyboardShortcutInfo;
Sunny Goyal9b29ca52017-02-17 10:39:44 -0800105import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -0700106import android.view.Menu;
vadimt9af34a72020-01-21 11:19:13 -0800107import android.view.MotionEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -0700108import android.view.View;
Adam Cohen96d30a12013-07-16 18:13:21 -0700109import android.view.ViewGroup;
Sunny Goyal7b8d63f2022-04-14 16:37:32 -0700110import android.view.ViewTreeObserver.OnPreDrawListener;
Schneider Victor-tulias25ecae12020-09-14 14:07:38 -0700111import android.view.WindowManager.LayoutParams;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -0800112import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700113import android.view.animation.OvershootInterpolator;
Schneider Victor-tulias25ecae12020-09-14 14:07:38 -0700114import android.widget.ImageView;
Adam Cohen96d30a12013-07-16 18:13:21 -0700115import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -0700116
Sunny Goyal35e7d382020-05-08 13:37:58 -0700117import androidx.annotation.CallSuper;
Andras Kloczl55edfe52021-05-14 12:21:30 +0200118import androidx.annotation.NonNull;
Sunny Goyal11a50742019-08-07 09:24:26 -0700119import androidx.annotation.Nullable;
Pinyao Tingc9074272019-11-11 11:51:44 -0800120import androidx.annotation.StringRes;
Sunny Goyalb1d7de22019-09-06 10:36:54 -0700121import androidx.annotation.VisibleForTesting;
Sunny Goyal11a50742019-08-07 09:24:26 -0700122
Sunny Goyal651077b2014-06-30 14:15:31 -0700123import com.android.launcher3.DropTarget.DragObject;
Schneider Victor-tuliasbdb0dd82021-11-03 16:26:41 -0700124import com.android.launcher3.accessibility.BaseAccessibilityDelegate.LauncherAction;
Sunny Goyalae502842016-06-17 08:43:56 -0700125import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Brian Isganitis1664c9f2022-01-14 23:15:47 -0500126import com.android.launcher3.allapps.ActivityAllAppsContainerView;
Jon Mirandaf58db152022-06-02 16:06:37 -0700127import com.android.launcher3.allapps.AllAppsRecyclerView;
vadimtd4c90e12019-06-04 13:59:43 -0700128import com.android.launcher3.allapps.AllAppsStore;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700129import com.android.launcher3.allapps.AllAppsTransitionController;
kholoud mohamedceec31b2022-02-03 15:44:25 +0000130import com.android.launcher3.allapps.BaseAllAppsContainerView;
Sunny Goyal7ede6112017-12-05 15:11:21 -0800131import com.android.launcher3.allapps.DiscoveryBounce;
Sunny Goyalf0b6db72018-08-13 16:10:14 -0700132import com.android.launcher3.anim.PropertyListBuilder;
Sunny Goyal8f965462020-04-23 19:00:36 -0700133import com.android.launcher3.compat.AccessibilityManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -0800134import com.android.launcher3.config.FeatureFlags;
vadimt00d42552018-12-11 17:59:36 -0800135import com.android.launcher3.dot.DotInfo;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700136import com.android.launcher3.dragndrop.DragController;
137import com.android.launcher3.dragndrop.DragLayer;
Tony Wickham39938cb2021-02-26 09:15:31 -0800138import com.android.launcher3.dragndrop.DragOptions;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700139import com.android.launcher3.dragndrop.DragView;
Sunny Goyal59969372021-05-06 12:11:44 -0700140import com.android.launcher3.dragndrop.LauncherDragController;
Sunny Goyal11a50742019-08-07 09:24:26 -0700141import com.android.launcher3.folder.FolderGridOrganizer;
Sunny Goyal26119432016-02-18 22:09:23 +0000142import com.android.launcher3.folder.FolderIcon;
Schneider Victor-tulias25ecae12020-09-14 14:07:38 -0700143import com.android.launcher3.icons.BitmapRenderer;
Sunny Goyalf840f102018-09-21 14:41:05 -0700144import com.android.launcher3.icons.IconCache;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700145import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Hyunyoung Song801f81f2020-06-19 02:58:53 -0700146import com.android.launcher3.logger.LauncherAtom;
Thiru Ramasamy5fbac632022-01-28 15:05:36 -0800147import com.android.launcher3.logger.LauncherAtom.ContainerInfo;
148import com.android.launcher3.logger.LauncherAtom.WorkspaceContainer;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700149import com.android.launcher3.logging.FileLog;
thiruramc96873c2021-02-11 15:13:47 -0800150import com.android.launcher3.logging.InstanceId;
151import com.android.launcher3.logging.InstanceIdSequence;
Hyunyoung Song801f81f2020-06-19 02:58:53 -0700152import com.android.launcher3.logging.StatsLogManager;
Sunny Goyal87dcde62019-07-17 20:35:56 -0700153import com.android.launcher3.model.BgDataModel.Callbacks;
Sunny Goyal60e68c92020-08-12 13:59:27 -0700154import com.android.launcher3.model.ItemInstallQueue;
Sunny Goyal53116c62020-08-07 16:32:18 -0700155import com.android.launcher3.model.ModelUtils;
Sunny Goyala535ae42017-02-27 10:07:13 -0800156import com.android.launcher3.model.ModelWriter;
kholoud mohamedc76b2032021-12-20 16:47:38 +0000157import com.android.launcher3.model.StringCache;
Pinyao Tingbbb9a9c2021-05-11 15:42:54 -0700158import com.android.launcher3.model.WidgetsModel;
Sunny Goyale396abf2020-04-06 15:11:17 -0700159import com.android.launcher3.model.data.AppInfo;
160import com.android.launcher3.model.data.FolderInfo;
161import com.android.launcher3.model.data.ItemInfo;
162import com.android.launcher3.model.data.LauncherAppWidgetInfo;
Sunny Goyale396abf2020-04-06 15:11:17 -0700163import com.android.launcher3.model.data.WorkspaceItemInfo;
Sunny Goyala535ae42017-02-27 10:07:13 -0800164import com.android.launcher3.notification.NotificationListener;
Sunny Goyale7b00122019-10-02 16:13:34 -0700165import com.android.launcher3.pm.PinRequestHelper;
Samuel Fufa41d909f2020-03-20 16:27:09 -0700166import com.android.launcher3.pm.UserCache;
Brian Isganitis149dbcb2021-11-08 18:12:38 -0500167import com.android.launcher3.popup.ArrowPopup;
Tony Wickham540913e2017-01-23 11:47:51 -0800168import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -0800169import com.android.launcher3.popup.PopupDataProvider;
Sunny Goyal3a1eadd2019-10-23 17:44:27 -0700170import com.android.launcher3.popup.SystemShortcut;
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700171import com.android.launcher3.qsb.QsbContainerView;
Sunny Goyal35e7d382020-05-08 13:37:58 -0700172import com.android.launcher3.statemanager.StateManager;
173import com.android.launcher3.statemanager.StateManager.StateHandler;
Sunny Goyal35e7d382020-05-08 13:37:58 -0700174import com.android.launcher3.statemanager.StatefulActivity;
Sunny Goyal623eddd2018-03-02 12:24:41 -0800175import com.android.launcher3.states.RotationHelper;
vadimt9af34a72020-01-21 11:19:13 -0800176import com.android.launcher3.testing.TestLogging;
vadimtbc5d7e62019-12-09 13:53:05 -0800177import com.android.launcher3.testing.TestProtocol;
Sunny Goyal210e1742019-10-17 12:05:38 -0700178import com.android.launcher3.touch.AllAppsSwipeController;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800179import com.android.launcher3.touch.ItemClickHandler;
Sunny Goyalb1d7de22019-09-06 10:36:54 -0700180import com.android.launcher3.uioverrides.plugins.PluginManagerWrapper;
Sunny Goyal2100c782016-08-22 16:00:03 -0700181import com.android.launcher3.util.ActivityResultInfo;
Sunny Goyale84c5b82019-09-26 17:05:31 -0700182import com.android.launcher3.util.ActivityTracker;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700183import com.android.launcher3.util.ComponentKey;
Sunny Goyalefb7e842018-10-04 15:11:00 -0700184import com.android.launcher3.util.IntArray;
Andras Kloczl55edfe52021-05-14 12:21:30 +0200185import com.android.launcher3.util.IntSet;
Tony Wickham133cd542020-04-08 17:48:41 -0700186import com.android.launcher3.util.OnboardingPrefs;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700187import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800188import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700189import com.android.launcher3.util.PendingRequestArgs;
Sunny Goyal4a48a982021-06-17 15:15:46 -0700190import com.android.launcher3.util.RunnableList;
Samuel Fufa41d909f2020-03-20 16:27:09 -0700191import com.android.launcher3.util.SafeCloseable;
Sunny Goyal8392c822017-06-20 10:03:56 -0700192import com.android.launcher3.util.SystemUiController;
Mario Bertschlera6936942017-05-31 14:48:19 -0700193import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700194import com.android.launcher3.util.Thunk;
Sunny Goyal210e1742019-10-17 12:05:38 -0700195import com.android.launcher3.util.TouchController;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700196import com.android.launcher3.util.TraceHelper;
Sunny Goyal326403e2017-10-02 12:45:10 -0700197import com.android.launcher3.util.UiThreadHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700198import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyalab770a12018-11-14 15:17:26 -0800199import com.android.launcher3.views.ActivityContext;
Jon Mirandaf58db152022-06-02 16:06:37 -0700200import com.android.launcher3.views.FloatingIconView;
Sunny Goyal30ac97d2020-06-24 23:47:46 -0700201import com.android.launcher3.views.FloatingSurfaceView;
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800202import com.android.launcher3.views.OptionsPopupView;
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400203import com.android.launcher3.views.ScrimView;
Yogisha Dixit741fae92021-02-22 14:03:44 +0000204import com.android.launcher3.widget.LauncherAppWidgetHost;
Sunny Goyal29947f02017-12-18 13:49:44 -0800205import com.android.launcher3.widget.LauncherAppWidgetHostView;
Yogisha Dixit741fae92021-02-22 14:03:44 +0000206import com.android.launcher3.widget.LauncherAppWidgetProviderInfo;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800207import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700208import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal29947f02017-12-18 13:49:44 -0800209import com.android.launcher3.widget.PendingAppWidgetHostView;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800210import com.android.launcher3.widget.WidgetAddFlowHandler;
Sunny Goyal337c81f2019-12-10 12:19:13 -0800211import com.android.launcher3.widget.WidgetManagerHelper;
Pinyao Tingc7a6c292019-08-26 14:36:02 -0700212import com.android.launcher3.widget.custom.CustomWidgetManager;
Steven Ng2f5648a2021-02-08 17:18:25 +0000213import com.android.launcher3.widget.model.WidgetsListBaseEntry;
214import com.android.launcher3.widget.picker.WidgetsFullSheet;
Sunny Goyal80a071b2021-12-27 13:30:08 +0530215import com.android.systemui.plugins.LauncherOverlayPlugin;
Sunny Goyalb1d7de22019-09-06 10:36:54 -0700216import com.android.systemui.plugins.PluginListener;
217import com.android.systemui.plugins.shared.LauncherExterns;
218import com.android.systemui.plugins.shared.LauncherOverlayManager;
219import com.android.systemui.plugins.shared.LauncherOverlayManager.LauncherOverlay;
220import com.android.systemui.plugins.shared.LauncherOverlayManager.LauncherOverlayCallbacks;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700221
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700222import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700223import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700224import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700225import java.util.Collection;
Jon Mirandac4a99bf2021-11-04 16:53:45 -0700226import java.util.Collections;
Zak Cohen658c67a2018-10-19 14:21:05 -0700227import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700228import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700229import java.util.List;
Thiru Ramasamy694407c2022-03-08 12:11:24 -0800230import java.util.Optional;
Sunny Goyalaae6fbb2019-01-31 16:05:58 -0800231import java.util.function.Predicate;
Sunny Goyalb1d7de22019-09-06 10:36:54 -0700232import java.util.function.Supplier;
Sunny Goyal3a1eadd2019-10-23 17:44:27 -0700233import java.util.stream.Stream;
Jon Mirandaf5197fc2019-09-04 16:55:41 -0700234
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800235/**
236 * Default launcher application.
237 */
Shikha Malhotra0f2e1b62022-03-27 21:02:21 +0000238public class Launcher extends StatefulActivity<LauncherState>
239 implements LauncherExterns, Callbacks, InvariantDeviceProfile.OnIDPChangeListener,
Sunny Goyal80a071b2021-12-27 13:30:08 +0530240 PluginListener<LauncherOverlayPlugin>, LauncherOverlayCallbacks {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700241 public static final String TAG = "Launcher";
Sunny Goyale84c5b82019-09-26 17:05:31 -0700242
243 public static final ActivityTracker<Launcher> ACTIVITY_TRACKER = new ActivityTracker<>();
244
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700245 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800246
Winson Chunga2413752012-04-03 14:22:34 -0700247 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700248
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800249 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700250 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700251
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700252 private static final int REQUEST_PICK_APPWIDGET = 9;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800253
Michael Jurka8b805b12012-04-18 14:23:14 -0700254 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800255 public static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
256 public static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700257
Jon Mirandac476d6e2017-05-04 16:30:01 -0700258 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700259
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700260 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
261
Mathew Inwood876a8462013-06-14 14:12:41 +0100262 /**
263 * IntentStarter uses request codes starting with this. This must be greater than all activity
264 * request codes used internally.
265 */
266 protected static final int REQUEST_LAST = 100;
267
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800268 // Type: int
Michael Jurka883f55b2010-10-21 15:47:14 -0700269 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700270 // Type: PendingRequestArgs
271 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
Sunny Goyaldedda052019-05-14 15:23:48 -0700272 // Type: int
273 private static final String RUNTIME_STATE_PENDING_REQUEST_CODE = "launcher.request_code";
Sunny Goyal2100c782016-08-22 16:00:03 -0700274 // Type: ActivityResultInfo
275 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700276 // Type: SparseArray<Parcelable>
277 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
Sunny Goyal12e3f1f2021-07-15 14:31:58 -0700278 // Type int[]
279 private static final String RUNTIME_STATE_CURRENT_SCREEN_IDS = "launcher.current_screen_ids";
Sunny Goyal17c72fb2019-10-14 14:06:38 -0700280
vadimt00d42552018-12-11 17:59:36 -0800281 public static final String ON_CREATE_EVT = "Launcher.onCreate";
Sunny Goyal17c72fb2019-10-14 14:06:38 -0700282 public static final String ON_START_EVT = "Launcher.onStart";
283 public static final String ON_RESUME_EVT = "Launcher.onResume";
284 public static final String ON_NEW_INTENT_EVT = "Launcher.onNewIntent";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800285
Sunny Goyal35e7d382020-05-08 13:37:58 -0700286 private StateManager<LauncherState> mStateManager;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700287
Adam Cohenad4e15c2013-10-17 16:21:35 -0700288 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800289
Winson Chunga2413752012-04-03 14:22:34 -0700290 // How long to wait before the new-shortcut animation automatically pans the workspace
Jon Mirandaf5197fc2019-09-04 16:55:41 -0700291 @VisibleForTesting public static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700292 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Jon Mirandaf5197fc2019-09-04 16:55:41 -0700293 @Thunk @VisibleForTesting public static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700294
Schneider Victor-tulias25ecae12020-09-14 14:07:38 -0700295 private static final int THEME_CROSS_FADE_ANIMATION_DURATION = 375;
296
Schneider Victor-tulias927d6dc2021-08-26 11:56:47 -0700297 private static final String DISPLAY_WORKSPACE_TRACE_METHOD_NAME = "DisplayWorkspaceFirstFrame";
298 private static final String DISPLAY_ALL_APPS_TRACE_METHOD_NAME = "DisplayAllApps";
299 public static final int DISPLAY_WORKSPACE_TRACE_COOKIE = 0;
300 public static final int DISPLAY_ALL_APPS_TRACE_COOKIE = 1;
301
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800302 private Configuration mOldConfig;
Jon Miranda54441f52018-01-24 15:38:25 -0800303
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700304 @Thunk
Shikha Malhotra0f2e1b62022-03-27 21:02:21 +0000305 Workspace<?> mWorkspace;
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700306 @Thunk
307 DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800308 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700309
Sunny Goyal337c81f2019-12-10 12:19:13 -0800310 private WidgetManagerHelper mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700311 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700312
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700313 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700314
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700315 @Thunk
316 Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700317
Sunny Goyal47328fd2016-05-25 18:56:41 -0700318 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700319
320 // Main container view for the all apps screen.
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700321 @Thunk
Brian Isganitis1664c9f2022-01-14 23:15:47 -0500322 ActivityAllAppsContainerView<Launcher> mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700323 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700324
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400325 // Scrim view for the all apps and overview state.
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700326 @Thunk
327 ScrimView mScrimView;
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400328
Winson Chung8ae41982017-11-10 11:42:13 -0800329 // UI and state for the overview panel
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800330 private View mOverviewPanel;
Winson Chung8ae41982017-11-10 11:42:13 -0800331
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700332 @Thunk
333 boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400334
Winson Chungc9bf6d42019-10-04 15:33:18 -0700335 // Used to notify when an activity launch has been deferred because launcher is not yet resumed
336 // TODO: See if we can remove this later
337 private Runnable mOnDeferredActivityLaunchCallback;
338
Sunny Goyal527c7d32015-08-28 15:19:36 -0700339 private ViewOnDrawExecutor mPendingExecutor;
Sunny Goyal7b8d63f2022-04-14 16:37:32 -0700340 private OnPreDrawListener mOnInitialBindListener;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700341
Joe Onorato9c1289c2009-08-17 11:03:03 -0400342 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800343 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800344 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700345 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400346
Tony Wickham010d2552017-01-20 08:15:28 -0800347 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700348
Andras Kloczl55edfe52021-05-14 12:21:30 +0200349 private IntSet mSynchronouslyBoundPages = new IntSet();
Sunny Goyal12e3f1f2021-07-15 14:31:58 -0700350 @NonNull private IntSet mPagesToBindSynchronously = new IntSet();
Adam Cohen1462de32012-07-24 22:34:36 -0700351
Winson Chung46353de2012-02-16 14:05:10 -0800352 // We only want to get the SharedPreferences once since it does an FS stat each time we get
353 // it from the context.
354 private SharedPreferences mSharedPrefs;
Brian Isganitis099945b2022-01-31 18:15:00 -0500355 private OnboardingPrefs<? extends Launcher> mOnboardingPrefs;
Winson Chung46353de2012-02-16 14:05:10 -0800356
Sunny Goyal2100c782016-08-22 16:00:03 -0700357 // Activity result which needs to be processed after workspace has loaded.
358 private ActivityResultInfo mPendingActivityResult;
359 /**
360 * Holds extra information required to handle a result from an external call, like
361 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
362 */
363 private PendingRequestArgs mPendingRequestArgs;
Sunny Goyaldedda052019-05-14 15:23:48 -0700364 // Request id for any pending activity result
Sunny Goyal210e1742019-10-17 12:05:38 -0700365 protected int mPendingActivityRequestCode = -1;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800366
Tracy Zhou4d7b2442019-12-09 13:42:57 -0800367 private ViewGroupFocusHelper mFocusHandler;
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700368
Sunny Goyal623eddd2018-03-02 12:24:41 -0800369 private RotationHelper mRotationHelper;
Sunny Goyal7779d622015-06-11 16:18:39 -0700370
Sunny Goyalb1d7de22019-09-06 10:36:54 -0700371 protected LauncherOverlayManager mOverlayManager;
372 // If true, overlay callbacks are deferred
373 private boolean mDeferOverlayCallbacks;
374 private final Runnable mDeferredOverlayCallbacks = this::checkIfOverlayStillDeferred;
375
Riddle Hsu979da642020-09-22 21:52:40 +0800376 protected long mLastTouchUpTime = -1;
Sunny Goyala306e592020-03-24 01:51:41 -0700377 private boolean mTouchInProgress;
Sunny Goyal9b180102020-03-11 10:02:29 -0700378
Samuel Fufa41d909f2020-03-20 16:27:09 -0700379 private SafeCloseable mUserChangedCallbackCloseable;
380
thiruramc96873c2021-02-11 15:13:47 -0800381 // New InstanceId is assigned to mAllAppsSessionLogId for each AllApps sessions.
382 // When Launcher is not in AllApps state mAllAppsSessionLogId will be null.
383 // User actions within AllApps state are logged with this InstanceId, to recreate AllApps
384 // session on the server side.
385 protected InstanceId mAllAppsSessionLogId;
386 private LauncherState mPrevLauncherState;
387
kholoud mohamedc76b2032021-12-20 16:47:38 +0000388 private StringCache mStringCache;
389
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800390 @Override
Schneider Victor-tulias927d6dc2021-08-26 11:56:47 -0700391 @TargetApi(Build.VERSION_CODES.S)
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800392 protected void onCreate(Bundle savedInstanceState) {
Schneider Victor-tulias927d6dc2021-08-26 11:56:47 -0700393 // Only use a hard-coded cookie since we only want to trace this once.
394 if (Utilities.ATLEAST_S) {
395 Trace.beginAsyncSection(
396 DISPLAY_WORKSPACE_TRACE_METHOD_NAME, DISPLAY_WORKSPACE_TRACE_COOKIE);
397 Trace.beginAsyncSection(DISPLAY_ALL_APPS_TRACE_METHOD_NAME,
398 DISPLAY_ALL_APPS_TRACE_COOKIE);
399 }
Winson Chung5cc62c72019-10-16 11:32:41 -0700400 Object traceToken = TraceHelper.INSTANCE.beginSection(ON_CREATE_EVT,
401 TraceHelper.FLAG_UI_EVENT);
Winson Chunga2413752012-04-03 14:22:34 -0700402 if (DEBUG_STRICT_MODE) {
403 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
404 .detectDiskReads()
405 .detectDiskWrites()
406 .detectNetwork() // or .detectAll() for all detectable problems
407 .penaltyLog()
408 .build());
409 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
410 .detectLeakedSqlLiteObjects()
411 .detectLeakedClosableObjects()
412 .penaltyLog()
413 .penaltyDeath()
414 .build());
415 }
416
Tony Wickhame78a70b2021-03-29 16:45:59 -0700417 if (Utilities.IS_DEBUG_DEVICE && FeatureFlags.NOTIFY_CRASHES.get()) {
418 final String notificationChannelId = "com.android.launcher3.Debug";
419 final String notificationChannelName = "Debug";
420 final String notificationTag = "Debug";
421 final int notificationId = 0;
422
423 NotificationManager notificationManager = getSystemService(NotificationManager.class);
424 notificationManager.createNotificationChannel(new NotificationChannel(
425 notificationChannelId, notificationChannelName,
426 NotificationManager.IMPORTANCE_HIGH));
427
428 Thread.currentThread().setUncaughtExceptionHandler((thread, throwable) -> {
429 String stackTrace = Log.getStackTraceString(throwable);
430
431 Intent shareIntent = new Intent(Intent.ACTION_SEND);
432 shareIntent.setType("text/plain");
433 shareIntent.putExtra(Intent.EXTRA_TEXT, stackTrace);
434 shareIntent = Intent.createChooser(shareIntent, null);
435 PendingIntent sharePendingIntent = PendingIntent.getActivity(
Sunny Goyalcd2ac0f2022-04-14 21:21:47 -0700436 this, 0, shareIntent, FLAG_UPDATE_CURRENT | FLAG_IMMUTABLE);
Tony Wickhame78a70b2021-03-29 16:45:59 -0700437
438 Notification notification = new Notification.Builder(this, notificationChannelId)
439 .setSmallIcon(android.R.drawable.ic_menu_close_clear_cancel)
440 .setContentTitle("Launcher crash detected!")
441 .setStyle(new Notification.BigTextStyle().bigText(stackTrace))
442 .addAction(android.R.drawable.ic_menu_share, "Share", sharePendingIntent)
443 .build();
444 notificationManager.notify(notificationTag, notificationId, notification);
445
446 Thread.UncaughtExceptionHandler defaultUncaughtExceptionHandler =
447 Thread.getDefaultUncaughtExceptionHandler();
448 if (defaultUncaughtExceptionHandler != null) {
449 defaultUncaughtExceptionHandler.uncaughtException(thread, throwable);
450 }
451 });
452 }
453
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800454 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400455
Sunny Goyal87f784c2017-01-11 10:48:34 -0800456 LauncherAppState app = LauncherAppState.getInstance(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800457 mOldConfig = new Configuration(getResources().getConfiguration());
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530458 mModel = app.getModel();
Sunny Goyal9d89f752020-06-19 11:09:24 -0700459
Sunny Goyal41d51a02019-05-14 09:44:34 -0700460 mRotationHelper = new RotationHelper(this);
Hyunyoung Song31971a32019-01-27 12:31:12 -0800461 InvariantDeviceProfile idp = app.getInvariantDeviceProfile();
462 initDeviceProfile(idp);
463 idp.addOnChangeListener(this);
Sunny Goyalf7258242015-10-19 16:59:07 -0700464 mSharedPrefs = Utilities.getPrefs(this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800465 mIconCache = app.getIconCache();
Sunny Goyal384b5782021-02-09 22:50:02 -0800466 mAccessibilityDelegate = createAccessibilityDelegate();
Adam Cohen2e6da152015-05-06 11:42:25 -0700467
Sunny Goyal59969372021-05-06 12:11:44 -0700468 mDragController = new LauncherDragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700469 mAllAppsController = new AllAppsTransitionController(this);
Sunny Goyal35e7d382020-05-08 13:37:58 -0700470 mStateManager = new StateManager<>(this, NORMAL);
Romain Guycbb89e42009-06-08 15:52:54 -0700471
Sunny Goyal35e7d382020-05-08 13:37:58 -0700472 mOnboardingPrefs = createOnboardingPrefs(mSharedPrefs);
Tony Wickham133cd542020-04-08 17:48:41 -0700473
Sunny Goyal337c81f2019-12-10 12:19:13 -0800474 mAppWidgetManager = new WidgetManagerHelper(this);
Cyrus Boadway0d309db2021-04-14 12:29:14 +0000475 mAppWidgetHost = createAppWidgetHost();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700476 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700477
Sunny Goyal9d89f752020-06-19 11:09:24 -0700478 inflateRootView(R.layout.launcher);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800479 setupViews();
Schneider Victor-tulias25ecae12020-09-14 14:07:38 -0700480 crossFadeWithPreviousAppearance();
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800481 mPopupDataProvider = new PopupDataProvider(this::updateNotificationDots);
Tony Wickham010d2552017-01-20 08:15:28 -0800482
Sunny Goyale84c5b82019-09-26 17:05:31 -0700483 boolean internalStateHandled = ACTIVITY_TRACKER.handleCreate(this);
Sunny Goyald3864fa2017-11-14 15:06:36 -0800484 if (internalStateHandled) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800485 if (savedInstanceState != null) {
486 // InternalStateHandler has already set the appropriate state.
487 // We dont need to do anything.
488 savedInstanceState.remove(RUNTIME_STATE);
489 }
490 }
491 restoreState(savedInstanceState);
Sunny Goyalbbece862019-03-01 13:13:52 -0800492 mStateManager.reapplyState();
Winson Chungb63b44c2017-11-10 17:54:44 -0800493
Sunny Goyalfe770c92016-09-27 14:38:58 -0700494 if (savedInstanceState != null) {
Sunny Goyal12e3f1f2021-07-15 14:31:58 -0700495 int[] pageIds = savedInstanceState.getIntArray(RUNTIME_STATE_CURRENT_SCREEN_IDS);
496 if (pageIds != null) {
497 mPagesToBindSynchronously = IntSet.wrap(pageIds);
498 }
Sunny Goyalfe770c92016-09-27 14:38:58 -0700499 }
Sunny Goyald3864fa2017-11-14 15:06:36 -0800500
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530501 if (!mModel.addCallbacksAndLoad(this)) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800502 if (!internalStateHandled) {
Sunny Goyal7b8d63f2022-04-14 16:37:32 -0700503 Log.d(BAD_STATE, "Launcher onCreate not binding sync, prevent drawing");
504 // If we are not binding synchronously, pause drawing until initial bind complete,
505 // so that the system could continue to show the device loading prompt
506 mOnInitialBindListener = Boolean.FALSE::booleanValue;
Sunny Goyald3864fa2017-11-14 15:06:36 -0800507 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800508 }
509
510 // For handling default keys
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800511 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800512
Sunny Goyal9d89f752020-06-19 11:09:24 -0700513 setContentView(getRootView());
Sunny Goyal7b8d63f2022-04-14 16:37:32 -0700514 if (mOnInitialBindListener != null) {
515 getRootView().getViewTreeObserver().addOnPreDrawListener(mOnInitialBindListener);
516 }
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800517 getRootView().dispatchInsets();
Mario Bertschlera6936942017-05-31 14:48:19 -0700518
Jon Miranda7fda2852017-07-19 16:07:01 -0700519 // Listen for broadcasts
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700520 registerReceiver(mScreenOffReceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));
Sunny Goyala616d2b2017-06-12 13:54:01 -0700521
Sunny Goyal8392c822017-06-20 10:03:56 -0700522 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
523 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700524
525 if (mLauncherCallbacks != null) {
526 mLauncherCallbacks.onCreate(savedInstanceState);
527 }
Sunny Goyalb1d7de22019-09-06 10:36:54 -0700528 mOverlayManager = getDefaultOverlay();
529 PluginManagerWrapper.INSTANCE.get(this).addPluginListener(this,
Sunny Goyal80a071b2021-12-27 13:30:08 +0530530 LauncherOverlayPlugin.class, false /* allowedMultiple */);
Sunny Goyalb1d7de22019-09-06 10:36:54 -0700531
Sunny Goyal623eddd2018-03-02 12:24:41 -0800532 mRotationHelper.initialize();
Winson Chung5cc62c72019-10-16 11:32:41 -0700533 TraceHelper.INSTANCE.endSection(traceToken);
Samuel Fufa41d909f2020-03-20 16:27:09 -0700534
535 mUserChangedCallbackCloseable = UserCache.INSTANCE.get(this).addUserChangeListener(
536 () -> getStateManager().goToState(NORMAL));
Schneider Victor-tulias8a35c762020-09-03 14:30:10 -0700537
538 if (Utilities.ATLEAST_R) {
Schneider Victor-tulias25ecae12020-09-14 14:07:38 -0700539 getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
Schneider Victor-tulias8a35c762020-09-03 14:30:10 -0700540 }
Stefan Andonian45fc3012022-05-05 22:48:09 +0000541 setTitle(R.string.home_screen);
Adam Cohen9211d422014-10-07 18:14:53 -0700542 }
543
Sunny Goyalb1d7de22019-09-06 10:36:54 -0700544 protected LauncherOverlayManager getDefaultOverlay() {
545 return new LauncherOverlayManager() { };
546 }
547
Brian Isganitis099945b2022-01-31 18:15:00 -0500548 protected OnboardingPrefs<? extends Launcher> createOnboardingPrefs(
549 SharedPreferences sharedPrefs) {
Sunny Goyal35e7d382020-05-08 13:37:58 -0700550 return new OnboardingPrefs<>(this, sharedPrefs);
Tony Wickham133cd542020-04-08 17:48:41 -0700551 }
552
Brian Isganitis099945b2022-01-31 18:15:00 -0500553 public OnboardingPrefs<? extends Launcher> getOnboardingPrefs() {
Tony Wickham133cd542020-04-08 17:48:41 -0700554 return mOnboardingPrefs;
555 }
556
Sunny Goyalb1d7de22019-09-06 10:36:54 -0700557 @Override
Sunny Goyal80a071b2021-12-27 13:30:08 +0530558 public void onPluginConnected(LauncherOverlayPlugin overlayManager, Context context) {
Sunny Goyalb1d7de22019-09-06 10:36:54 -0700559 switchOverlay(() -> overlayManager.createOverlayManager(this, this));
560 }
561
562 @Override
Sunny Goyal80a071b2021-12-27 13:30:08 +0530563 public void onPluginDisconnected(LauncherOverlayPlugin plugin) {
Sunny Goyalb1d7de22019-09-06 10:36:54 -0700564 switchOverlay(this::getDefaultOverlay);
565 }
566
567 private void switchOverlay(Supplier<LauncherOverlayManager> overlaySupplier) {
568 if (mOverlayManager != null) {
569 mOverlayManager.onActivityDestroyed(this);
570 }
571 mOverlayManager = overlaySupplier.get();
572 if (getRootView().isAttachedToWindow()) {
573 mOverlayManager.onAttachedToWindow();
574 }
575 mDeferOverlayCallbacks = true;
576 checkIfOverlayStillDeferred();
577 }
578
579 @Override
Brian Isganitis099945b2022-01-31 18:15:00 -0500580 public void dispatchDeviceProfileChanged() {
Sunny Goyalb1d7de22019-09-06 10:36:54 -0700581 super.dispatchDeviceProfileChanged();
582 mOverlayManager.onDeviceProvideChanged();
583 }
584
Sunny Goyal7779d622015-06-11 16:18:39 -0700585 @Override
Winson Chung8eb49e02018-07-16 13:33:31 -0700586 public void onEnterAnimationComplete() {
587 super.onEnterAnimationComplete();
Sunny Goyalbe8c3402019-05-22 15:17:52 -0700588 mRotationHelper.setCurrentTransitionRequest(REQUEST_NONE);
Sunny Goyal5c20fdd2022-04-05 20:58:03 -0700589 // Starting with Android S, onEnterAnimationComplete is sent immediately
590 // causing the surface to get removed before the animation completed (b/175345344).
591 // Instead we rely on next user touch event to remove the view and optionally a callback
592 // from system from Android T onwards.
593 if (!Utilities.ATLEAST_S) {
594 AbstractFloatingView.closeOpenViews(this, false, TYPE_ICON_SURFACE);
595 }
Winson Chung8eb49e02018-07-16 13:33:31 -0700596 }
597
598 @Override
Jerry Chang09b40242021-08-27 13:29:01 +0800599 public void onMultiWindowModeChanged(boolean isInMultiWindowMode, Configuration newConfig) {
600 super.onMultiWindowModeChanged(isInMultiWindowMode, newConfig);
601 // Always update device profile when multi window mode changed.
602 initDeviceProfile(mDeviceProfile.inv);
603 dispatchDeviceProfileChanged();
604 }
605
606 @Override
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800607 public void onConfigurationChanged(Configuration newConfig) {
608 int diff = newConfig.diff(mOldConfig);
609 if ((diff & (CONFIG_ORIENTATION | CONFIG_SCREEN_SIZE)) != 0) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700610 onIdpChanged(false);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800611 }
612
613 mOldConfig.setTo(newConfig);
614 super.onConfigurationChanged(newConfig);
615 }
616
Sunny Goyal605bcf32018-03-02 15:16:12 -0800617 @Override
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700618 public void onIdpChanged(boolean modelPropertiesChanged) {
619 initDeviceProfile(mDeviceProfile.inv);
Sunny Goyal371ea052019-03-06 15:38:32 -0800620 dispatchDeviceProfileChanged();
621 reapplyUi();
622 mDragLayer.recreateControllers();
623
Sunny Goyal76ba0a32019-06-10 11:56:11 -0700624 // Calling onSaveInstanceState ensures that static cache used by listWidgets is
625 // initialized properly.
626 onSaveInstanceState(new Bundle());
Sunny Goyalc4d32012020-04-03 17:10:11 -0700627 mModel.rebindCallbacks();
Hyunyoung Song31971a32019-01-27 12:31:12 -0800628 }
629
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400630 public void onAssistantVisibilityChanged(float visibility) {
Sunny Goyal0b5b8d42021-03-31 14:56:47 -0700631 mHotseat.getQsb().setAlpha(1f - visibility);
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400632 }
633
Bill Lina1221bc2021-06-03 01:49:27 +0800634 /**
635 * Called when one handed mode activated and deactivated.
636 * @param activated true if one handed mode activated, false otherwise.
637 */
638 public void onOneHandedStateChanged(boolean activated) {
639 mDragLayer.onOneHandedModeStateChanged(activated);
640 }
641
Abhilasha Chahaldfd154f2022-02-27 09:58:49 +0000642 protected void initDeviceProfile(InvariantDeviceProfile idp) {
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800643 // Load configuration-specific DeviceProfile
Jerry Changbfac46a2021-09-08 08:20:58 +0800644 mDeviceProfile = idp.getDeviceProfile(this);
645 if (isInMultiWindowMode()) {
646 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(
647 this, getMultiWindowDisplaySize());
648 }
Sunny Goyalae6e3182019-04-30 12:04:37 -0700649
Sunny Goyal59d086c2018-05-07 17:31:40 -0700650 onDeviceProfileInitiated();
Sunny Goyal711c5962021-06-23 12:36:18 -0700651 mModelWriter = mModel.getWriter(getDeviceProfile().isVerticalBarLayout(), true, this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800652 }
653
Sunny Goyal623eddd2018-03-02 12:24:41 -0800654 public RotationHelper getRotationHelper() {
655 return mRotationHelper;
656 }
657
Tracy Zhou4d7b2442019-12-09 13:42:57 -0800658 public ViewGroupFocusHelper getFocusHandler() {
659 return mFocusHandler;
660 }
661
Sunny Goyal35e7d382020-05-08 13:37:58 -0700662 @Override
663 public StateManager<LauncherState> getStateManager() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700664 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700665 }
666
Adam Cohen9211d422014-10-07 18:14:53 -0700667 private LauncherCallbacks mLauncherCallbacks;
668
Sunny Goyal32554d12015-12-03 15:31:25 -0800669 /**
670 * Call this after onCreate to set or clear overlay.
671 */
Sunny Goyalb1d7de22019-09-06 10:36:54 -0700672 @Override
Sunny Goyal32554d12015-12-03 15:31:25 -0800673 public void setLauncherOverlay(LauncherOverlay overlay) {
674 if (overlay != null) {
Lucas Dupin87d75202021-07-28 10:24:54 -0700675 overlay.setOverlayCallbacks(this);
Sunny Goyal32554d12015-12-03 15:31:25 -0800676 }
677 mWorkspace.setLauncherOverlay(overlay);
678 }
679
Sunny Goyalb1d7de22019-09-06 10:36:54 -0700680 @Override
681 public void runOnOverlayHidden(Runnable runnable) {
682 getWorkspace().runOnOverlayHidden(runnable);
683 }
684
Adam Cohen9211d422014-10-07 18:14:53 -0700685 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
686 mLauncherCallbacks = callbacks;
687 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700688 }
689
Hyunyoung Song3f471442015-04-08 19:01:34 -0700690 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700691 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
692 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700693 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700694 }
695
Schneider Victor-tulias5173fd82021-10-19 14:47:23 -0700696 @NonNull
697 @Override
Tony Wickham010d2552017-01-20 08:15:28 -0800698 public PopupDataProvider getPopupDataProvider() {
699 return mPopupDataProvider;
700 }
701
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700702 @Override
Tony Wickhamf34bee82018-12-03 18:11:39 -0800703 public DotInfo getDotInfoForItem(ItemInfo info) {
704 return mPopupDataProvider.getDotInfoForItem(info);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700705 }
706
707 @Override
708 public void invalidateParent(ItemInfo info) {
Jon Mirandae6f3fa42019-03-04 09:14:40 -0800709 if (info.container >= 0) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700710 View folderIcon = getWorkspace().getHomescreenIconByItemId(info.container);
Jon Mirandae6f3fa42019-03-04 09:14:40 -0800711 if (folderIcon instanceof FolderIcon && folderIcon.getTag() instanceof FolderInfo) {
Sunny Goyal11a50742019-08-07 09:24:26 -0700712 if (new FolderGridOrganizer(getDeviceProfile().inv)
713 .setFolderInfo((FolderInfo) folderIcon.getTag())
714 .isItemInPreview(info.rank)) {
Jon Mirandae6f3fa42019-03-04 09:14:40 -0800715 folderIcon.invalidate();
716 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700717 }
718 }
719 }
720
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000721 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700722 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
723 * a configuration step, this allows the proper animations to run after other transitions.
724 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700725 private int completeAdd(
Sunny Goyal2100c782016-08-22 16:00:03 -0700726 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
Sunny Goyalefb7e842018-10-04 15:11:00 -0700727 int screenId = info.screenId;
Sunny Goyal2100c782016-08-22 16:00:03 -0700728 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700729 // When the screen id represents an actual screen (as opposed to a rank) we make sure
730 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700731 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700732 }
Adam Cohendb364c32014-05-20 14:23:40 -0700733
Sunny Goyal2100c782016-08-22 16:00:03 -0700734 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800735 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700736 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Pinyao Tingc9074272019-11-11 11:51:44 -0800737 announceForAccessibility(R.string.item_added_to_workspace);
Winson Chungb8472bb2011-08-05 13:49:21 -0700738 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800739 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700740 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700741 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700742 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal977838b2022-06-27 13:15:41 -0700743 getStatsLogManager().logger().withItemInfo(info).log(LAUNCHER_WIDGET_RECONFIGURED);
Sunny Goyal2100c782016-08-22 16:00:03 -0700744 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700745 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700746 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700747 int widgetId = appWidgetId;
748 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700749 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700750 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700751 // Since the view was just bound, also launch the configure activity if needed
752 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
753 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800754 if (provider != null) {
755 new WidgetAddFlowHandler(provider)
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700756 .startConfigActivity(this, widgetInfo,
757 REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700758 }
759 }
760 break;
761 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800762 }
Adam Cohendb364c32014-05-20 14:23:40 -0700763 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800764 }
765
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800766 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700767 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700768 if (isWorkspaceLoading()) {
769 // process the result once the workspace has loaded.
770 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
771 return;
772 }
773 mPendingActivityResult = null;
774
Winson Chunge341d302013-08-16 14:31:00 -0700775 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700776 final PendingRequestArgs requestArgs = mPendingRequestArgs;
777 setWaitingForResult(null);
778 if (requestArgs == null) {
779 return;
780 }
781
782 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700783
Adam Cohenad4e15c2013-10-17 16:21:35 -0700784 Runnable exitSpringLoaded = new Runnable() {
785 @Override
786 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700787 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700788 }
789 };
790
Michael Jurka8b805b12012-04-18 14:23:14 -0700791 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700792 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700793 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700794 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
795 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700796 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Sunny Goyal6e379fc2020-03-19 16:08:14 -0700797 mWorkspace.removeExtraEmptyScreenDelayed(
798 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false, exitSpringLoaded);
Michael Jurka8b805b12012-04-18 14:23:14 -0700799 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700800 addAppWidgetImpl(
801 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800802 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700803 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700804 }
805 return;
806 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200807
Adam Cohened66b2b2012-01-23 17:28:51 -0800808 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700809 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700810
Adam Cohened66b2b2012-01-23 17:28:51 -0800811 // We have special handling for widgets
812 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800813 final int appWidgetId;
814 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
815 : -1;
816 if (widgetId < 0) {
817 appWidgetId = pendingAddWidgetId;
818 } else {
819 appWidgetId = widgetId;
820 }
821
Adam Cohenad4e15c2013-10-17 16:21:35 -0700822 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800823 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700824 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
825 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700826 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700827 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Sunny Goyal6e379fc2020-03-19 16:08:14 -0700828 mWorkspace.removeExtraEmptyScreenDelayed(
829 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false,
830 () -> getStateManager().goToState(NORMAL));
Sunny Goyal2100c782016-08-22 16:00:03 -0700831 } else {
832 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
833 // When the screen id represents an actual screen (as opposed to a rank)
834 // we make sure that the drop page actually exists.
835 requestArgs.screenId =
836 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700837 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700838 final CellLayout dropLayout =
839 mWorkspace.getScreenWithId(requestArgs.screenId);
840
841 dropLayout.setDropPending(true);
842 final Runnable onComplete = new Runnable() {
843 @Override
844 public void run() {
845 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
846 dropLayout.setDropPending(false);
847 }
848 };
Sunny Goyal6e379fc2020-03-19 16:08:14 -0700849 mWorkspace.removeExtraEmptyScreenDelayed(
850 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false, onComplete);
Winson Chung5aaab772012-04-27 15:24:02 -0700851 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800852 return;
853 }
854
Sunny Goyald478c832016-04-01 12:04:16 -0700855 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
856 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700857 if (resultCode == RESULT_OK) {
858 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700859 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700860 }
861 // Leave the widget in the pending state if the user canceled the configure.
862 return;
863 }
864
Sunny Goyalaad90582015-07-09 14:22:50 -0700865 if (requestCode == REQUEST_CREATE_SHORTCUT) {
866 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700867 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
868 completeAdd(requestCode, data, -1, requestArgs);
Sunny Goyal6e379fc2020-03-19 16:08:14 -0700869 mWorkspace.removeExtraEmptyScreenDelayed(
870 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false, exitSpringLoaded);
Sunny Goyal2100c782016-08-22 16:00:03 -0700871
Sunny Goyalaad90582015-07-09 14:22:50 -0700872 } else if (resultCode == RESULT_CANCELED) {
Sunny Goyal6e379fc2020-03-19 16:08:14 -0700873 mWorkspace.removeExtraEmptyScreenDelayed(
874 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false, exitSpringLoaded);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800875 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800876 }
Hongming Jind91cb6e2020-01-27 16:23:03 -0800877
Adam Cohened66b2b2012-01-23 17:28:51 -0800878 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800879 }
880
881 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700882 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800883 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyaldedda052019-05-14 15:23:48 -0700884 mPendingActivityRequestCode = -1;
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800885 handleActivityResult(requestCode, resultCode, data);
Adam Cohened66b2b2012-01-23 17:28:51 -0800886 }
887
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700888 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700889 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600890 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700891 PendingRequestArgs pendingArgs = mPendingRequestArgs;
892 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
893 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
894 setWaitingForResult(null);
895
Sunny Goyal28c6b962015-10-12 11:42:05 -0700896 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700897 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700898 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700899 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700900 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700901 Intent intent = pendingArgs.getPendingIntent();
902
Sunny Goyal28c6b962015-10-12 11:42:05 -0700903 if (grantResults.length > 0
904 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal852537f2020-07-15 17:02:16 -0700905 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700906 } else {
907 // TODO: Show a snack bar with link to settings
908 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700909 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700910 }
911 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600912 }
913
Adam Cohendb364c32014-05-20 14:23:40 -0700914 /**
915 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
916 *
917 * @param screenId the screen id to check
918 * @return the new screen, or screenId if it exists
919 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700920 private int ensurePendingDropLayoutExists(int screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800921 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700922 if (dropLayout == null) {
923 // it's possible that the add screen was removed because it was
924 // empty and a re-bind occurred
Andras Kloczl408a54f2021-08-28 00:59:08 +0100925 mWorkspace.addExtraEmptyScreens();
926 IntSet emptyPagesAdded = mWorkspace.commitExtraEmptyScreens();
927 return emptyPagesAdded.isEmpty() ? -1 : emptyPagesAdded.getArray().get(0);
Adam Cohendb364c32014-05-20 14:23:40 -0700928 }
Andras Kloczl408a54f2021-08-28 00:59:08 +0100929 return screenId;
Adam Cohendb364c32014-05-20 14:23:40 -0700930 }
931
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700932 @Thunk
933 void completeTwoStageWidgetDrop(
Sunny Goyal2100c782016-08-22 16:00:03 -0700934 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
935 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800936 Runnable onCompleteRunnable = null;
937 int animationType = 0;
938
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700939 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800940 if (resultCode == RESULT_OK) {
941 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
942 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800943 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700944 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800945 onCompleteRunnable = new Runnable() {
946 @Override
947 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700948 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700949 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800950 }
951 };
952 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800953 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800954 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800955 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700956 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700957 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700958 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
959 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700960 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700961 // The animated view may be null in the case of a rotation during widget configuration
962 onCompleteRunnable.run();
963 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800964 }
965
966 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700967 protected void onStop() {
968 super.onStop();
Sunny Goyalb1d7de22019-09-06 10:36:54 -0700969 if (mDeferOverlayCallbacks) {
970 checkIfOverlayStillDeferred();
971 } else {
972 mOverlayManager.onActivityStopped(this);
Adam Cohen9211d422014-10-07 18:14:53 -0700973 }
Hyunyoung Song5ee7c132021-05-11 13:52:36 -0700974 hideKeyboard();
Hyunyoung Song95786e02020-09-15 00:34:10 -0700975 logStopAndResume(false /* isResume */);
vadimt04ebcf42021-02-03 15:31:51 -0800976 mAppWidgetHost.setActivityStarted(false);
Schneider Victor-tulias5173fd82021-10-19 14:47:23 -0700977 NotificationListener.removeNotificationsChangedListener(getPopupDataProvider());
Michael Jurkacd496d72013-04-11 11:32:45 -0700978 }
979
980 @Override
981 protected void onStart() {
Winson Chung5cc62c72019-10-16 11:32:41 -0700982 Object traceToken = TraceHelper.INSTANCE.beginSection(ON_START_EVT,
983 TraceHelper.FLAG_UI_EVENT);
Michael Jurkacd496d72013-04-11 11:32:45 -0700984 super.onStart();
Sunny Goyalb1d7de22019-09-06 10:36:54 -0700985 if (!mDeferOverlayCallbacks) {
986 mOverlayManager.onActivityStarted(this);
Adam Cohen9211d422014-10-07 18:14:53 -0700987 }
Sunny Goyalb1d7de22019-09-06 10:36:54 -0700988
vadimt04ebcf42021-02-03 15:31:51 -0800989 mAppWidgetHost.setActivityStarted(true);
Winson Chung5cc62c72019-10-16 11:32:41 -0700990 TraceHelper.INSTANCE.endSection(traceToken);
Michael Jurkacd496d72013-04-11 11:32:45 -0700991 }
992
Sunny Goyal35e7d382020-05-08 13:37:58 -0700993 @Override
994 @CallSuper
995 protected void onDeferredResumed() {
Hyunyoung Song95786e02020-09-15 00:34:10 -0700996 logStopAndResume(true /* isResume */);
Sunny Goyala002c6c2019-02-12 16:20:10 -0800997
Sunny Goyal35e7d382020-05-08 13:37:58 -0700998 // Process any items that were added while Launcher was away.
Sunny Goyal60e68c92020-08-12 13:59:27 -0700999 ItemInstallQueue.INSTANCE.get(this)
1000 .resumeModelPush(FLAG_ACTIVITY_PAUSED);
Sunny Goyale9c6f4c2019-05-30 11:35:25 -07001001
Sunny Goyal35e7d382020-05-08 13:37:58 -07001002 // Refresh shortcuts if the permission changed.
Sunny Goyal8b74cc72020-07-27 17:50:33 -07001003 mModel.validateModelDataOnResume();
Sunny Goyale9c6f4c2019-05-30 11:35:25 -07001004
Sunny Goyal35e7d382020-05-08 13:37:58 -07001005 // Set the notification listener and fetch updated notifications when we resume
Schneider Victor-tulias5173fd82021-10-19 14:47:23 -07001006 NotificationListener.addNotificationsChangedListener(mPopupDataProvider);
Winson Chung87583112019-06-10 14:20:58 -07001007
Sunny Goyal35e7d382020-05-08 13:37:58 -07001008 DiscoveryBounce.showForHomeIfNeeded(this);
vadimt04ebcf42021-02-03 15:31:51 -08001009 mAppWidgetHost.setActivityResumed(true);
Sunny Goyaldedda052019-05-14 15:23:48 -07001010 }
1011
Hyunyoung Song95786e02020-09-15 00:34:10 -07001012 private void logStopAndResume(boolean isResume) {
Hyunyoung Song77e29772020-08-25 00:53:48 -07001013 if (mPendingExecutor != null) return;
Hyunyoung Song801f81f2020-06-19 02:58:53 -07001014 int pageIndex = mWorkspace.isOverlayShown() ? -1 : mWorkspace.getCurrentPage();
Hyunyoung Song95786e02020-09-15 00:34:10 -07001015 int statsLogOrdinal = mStateManager.getState().statsLogOrdinal;
Hyunyoung Song801f81f2020-06-19 02:58:53 -07001016
1017 StatsLogManager.EventEnum event;
1018 StatsLogManager.StatsLogger logger = getStatsLogManager().logger();
Hyunyoung Song95786e02020-09-15 00:34:10 -07001019 if (isResume) {
Hyunyoung Song801f81f2020-06-19 02:58:53 -07001020 logger.withSrcState(LAUNCHER_STATE_BACKGROUND)
Hyunyoung Song95786e02020-09-15 00:34:10 -07001021 .withDstState(mStateManager.getState().statsLogOrdinal);
Hyunyoung Song801f81f2020-06-19 02:58:53 -07001022 event = LAUNCHER_ONRESUME;
1023 } else { /* command == Action.Command.STOP */
Hyunyoung Song95786e02020-09-15 00:34:10 -07001024 logger.withSrcState(mStateManager.getState().statsLogOrdinal)
Hyunyoung Song801f81f2020-06-19 02:58:53 -07001025 .withDstState(LAUNCHER_STATE_BACKGROUND);
1026 event = LAUNCHER_ONSTOP;
1027 }
1028
Hyunyoung Song95786e02020-09-15 00:34:10 -07001029 if (statsLogOrdinal == LAUNCHER_STATE_HOME && mWorkspace != null) {
Hyunyoung Song801f81f2020-06-19 02:58:53 -07001030 logger.withContainerInfo(LauncherAtom.ContainerInfo.newBuilder()
1031 .setWorkspace(
1032 LauncherAtom.WorkspaceContainer.newBuilder()
1033 .setPageIndex(pageIndex)).build());
Becky Qiubfc2da12019-08-01 11:09:38 -07001034 }
Hyunyoung Song801f81f2020-06-19 02:58:53 -07001035 logger.log(event);
Sunny Goyalb1d7de22019-09-06 10:36:54 -07001036 }
Becky Qiubfc2da12019-08-01 11:09:38 -07001037
Sunny Goyalb1d7de22019-09-06 10:36:54 -07001038 private void scheduleDeferredCheck() {
1039 mHandler.removeCallbacks(mDeferredOverlayCallbacks);
1040 postAsyncCallback(mHandler, mDeferredOverlayCallbacks);
1041 }
1042
1043 private void checkIfOverlayStillDeferred() {
1044 if (!mDeferOverlayCallbacks) {
1045 return;
1046 }
Sunny Goyal8f965462020-04-23 19:00:36 -07001047 if (isStarted() && (!hasBeenResumed()
1048 || mStateManager.getState().hasFlag(FLAG_NON_INTERACTIVE))) {
Sunny Goyalb1d7de22019-09-06 10:36:54 -07001049 return;
1050 }
1051 mDeferOverlayCallbacks = false;
1052
1053 // Move the client to the correct state. Calling the same method twice is no-op.
1054 if (isStarted()) {
1055 mOverlayManager.onActivityStarted(this);
1056 }
1057 if (hasBeenResumed()) {
1058 mOverlayManager.onActivityResumed(this);
1059 } else {
1060 mOverlayManager.onActivityPaused(this);
1061 }
1062 if (!isStarted()) {
1063 mOverlayManager.onActivityStopped(this);
1064 }
1065 }
1066
1067 public void deferOverlayCallbacksUntilNextResumeOrStop() {
1068 mDeferOverlayCallbacks = true;
1069 }
1070
1071 public LauncherOverlayManager getOverlayManager() {
1072 return mOverlayManager;
Becky Qiubfc2da12019-08-01 11:09:38 -07001073 }
1074
Sunny Goyal35e7d382020-05-08 13:37:58 -07001075 @Override
Winson Chungef528762019-09-06 12:05:52 -07001076 public void onStateSetStart(LauncherState state) {
Sunny Goyal35e7d382020-05-08 13:37:58 -07001077 super.onStateSetStart(state);
Sunny Goyalb1d7de22019-09-06 10:36:54 -07001078 if (mDeferOverlayCallbacks) {
1079 scheduleDeferredCheck();
Sunny Goyale9c6f4c2019-05-30 11:35:25 -07001080 }
Sunny Goyal210e1742019-10-17 12:05:38 -07001081 addActivityFlags(ACTIVITY_STATE_TRANSITION_ACTIVE);
Sunny Goyal8f965462020-04-23 19:00:36 -07001082
Sunny Goyal8f965462020-04-23 19:00:36 -07001083 if (state == SPRING_LOADED) {
1084 // Prevent any Un/InstallShortcutReceivers from updating the db while we are
1085 // not on homescreen
Sunny Goyal60e68c92020-08-12 13:59:27 -07001086 ItemInstallQueue.INSTANCE.get(this).pauseModelPush(FLAG_DRAG_AND_DROP);
Sunny Goyal8f965462020-04-23 19:00:36 -07001087 getRotationHelper().setCurrentStateRequest(REQUEST_LOCK);
1088
1089 mWorkspace.showPageIndicatorAtCurrentScroll();
1090 mWorkspace.setClipChildren(false);
1091 }
1092 // When multiple pages are visible, show persistent page indicator
1093 mWorkspace.getPageIndicator().setShouldAutoHide(!state.hasFlag(FLAG_MULTI_PAGE));
thiruram0eabc782021-04-02 14:30:10 -07001094
thiruramc96873c2021-02-11 15:13:47 -08001095 mPrevLauncherState = mStateManager.getCurrentStableState();
thiruram0eabc782021-04-02 14:30:10 -07001096 if (mPrevLauncherState != state && ALL_APPS.equals(state)
1097 // Making sure mAllAppsSessionLogId is null to avoid double logging.
1098 && mAllAppsSessionLogId == null) {
1099 // creates new instance ID since new all apps session is started.
1100 mAllAppsSessionLogId = new InstanceIdSequence().newInstanceId();
Thiru Ramasamy694407c2022-03-08 12:11:24 -08001101 if (getAllAppsEntryEvent().isPresent()) {
1102 getStatsLogManager().logger()
1103 .withContainerInfo(ContainerInfo.newBuilder()
1104 .setWorkspace(WorkspaceContainer.newBuilder()
1105 .setPageIndex(getWorkspace().getCurrentPage())).build())
1106 .log(getAllAppsEntryEvent().get());
1107 }
thiruram0eabc782021-04-02 14:30:10 -07001108 }
Sunny Goyaldedda052019-05-14 15:23:48 -07001109 }
1110
Thiru Ramasamy5fbac632022-01-28 15:05:36 -08001111 /**
1112 * Returns {@link EventEnum} that should be logged when Launcher enters into AllApps state.
1113 */
Thiru Ramasamy694407c2022-03-08 12:11:24 -08001114 protected Optional<EventEnum> getAllAppsEntryEvent() {
1115 return Optional.of(FeatureFlags.ENABLE_DEVICE_SEARCH.get()
Thiru Ramasamy5fbac632022-01-28 15:05:36 -08001116 ? LAUNCHER_ALLAPPS_ENTRY_WITH_DEVICE_SEARCH
Thiru Ramasamy694407c2022-03-08 12:11:24 -08001117 : LAUNCHER_ALLAPPS_ENTRY);
Thiru Ramasamy5fbac632022-01-28 15:05:36 -08001118 }
1119
Sunny Goyal35e7d382020-05-08 13:37:58 -07001120 @Override
Winson Chungef528762019-09-06 12:05:52 -07001121 public void onStateSetEnd(LauncherState state) {
Sunny Goyalfee1b252020-12-14 12:30:38 -08001122 super.onStateSetEnd(state);
vadimt04ebcf42021-02-03 15:31:51 -08001123 getAppWidgetHost().setStateIsNormal(state == LauncherState.NORMAL);
Sunny Goyal8f965462020-04-23 19:00:36 -07001124 getWorkspace().setClipChildren(!state.hasFlag(FLAG_MULTI_PAGE));
1125
Winson Chungef528762019-09-06 12:05:52 -07001126 finishAutoCancelActionMode();
Sunny Goyal210e1742019-10-17 12:05:38 -07001127 removeActivityFlags(ACTIVITY_STATE_TRANSITION_ACTIVE);
Sunny Goyal8f965462020-04-23 19:00:36 -07001128
1129 // dispatch window state changed
1130 getWindow().getDecorView().sendAccessibilityEvent(TYPE_WINDOW_STATE_CHANGED);
1131 AccessibilityManagerCompat.sendStateEventToTest(this, state.ordinal);
1132
1133 if (state == NORMAL) {
1134 // Re-enable any Un/InstallShortcutReceiver and now process any queued items
Sunny Goyal60e68c92020-08-12 13:59:27 -07001135 ItemInstallQueue.INSTANCE.get(this)
1136 .resumeModelPush(FLAG_DRAG_AND_DROP);
Sunny Goyal8f965462020-04-23 19:00:36 -07001137
1138 // Clear any rotation locks when going to normal state
1139 getRotationHelper().setCurrentStateRequest(REQUEST_NONE);
1140 }
thiruramc96873c2021-02-11 15:13:47 -08001141
thiruramea56f842021-06-02 16:02:37 -07001142 if (ALL_APPS.equals(mPrevLauncherState) && !ALL_APPS.equals(state)
thiruram0eabc782021-04-02 14:30:10 -07001143 // Making sure mAllAppsSessionLogId is not null to avoid double logging.
thiruramc96873c2021-02-11 15:13:47 -08001144 && mAllAppsSessionLogId != null) {
Samuel Fuface820f52021-06-18 17:23:48 -05001145 getAppsView().reset(false);
Thiru Ramasamy694407c2022-03-08 12:11:24 -08001146 getAllAppsExitEvent().ifPresent(getStatsLogManager().logger()::log);
thiruramc96873c2021-02-11 15:13:47 -08001147 mAllAppsSessionLogId = null;
1148 }
Winson Chungef528762019-09-06 12:05:52 -07001149 }
1150
Thiru Ramasamy694407c2022-03-08 12:11:24 -08001151 /**
1152 * Returns {@link EventEnum} that should be logged when Launcher exists from AllApps state.
1153 */
1154 protected Optional<EventEnum> getAllAppsExitEvent() {
1155 return Optional.of(LAUNCHER_ALLAPPS_EXIT);
1156 }
1157
Michael Jurkacd496d72013-04-11 11:32:45 -07001158 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001159 protected void onResume() {
Winson Chung5cc62c72019-10-16 11:32:41 -07001160 Object traceToken = TraceHelper.INSTANCE.beginSection(ON_RESUME_EVT,
1161 TraceHelper.FLAG_UI_EVENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001162 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001163
Sunny Goyalb1d7de22019-09-06 10:36:54 -07001164 if (mDeferOverlayCallbacks) {
1165 scheduleDeferredCheck();
1166 } else {
1167 mOverlayManager.onActivityResumed(this);
Adam Cohen9211d422014-10-07 18:14:53 -07001168 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -08001169
Stefan Andoniand123fbd2022-04-07 20:04:55 +00001170 AbstractFloatingView.closeAllOpenViewsExcept(this, false, TYPE_REBIND_SAFE);
Jon Mirandaa82a7dc2022-05-10 11:48:32 -07001171 DragView.removeAllViews(this);
Winson Chung5cc62c72019-10-16 11:32:41 -07001172 TraceHelper.INSTANCE.endSection(traceToken);
Adam Cohen06dff352012-06-01 17:17:08 -07001173 }
1174
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001175 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001176 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001177 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyal60e68c92020-08-12 13:59:27 -07001178 ItemInstallQueue.INSTANCE.get(this).pauseModelPush(FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -07001179
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001180 super.onPause();
Joe Onorato24b6fd82009-11-12 13:47:09 -08001181 mDragController.cancelDrag();
Sunny Goyal9b180102020-03-11 10:02:29 -07001182 mLastTouchUpTime = -1;
Hyunyoung Song497708c2019-05-01 16:16:53 -07001183 mDropTargetBar.animateToVisibility(false);
Sunny Goyalb1d7de22019-09-06 10:36:54 -07001184
1185 if (!mDeferOverlayCallbacks) {
1186 mOverlayManager.onActivityPaused(this);
Adam Cohenbffe7452013-07-22 18:21:45 -07001187 }
vadimt04ebcf42021-02-03 15:31:51 -08001188 mAppWidgetHost.setActivityResumed(false);
Adam Cohenbffe7452013-07-22 18:21:45 -07001189 }
1190
Lucas Dupin87d75202021-07-28 10:24:54 -07001191 /**
1192 * {@code LauncherOverlayCallbacks} scroll amount.
1193 * Indicates transition progress to -1 screen.
1194 * @param progress From 0 to 1.
1195 */
1196 @Override
1197 public void onScrollChanged(float progress) {
1198 if (mWorkspace != null) {
1199 mWorkspace.onOverlayScrollChanged(progress);
Sunny Goyalc86df472016-02-25 09:19:38 -08001200 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001201 }
1202
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001203 /**
1204 * Restores the previous state, if it exists.
1205 *
1206 * @param savedState The previous state.
1207 */
1208 private void restoreState(Bundle savedState) {
1209 if (savedState == null) {
1210 return;
1211 }
1212
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001213 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -07001214 LauncherState[] stateValues = LauncherState.values();
1215 LauncherState state = stateValues[stateOrdinal];
Sunny Goyal6c0c61a2020-12-08 19:19:56 -08001216
1217 NonConfigInstance lastInstance = (NonConfigInstance) getLastNonConfigurationInstance();
1218 boolean forceRestore = lastInstance != null
1219 && (lastInstance.config.diff(mOldConfig) & CONFIG_UI_MODE) != 0;
1220 if (forceRestore || !state.shouldDisableRestore()) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001221 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001222 }
1223
Samuel Fufaca37b8a2019-08-19 17:04:36 -07001224 PendingRequestArgs requestArgs = savedState.getParcelable(
1225 RUNTIME_STATE_PENDING_REQUEST_ARGS);
Sunny Goyal2100c782016-08-22 16:00:03 -07001226 if (requestArgs != null) {
1227 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001228 }
Sunny Goyaldedda052019-05-14 15:23:48 -07001229 mPendingActivityRequestCode = savedState.getInt(RUNTIME_STATE_PENDING_REQUEST_CODE);
Sunny Goyal2100c782016-08-22 16:00:03 -07001230
1231 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001232
1233 SparseArray<Parcelable> widgetsState =
1234 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
1235 if (widgetsState != null) {
1236 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
1237 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001238 }
1239
1240 /**
1241 * Finds all the views we need and configure them properly.
1242 */
Tony Wickham04fca162020-02-05 15:06:52 -08001243 protected void setupViews() {
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001244 mDragLayer = findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001245 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -07001246 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001247 mWorkspace.initParentViews(mDragLayer);
Sreyas321bfae2020-02-21 17:53:02 -08001248 mOverviewPanel = findViewById(R.id.overview_panel);
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001249 mHotseat = findViewById(R.id.hotseat);
Adam Cohenb66675a2020-05-15 16:16:17 -07001250 mHotseat.setWorkspace(mWorkspace);
Craig Mautner360310b2012-10-26 15:13:08 -07001251
Winson Chung4c98d922011-05-31 16:50:48 -07001252 // Setup the drag layer
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001253 mDragLayer.setup(mDragController, mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001254
Hyunyoung Song645764e2016-06-06 14:19:02 -07001255 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001256 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1257 // default state, otherwise we will update to the wrong offsets in RTL
1258 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalbb030562021-07-19 16:28:39 -07001259 mWorkspace.bindAndInitFirstWorkspaceScreen();
Hyunyoung Song645764e2016-06-06 14:19:02 -07001260 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001261
Tony Wickham34d2c912015-09-11 09:27:58 -07001262 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001263 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001264
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001265 // Setup Apps
1266 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001267
Govinda Wassermanc06e1512019-04-09 09:56:53 -04001268 // Setup Scrim
1269 mScrimView = findViewById(R.id.scrim_view);
1270
Winson Chung3d503fb2011-07-13 17:25:49 -07001271 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Sunny Goyal47328fd2016-05-25 18:56:41 -07001272 mDropTargetBar.setup(mDragController);
Samuel Fufaacebd1f2021-05-06 10:02:03 -05001273 mAllAppsController.setupViews(mScrimView, mAppsView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001274 }
1275
1276 /**
1277 * Creates a view representing a shortcut.
1278 *
1279 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001280 */
Sunny Goyal95899162019-03-27 16:03:06 -07001281 View createShortcut(WorkspaceItemInfo info) {
Schneider Victor-tulias9f922d32022-04-27 16:39:29 -07001282 // This can be called before PagedView#pageScrollsInitialized returns true, so use the
1283 // first page, which we always assume to be present.
1284 return createShortcut((ViewGroup) mWorkspace.getChildAt(0), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001285 }
1286
1287 /**
1288 * Creates a view representing a shortcut inflated from the specified resource.
1289 *
Schneider Victor-tulias9f922d32022-04-27 16:39:29 -07001290 * @param parent The group the shortcut belongs to. This is not necessarily the group where
1291 * the shortcut should be added.
Samuel Fufaca37b8a2019-08-19 17:04:36 -07001292 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001293 * @return A View inflated from layoutResId.
1294 */
Sunny Goyal95899162019-03-27 16:03:06 -07001295 public View createShortcut(ViewGroup parent, WorkspaceItemInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -08001296 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
1297 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal95899162019-03-27 16:03:06 -07001298 favorite.applyFromWorkspaceItem(info);
Sunny Goyal8a2a63b2018-03-06 22:15:18 -08001299 favorite.setOnClickListener(ItemClickHandler.INSTANCE);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001300 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001301 return favorite;
1302 }
1303
1304 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -07001305 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001306 *
1307 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001308 */
sfufa@google.comd0a33232021-09-19 15:59:36 -07001309 protected void completeAddShortcut(Intent data, int container, int screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001310 int cellY, PendingRequestArgs args) {
Pinyao Ting5eee5f42019-10-31 16:47:35 -07001311 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
Jon Mirandac476d6e2017-05-04 16:30:01 -07001312 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001313 return;
1314 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001315
Jon Mirandac476d6e2017-05-04 16:30:01 -07001316 int[] cellXY = mTmpAddItemCellCoordinates;
1317 CellLayout layout = getCellLayout(container, screenId);
1318
Sunny Goyaleaf7a952020-07-29 16:54:20 -07001319 WorkspaceItemInfo info = PinRequestHelper.createWorkspaceItemFromPinItemRequest(
Sunny Goyale7b00122019-10-02 16:13:34 -07001320 this, PinRequestHelper.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001321
1322 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001323 // Legacy shortcuts are only supported for primary profile.
1324 info = Process.myUserHandle().equals(args.user)
Sunny Goyal53116c62020-08-07 16:32:18 -07001325 ? ModelUtils.fromLegacyShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001326
Sunny Goyalb57645f2017-03-20 13:57:28 -07001327 if (info == null) {
1328 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1329 return;
1330 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001331 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001332 // The app is trying to add a shortcut without sufficient permissions
1333 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1334 return;
1335 }
1336 }
1337
Jon Mirandac476d6e2017-05-04 16:30:01 -07001338 if (container < 0) {
1339 // Adding a shortcut to the Workspace.
1340 final View view = createShortcut(info);
1341 boolean foundCellSpan = false;
1342 // First we check if we already know the exact location where we want to add this item.
1343 if (cellX >= 0 && cellY >= 0) {
1344 cellXY[0] = cellX;
1345 cellXY[1] = cellY;
1346 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001347
Hyunyoung Song1ae42422020-01-26 23:25:14 -08001348 DragObject dragObject = new DragObject(getApplicationContext());
1349 dragObject.dragInfo = info;
Jon Mirandac476d6e2017-05-04 16:30:01 -07001350 // If appropriate, either create a folder or add to an existing folder
1351 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Hyunyoung Song1ae42422020-01-26 23:25:14 -08001352 true, dragObject)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001353 return;
1354 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001355 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1356 true)) {
1357 return;
1358 }
1359 } else {
1360 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1361 }
1362
1363 if (!foundCellSpan) {
Yogisha Dixit104d09f2021-07-19 14:05:17 +01001364 mWorkspace.onNoCellFound(layout, info, /* logInstanceId= */ null);
Adam Cohen558baaf2011-08-15 15:22:57 -07001365 return;
1366 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001367
1368 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1369 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001370 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001371 // Adding a shortcut to a Folder.
Sunny Goyale29897f52017-07-20 10:09:42 -07001372 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001373 if (folderIcon != null) {
1374 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1375 folderInfo.add(info, args.rank, false);
1376 } else {
Sunny Goyale29897f52017-07-20 10:09:42 -07001377 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001378 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001379 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001380 }
1381
Tony Wickhamc7cbf252021-05-24 15:46:48 -07001382 @Override
1383 public @Nullable FolderIcon findFolderIcon(final int folderIconId) {
Sunny Goyal83fd25e2018-07-09 16:47:01 -07001384 return (FolderIcon) mWorkspace.getHomescreenIconByItemId(folderIconId);
Sunny Goyale29897f52017-07-20 10:09:42 -07001385 }
1386
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001387 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001388 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001389 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001390 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001391 */
Samuel Fufaca37b8a2019-08-19 17:04:36 -07001392 @Thunk
1393 void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001394 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1395
Adam Cohened66b2b2012-01-23 17:28:51 -08001396 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001397 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001398 }
Romain Guycbb89e42009-06-08 15:52:54 -07001399
Yogisha Dixit0b15c7e2021-05-10 12:01:58 +01001400 if (hostView == null) {
1401 // Perform actual inflation because we're live
1402 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1403 }
1404
Adam Cohen59400422014-03-05 18:07:04 -08001405 LauncherAppWidgetInfo launcherInfo;
Yogisha Dixit0b15c7e2021-05-10 12:01:58 +01001406 launcherInfo =
1407 new LauncherAppWidgetInfo(
1408 appWidgetId, appWidgetInfo.provider, appWidgetInfo, hostView);
Sunny Goyal2100c782016-08-22 16:00:03 -07001409 launcherInfo.spanX = itemInfo.spanX;
1410 launcherInfo.spanY = itemInfo.spanY;
1411 launcherInfo.minSpanX = itemInfo.minSpanX;
1412 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001413 launcherInfo.user = appWidgetInfo.getProfile();
Yogisha Dixit658c5da2021-05-24 23:23:15 +01001414 if (itemInfo instanceof PendingAddWidgetInfo) {
1415 launcherInfo.sourceContainer = ((PendingAddWidgetInfo) itemInfo).sourceContainer;
1416 } else if (itemInfo instanceof PendingRequestArgs) {
1417 launcherInfo.sourceContainer =
1418 ((PendingRequestArgs) itemInfo).getWidgetSourceContainer();
1419 }
Romain Guycbb89e42009-06-08 15:52:54 -07001420
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001421 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001422 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001423
Sunny Goyal2100c782016-08-22 16:00:03 -07001424 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301425 prepareAppWidget(hostView, launcherInfo);
1426 mWorkspace.addInScreen(hostView, launcherInfo);
Pinyao Tingc9074272019-11-11 11:51:44 -08001427 announceForAccessibility(R.string.item_added_to_workspace);
Yogisha Dixit037a5362021-03-22 14:13:03 +00001428
1429 // Show the widget resize frame.
1430 if (hostView instanceof LauncherAppWidgetHostView) {
1431 final LauncherAppWidgetHostView launcherHostView = (LauncherAppWidgetHostView) hostView;
1432 CellLayout cellLayout = getCellLayout(launcherInfo.container, launcherInfo.screenId);
1433 if (mStateManager.getState() == NORMAL) {
Yogisha Dixit7b1d25b2021-10-14 13:06:40 +01001434 AppWidgetResizeFrame.showForWidget(launcherHostView, cellLayout);
Yogisha Dixit037a5362021-03-22 14:13:03 +00001435 } else {
1436 mStateManager.addStateListener(new StateManager.StateListener<LauncherState>() {
1437 @Override
1438 public void onStateTransitionComplete(LauncherState finalState) {
1439 if (mPrevLauncherState == SPRING_LOADED && finalState == NORMAL) {
1440 AppWidgetResizeFrame.showForWidget(launcherHostView, cellLayout);
1441 mStateManager.removeStateListener(this);
1442 }
1443 }
1444 });
1445 }
1446 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001447 }
Romain Guycbb89e42009-06-08 15:52:54 -07001448
Sunny Goyald5462aa2016-11-22 16:52:39 +05301449 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001450 hostView.setTag(item);
1451 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001452 hostView.setFocusable(true);
1453 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001454 }
1455
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001456 private final BroadcastReceiver mScreenOffReceiver = new BroadcastReceiver() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001457 @Override
1458 public void onReceive(Context context, Intent intent) {
Tracy Zhouf20a4bd2021-04-19 21:37:08 -07001459 onScreenOff();
Adam Cohended9f8d2010-11-03 13:25:16 -07001460 }
1461 };
1462
Sunny Goyal9c2b9602020-01-07 13:07:55 -08001463 private void updateNotificationDots(Predicate<PackageUserKey> updatedDots) {
Tony Wickhamf34bee82018-12-03 18:11:39 -08001464 mWorkspace.updateNotificationDots(updatedDots);
1465 mAppsView.getAppsStore().updateNotificationDots(updatedDots);
Tony Wickham010d2552017-01-20 08:15:28 -08001466 }
1467
Adam Cohended9f8d2010-11-03 13:25:16 -07001468 @Override
1469 public void onAttachedToWindow() {
1470 super.onAttachedToWindow();
Sunny Goyalb1d7de22019-09-06 10:36:54 -07001471 mOverlayManager.onAttachedToWindow();
Adam Cohended9f8d2010-11-03 13:25:16 -07001472 }
1473
1474 @Override
1475 public void onDetachedFromWindow() {
1476 super.onDetachedFromWindow();
Sunny Goyalb1d7de22019-09-06 10:36:54 -07001477 mOverlayManager.onDetachedFromWindow();
1478 closeContextMenu();
Adam Cohended9f8d2010-11-03 13:25:16 -07001479 }
1480
Schneider Victor-tulias25ecae12020-09-14 14:07:38 -07001481 @Override
1482 public Object onRetainNonConfigurationInstance() {
Sunny Goyal6c0c61a2020-12-08 19:19:56 -08001483 NonConfigInstance instance = new NonConfigInstance();
1484 instance.config = new Configuration(mOldConfig);
1485
Schneider Victor-tulias25ecae12020-09-14 14:07:38 -07001486 int width = mDragLayer.getWidth();
1487 int height = mDragLayer.getHeight();
1488
Schneider Victor-tulias5aad9d82020-12-15 10:47:44 -05001489 if (FeatureFlags.ENABLE_LAUNCHER_ACTIVITY_THEME_CROSSFADE.get()
1490 && width > 0
1491 && height > 0) {
Sunny Goyal6c0c61a2020-12-08 19:19:56 -08001492 instance.snapshot =
1493 BitmapRenderer.createHardwareBitmap(width, height, mDragLayer::draw);
Schneider Victor-tulias25ecae12020-09-14 14:07:38 -07001494 }
Sunny Goyal6c0c61a2020-12-08 19:19:56 -08001495 return instance;
Schneider Victor-tulias25ecae12020-09-14 14:07:38 -07001496 }
1497
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001498 public AllAppsTransitionController getAllAppsController() {
1499 return mAllAppsController;
1500 }
1501
Sunny Goyal9d69c8d2018-03-19 13:41:31 -07001502 @Override
Winson Chunga6945242014-01-08 14:04:34 -08001503 public DragLayer getDragLayer() {
1504 return mDragLayer;
1505 }
1506
Brian Isganitis1664c9f2022-01-14 23:15:47 -05001507 @Override
1508 public ActivityAllAppsContainerView<Launcher> getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001509 return mAppsView;
1510 }
1511
Shikha Malhotra0f2e1b62022-03-27 21:02:21 +00001512 public Workspace<?> getWorkspace() {
Winson Chunga6945242014-01-08 14:04:34 -08001513 return mWorkspace;
1514 }
1515
1516 public Hotseat getHotseat() {
1517 return mHotseat;
1518 }
1519
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001520 public <T extends View> T getOverviewPanel() {
Winson Chung8ae41982017-11-10 11:42:13 -08001521 return (T) mOverviewPanel;
Winson Chunga6945242014-01-08 14:04:34 -08001522 }
1523
Sunny Goyal47328fd2016-05-25 18:56:41 -07001524 public DropTargetBar getDropTargetBar() {
1525 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001526 }
1527
Tony Wickhamb4821882021-04-23 14:26:45 -07001528 @Override
Tony Wickham490a8ab2020-04-01 17:03:58 -07001529 public ScrimView getScrimView() {
1530 return mScrimView;
1531 }
1532
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001533 public LauncherAppWidgetHost getAppWidgetHost() {
1534 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001535 }
Romain Guycbb89e42009-06-08 15:52:54 -07001536
Cyrus Boadway0d309db2021-04-14 12:29:14 +00001537 protected LauncherAppWidgetHost createAppWidgetHost() {
1538 return new LauncherAppWidgetHost(this,
1539 appWidgetId -> getWorkspace().removeWidget(appWidgetId));
1540 }
1541
Winson Chunga9abd0e2010-10-27 17:18:37 -07001542 public LauncherModel getModel() {
1543 return mModel;
1544 }
1545
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001546 public ModelWriter getModelWriter() {
1547 return mModelWriter;
1548 }
1549
Sunny Goyalb1d7de22019-09-06 10:36:54 -07001550 @Override
Adam Cohen79d90c52016-04-22 13:29:20 -07001551 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001552 return mSharedPrefs;
1553 }
1554
Sunny Goyalb1d7de22019-09-06 10:36:54 -07001555 @Override
1556 public SharedPreferences getDevicePrefs() {
1557 return Utilities.getDevicePrefs(this);
1558 }
1559
Samuel Fufaca37b8a2019-08-19 17:04:36 -07001560 public int getOrientation() {
1561 return mOldConfig.orientation;
1562 }
Jon Miranda6bed3502017-09-19 14:43:17 -07001563
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001564 @Override
1565 protected void onNewIntent(Intent intent) {
vadimtbaf5fbb2019-12-16 18:56:09 -08001566 if (Utilities.IS_RUNNING_IN_TEST_HARNESS) {
1567 Log.d(TestProtocol.PERMANENT_DIAG_TAG, "Launcher.onNewIntent: " + intent);
1568 }
Winson Chung5cc62c72019-10-16 11:32:41 -07001569 Object traceToken = TraceHelper.INSTANCE.beginSection(ON_NEW_INTENT_EVT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001570 super.onNewIntent(intent);
1571
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001572 boolean alreadyOnHome = hasWindowFocus() && ((intent.getFlags() &
Winson15f8b172015-08-19 11:02:39 -07001573 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1574 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001575
1576 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001577 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001578 && AbstractFloatingView.getTopOpenView(this) == null;
Winson15f8b172015-08-19 11:02:39 -07001579 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
Winson Chung1c2d5222021-06-08 11:50:13 -07001580 boolean internalStateHandled = ACTIVITY_TRACKER.handleNewIntent(this);
Hyunyoung Song5ee7c132021-05-11 13:52:36 -07001581 hideKeyboard();
kholoud mohamedceec31b2022-02-03 15:44:25 +00001582
Winson15f8b172015-08-19 11:02:39 -07001583 if (isActionMain) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001584 if (!internalStateHandled) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001585 // In all these cases, only animate if we're already on home
Andy Wickham65ced1b2020-03-07 02:14:19 +00001586 closeOpenViews(isStarted());
Jon Mirandafeba90f2016-10-06 10:53:29 -07001587
Sunny Goyala2467272018-03-16 14:53:05 -07001588 if (!isInState(NORMAL)) {
1589 // Only change state, if not already the same. This prevents cancelling any
1590 // animations running as part of resume
Sunny Goyalfee1b252020-12-14 12:30:38 -08001591 mStateManager.goToState(NORMAL, mStateManager.shouldAnimateStateChange());
Sunny Goyala2467272018-03-16 14:53:05 -07001592 }
Sunny Goyald3864fa2017-11-14 15:06:36 -08001593
1594 // Reset the apps view
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001595 if (!alreadyOnHome) {
Sunny Goyal7185dd62018-03-14 17:51:49 -07001596 mAppsView.reset(isStarted() /* animate */);
Sunny Goyald3864fa2017-11-14 15:06:36 -08001597 }
1598
Sunny Goyalaad33592018-08-07 17:20:35 -07001599 if (shouldMoveToDefaultScreen && !mWorkspace.isHandlingTouch()) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001600 mWorkspace.post(mWorkspace::moveToDefaultScreen);
1601 }
1602 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001603
Adam Cohen9211d422014-10-07 18:14:53 -07001604 if (mLauncherCallbacks != null) {
Sunny Goyal85462132018-04-24 11:39:37 -07001605 mLauncherCallbacks.onHomeIntent(internalStateHandled);
Adam Cohen9211d422014-10-07 18:14:53 -07001606 }
Sunny Goyalb1d7de22019-09-06 10:36:54 -07001607 mOverlayManager.hideOverlay(isStarted() && !isForceInvisible());
Sunny Goyal30ac97d2020-06-24 23:47:46 -07001608 handleGestureContract(intent);
Sunny Goyal512a2c12020-04-08 12:52:45 -07001609 } else if (Intent.ACTION_ALL_APPS.equals(intent.getAction())) {
Winson Chungdd15ace2020-08-04 15:52:01 -07001610 showAllAppsFromIntent(alreadyOnHome);
kholoud mohamedceec31b2022-02-03 15:44:25 +00001611 } else if (Intent.ACTION_SHOW_WORK_APPS.equals(intent.getAction())) {
1612 showAllAppsWorkTabFromIntent(alreadyOnHome);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001613 }
Winson15f8b172015-08-19 11:02:39 -07001614
Winson Chung5cc62c72019-10-16 11:32:41 -07001615 TraceHelper.INSTANCE.endSection(traceToken);
Adam Coppa120b8e2013-11-12 16:26:04 +00001616 }
1617
Winson Chungdd15ace2020-08-04 15:52:01 -07001618 protected void showAllAppsFromIntent(boolean alreadyOnHome) {
1619 AbstractFloatingView.closeAllOpenViews(this);
1620 getStateManager().goToState(ALL_APPS, alreadyOnHome);
1621 }
1622
kholoud mohamedceec31b2022-02-03 15:44:25 +00001623 private void showAllAppsWorkTabFromIntent(boolean alreadyOnHome) {
1624 showAllAppsFromIntent(alreadyOnHome);
1625 mAppsView.switchToTab(BaseAllAppsContainerView.AdapterHolder.WORK);
1626 }
1627
Sunny Goyaldeb91c42020-03-24 02:17:59 -07001628 /**
Sunny Goyal30ac97d2020-06-24 23:47:46 -07001629 * Handles gesture nav contract
1630 */
1631 protected void handleGestureContract(Intent intent) {
1632 GestureNavContract gnc = GestureNavContract.fromIntent(intent);
1633 if (gnc != null) {
1634 AbstractFloatingView.closeOpenViews(this, false, TYPE_ICON_SURFACE);
1635 FloatingSurfaceView.show(this, gnc);
1636 }
1637 }
1638
1639 /**
Sunny Goyaldeb91c42020-03-24 02:17:59 -07001640 * Hides the keyboard if visible
1641 */
1642 public void hideKeyboard() {
1643 final View v = getWindow().peekDecorView();
1644 if (v != null && v.getWindowToken() != null) {
1645 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
1646 }
1647 }
1648
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001649 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001650 public void onRestoreInstanceState(Bundle state) {
1651 super.onRestoreInstanceState(state);
Andras Kloczl55edfe52021-05-14 12:21:30 +02001652 if (mSynchronouslyBoundPages != null) {
Sunny Goyal12e3f1f2021-07-15 14:31:58 -07001653 mSynchronouslyBoundPages.forEach(screenId -> {
1654 int pageIndex = mWorkspace.getPageIndexForScreenId(screenId);
1655 if (pageIndex != PagedView.INVALID_PAGE) {
1656 mWorkspace.restoreInstanceStateForChild(pageIndex);
1657 }
1658 });
Andras Kloczl55edfe52021-05-14 12:21:30 +02001659 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001660 }
1661
1662 @Override
1663 protected void onSaveInstanceState(Bundle outState) {
Sunny Goyal12e3f1f2021-07-15 14:31:58 -07001664 outState.putIntArray(RUNTIME_STATE_CURRENT_SCREEN_IDS,
1665 mWorkspace.getCurrentPageScreenIds().getArray().toArray());
Sunny Goyalea609262017-10-25 15:47:38 -07001666 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001667
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001668 AbstractFloatingView widgets = AbstractFloatingView
1669 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1670 if (widgets != null) {
1671 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1672 widgets.saveHierarchyState(widgetsState);
1673 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1674 } else {
1675 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1676 }
1677
Pinyao Tinga74b63a2019-07-17 23:26:06 -07001678 finishAutoCancelActionMode();
Tony Wickham49c8d292016-07-01 11:44:34 -07001679
Sunny Goyal2100c782016-08-22 16:00:03 -07001680 if (mPendingRequestArgs != null) {
1681 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1682 }
Sunny Goyaldedda052019-05-14 15:23:48 -07001683 outState.putInt(RUNTIME_STATE_PENDING_REQUEST_CODE, mPendingActivityRequestCode);
1684
Sunny Goyal2100c782016-08-22 16:00:03 -07001685 if (mPendingActivityResult != null) {
1686 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001687 }
1688
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001689 super.onSaveInstanceState(outState);
Sunny Goyalb1d7de22019-09-06 10:36:54 -07001690 mOverlayManager.onActivitySaveInstanceState(this, outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001691 }
1692
1693 @Override
1694 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001695 super.onDestroy();
Sunny Goyale84c5b82019-09-26 17:05:31 -07001696 ACTIVITY_TRACKER.onActivityDestroyed(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001697
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001698 unregisterReceiver(mScreenOffReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001699 mWorkspace.removeFolderListeners();
Sunny Goyalb1d7de22019-09-06 10:36:54 -07001700 PluginManagerWrapper.INSTANCE.get(this).removePluginListener(this);
Winson Chunge7a03942011-08-05 15:05:12 -07001701
Sunny Goyala7a5bf32020-01-05 15:35:29 +05301702 mModel.removeCallbacks(this);
Sunny Goyal623eddd2018-03-02 12:24:41 -08001703 mRotationHelper.destroy();
Sunny Goyal745bad92016-05-02 10:54:12 -07001704
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001705 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001706 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001707 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001708 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001709 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001710
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001711 TextKeyListener.getInstance().release();
Tony2917a8b2017-07-31 23:29:42 -07001712 clearPendingBinds();
Hyunyoung Song31971a32019-01-27 12:31:12 -08001713 LauncherAppState.getIDP(this).removeOnChangeListener(this);
Sunny Goyalb1d7de22019-09-06 10:36:54 -07001714
1715 mOverlayManager.onActivityDestroyed(this);
Samuel Fufa41d909f2020-03-20 16:27:09 -07001716 mUserChangedCallbackCloseable.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001717 }
1718
Sunny Goyalae502842016-06-17 08:43:56 -07001719 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1720 return mAccessibilityDelegate;
1721 }
1722
Adam Cohena9cf38f2011-05-02 15:36:58 -07001723 public DragController getDragController() {
1724 return mDragController;
1725 }
1726
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001727 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001728 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyaldedda052019-05-14 15:23:48 -07001729 if (requestCode != -1) {
1730 mPendingActivityRequestCode = requestCode;
1731 }
Sunny Goyal210e1742019-10-17 12:05:38 -07001732 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001733 }
1734
1735 @Override
Sunny Goyaldedda052019-05-14 15:23:48 -07001736 public void startIntentSenderForResult(IntentSender intent, int requestCode,
Adam Cohen173f7112015-03-27 15:14:00 -07001737 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Sunny Goyaldedda052019-05-14 15:23:48 -07001738 if (requestCode != -1) {
1739 mPendingActivityRequestCode = requestCode;
1740 }
Sunny Goyal210e1742019-10-17 12:05:38 -07001741 try {
1742 super.startIntentSenderForResult(intent, requestCode,
1743 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1744 } catch (IntentSender.SendIntentException e) {
1745 throw new ActivityNotFoundException();
Adam Cohen173f7112015-03-27 15:14:00 -07001746 }
1747 }
1748
Winson Chung70d72102011-08-12 11:24:35 -07001749 /**
1750 * Indicates that we want global search for this activity by setting the globalSearch
1751 * argument for {@link #startSearch} to true.
1752 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001753 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001754 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001755 Bundle appSearchData, boolean globalSearch) {
1756 if (appSearchData == null) {
1757 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001758 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001759 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001760
Sunny Goyal6f28e712016-09-13 14:19:29 -07001761 if (mLauncherCallbacks == null ||
1762 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1763 // Starting search from the callbacks failed. Start the default global search.
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001764 super.startSearch(initialQuery, selectInitialQuery, appSearchData, true);
Ian Parkinson047036e2014-09-01 15:40:53 +01001765 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001766
1767 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001768 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001769 }
1770
Joe Onorato9c1289c2009-08-17 11:03:03 -04001771 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001772 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001773 }
1774
Adam Cohen517a7f52014-03-01 12:12:59 -08001775 public boolean isWorkspaceLoading() {
1776 return mWorkspaceLoading;
1777 }
1778
Brian Isganitis099945b2022-01-31 18:15:00 -05001779 @Override
1780 public boolean isBindingItems() {
1781 return mWorkspaceLoading;
1782 }
1783
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001784 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001785 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001786 }
1787
Sunny Goyal04a324a2017-01-20 21:08:59 -08001788 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001789 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001790 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001791
Sunny Goyal2100c782016-08-22 16:00:03 -07001792 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001793 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001794 if (LOGD) {
1795 Log.d(TAG, "Adding widget from drop");
1796 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001797 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001798 }
1799
Sunny Goyal2100c782016-08-22 16:00:03 -07001800 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001801 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
Samuel Fufaca37b8a2019-08-19 17:04:36 -07001802 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info,
1803 REQUEST_CREATE_APPWIDGET)) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001804 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001805
Adam Cohenad4e15c2013-10-17 16:21:35 -07001806 Runnable onComplete = new Runnable() {
1807 @Override
1808 public void run() {
1809 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001810 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001811 }
1812 };
Samuel Fufaca37b8a2019-08-19 17:04:36 -07001813 completeAddAppWidget(appWidgetId, info, boundWidget,
1814 addFlowHandler.getProviderInfo(this));
Sunny Goyal6e379fc2020-03-19 16:08:14 -07001815 mWorkspace.removeExtraEmptyScreenDelayed(delay, false, onComplete);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001816 }
1817 }
Romain Guycbb89e42009-06-08 15:52:54 -07001818
Sunny Goyalefb7e842018-10-04 15:11:00 -07001819 public void addPendingItem(PendingAddItemInfo info, int container, int screenId,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001820 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001821 info.container = container;
1822 info.screenId = screenId;
1823 if (cell != null) {
1824 info.cellX = cell[0];
1825 info.cellY = cell[1];
1826 }
1827 info.spanX = spanX;
1828 info.spanY = spanY;
1829
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001830 switch (info.itemType) {
1831 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1832 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001833 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001834 break;
1835 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001836 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001837 break;
1838 default:
1839 throw new IllegalStateException("Unknown item type: " + info.itemType);
Samuel Fufaca37b8a2019-08-19 17:04:36 -07001840 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001841 }
1842
Adam Cohenfbba09b2011-07-18 21:43:05 -07001843 /**
1844 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001845 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001846 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001847 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1848 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
vadimtcf275c12020-04-15 12:43:01 -07001849 TestLogging.recordEvent(TestProtocol.SEQUENCE_MAIN, "start: processShortcutFromDrop");
Sunny Goyal782f0c92017-01-19 10:27:54 -08001850 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1851 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1852 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001853 }
1854
Adam Cohenfbba09b2011-07-18 21:43:05 -07001855 /**
1856 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001857 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001858 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001859 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001860 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001861 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001862 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001863 // In the case where we've prebound the widget, we remove it from the DragLayer
1864 if (LOGD) {
1865 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1866 }
1867 getDragLayer().removeView(hostView);
1868
Adam Cohened66b2b2012-01-23 17:28:51 -08001869 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001870 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001871
1872 // Clear the boundWidget so that it doesn't get destroyed.
1873 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001874 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001875 // In this case, we either need to start an activity to get permission to bind
1876 // the widget, or we need to start an activity to configure the widget, or both.
Pinyao Tingc7a6c292019-08-26 14:36:02 -07001877 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1878 appWidgetId = CustomWidgetManager.INSTANCE.get(this).getWidgetIdForCustomProvider(
1879 info.componentName);
Sunny Goyal952e63d2017-08-16 04:59:08 -07001880 } else {
1881 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1882 }
Adam Cohendd70d662012-10-04 16:53:44 -07001883 Bundle options = info.bindOptions;
1884
Sunny Goyalffe83f12014-08-14 17:39:34 -07001885 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1886 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001887 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001888 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001889 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001890 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001891 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001892 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001893 }
1894
Samuel Fufa746d4202020-02-02 18:55:44 -08001895 /**
1896 * Creates and adds new folder to CellLayout
1897 */
Samuel Fufa28c3e452020-02-08 16:07:16 -08001898 public FolderIcon addFolder(CellLayout layout, int container, final int screenId, int cellX,
1899 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001900 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001901
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001902 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001903 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001904
1905 // Create the view
Samuel Fufa746d4202020-02-02 18:55:44 -08001906 FolderIcon newFolder = FolderIcon.inflateFolderAndIcon(R.layout.folder_icon, this, layout,
1907 folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301908 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001909 // Force measure the new folder icon
1910 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1911 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001912 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001913 }
Romain Guycbb89e42009-06-08 15:52:54 -07001914
Tony Wickham1906cc32021-02-11 11:55:24 -08001915 @Override
1916 public Rect getFolderBoundingBox() {
1917 // We need to bound the folder to the currently visible workspace area
Andras Kloczl3a4ba0b2021-03-29 18:47:14 +02001918 return getWorkspace().getPageAreaRelativeToDragLayer();
Tony Wickham1906cc32021-02-11 11:55:24 -08001919 }
1920
1921 @Override
1922 public void updateOpenFolderPosition(int[] inOutPosition, Rect bounds, int width, int height) {
1923 int left = inOutPosition[0];
1924 int top = inOutPosition[1];
1925 DeviceProfile grid = getDeviceProfile();
1926 int distFromEdgeOfScreen = getWorkspace().getPaddingLeft();
1927 if (grid.isPhone && (grid.availableWidthPx - width) < 4 * distFromEdgeOfScreen) {
1928 // Center the folder if it is very close to being centered anyway, by virtue of
1929 // filling the majority of the viewport. ie. remove it from the uncanny valley
1930 // of centeredness.
1931 left = (grid.availableWidthPx - width) / 2;
1932 } else if (width >= bounds.width()) {
1933 // If the folder doesn't fit within the bounds, center it about the desired bounds
1934 left = bounds.left + (bounds.width() - width) / 2;
1935 }
1936 if (height >= bounds.height()) {
1937 // Folder height is greater than page height, center on page
1938 top = bounds.top + (bounds.height() - height) / 2;
1939 } else {
1940 // Folder height is less than page height, so bound it to the absolute open folder
1941 // bounds if necessary
1942 Rect folderBounds = grid.getAbsoluteOpenFolderBounds();
1943 left = Math.max(folderBounds.left, Math.min(left, folderBounds.right - width));
1944 top = Math.max(folderBounds.top, Math.min(top, folderBounds.bottom - height));
1945 }
1946 inOutPosition[0] = left;
1947 inOutPosition[1] = top;
1948 }
1949
Winsonc0b52fe2015-09-09 16:38:15 -07001950 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001951 * Unbinds the view for the specified item, and removes the item and all its children.
1952 *
1953 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001954 * @param itemInfo the {@link ItemInfo} for this view.
1955 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001956 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001957 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Pinyao Ting6a41d562022-05-10 23:47:36 +00001958 return removeItem(v, itemInfo, deleteFromDb, null);
1959 }
1960
1961 /**
1962 * Unbinds the view for the specified item, and removes the item and all its children.
1963 *
1964 * @param v the view being removed.
1965 * @param itemInfo the {@link ItemInfo} for this view.
1966 * @param deleteFromDb whether or not to delete this item from the db.
1967 * @param reason the resaon for removal.
1968 */
1969 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb,
1970 @Nullable final String reason) {
Sunny Goyal95899162019-03-27 16:03:06 -07001971 if (itemInfo instanceof WorkspaceItemInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001972 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001973 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1974 if (folderIcon instanceof FolderIcon) {
Sunny Goyal95899162019-03-27 16:03:06 -07001975 ((FolderInfo) folderIcon.getTag()).remove((WorkspaceItemInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001976 } else {
1977 mWorkspace.removeWorkspaceItem(v);
1978 }
Winsonc0b52fe2015-09-09 16:38:15 -07001979 if (deleteFromDb) {
Pinyao Ting6a41d562022-05-10 23:47:36 +00001980 getModelWriter().deleteItemFromDatabase(itemInfo, reason);
Winsonc0b52fe2015-09-09 16:38:15 -07001981 }
1982 } else if (itemInfo instanceof FolderInfo) {
1983 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001984 if (v instanceof FolderIcon) {
1985 ((FolderIcon) v).removeListeners();
1986 }
Winsonc0b52fe2015-09-09 16:38:15 -07001987 mWorkspace.removeWorkspaceItem(v);
1988 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001989 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001990 }
1991 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1992 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001993 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001994 if (deleteFromDb) {
Pinyao Ting6a41d562022-05-10 23:47:36 +00001995 getModelWriter().deleteWidgetInfo(widgetInfo, getAppWidgetHost(), reason);
Winsonc0b52fe2015-09-09 16:38:15 -07001996 }
1997 } else {
1998 return false;
1999 }
2000 return true;
2001 }
2002
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002003 @Override
2004 public boolean dispatchKeyEvent(KeyEvent event) {
vadimt6b534ee2021-08-06 15:49:02 -07002005 TestLogging.recordKeyEvent(TestProtocol.SEQUENCE_MAIN, "Key event", event);
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002006 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002007 }
2008
Joe Onorato88ec0992009-11-19 13:16:06 -08002009 @Override
vadimt9af34a72020-01-21 11:19:13 -08002010 public boolean dispatchTouchEvent(MotionEvent ev) {
Sunny Goyala306e592020-03-24 01:51:41 -07002011 switch (ev.getAction()) {
2012 case MotionEvent.ACTION_DOWN:
2013 mTouchInProgress = true;
2014 break;
2015 case MotionEvent.ACTION_UP:
Riddle Hsub30adcb2020-10-07 15:57:44 +08002016 mLastTouchUpTime = SystemClock.uptimeMillis();
Sunny Goyala306e592020-03-24 01:51:41 -07002017 // Follow through
2018 case MotionEvent.ACTION_CANCEL:
2019 mTouchInProgress = false;
2020 break;
Sunny Goyal9b180102020-03-11 10:02:29 -07002021 }
vadimtd633c9c2020-01-22 18:00:37 -08002022 TestLogging.recordMotionEvent(TestProtocol.SEQUENCE_MAIN, "Touch event", ev);
vadimt9af34a72020-01-21 11:19:13 -08002023 return super.dispatchTouchEvent(ev);
2024 }
2025
Sunny Goyala306e592020-03-24 01:51:41 -07002026 /**
2027 * Returns true if a touch interaction is in progress
2028 */
2029 public boolean isTouchInProgress() {
2030 return mTouchInProgress;
2031 }
2032
vadimt9af34a72020-01-21 11:19:13 -08002033 @Override
Joe Onorato88ec0992009-11-19 13:16:06 -08002034 public void onBackPressed() {
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08002035 if (finishAutoCancelActionMode()) {
2036 return;
2037 }
Adam Cohen9211d422014-10-07 18:14:53 -07002038
Sunny Goyal45478022015-06-08 16:52:41 -07002039 if (mDragController.isDragging()) {
2040 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002041 return;
2042 }
2043
Jon Mirandafeba90f2016-10-06 10:53:29 -07002044 // Note: There should be at most one log per method call. This is enforced implicitly
2045 // by using if-else statements.
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002046 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
Sunny Goyal3f2637f2022-05-05 12:24:08 -07002047 if (topView == null || !topView.onBackPressed()) {
2048 // Not handled by the floating view.
2049 onStateBack();
Michael Jurkaaf442092010-06-10 17:01:57 -07002050 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002051 }
2052
Sunny Goyal3f2637f2022-05-05 12:24:08 -07002053 protected void onStateBack() {
2054 mStateManager.getState().onBackPressed(this);
2055 }
2056
Tracy Zhouf20a4bd2021-04-19 21:37:08 -07002057 protected void onScreenOff() {
2058 // Reset AllApps to its initial state only if we are not in the middle of
2059 // processing a multi-step drop
2060 if (mPendingRequestArgs == null) {
2061 if (!isInState(NORMAL)) {
2062 onUiChangedWhileSleeping();
2063 }
2064 mStateManager.goToState(NORMAL);
2065 }
2066 }
2067
Sunny Goyalfe770c92016-09-27 14:38:58 -07002068 @TargetApi(Build.VERSION_CODES.M)
Sunny Goyal0b0847b2018-03-14 12:30:11 -07002069 @Override
Brian Isganitisbde3c8b2022-03-15 13:35:06 -07002070 public boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -07002071 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2072 // corresponding permission. Show the appropriate permission prompt if that
2073 // is the case.
2074 if (intent.getComponent() == null
2075 && Intent.ACTION_CALL.equals(intent.getAction())
2076 && checkSelfPermission(android.Manifest.permission.CALL_PHONE) !=
2077 PackageManager.PERMISSION_GRANTED) {
2078
2079 setWaitingForResult(PendingRequestArgs
2080 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
2081 requestPermissions(new String[]{android.Manifest.permission.CALL_PHONE},
2082 REQUEST_PERMISSION_CALL_PHONE);
2083 return true;
2084 } else {
2085 return false;
2086 }
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002087 }
2088
Hyunyoung Song46d07f72018-05-22 15:41:25 -07002089 @Override
Sunny Goyal852537f2020-07-15 17:02:16 -07002090 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Winson Chung0b70cd42019-06-17 22:34:33 -07002091 if (!hasBeenResumed()) {
2092 // Workaround an issue where the WM launch animation is clobbered when finishing the
2093 // recents animation into launcher. Defer launching the activity until Launcher is
2094 // next resumed.
Sunny Goyal852537f2020-07-15 17:02:16 -07002095 addOnResumeCallback(() -> startActivitySafely(v, intent, item));
Winson Chungc9bf6d42019-10-04 15:33:18 -07002096 if (mOnDeferredActivityLaunchCallback != null) {
2097 mOnDeferredActivityLaunchCallback.run();
2098 mOnDeferredActivityLaunchCallback = null;
2099 }
Winson Chung0b70cd42019-06-17 22:34:33 -07002100 return true;
2101 }
2102
Sunny Goyal852537f2020-07-15 17:02:16 -07002103 boolean success = super.startActivitySafely(v, intent, item);
Hyunyoung Song1241e612018-05-15 21:46:51 -07002104 if (success && v instanceof BubbleTextView) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -07002105 // This is set to the view that launched the activity that navigated the user away
2106 // from launcher. Since there is no callback for when the activity has finished
2107 // launching, enable the press state and keep this reference to reset the press
2108 // state when we return to launcher.
2109 BubbleTextView btv = (BubbleTextView) v;
2110 btv.setStayPressed(true);
Sunny Goyalb65d7662021-03-07 15:09:11 -08002111 addOnResumeCallback(() -> btv.setStayPressed(false));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002112 }
Hyunyoung Song1241e612018-05-15 21:46:51 -07002113 return success;
Michael Jurka86a720e2012-05-09 11:23:23 -07002114 }
2115
Winson Chung3d503fb2011-07-13 17:25:49 -07002116 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08002117 // TODO: Remove this method
Sunny Goyal876e4622018-11-02 13:50:40 -07002118 return mHotseat != null && (layout == mHotseat);
Winson Chung3d503fb2011-07-13 17:25:49 -07002119 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002120
Winson Chung3d503fb2011-07-13 17:25:49 -07002121 /**
2122 * Returns the CellLayout of the specified container at the specified screen.
2123 */
Sunny Goyalefb7e842018-10-04 15:11:00 -07002124 public CellLayout getCellLayout(int container, int screenId) {
Sunny Goyal876e4622018-11-02 13:50:40 -07002125 return (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)
2126 ? mHotseat : mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002127 }
2128
Michael Jurkae326f182011-11-21 14:05:46 -08002129 @Override
2130 public void onTrimMemory(int level) {
2131 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07002132 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2133 // The widget preview db can result in holding onto over
2134 // 3MB of memory for caching which isn't necessary.
2135 SQLiteDatabase.releaseMemory();
2136
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08002137 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07002138 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08002139 }
2140 }
2141
Michael Jurkad7c28052012-04-27 15:43:36 -07002142 @Override
2143 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07002144 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07002145 final List<CharSequence> text = event.getText();
2146 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07002147 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002148 // TODO: When can workspace be null?
2149 text.add(mWorkspace == null
Sunny Goyal52e25452020-07-20 12:38:01 -07002150 ? getString(R.string.home_screen)
Sunny Goyalea609262017-10-25 15:47:38 -07002151 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07002152 return result;
2153 }
2154
Michael Jurka7607c2f2013-04-03 14:33:19 -07002155 /**
Winson Chungc9bf6d42019-10-04 15:33:18 -07002156 * Persistant callback which notifies when an activity launch is deferred because the activity
2157 * was not yet resumed.
2158 */
2159 public void setOnDeferredActivityLaunchCallback(Runnable callback) {
2160 mOnDeferredActivityLaunchCallback = callback;
2161 }
2162
2163 /**
Andras Kloczl55edfe52021-05-14 12:21:30 +02002164 * Sets the next pages to bind synchronously on next bind.
2165 * @param pages should not be null.
Sunny Goyala7a5bf32020-01-05 15:35:29 +05302166 */
Andras Kloczl55edfe52021-05-14 12:21:30 +02002167 public void setPagesToBindSynchronously(@NonNull IntSet pages) {
2168 mPagesToBindSynchronously = pages;
Sunny Goyala7a5bf32020-01-05 15:35:29 +05302169 }
2170
Sunny Goyal93f878c2016-03-30 17:31:24 -07002171 @Override
Sunny Goyal12e3f1f2021-07-15 14:31:58 -07002172 public IntSet getPagesToBindSynchronously(IntArray orderedScreenIds) {
Alex Chau49354042021-08-25 13:21:58 +01002173 IntSet visibleIds;
2174 if (!mPagesToBindSynchronously.isEmpty()) {
2175 visibleIds = mPagesToBindSynchronously;
2176 } else if (!mWorkspaceLoading) {
2177 visibleIds = mWorkspace.getCurrentPageScreenIds();
2178 } else {
2179 // If workspace binding is still in progress, getCurrentPageScreenIds won't be accurate,
2180 // and we should use mSynchronouslyBoundPages that's set during initial binding.
2181 visibleIds = mSynchronouslyBoundPages;
2182 }
Sunny Goyal12e3f1f2021-07-15 14:31:58 -07002183 IntArray actualIds = new IntArray();
2184
Sunny Goyal12e3f1f2021-07-15 14:31:58 -07002185 IntSet result = new IntSet();
2186 if (visibleIds.isEmpty()) {
Schneider Victor-tulias00213382021-09-27 13:41:10 -07002187 if (TestProtocol.sDebugTracing) {
2188 Log.d(TestProtocol.NULL_INT_SET, "getPagesToBindSynchronously (1): "
2189 + result);
2190 }
Sunny Goyal12e3f1f2021-07-15 14:31:58 -07002191 return result;
2192 }
2193 for (int id : orderedScreenIds.toArray()) {
Alex Chau1e448462021-08-13 21:48:35 +01002194 actualIds.add(id);
Sunny Goyal12e3f1f2021-07-15 14:31:58 -07002195 }
2196 int firstId = visibleIds.getArray().get(0);
Steven Ng4b346102021-09-14 19:17:09 +01002197 int pairId = mWorkspace.getScreenPair(firstId);
Alex Chau62f129b2021-09-03 16:54:27 +01002198 // Double check that actual screenIds contains the visibleId, as empty screens are hidden
2199 // in single panel.
Sunny Goyal12e3f1f2021-07-15 14:31:58 -07002200 if (actualIds.contains(firstId)) {
2201 result.add(firstId);
Alex Chau62f129b2021-09-03 16:54:27 +01002202 if (mDeviceProfile.isTwoPanels && actualIds.contains(pairId)) {
2203 result.add(pairId);
Sunny Goyal12e3f1f2021-07-15 14:31:58 -07002204 }
Alex Chau62f129b2021-09-03 16:54:27 +01002205 } else if (LauncherAppState.getIDP(this).supportedProfiles.stream().anyMatch(
2206 deviceProfile -> deviceProfile.isTwoPanels) && actualIds.contains(pairId)) {
2207 // Add the right panel if left panel is hidden when switching display, due to empty
2208 // pages being hidden in single panel.
2209 result.add(pairId);
Sunny Goyal12e3f1f2021-07-15 14:31:58 -07002210 }
Schneider Victor-tulias00213382021-09-27 13:41:10 -07002211 if (TestProtocol.sDebugTracing) {
2212 Log.d(TestProtocol.NULL_INT_SET, "getPagesToBindSynchronously (2): "
2213 + result);
2214 }
Sunny Goyal12e3f1f2021-07-15 14:31:58 -07002215 return result;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002216 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002217
Joe Onorato9c1289c2009-08-17 11:03:03 -04002218 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07002219 * Clear any pending bind callbacks. This is called when is loader is planning to
2220 * perform a full rebind from scratch.
2221 */
2222 @Override
2223 public void clearPendingBinds() {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002224 if (mPendingExecutor != null) {
Sunny Goyal4a48a982021-06-17 15:15:46 -07002225 mPendingExecutor.cancel();
Sunny Goyal527c7d32015-08-28 15:19:36 -07002226 mPendingExecutor = null;
Sunny Goyal6b6d0a32019-08-05 17:19:32 -07002227
2228 // We might have set this flag previously and forgot to clear it.
2229 mAppsView.getAppsStore()
2230 .disableDeferUpdatesSilently(AllAppsStore.DEFER_UPDATES_NEXT_DRAW);
Sunny Goyal527c7d32015-08-28 15:19:36 -07002231 }
2232 }
2233
2234 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002235 * Refreshes the shortcuts shown on the workspace.
2236 *
2237 * Implementation of the method from LauncherModel.Callbacks.
2238 */
2239 public void startBinding() {
Winson Chung5cc62c72019-10-16 11:32:41 -07002240 Object traceToken = TraceHelper.INSTANCE.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002241 // Floating panels (except the full widget sheet) are associated with individual icons. If
2242 // we are starting a fresh bind, close all such panels as all the icons are about
2243 // to go away.
Pinyao Tinga74b63a2019-07-17 23:26:06 -07002244 AbstractFloatingView.closeOpenViews(this, true, TYPE_ALL & ~TYPE_REBIND_SAFE);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08002245
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002246 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08002247
Winson Chungd64d1762013-08-20 14:37:16 -07002248 // Clear the workspace because it's going to be rebound
Sunny Goyal2db53422019-03-01 16:06:12 -08002249 mDragController.cancelDrag();
2250
Adam Cohendf035382011-04-11 17:22:04 -07002251 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07002252 mWorkspace.removeAllWorkspaceScreens();
Sunny Goyalc11fac32018-02-27 19:20:15 -08002253 mAppWidgetHost.clearViews();
Winson Chungd64d1762013-08-20 14:37:16 -07002254
Winson Chung3d503fb2011-07-13 17:25:49 -07002255 if (mHotseat != null) {
Sunny Goyalc4d32012020-04-03 17:10:11 -07002256 mHotseat.resetLayout(getDeviceProfile().isVerticalBarLayout());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002257 }
Winson Chung5cc62c72019-10-16 11:32:41 -07002258 TraceHelper.INSTANCE.endSection(traceToken);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002259 }
2260
Adam Cohendcd297f2013-06-18 13:13:40 -07002261 @Override
Sunny Goyalefb7e842018-10-04 15:11:00 -07002262 public void bindScreens(IntArray orderedScreenIds) {
Alex Chau1e448462021-08-13 21:48:35 +01002263 int firstScreenPosition = 0;
Jon Miranda7143ba62019-03-15 09:00:05 -07002264 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
Andras Kloczldbf577a2021-05-26 15:02:52 +02002265 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != firstScreenPosition) {
Sunny Goyalefb7e842018-10-04 15:11:00 -07002266 orderedScreenIds.removeValue(Workspace.FIRST_SCREEN_ID);
Andras Kloczldbf577a2021-05-26 15:02:52 +02002267 orderedScreenIds.add(firstScreenPosition, Workspace.FIRST_SCREEN_ID);
Jon Miranda7143ba62019-03-15 09:00:05 -07002268 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002269 // If there are no screens, we need to have an empty screen
Andras Kloczl408a54f2021-08-28 00:59:08 +01002270 mWorkspace.addExtraEmptyScreens();
Adam Cohen5084cba2013-09-03 12:01:16 -07002271 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002272 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07002273
Winsonc7d2e832016-07-28 12:24:55 -07002274 // After we have added all the screens, if the wallpaper was locked to the default state,
2275 // then notify to indicate that it can be released and a proper wallpaper offset can be
2276 // computed before the next layout
2277 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07002278 }
2279
Sunny Goyalefb7e842018-10-04 15:11:00 -07002280 private void bindAddScreens(IntArray orderedScreenIds) {
Andras Kloczl8c574de2021-08-17 23:12:17 +02002281 if (mDeviceProfile.isTwoPanels) {
2282 // Some empty pages might have been removed while the phone was in a single panel
2283 // mode, so we want to add those empty pages back.
2284 IntSet screenIds = IntSet.wrap(orderedScreenIds);
Steven Ng4b346102021-09-14 19:17:09 +01002285 orderedScreenIds.forEach(screenId -> screenIds.add(mWorkspace.getScreenPair(screenId)));
Andras Kloczl8c574de2021-08-17 23:12:17 +02002286 orderedScreenIds = screenIds.getArray();
2287 }
2288
Adam Cohendcd297f2013-06-18 13:13:40 -07002289 int count = orderedScreenIds.size();
2290 for (int i = 0; i < count; i++) {
Sunny Goyalefb7e842018-10-04 15:11:00 -07002291 int screenId = orderedScreenIds.get(i);
Andras Kloczldbf577a2021-05-26 15:02:52 +02002292 if (FeatureFlags.QSB_ON_FIRST_SCREEN && screenId == Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002293 // No need to bind the first screen, as its always bound.
Andras Kloczldbf577a2021-05-26 15:02:52 +02002294 continue;
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002295 }
Andras Kloczldbf577a2021-05-26 15:02:52 +02002296 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
Adam Cohendcd297f2013-06-18 13:13:40 -07002297 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002298 }
2299
Sunny Goyalb23980c2017-08-17 07:45:25 -07002300 @Override
Tony Wickham6a71a5b2018-08-21 11:40:23 -07002301 public void preAddApps() {
2302 // If there's an undo snackbar, force it to complete to ensure empty screens are removed
2303 // before trying to add new items.
2304 mModelWriter.commitDelete();
2305 AbstractFloatingView snackbar = AbstractFloatingView.getOpenView(this, TYPE_SNACKBAR);
2306 if (snackbar != null) {
2307 snackbar.post(() -> snackbar.close(true));
2308 }
2309 }
2310
2311 @Override
Sunny Goyalefb7e842018-10-04 15:11:00 -07002312 public void bindAppsAdded(IntArray newScreens, ArrayList<ItemInfo> addNotAnimated,
Sunny Goyal29947f02017-12-18 13:49:44 -08002313 ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07002314 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08002315 if (newScreens != null) {
Alex Chau817e0fa2021-09-08 20:46:32 +01002316 // newScreens can contain an empty right panel that is already bound, but not known
2317 // by BgDataModel.
2318 newScreens.removeAllValues(mWorkspace.mScreenOrder);
Adam Cohen76a47a12014-02-05 11:47:43 -08002319 bindAddScreens(newScreens);
2320 }
Winson Chungd64d1762013-08-20 14:37:16 -07002321
2322 // We add the items without animation on non-visible pages, and with
2323 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08002324 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002325 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002326 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002327 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002328 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07002329 }
Winson Chungc58497e2013-09-03 17:48:37 -07002330
Winson Chung87412982013-10-03 18:34:14 -07002331 // Remove the extra empty screen
Sunny Goyal6e379fc2020-03-19 16:08:14 -07002332 mWorkspace.removeExtraEmptyScreen(false);
Winson Chungd64d1762013-08-20 14:37:16 -07002333 }
2334
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002335 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002336 * Bind the items start-end from the list.
2337 *
2338 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002339 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07002340 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07002341 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Schneider Victor-tulias68706eb2021-04-29 15:24:54 -07002342 bindItems(items, forceAnimateIcons, /* focusFirstItemForAccessibility= */ false);
2343 }
2344
2345
2346 /**
2347 * Bind the items start-end from the list.
2348 *
2349 * Implementation of the method from LauncherModel.Callbacks.
2350 *
2351 * @param focusFirstItemForAccessibility true iff the first item to be added to the workspace
2352 * should be focused for accessibility.
2353 */
2354 public void bindItems(
2355 final List<ItemInfo> items,
2356 final boolean forceAnimateIcons,
2357 final boolean focusFirstItemForAccessibility) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002358 // Get the list of added items and intersect them with the set of items here
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002359 final Collection<Animator> bounceAnims = new ArrayList<>();
Schneider Victor-tulias68fb4162021-09-16 13:24:01 -07002360 boolean canAnimatePageChange = canAnimatePageChange();
Shikha Malhotraf78da1b2022-04-11 10:23:18 +00002361 Workspace<?> workspace = mWorkspace;
Sunny Goyalefb7e842018-10-04 15:11:00 -07002362 int newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07002363 int end = items.size();
Schneider Victor-tulias68706eb2021-04-29 15:24:54 -07002364 View newView = null;
Sunny Goyalb23980c2017-08-17 07:45:25 -07002365 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002366 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002367
2368 // Short circuit if we are loading dock items for a configuration which has no dock
2369 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2370 mHotseat == null) {
2371 continue;
2372 }
2373
Sunny Goyal639e9062015-08-19 19:17:06 -07002374 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002375 switch (item.itemType) {
2376 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2377 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08002378 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Sunny Goyal95899162019-03-27 16:03:06 -07002379 WorkspaceItemInfo info = (WorkspaceItemInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07002380 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002381 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002382 }
2383 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Tracy Zhou4d7b2442019-12-09 13:42:57 -08002384 view = FolderIcon.inflateFolderAndIcon(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002385 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002386 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002387 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002388 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07002389 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2390 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07002391 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002392 if (view == null) {
2393 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002394 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08002395 break;
2396 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002397 default:
2398 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002399 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002400
Samuel Fufaca37b8a2019-08-19 17:04:36 -07002401 /*
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002402 * Remove colliding items.
2403 */
2404 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
2405 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
2406 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
2407 View v = cl.getChildAt(item.cellX, item.cellY);
Jon Miranda0aff1152021-11-05 16:36:07 -07002408 if (v == null) {
2409 Log.e(TAG, "bindItems failed when removing colliding item=" + item);
2410 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002411 Object tag = v.getTag();
2412 String desc = "Collision while binding workspace item: " + item
2413 + ". Collides with " + tag;
Zak Cohen3eeb41d2020-02-14 14:15:13 -08002414 if (FeatureFlags.IS_STUDIO_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002415 throw (new RuntimeException(desc));
2416 } else {
Pinyao Ting6a41d562022-05-10 23:47:36 +00002417 getModelWriter().deleteItemFromDatabase(item, desc);
Jon Miranda701774d2022-02-10 11:09:04 -08002418 if (TestProtocol.sDebugTracing) {
2419 Log.d(TestProtocol.MISSING_PROMISE_ICON,
2420 TAG + "bindItems failed for item=" + item);
2421 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002422 continue;
2423 }
2424 }
2425 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302426 workspace.addInScreenFromBind(view, item);
Schneider Victor-tulias68fb4162021-09-16 13:24:01 -07002427 if (forceAnimateIcons) {
Sunny Goyal639e9062015-08-19 19:17:06 -07002428 // Animate all the applications up now
2429 view.setAlpha(0f);
2430 view.setScaleX(0f);
2431 view.setScaleY(0f);
2432 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08002433 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07002434 }
Schneider Victor-tulias68706eb2021-04-29 15:24:54 -07002435
2436 if (newView == null) {
2437 newView = view;
2438 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002439 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07002440
Schneider Victor-tulias68706eb2021-04-29 15:24:54 -07002441 View viewToFocus = newView;
2442 // Animate to the correct pager
Schneider Victor-tulias68fb4162021-09-16 13:24:01 -07002443 if (forceAnimateIcons && newItemsScreenId > -1) {
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002444 AnimatorSet anim = new AnimatorSet();
2445 anim.playTogether(bounceAnims);
Schneider Victor-tulias68706eb2021-04-29 15:24:54 -07002446 if (focusFirstItemForAccessibility && viewToFocus != null) {
2447 anim.addListener(new AnimatorListenerAdapter() {
2448 @Override
2449 public void onAnimationEnd(Animator animation) {
2450 viewToFocus.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
2451 }
2452 });
2453 }
Jon Miranda64eb8ea2018-03-29 11:43:58 -07002454
Sunny Goyalefb7e842018-10-04 15:11:00 -07002455 int currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002456 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
2457 final Runnable startBounceAnimRunnable = anim::start;
2458
Schneider Victor-tulias68fb4162021-09-16 13:24:01 -07002459 if (canAnimatePageChange && newItemsScreenId != currentScreenId) {
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002460 // We post the animation slightly delayed to prevent slowdowns
2461 // when we are loading right after we return to launcher.
2462 mWorkspace.postDelayed(new Runnable() {
2463 public void run() {
2464 if (mWorkspace != null) {
Andy Wickham65ced1b2020-03-07 02:14:19 +00002465 closeOpenViews(false);
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002466
2467 mWorkspace.snapToPage(newScreenIndex);
2468 mWorkspace.postDelayed(startBounceAnimRunnable,
2469 NEW_APPS_ANIMATION_DELAY);
Winson Chung94d67682013-09-25 16:29:40 -07002470 }
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002471 }
2472 }, NEW_APPS_PAGE_MOVE_DELAY);
2473 } else {
2474 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07002475 }
Schneider Victor-tulias68706eb2021-04-29 15:24:54 -07002476 } else if (focusFirstItemForAccessibility && viewToFocus != null) {
2477 viewToFocus.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
Winson Chung64359a52013-07-08 17:17:08 -07002478 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002479 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002480 }
2481
Joe Onorato9c1289c2009-08-17 11:03:03 -04002482 /**
2483 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002484 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07002485 public void bindAppWidget(LauncherAppWidgetInfo item) {
2486 View view = inflateAppWidget(item);
2487 if (view != null) {
2488 mWorkspace.addInScreen(view, item);
2489 mWorkspace.requestLayout();
2490 }
2491 }
2492
2493 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Samuel Fufaca37b8a2019-08-19 17:04:36 -07002494 if (item.hasOptionFlag(LauncherAppWidgetInfo.OPTION_SEARCH_WIDGET)) {
2495 item.providerName = QsbContainerView.getSearchComponentName(this);
2496 if (item.providerName == null) {
Pinyao Ting6a41d562022-05-10 23:47:36 +00002497 getModelWriter().deleteItemFromDatabase(item,
2498 "search widget removed because search component cannot be found");
Samuel Fufaca37b8a2019-08-19 17:04:36 -07002499 return null;
2500 }
2501 }
Sunny Goyal17c72fb2019-10-14 14:06:38 -07002502 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002503 if (mIsSafeModeEnabled) {
Sunny Goyal17c72fb2019-10-14 14:06:38 -07002504 view = new PendingAppWidgetHostView(this, item, mIconCache, true);
Sunny Goyald5462aa2016-11-22 16:52:39 +05302505 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002506 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002507 }
2508
Winson Chung5cc62c72019-10-16 11:32:41 -07002509 Object traceToken = TraceHelper.INSTANCE.beginSection("BIND_WIDGET_id=" + item.appWidgetId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002510
Sunny Goyal17c72fb2019-10-14 14:06:38 -07002511 try {
2512 final LauncherAppWidgetProviderInfo appWidgetInfo;
Steven Ngacd9d792021-06-25 11:51:44 +01002513 String removalReason = "";
Adam Cohen59400422014-03-05 18:07:04 -08002514
Sunny Goyal17c72fb2019-10-14 14:06:38 -07002515 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2516 // If the provider is not ready, bind as a pending widget.
2517 appWidgetInfo = null;
Steven Ngacd9d792021-06-25 11:51:44 +01002518 removalReason = "the provider isn't ready.";
Sunny Goyal17c72fb2019-10-14 14:06:38 -07002519 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2520 // The widget id is not valid. Try to find the widget based on the provider info.
2521 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
Pinyao Tingbbb9a9c2021-05-11 15:42:54 -07002522 if (appWidgetInfo == null) {
2523 if (WidgetsModel.GO_DISABLE_WIDGETS) {
Steven Ngacd9d792021-06-25 11:51:44 +01002524 removalReason = "widgets are disabled on go device.";
Pinyao Tingbbb9a9c2021-05-11 15:42:54 -07002525 } else {
Steven Ngacd9d792021-06-25 11:51:44 +01002526 removalReason =
2527 "WidgetManagerHelper cannot find a provider from provider info.";
Pinyao Tingbbb9a9c2021-05-11 15:42:54 -07002528 }
2529 }
Sunny Goyal17c72fb2019-10-14 14:06:38 -07002530 } else {
2531 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
Pinyao Tingbbb9a9c2021-05-11 15:42:54 -07002532 if (appWidgetInfo == null) {
2533 if (item.appWidgetId <= LauncherAppWidgetInfo.CUSTOM_WIDGET_ID) {
Steven Ngacd9d792021-06-25 11:51:44 +01002534 removalReason =
2535 "CustomWidgetManager cannot find provider from that widget id.";
Pinyao Tingbbb9a9c2021-05-11 15:42:54 -07002536 } else {
Steven Ngacd9d792021-06-25 11:51:44 +01002537 removalReason = "AppWidgetManager cannot find provider for that widget id."
2538 + " It could be because AppWidgetService is not available, or the"
Pinyao Tingbbb9a9c2021-05-11 15:42:54 -07002539 + " appWidgetId has not been bound to a the provider yet, or you"
2540 + " don't have access to that appWidgetId.";
2541 }
2542 }
Sunny Goyalff572272014-07-23 13:58:07 -07002543 }
Sunny Goyalff572272014-07-23 13:58:07 -07002544
Sunny Goyal17c72fb2019-10-14 14:06:38 -07002545 // If the provider is ready, but the width is not yet restored, try to restore it.
2546 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
2547 && (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
2548 if (appWidgetInfo == null) {
Pinyao Ting6a41d562022-05-10 23:47:36 +00002549 getModelWriter().deleteItemFromDatabase(item,
2550 "Removing restored widget: id=" + item.appWidgetId
Pinyao Tingbbb9a9c2021-05-11 15:42:54 -07002551 + " belongs to component " + item.providerName + " user " + item.user
Steven Ngacd9d792021-06-25 11:51:44 +01002552 + ", as the provider is null and " + removalReason);
Sunny Goyal17c72fb2019-10-14 14:06:38 -07002553 return null;
2554 }
Sunny Goyalff572272014-07-23 13:58:07 -07002555
Sunny Goyal17c72fb2019-10-14 14:06:38 -07002556 // If we do not have a valid id, try to bind an id.
2557 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2558 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2559 // Id has not been allocated yet. Allocate a new id.
2560 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
2561 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyal86df1382016-08-10 15:03:22 -07002562
Sunny Goyal17c72fb2019-10-14 14:06:38 -07002563 // Also try to bind the widget. If the bind fails, the user will be shown
2564 // a click to setup UI, which will ask for the bind permission.
Yogisha Dixit658c5da2021-05-24 23:23:15 +01002565 PendingAddWidgetInfo pendingInfo =
2566 new PendingAddWidgetInfo(appWidgetInfo, item.sourceContainer);
Sunny Goyal17c72fb2019-10-14 14:06:38 -07002567 pendingInfo.spanX = item.spanX;
2568 pendingInfo.spanY = item.spanY;
2569 pendingInfo.minSpanX = item.minSpanX;
2570 pendingInfo.minSpanY = item.minSpanY;
Sunny Goyal19ff7282021-04-22 10:12:54 -07002571 Bundle options = pendingInfo.getDefaultSizeOptions(this);
Sunny Goyal17c72fb2019-10-14 14:06:38 -07002572
2573 boolean isDirectConfig =
2574 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2575 if (isDirectConfig && item.bindOptions != null) {
2576 Bundle newOptions = item.bindOptions.getExtras();
2577 if (options != null) {
2578 newOptions.putAll(options);
2579 }
2580 options = newOptions;
Sunny Goyal86df1382016-08-10 15:03:22 -07002581 }
Sunny Goyal17c72fb2019-10-14 14:06:38 -07002582 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2583 item.appWidgetId, appWidgetInfo, options);
2584
2585 // We tried to bind once. If we were not able to bind, we would need to
2586 // go through the permission dialog, which means we cannot skip the config
2587 // activity.
2588 item.bindOptions = null;
2589 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2590
2591 // Bind succeeded
2592 if (success) {
2593 // If the widget has a configure activity, it is still needs to set it
2594 // up, otherwise the widget is ready to go.
2595 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
2596 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2597 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
2598 }
2599
2600 getModelWriter().updateItemInDatabase(item);
Sunny Goyal86df1382016-08-10 15:03:22 -07002601 }
Sunny Goyal17c72fb2019-10-14 14:06:38 -07002602 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
2603 && (appWidgetInfo.configure == null)) {
2604 // The widget was marked as UI not ready, but there is no configure activity to
2605 // update the UI.
2606 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002607 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002608 }
Samuel Fufa9151c012020-02-24 17:06:28 -08002609 else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
2610 && appWidgetInfo.configure != null) {
2611 if (mAppWidgetManager.isAppWidgetRestored(item.appWidgetId)) {
2612 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
2613 getModelWriter().updateItemInDatabase(item);
2614 }
2615 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002616 }
2617
Sunny Goyal17c72fb2019-10-14 14:06:38 -07002618 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2619 // Verify that we own the widget
2620 if (appWidgetInfo == null) {
Pinyao Ting0d97f8d2020-12-04 15:12:26 -08002621 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Pinyao Ting6a41d562022-05-10 23:47:36 +00002622 getModelWriter().deleteWidgetInfo(item, getAppWidgetHost(), removalReason);
Sunny Goyal17c72fb2019-10-14 14:06:38 -07002623 return null;
2624 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002625
Sunny Goyal17c72fb2019-10-14 14:06:38 -07002626 item.minSpanX = appWidgetInfo.minSpanX;
2627 item.minSpanY = appWidgetInfo.minSpanY;
2628 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Samuel Fufa9151c012020-02-24 17:06:28 -08002629 } else if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)
2630 && appWidgetInfo != null) {
2631 mAppWidgetHost.addPendingView(item.appWidgetId,
2632 new PendingAppWidgetHostView(this, item, mIconCache, false));
2633 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal17c72fb2019-10-14 14:06:38 -07002634 } else {
2635 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
2636 }
2637 prepareAppWidget(view, item);
2638 } finally {
Winson Chung5cc62c72019-10-16 11:32:41 -07002639 TraceHelper.INSTANCE.endSection(traceToken);
Sunny Goyal17c72fb2019-10-14 14:06:38 -07002640 }
2641
Sunny Goyalb23980c2017-08-17 07:45:25 -07002642 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002643 }
2644
Sunny Goyalff572272014-07-23 13:58:07 -07002645 /**
2646 * Restores a pending widget.
2647 *
2648 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002649 */
Sunny Goyald478c832016-04-01 12:04:16 -07002650 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002651 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2652 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2653 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002654 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002655 }
2656
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002657 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002658 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002659 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2660 info.pendingItemInfo = null;
2661 }
Sunny Goyalff572272014-07-23 13:58:07 -07002662
Sunny Goyalba47faa2017-10-04 16:50:57 -07002663 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002664 view.reInflate();
Sunny Goyalba47faa2017-10-04 16:50:57 -07002665 }
2666
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002667 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002668 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002669 }
2670
Sunny Goyal527c7d32015-08-28 15:19:36 -07002671 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2672 if (mPendingExecutor == executor) {
2673 mPendingExecutor = null;
2674 }
2675 }
2676
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002677 @Override
Schneider Victor-tulias927d6dc2021-08-26 11:56:47 -07002678 @TargetApi(Build.VERSION_CODES.S)
Sunny Goyal4a48a982021-06-17 15:15:46 -07002679 public void onInitialBindComplete(IntSet boundPages, RunnableList pendingTasks) {
2680 mSynchronouslyBoundPages = boundPages;
Sunny Goyal4a48a982021-06-17 15:15:46 -07002681 mPagesToBindSynchronously = new IntSet();
2682
2683 clearPendingBinds();
2684 ViewOnDrawExecutor executor = new ViewOnDrawExecutor(pendingTasks);
2685 mPendingExecutor = executor;
2686 if (!isInState(ALL_APPS)) {
2687 mAppsView.getAppsStore().enableDeferUpdates(AllAppsStore.DEFER_UPDATES_NEXT_DRAW);
2688 pendingTasks.add(() -> mAppsView.getAppsStore().disableDeferUpdates(
2689 AllAppsStore.DEFER_UPDATES_NEXT_DRAW));
2690 }
2691
Sunny Goyal7b8d63f2022-04-14 16:37:32 -07002692 if (mOnInitialBindListener != null) {
2693 getRootView().getViewTreeObserver().removeOnPreDrawListener(mOnInitialBindListener);
2694 mOnInitialBindListener = null;
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002695 }
Sunny Goyal7b8d63f2022-04-14 16:37:32 -07002696
2697 executor.onLoadAnimationCompleted();
Sunny Goyal4a48a982021-06-17 15:15:46 -07002698 executor.attachTo(this);
Schneider Victor-tulias927d6dc2021-08-26 11:56:47 -07002699 if (Utilities.ATLEAST_S) {
2700 Trace.endAsyncSection(DISPLAY_WORKSPACE_TRACE_METHOD_NAME,
2701 DISPLAY_WORKSPACE_TRACE_COOKIE);
2702 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002703 }
2704
Joe Onorato9c1289c2009-08-17 11:03:03 -04002705 /**
2706 * Callback saying that there aren't any more items to bind.
2707 *
2708 * Implementation of the method from LauncherModel.Callbacks.
2709 */
Andras Kloczl55edfe52021-05-14 12:21:30 +02002710 public void finishBindingItems(IntSet pagesBoundFirst) {
Winson Chung5cc62c72019-10-16 11:32:41 -07002711 Object traceToken = TraceHelper.INSTANCE.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002712 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002713
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002714 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002715
Sunny Goyal2100c782016-08-22 16:00:03 -07002716 if (mPendingActivityResult != null) {
2717 handleActivityResult(mPendingActivityResult.requestCode,
2718 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2719 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002720 }
2721
Andras Kloczl55edfe52021-05-14 12:21:30 +02002722 int currentPage = pagesBoundFirst != null && !pagesBoundFirst.isEmpty()
Sunny Goyal12e3f1f2021-07-15 14:31:58 -07002723 ? mWorkspace.getPageIndexForScreenId(pagesBoundFirst.getArray().get(0))
2724 : PagedView.INVALID_PAGE;
Tonyf80e8932018-12-21 11:58:04 -08002725 // When undoing the removal of the last item on a page, return to that page.
Tony Wickham03f27012019-04-25 14:22:54 -07002726 // Since we are just resetting the current page without user interaction,
2727 // override the previous page so we don't log the page switch.
Andras Kloczl55edfe52021-05-14 12:21:30 +02002728 mWorkspace.setCurrentPage(currentPage, currentPage /* overridePrevPage */);
2729 mPagesToBindSynchronously = new IntSet();
Tony Wickham6a71a5b2018-08-21 11:40:23 -07002730
Sunny Goyalac6f69f2019-08-16 11:59:55 -07002731 // Cache one page worth of icons
2732 getViewCache().setCacheSize(R.layout.folder_application,
2733 mDeviceProfile.inv.numFolderColumns * mDeviceProfile.inv.numFolderRows);
2734 getViewCache().setCacheSize(R.layout.folder_page, 2);
2735
Winson Chung5cc62c72019-10-16 11:32:41 -07002736 TraceHelper.INSTANCE.endSection(traceToken);
Winson Chungf0c6ae02012-03-21 16:10:31 -07002737 }
2738
Schneider Victor-tulias68fb4162021-09-16 13:24:01 -07002739 private boolean canAnimatePageChange() {
Sunny Goyal9b180102020-03-11 10:02:29 -07002740 if (mDragController.isDragging()) {
2741 return false;
2742 } else {
Riddle Hsu979da642020-09-22 21:52:40 +08002743 return (SystemClock.uptimeMillis() - mLastTouchUpTime)
Sunny Goyal9b180102020-03-11 10:02:29 -07002744 > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
2745 }
Winson Chunga2413752012-04-03 14:22:34 -07002746 }
2747
Jon Mirandac4a99bf2021-11-04 16:53:45 -07002748 /**
2749 * Similar to {@link #getFirstMatch} but optimized to finding a suitable view for the app close
2750 * animation.
2751 *
2752 * @param preferredItemId The id of the preferred item to match to if it exists.
2753 * @param packageName The package name of the app to match.
2754 * @param user The user of the app to match.
Jon Mirandab42e1242022-03-08 16:37:35 -08002755 * @param supportsAllAppsState If true and we are in All Apps state, looks for view in All Apps.
2756 * Else we only looks on the workspace.
Jon Mirandac4a99bf2021-11-04 16:53:45 -07002757 */
Jon Mirandaf58db152022-06-02 16:06:37 -07002758 public @Nullable View getFirstMatchForAppClose(int preferredItemId, String packageName,
2759 UserHandle user, boolean supportsAllAppsState) {
Sunny Goyal32084d42022-05-02 10:00:07 -07002760 final Predicate<ItemInfo> preferredItem = info ->
Jon Mirandac4a99bf2021-11-04 16:53:45 -07002761 info != null && info.id == preferredItemId;
Sunny Goyal32084d42022-05-02 10:00:07 -07002762 final Predicate<ItemInfo> packageAndUserAndApp = info ->
Jon Mirandac4a99bf2021-11-04 16:53:45 -07002763 info != null
2764 && info.itemType == ITEM_TYPE_APPLICATION
2765 && info.user.equals(user)
2766 && info.getTargetComponent() != null
2767 && TextUtils.equals(info.getTargetComponent().getPackageName(),
2768 packageName);
2769
Jon Mirandab42e1242022-03-08 16:37:35 -08002770 if (supportsAllAppsState && isInState(LauncherState.ALL_APPS)) {
Jon Mirandaf58db152022-06-02 16:06:37 -07002771 AllAppsRecyclerView activeRecyclerView = mAppsView.getActiveRecyclerView();
2772 View v = getFirstMatch(Collections.singletonList(activeRecyclerView),
Jon Mirandac4a99bf2021-11-04 16:53:45 -07002773 preferredItem, packageAndUserAndApp);
Jon Mirandaf58db152022-06-02 16:06:37 -07002774
Jon Mirandaf26bdcb2022-06-09 16:09:30 -07002775 if (v != null && activeRecyclerView.getCurrentScrollY() > 0) {
Jon Mirandaf58db152022-06-02 16:06:37 -07002776 RectF locationBounds = new RectF();
2777 FloatingIconView.getLocationBoundsForView(this, v, false, locationBounds,
2778 new Rect());
2779 if (locationBounds.top < mAppsView.getHeaderBottom()) {
2780 // Icon is covered by scrim, return null to play fallback animation.
2781 return null;
2782 }
2783 }
2784
2785 return v;
Jon Mirandac4a99bf2021-11-04 16:53:45 -07002786 } else {
2787 List<ViewGroup> containers = new ArrayList<>(mWorkspace.getPanelCount() + 1);
2788 containers.add(mWorkspace.getHotseat().getShortcutsAndWidgets());
2789 mWorkspace.forEachVisiblePage(page
2790 -> containers.add(((CellLayout) page).getShortcutsAndWidgets()));
2791
2792 // Order: Preferred item by itself or in folder, then by matching package/user
Jon Miranda78d0d4f2022-01-24 18:00:04 -08002793 return getFirstMatch(containers, preferredItem, forFolderMatch(preferredItem),
2794 packageAndUserAndApp, forFolderMatch(packageAndUserAndApp));
Jon Mirandac4a99bf2021-11-04 16:53:45 -07002795 }
2796 }
2797
2798 /**
2799 * Finds the first view matching the ordered operators across the given viewgroups in order.
2800 * @param containers List of ViewGroups to scan, in order of preference.
2801 * @param operators List of operators, in order starting from best matching operator.
2802 */
Jon Mirandaf26bdcb2022-06-09 16:09:30 -07002803 @Nullable
Jon Mirandac4a99bf2021-11-04 16:53:45 -07002804 private static View getFirstMatch(Iterable<ViewGroup> containers,
Sunny Goyal32084d42022-05-02 10:00:07 -07002805 final Predicate<ItemInfo>... operators) {
2806 for (Predicate<ItemInfo> operator : operators) {
Jon Mirandac4a99bf2021-11-04 16:53:45 -07002807 for (ViewGroup container : containers) {
2808 View match = mapOverViewGroup(container, operator);
2809 if (match != null) {
2810 return match;
2811 }
2812 }
2813 }
2814 return null;
2815 }
2816
2817 /**
2818 * Returns the first view matching the operator in the given ViewGroups, or null if none.
2819 * Forward iteration matters.
2820 */
Jon Mirandaf26bdcb2022-06-09 16:09:30 -07002821 @Nullable
Sunny Goyal32084d42022-05-02 10:00:07 -07002822 private static View mapOverViewGroup(ViewGroup container, Predicate<ItemInfo> op) {
Jon Mirandac4a99bf2021-11-04 16:53:45 -07002823 final int itemCount = container.getChildCount();
2824 for (int itemIdx = 0; itemIdx < itemCount; itemIdx++) {
2825 View item = container.getChildAt(itemIdx);
Sunny Goyal32084d42022-05-02 10:00:07 -07002826 if (op.test((ItemInfo) item.getTag())) {
Jon Mirandac4a99bf2021-11-04 16:53:45 -07002827 return item;
2828 }
2829 }
2830 return null;
2831 }
2832
Winson Chungc9168342013-06-26 14:54:55 -07002833 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002834 ValueAnimator bounceAnim = new PropertyListBuilder().alpha(1).scale(1).build(v)
Sunny Goyal60e68c92020-08-12 13:59:27 -07002835 .setDuration(ItemInstallQueue.NEW_SHORTCUT_BOUNCE_DURATION);
2836 bounceAnim.setStartDelay(i * ItemInstallQueue.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002837 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002838 return bounceAnim;
2839 }
2840
Pinyao Tingc9074272019-11-11 11:51:44 -08002841 private void announceForAccessibility(@StringRes int stringResId) {
2842 getDragLayer().announceForAccessibility(getString(stringResId));
2843 }
2844
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002845 /**
Josh Tsuji231c9222022-01-11 20:43:59 -05002846 * Informs us that the overlay (-1 screen, typically), has either become visible or invisible.
2847 */
2848 public void onOverlayVisibilityChanged(boolean visible) {}
2849
2850 /**
2851 * Informs us that the page transition has ended, so that we can react to the newly selected
2852 * page if we want to.
2853 */
2854 public void onPageEndTransition() {}
2855
2856 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002857 * Add the icons for all apps.
2858 *
2859 * Implementation of the method from LauncherModel.Callbacks.
2860 */
Sunny Goyal28daf4a2020-06-08 17:06:09 -07002861 @Override
Schneider Victor-tulias927d6dc2021-08-26 11:56:47 -07002862 @TargetApi(Build.VERSION_CODES.S)
Sunny Goyal28daf4a2020-06-08 17:06:09 -07002863 public void bindAllApplications(AppInfo[] apps, int flags) {
2864 mAppsView.getAppsStore().setApps(apps, flags);
Sunny Goyal1818fb72020-11-09 14:32:48 -08002865 PopupContainerWithArrow.dismissInvalidPopup(this);
Schneider Victor-tulias927d6dc2021-08-26 11:56:47 -07002866 if (Utilities.ATLEAST_S) {
2867 Trace.endAsyncSection(DISPLAY_ALL_APPS_TRACE_METHOD_NAME,
2868 DISPLAY_ALL_APPS_TRACE_COOKIE);
2869 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002870 }
2871
2872 /**
Zak Cohen658c67a2018-10-19 14:21:05 -07002873 * Copies LauncherModel's map of activities to shortcut counts to Launcher's. This is necessary
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002874 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2875 */
2876 @Override
Zak Cohen658c67a2018-10-19 14:21:05 -07002877 public void bindDeepShortcutMap(HashMap<ComponentKey, Integer> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002878 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002879 }
2880
Sunny Goyal4390ace2014-10-13 11:33:11 -07002881 @Override
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +00002882 public void bindIncrementalDownloadProgressUpdated(AppInfo app) {
2883 mAppsView.getAppsStore().updateProgressBar(app);
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002884 }
2885
2886 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002887 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002888 mWorkspace.widgetsRestored(widgets);
2889 }
2890
Joe Onorato9c1289c2009-08-17 11:03:03 -04002891 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002892 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002893 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002894 *
2895 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002896 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002897 @Override
Sunny Goyal28f50752020-07-21 14:15:13 -07002898 public void bindWorkspaceItemsChanged(List<WorkspaceItemInfo> updated) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002899 if (!updated.isEmpty()) {
Sunny Goyal711c5962021-06-23 12:36:18 -07002900 mWorkspace.updateWorkspaceItems(updated, this);
Sunny Goyal1818fb72020-11-09 14:32:48 -08002901 PopupContainerWithArrow.dismissInvalidPopup(this);
Sunny Goyal4390ace2014-10-13 11:33:11 -07002902 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002903 }
2904
2905 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002906 * Update the state of a package, typically related to install state.
2907 *
2908 * Implementation of the method from LauncherModel.Callbacks.
2909 */
Sunny Goyale755d462014-07-22 13:48:29 -07002910 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002911 public void bindRestoreItemsChange(HashSet<ItemInfo> updates) {
Sunny Goyal711c5962021-06-23 12:36:18 -07002912 mWorkspace.updateRestoreItems(updates, this);
Sunny Goyala22666f2014-09-18 13:25:15 -07002913 }
2914
2915 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002916 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07002917 * in addition to specific applications to remove, the reason being that
2918 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002919 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07002920 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002921 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07002922 @Override
Sunny Goyal32084d42022-05-02 10:00:07 -07002923 public void bindWorkspaceComponentsRemoved(Predicate<ItemInfo> matcher) {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002924 mWorkspace.removeItemsByMatcher(matcher);
2925 mDragController.onAppsRemoved(matcher);
Sunny Goyal1818fb72020-11-09 14:32:48 -08002926 PopupContainerWithArrow.dismissInvalidPopup(this);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002927 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002928
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002929 @Override
Steven Ng2f5648a2021-02-08 17:18:25 +00002930 public void bindAllWidgets(final List<WidgetsListBaseEntry> allWidgets) {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002931 mPopupDataProvider.setAllWidgets(allWidgets);
Tony Wickham26b17462017-03-20 17:12:24 -07002932 }
2933
kholoud mohamedc76b2032021-12-20 16:47:38 +00002934 @Override
2935 public void bindStringCache(StringCache cache) {
2936 mStringCache = cache;
2937 }
2938
2939 @Override
2940 public StringCache getStringCache() {
2941 return mStringCache;
2942 }
2943
Tony Wickham86222d22017-03-29 15:30:43 -07002944 /**
2945 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
2946 * refreshes the widgets and shortcuts associated with the given package/user
2947 */
2948 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07002949 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002950 }
2951
Winson Chung80baf5a2010-08-09 16:03:15 -07002952 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002953 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08002954 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002955 @Override
2956 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
2957 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002958
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002959 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
2960 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07002961 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002962 writer.println(prefix + " Homescreen " + i);
2963
2964 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
2965 for (int j = 0; j < layout.getChildCount(); j++) {
2966 Object tag = layout.getChildAt(j).getTag();
2967 if (tag != null) {
2968 writer.println(prefix + " " + tag.toString());
2969 }
2970 }
2971 }
2972
2973 writer.println(prefix + " Hotseat");
Sunny Goyal876e4622018-11-02 13:50:40 -07002974 ViewGroup layout = mHotseat.getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07002975 for (int j = 0; j < layout.getChildCount(); j++) {
2976 Object tag = layout.getChildAt(j).getTag();
2977 if (tag != null) {
2978 writer.println(prefix + " " + tag.toString());
2979 }
Adam Cohen4caf2982013-08-20 18:54:31 -07002980 }
Sunny Goyala1365452015-10-01 15:46:24 -07002981 }
2982
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002983 writer.println(prefix + "Misc:");
Winson Chungef528762019-09-06 12:05:52 -07002984 dumpMisc(prefix + "\t", writer);
2985 writer.println(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
2986 writer.println(prefix + "\tmPendingRequestArgs=" + mPendingRequestArgs
2987 + " mPendingActivityResult=" + mPendingActivityResult);
2988 writer.println(prefix + "\tmRotationHelper: " + mRotationHelper);
2989 writer.println(prefix + "\tmAppWidgetHost.isListening: " + mAppWidgetHost.isListening());
2990
Tony Wickham646e4482020-04-06 17:25:53 -07002991 // Extra logging for general debugging
Hyunyoung Songa310a802019-04-25 13:57:16 -07002992 mDragLayer.dump(prefix, writer);
2993 mStateManager.dump(prefix, writer);
Tony Wickham646e4482020-04-06 17:25:53 -07002994 mPopupDataProvider.dump(prefix, writer);
Jon Miranda58561d42021-03-17 10:51:54 -04002995 mDeviceProfile.dump(prefix, writer);
Sunny Goyal2bba1902018-02-27 12:20:50 -08002996
2997 try {
2998 FileLog.flushAll(writer);
2999 } catch (Exception e) {
3000 // Ignore
3001 }
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003002
3003 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003004
Adam Cohen9211d422014-10-07 18:14:53 -07003005 if (mLauncherCallbacks != null) {
3006 mLauncherCallbacks.dump(prefix, fd, writer, args);
3007 }
Sunny Goyalb1d7de22019-09-06 10:36:54 -07003008 mOverlayManager.dump(prefix, writer);
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003009 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003010
Sunny Goyal66b24572016-09-21 15:57:55 -07003011 @Override
Sunny Goyal66b24572016-09-21 15:57:55 -07003012 public void onProvideKeyboardShortcuts(
3013 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
3014
3015 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07003016 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07003017 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
3018 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
Vadim Tryshev74c261c2018-06-12 13:44:43 -07003019 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.widget_button_text),
3020 KeyEvent.KEYCODE_W, KeyEvent.META_CTRL_ON));
Sunny Goyal66b24572016-09-21 15:57:55 -07003021 }
Sunny Goyal384b5782021-02-09 22:50:02 -08003022 getSupportedActions(this, getCurrentFocus()).forEach(la ->
Vadim Tryshev3455f8d2018-05-11 20:02:20 -07003023 shortcutInfos.add(new KeyboardShortcutInfo(
Sunny Goyal384b5782021-02-09 22:50:02 -08003024 la.accessibilityAction.getLabel(), la.keyCode, KeyEvent.META_CTRL_ON)));
Sunny Goyal66b24572016-09-21 15:57:55 -07003025 if (!shortcutInfos.isEmpty()) {
3026 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
3027 }
3028
3029 super.onProvideKeyboardShortcuts(data, menu, deviceId);
3030 }
3031
3032 @Override
3033 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
3034 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
3035 switch (keyCode) {
3036 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07003037 if (isInState(NORMAL)) {
3038 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07003039 return true;
3040 }
3041 break;
Vadim Tryshev74c261c2018-06-12 13:44:43 -07003042 case KeyEvent.KEYCODE_W:
3043 if (isInState(NORMAL)) {
3044 OptionsPopupView.openWidgets(this);
3045 return true;
3046 }
3047 break;
Sunny Goyal384b5782021-02-09 22:50:02 -08003048 default:
3049 for (LauncherAction la : getSupportedActions(this, getCurrentFocus())) {
3050 if (la.keyCode == keyCode) {
3051 return la.invokeFromKeyboard(getCurrentFocus());
3052 }
3053 }
Sunny Goyal66b24572016-09-21 15:57:55 -07003054 }
3055 }
3056 return super.onKeyShortcut(keyCode, event);
3057 }
3058
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08003059 @Override
3060 public boolean onKeyUp(int keyCode, KeyEvent event) {
3061 if (keyCode == KeyEvent.KEYCODE_MENU) {
3062 // KEYCODE_MENU is sent by some tests, for example
3063 // LauncherJankTests#testWidgetsContainerFling. Don't just remove its handling.
3064 if (!mDragController.isDragging() && !mWorkspace.isSwitchingState() &&
3065 isInState(NORMAL)) {
3066 // Close any open floating views.
Andy Wickham65ced1b2020-03-07 02:14:19 +00003067 closeOpenViews();
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08003068
3069 // Setting the touch point to (-1, -1) will show the options popup in the center of
3070 // the screen.
vadimtbc5d7e62019-12-09 13:53:05 -08003071 if (Utilities.IS_RUNNING_IN_TEST_HARNESS) {
3072 Log.d(TestProtocol.PERMANENT_DIAG_TAG, "Opening options popup on key up");
3073 }
Sunny Goyal831212e2021-05-14 14:24:09 -07003074 showDefaultOptions(-1, -1);
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08003075 }
3076 return true;
3077 }
3078 return super.onKeyUp(keyCode, event);
3079 }
3080
Sunny Goyal831212e2021-05-14 14:24:09 -07003081 /**
3082 * Shows the default options popup
3083 */
3084 public void showDefaultOptions(float x, float y) {
Brian Isganitisa85ae692021-09-23 19:56:24 -04003085 OptionsPopupView.show(this, getPopupTarget(x, y), OptionsPopupView.getOptions(this),
3086 false);
3087 }
3088
3089 /**
3090 * Returns target rectangle for anchoring a popup menu.
3091 */
3092 protected RectF getPopupTarget(float x, float y) {
Sunny Goyal831212e2021-05-14 14:24:09 -07003093 float halfSize = getResources().getDimension(R.dimen.options_menu_thumb_size) / 2;
3094 if (x < 0 || y < 0) {
3095 x = mDragLayer.getWidth() / 2;
3096 y = mDragLayer.getHeight() / 2;
3097 }
Brian Isganitisa85ae692021-09-23 19:56:24 -04003098 return new RectF(x - halfSize, y - halfSize, x + halfSize, y + halfSize);
3099 }
3100
3101 @Override
3102 public boolean shouldUseColorExtractionForPopup() {
3103 return getTopOpenViewWithType(this, TYPE_FOLDER) == null
3104 && getStateManager().getState() != LauncherState.ALL_APPS;
Sunny Goyal831212e2021-05-14 14:24:09 -07003105 }
3106
Sunny Goyalb5181c82021-03-10 07:39:12 +00003107 @Override
3108 protected void collectStateHandlers(List<StateHandler> out) {
3109 out.add(getAllAppsController());
3110 out.add(getWorkspace());
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07003111 }
3112
Sunny Goyal210e1742019-10-17 12:05:38 -07003113 public TouchController[] createTouchControllers() {
3114 return new TouchController[] {getDragController(), new AllAppsSwipeController(this)};
3115 }
3116
Sunny Goyal210e1742019-10-17 12:05:38 -07003117 public void useFadeOutAnimationForLauncherStart(CancellationSignal signal) { }
3118
3119 public void onDragLayerHierarchyChanged() { }
3120
Vinit Nayakf9b585b2019-07-10 14:25:32 -07003121 @Override
3122 public void returnToHomescreen() {
3123 super.returnToHomescreen();
3124 getStateManager().goToState(LauncherState.NORMAL);
3125 }
3126
Andy Wickham65ced1b2020-03-07 02:14:19 +00003127 private void closeOpenViews() {
3128 closeOpenViews(true);
3129 }
3130
3131 protected void closeOpenViews(boolean animate) {
3132 AbstractFloatingView.closeAllOpenViews(this, animate);
3133 }
3134
Sunny Goyal3a1eadd2019-10-23 17:44:27 -07003135 public Stream<SystemShortcut.Factory> getSupportedShortcuts() {
Samuel Fufa5cf3e862020-02-03 20:22:54 -08003136 return Stream.of(APP_INFO, WIDGETS, INSTALL);
Sunny Goyal3a1eadd2019-10-23 17:44:27 -07003137 }
3138
Sunny Goyal384b5782021-02-09 22:50:02 -08003139 protected LauncherAccessibilityDelegate createAccessibilityDelegate() {
3140 return new LauncherAccessibilityDelegate(this);
3141 }
Sunny Goyalf2393f12020-04-01 20:13:12 -07003142
3143 /**
3144 * @see LauncherState#getOverviewScaleAndOffset(Launcher)
3145 */
3146 public float[] getNormalOverviewScaleAndOffset() {
Tony Wickham82d5a062021-05-14 14:37:12 -07003147 return new float[] {NO_SCALE, NO_OFFSET};
Sunny Goyalf2393f12020-04-01 20:13:12 -07003148 }
3149
Sunny Goyal210e1742019-10-17 12:05:38 -07003150 public static Launcher getLauncher(Context context) {
Winson Chung8687bc22020-02-27 23:34:24 -08003151 return fromContext(context);
Sunny Goyal210e1742019-10-17 12:05:38 -07003152 }
3153
Sunny Goyal3dce5f32017-10-05 11:40:05 -07003154 /**
Sunny Goyalab770a12018-11-14 15:17:26 -08003155 * Just a wrapper around the type cast to allow easier tracking of calls.
3156 */
3157 public static <T extends Launcher> T cast(ActivityContext activityContext) {
3158 return (T) activityContext;
3159 }
3160
Schneider Victor-tulias25ecae12020-09-14 14:07:38 -07003161 /**
3162 * Cross-fades the launcher's updated appearance with its previous appearance.
3163 *
3164 * This method is used to cross-fade UI updates on activity creation, specifically dark mode
3165 * updates.
3166 */
3167 private void crossFadeWithPreviousAppearance() {
Sunny Goyal6c0c61a2020-12-08 19:19:56 -08003168 NonConfigInstance lastInstance = (NonConfigInstance) getLastNonConfigurationInstance();
Schneider Victor-tulias25ecae12020-09-14 14:07:38 -07003169
Sunny Goyal6c0c61a2020-12-08 19:19:56 -08003170 if (lastInstance == null || lastInstance.snapshot == null) {
Schneider Victor-tulias25ecae12020-09-14 14:07:38 -07003171 return;
3172 }
3173
3174 ImageView crossFadeHelper = new ImageView(this);
Sunny Goyal6c0c61a2020-12-08 19:19:56 -08003175 crossFadeHelper.setImageBitmap(lastInstance.snapshot);
Schneider Victor-tulias25ecae12020-09-14 14:07:38 -07003176 crossFadeHelper.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
3177
3178 InsettableFrameLayout.LayoutParams layoutParams = new InsettableFrameLayout.LayoutParams(
3179 InsettableFrameLayout.LayoutParams.MATCH_PARENT,
3180 InsettableFrameLayout.LayoutParams.MATCH_PARENT);
3181
3182 layoutParams.ignoreInsets = true;
3183
3184 crossFadeHelper.setLayoutParams(layoutParams);
3185
3186 getRootView().addView(crossFadeHelper);
3187
3188 crossFadeHelper
3189 .animate()
3190 .setDuration(THEME_CROSS_FADE_ANIMATION_DURATION)
3191 .alpha(0f)
3192 .withEndAction(() -> getRootView().removeView(crossFadeHelper))
3193 .start();
3194 }
Sunny Goyal6c0c61a2020-12-08 19:19:56 -08003195
Sunny Goyalb65d7662021-03-07 15:09:11 -08003196 public boolean supportsAdaptiveIconAnimation(View clickedView) {
3197 return false;
3198 }
3199
Tony Wickham39938cb2021-02-26 09:15:31 -08003200 public DragOptions getDefaultWorkspaceDragOptions() {
3201 return new DragOptions();
3202 }
3203
Schneider Victor-tulias9b6babf2022-03-24 14:47:32 -07003204 /**
3205 * Animates Launcher elements during a transition to the All Apps page.
3206 *
3207 * @param progress Transition progress from 0 to 1; where 0 => home and 1 => all apps.
3208 */
3209 public void onAllAppsTransition(float progress) {
3210 // No-Op
3211 }
3212
3213 /**
3214 * Animates Launcher elements during a transition to the Widgets pages.
3215 *
3216 * @param progress Transition progress from 0 to 1; where 0 => home and 1 => widgets.
3217 */
3218 public void onWidgetsTransition(float progress) {
3219 // No-Op
3220 }
3221
Sunny Goyal6c0c61a2020-12-08 19:19:56 -08003222 private static class NonConfigInstance {
3223 public Configuration config;
3224 public Bitmap snapshot;
3225 }
thiruramc96873c2021-02-11 15:13:47 -08003226
3227 @Override
3228 public StatsLogManager getStatsLogManager() {
3229 return super.getStatsLogManager().withDefaultInstanceId(mAllAppsSessionLogId);
3230 }
Brian Isganitis149dbcb2021-11-08 18:12:38 -05003231
3232 /**
3233 * Returns the current popup for testing, if any.
3234 */
3235 @VisibleForTesting
3236 @Nullable
3237 public ArrowPopup<?> getOptionsPopup() {
3238 return findViewById(R.id.popup_container);
3239 }
Schneider Victor-tulias789a6a92022-03-04 12:06:09 -08003240
3241 /** Pauses view updates that should not be run during the app launch animation. */
3242 public void pauseExpensiveViewUpdates() {
3243 // Pause page indicator animations as they lead to layer trashing.
Shikha Malhotrae957b602022-04-11 07:57:18 +00003244 getWorkspace().getPageIndicator().pauseAnimations();
Jon Miranda46ecc0c2022-06-02 11:34:15 -07003245
3246 getWorkspace().mapOverItems((info, view) -> {
3247 if (view instanceof LauncherAppWidgetHostView) {
3248 ((LauncherAppWidgetHostView) view).beginDeferringUpdates();
3249 }
3250 return false; // Return false to continue iterating through all the items.
3251 });
Schneider Victor-tulias789a6a92022-03-04 12:06:09 -08003252 }
3253
3254 /** Resumes view updates at the end of the app launch animation. */
3255 public void resumeExpensiveViewUpdates() {
Shikha Malhotrae957b602022-04-11 07:57:18 +00003256 getWorkspace().getPageIndicator().skipAnimationsToEnd();
Shikha Malhotra0f2e1b62022-03-27 21:02:21 +00003257
Jon Miranda46ecc0c2022-06-02 11:34:15 -07003258 getWorkspace().mapOverItems((info, view) -> {
3259 if (view instanceof LauncherAppWidgetHostView) {
3260 ((LauncherAppWidgetHostView) view).endDeferringUpdates();
3261 }
3262 return false; // Return false to continue iterating through all the items.
3263 });
3264 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003265}