blob: 873b066eec9db69ac256d85634108730b8166f24 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080019import static android.content.pm.ActivityInfo.CONFIG_ORIENTATION;
20import static android.content.pm.ActivityInfo.CONFIG_SCREEN_SIZE;
Sunny Goyal8f965462020-04-23 19:00:36 -070021import static android.view.accessibility.AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED;
Vinit Nayak48b28022020-04-27 20:48:49 +000022
Pinyao Tinga74b63a2019-07-17 23:26:06 -070023import static com.android.launcher3.AbstractFloatingView.TYPE_ALL;
24import static com.android.launcher3.AbstractFloatingView.TYPE_REBIND_SAFE;
Tony Wickham6a71a5b2018-08-21 11:40:23 -070025import static com.android.launcher3.AbstractFloatingView.TYPE_SNACKBAR;
Sunny Goyal8f965462020-04-23 19:00:36 -070026import static com.android.launcher3.InstallShortcutReceiver.FLAG_DRAG_AND_DROP;
Sunny Goyal4c7f2152017-10-17 17:17:16 -070027import static com.android.launcher3.LauncherAnimUtils.SPRING_LOADED_EXIT_DELAY;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -070028import static com.android.launcher3.LauncherState.ALL_APPS;
Sunny Goyal8f965462020-04-23 19:00:36 -070029import static com.android.launcher3.LauncherState.FLAG_CLOSE_POPUPS;
30import static com.android.launcher3.LauncherState.FLAG_MULTI_PAGE;
31import static com.android.launcher3.LauncherState.FLAG_NON_INTERACTIVE;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -070032import static com.android.launcher3.LauncherState.NORMAL;
Sunny Goyalf2393f12020-04-01 20:13:12 -070033import static com.android.launcher3.LauncherState.NO_OFFSET;
34import static com.android.launcher3.LauncherState.NO_SCALE;
Govinda Wassermanc06e1512019-04-09 09:56:53 -040035import static com.android.launcher3.LauncherState.OVERVIEW;
36import static com.android.launcher3.LauncherState.OVERVIEW_PEEK;
Sunny Goyal8f965462020-04-23 19:00:36 -070037import static com.android.launcher3.LauncherState.SPRING_LOADED;
Sunny Goyalb1d7de22019-09-06 10:36:54 -070038import static com.android.launcher3.Utilities.postAsyncCallback;
Sunny Goyal6001ea22018-05-10 16:31:00 -070039import static com.android.launcher3.dragndrop.DragLayer.ALPHA_INDEX_LAUNCHER_LOAD;
Sunny Goyalaeb16432017-10-16 11:46:41 -070040import static com.android.launcher3.logging.LoggerUtils.newContainerTarget;
Sunny Goyal3a1eadd2019-10-23 17:44:27 -070041import static com.android.launcher3.popup.SystemShortcut.APP_INFO;
Sunny Goyal3a1eadd2019-10-23 17:44:27 -070042import static com.android.launcher3.popup.SystemShortcut.INSTALL;
43import static com.android.launcher3.popup.SystemShortcut.WIDGETS;
Sunny Goyal8f965462020-04-23 19:00:36 -070044import static com.android.launcher3.states.RotationHelper.REQUEST_LOCK;
Sunny Goyalbe8c3402019-05-22 15:17:52 -070045import static com.android.launcher3.states.RotationHelper.REQUEST_NONE;
Sunny Goyal326403e2017-10-02 12:45:10 -070046
Gilles Debunnedd6c9922010-10-25 11:23:41 -070047import android.animation.Animator;
Sunny Goyal6001ea22018-05-10 16:31:00 -070048import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070049import android.animation.AnimatorSet;
Sunny Goyal6001ea22018-05-10 16:31:00 -070050import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070051import android.animation.ValueAnimator;
Adam Cohen0b395352014-06-09 22:54:36 +000052import android.annotation.TargetApi;
Tony Wickham005df0b2018-02-13 11:28:12 -080053import android.app.ActivityOptions;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080054import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070055import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080057import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080058import android.content.ComponentCallbacks2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070061import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070062import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070063import android.content.SharedPreferences;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064import android.content.pm.PackageManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080065import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070066import android.database.sqlite.SQLiteDatabase;
Adam Cohen0f668f32014-09-08 19:54:17 +020067import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.os.Bundle;
Sunny Goyal210e1742019-10-17 12:05:38 -070069import android.os.CancellationSignal;
Hyunyoung Songbd6fba92018-05-16 15:54:31 -070070import android.os.Handler;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070071import android.os.Parcelable;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080072import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070073import android.os.StrictMode;
Michael Jurkaa33411c2012-06-14 16:18:21 -070074import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070076import android.util.Log;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070077import android.util.SparseArray;
Adam Cohen96d30a12013-07-16 18:13:21 -070078import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070079import android.view.KeyboardShortcutGroup;
80import android.view.KeyboardShortcutInfo;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080081import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -070082import android.view.Menu;
vadimt9af34a72020-01-21 11:19:13 -080083import android.view.MotionEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070084import android.view.View;
Adam Cohen96d30a12013-07-16 18:13:21 -070085import android.view.ViewGroup;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080086import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070087import android.view.animation.OvershootInterpolator;
Adam Cohen96d30a12013-07-16 18:13:21 -070088import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070089
Sunny Goyal11a50742019-08-07 09:24:26 -070090import androidx.annotation.Nullable;
Pinyao Tingc9074272019-11-11 11:51:44 -080091import androidx.annotation.StringRes;
Sunny Goyalb1d7de22019-09-06 10:36:54 -070092import androidx.annotation.VisibleForTesting;
Sunny Goyal11a50742019-08-07 09:24:26 -070093
Sunny Goyal651077b2014-06-30 14:15:31 -070094import com.android.launcher3.DropTarget.DragObject;
Sunny Goyalf67db352020-04-24 00:38:11 -070095import com.android.launcher3.LauncherStateManager.AtomicAnimationFactory;
Sunny Goyal210e1742019-10-17 12:05:38 -070096import com.android.launcher3.LauncherStateManager.StateHandler;
Sunny Goyalae502842016-06-17 08:43:56 -070097import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070098import com.android.launcher3.allapps.AllAppsContainerView;
vadimtd4c90e12019-06-04 13:59:43 -070099import com.android.launcher3.allapps.AllAppsStore;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700100import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal7ede6112017-12-05 15:11:21 -0800101import com.android.launcher3.allapps.DiscoveryBounce;
Sunny Goyalf0b6db72018-08-13 16:10:14 -0700102import com.android.launcher3.anim.PropertyListBuilder;
Sunny Goyal8f965462020-04-23 19:00:36 -0700103import com.android.launcher3.compat.AccessibilityManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -0800104import com.android.launcher3.config.FeatureFlags;
vadimt00d42552018-12-11 17:59:36 -0800105import com.android.launcher3.dot.DotInfo;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700106import com.android.launcher3.dragndrop.DragController;
107import com.android.launcher3.dragndrop.DragLayer;
108import com.android.launcher3.dragndrop.DragView;
Samuel Fufa28c3e452020-02-08 16:07:16 -0800109import com.android.launcher3.folder.Folder;
Sunny Goyal11a50742019-08-07 09:24:26 -0700110import com.android.launcher3.folder.FolderGridOrganizer;
Sunny Goyal26119432016-02-18 22:09:23 +0000111import com.android.launcher3.folder.FolderIcon;
Sunny Goyalf840f102018-09-21 14:41:05 -0700112import com.android.launcher3.icons.IconCache;
Sunny Goyal66b24572016-09-21 15:57:55 -0700113import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700114import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700115import com.android.launcher3.logging.FileLog;
Hyunyoung Songfc007472018-10-25 14:09:50 -0700116import com.android.launcher3.logging.StatsLogUtils;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700117import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyal369212a2019-03-26 15:03:57 -0700118import com.android.launcher3.model.AppLaunchTracker;
Sunny Goyal87dcde62019-07-17 20:35:56 -0700119import com.android.launcher3.model.BgDataModel.Callbacks;
Sunny Goyala535ae42017-02-27 10:07:13 -0800120import com.android.launcher3.model.ModelWriter;
Sunny Goyale396abf2020-04-06 15:11:17 -0700121import com.android.launcher3.model.data.AppInfo;
122import com.android.launcher3.model.data.FolderInfo;
123import com.android.launcher3.model.data.ItemInfo;
124import com.android.launcher3.model.data.LauncherAppWidgetInfo;
125import com.android.launcher3.model.data.PromiseAppInfo;
126import com.android.launcher3.model.data.WorkspaceItemInfo;
Sunny Goyala535ae42017-02-27 10:07:13 -0800127import com.android.launcher3.notification.NotificationListener;
Sunny Goyale7b00122019-10-02 16:13:34 -0700128import com.android.launcher3.pm.PinRequestHelper;
Samuel Fufa41d909f2020-03-20 16:27:09 -0700129import com.android.launcher3.pm.UserCache;
Tony Wickham540913e2017-01-23 11:47:51 -0800130import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -0800131import com.android.launcher3.popup.PopupDataProvider;
Sunny Goyal3a1eadd2019-10-23 17:44:27 -0700132import com.android.launcher3.popup.SystemShortcut;
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700133import com.android.launcher3.qsb.QsbContainerView;
Sunny Goyal623eddd2018-03-02 12:24:41 -0800134import com.android.launcher3.states.RotationHelper;
vadimt9af34a72020-01-21 11:19:13 -0800135import com.android.launcher3.testing.TestLogging;
vadimtbc5d7e62019-12-09 13:53:05 -0800136import com.android.launcher3.testing.TestProtocol;
Sunny Goyal210e1742019-10-17 12:05:38 -0700137import com.android.launcher3.touch.AllAppsSwipeController;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800138import com.android.launcher3.touch.ItemClickHandler;
Sunny Goyalb1d7de22019-09-06 10:36:54 -0700139import com.android.launcher3.uioverrides.plugins.PluginManagerWrapper;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530140import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
141import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
Sunny Goyalaeb16432017-10-16 11:46:41 -0700142import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyal2100c782016-08-22 16:00:03 -0700143import com.android.launcher3.util.ActivityResultInfo;
Sunny Goyale84c5b82019-09-26 17:05:31 -0700144import com.android.launcher3.util.ActivityTracker;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700145import com.android.launcher3.util.ComponentKey;
Sunny Goyalefb7e842018-10-04 15:11:00 -0700146import com.android.launcher3.util.IntArray;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700147import com.android.launcher3.util.ItemInfoMatcher;
Sunny Goyal6001ea22018-05-10 16:31:00 -0700148import com.android.launcher3.util.MultiValueAlpha;
149import com.android.launcher3.util.MultiValueAlpha.AlphaProperty;
Tony Wickham133cd542020-04-08 17:48:41 -0700150import com.android.launcher3.util.OnboardingPrefs;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700151import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800152import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700153import com.android.launcher3.util.PendingRequestArgs;
Samuel Fufa41d909f2020-03-20 16:27:09 -0700154import com.android.launcher3.util.SafeCloseable;
Pinyao Ting49a3e692019-07-26 12:28:38 -0700155import com.android.launcher3.util.ShortcutUtil;
Sunny Goyal8392c822017-06-20 10:03:56 -0700156import com.android.launcher3.util.SystemUiController;
Mario Bertschlera6936942017-05-31 14:48:19 -0700157import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700158import com.android.launcher3.util.Thunk;
Sunny Goyal210e1742019-10-17 12:05:38 -0700159import com.android.launcher3.util.TouchController;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700160import com.android.launcher3.util.TraceHelper;
Sunny Goyal326403e2017-10-02 12:45:10 -0700161import com.android.launcher3.util.UiThreadHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700162import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyalab770a12018-11-14 15:17:26 -0800163import com.android.launcher3.views.ActivityContext;
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800164import com.android.launcher3.views.OptionsPopupView;
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400165import com.android.launcher3.views.ScrimView;
Sunny Goyal29947f02017-12-18 13:49:44 -0800166import com.android.launcher3.widget.LauncherAppWidgetHostView;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800167import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700168import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal29947f02017-12-18 13:49:44 -0800169import com.android.launcher3.widget.PendingAppWidgetHostView;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800170import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700171import com.android.launcher3.widget.WidgetHostViewLoader;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700172import com.android.launcher3.widget.WidgetListRowEntry;
Sunny Goyal337c81f2019-12-10 12:19:13 -0800173import com.android.launcher3.widget.WidgetManagerHelper;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700174import com.android.launcher3.widget.WidgetsFullSheet;
Pinyao Tingc7a6c292019-08-26 14:36:02 -0700175import com.android.launcher3.widget.custom.CustomWidgetManager;
Sunny Goyalb1d7de22019-09-06 10:36:54 -0700176import com.android.systemui.plugins.OverlayPlugin;
177import com.android.systemui.plugins.PluginListener;
178import com.android.systemui.plugins.shared.LauncherExterns;
179import com.android.systemui.plugins.shared.LauncherOverlayManager;
180import com.android.systemui.plugins.shared.LauncherOverlayManager.LauncherOverlay;
181import com.android.systemui.plugins.shared.LauncherOverlayManager.LauncherOverlayCallbacks;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700182
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700183import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700184import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700185import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700186import java.util.Collection;
Zak Cohen658c67a2018-10-19 14:21:05 -0700187import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700188import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700189import java.util.List;
Sunny Goyalaae6fbb2019-01-31 16:05:58 -0800190import java.util.function.Predicate;
Sunny Goyalb1d7de22019-09-06 10:36:54 -0700191import java.util.function.Supplier;
Sunny Goyal3a1eadd2019-10-23 17:44:27 -0700192import java.util.stream.Stream;
Jon Mirandaf5197fc2019-09-04 16:55:41 -0700193
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194/**
195 * Default launcher application.
196 */
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700197public class Launcher extends BaseDraggingActivity implements LauncherExterns,
Samuel Fufaa579ddc2020-02-27 16:59:19 -0800198 Callbacks, InvariantDeviceProfile.OnIDPChangeListener, PluginListener<OverlayPlugin> {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700199 public static final String TAG = "Launcher";
Sunny Goyale84c5b82019-09-26 17:05:31 -0700200
201 public static final ActivityTracker<Launcher> ACTIVITY_TRACKER = new ActivityTracker<>();
202
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700203 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800204
Winson Chunga2413752012-04-03 14:22:34 -0700205 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700206
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800207 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700208 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700209
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700210 private static final int REQUEST_PICK_APPWIDGET = 9;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211
Michael Jurka8b805b12012-04-18 14:23:14 -0700212 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800213 public static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
214 public static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700215
Jon Mirandac476d6e2017-05-04 16:30:01 -0700216 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700217
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700218 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
219
Mathew Inwood876a8462013-06-14 14:12:41 +0100220 /**
221 * IntentStarter uses request codes starting with this. This must be greater than all activity
222 * request codes used internally.
223 */
224 protected static final int REQUEST_LAST = 100;
225
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800226 // Type: int
227 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700228 // Type: int
229 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700230 // Type: PendingRequestArgs
231 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
Sunny Goyaldedda052019-05-14 15:23:48 -0700232 // Type: int
233 private static final String RUNTIME_STATE_PENDING_REQUEST_CODE = "launcher.request_code";
Sunny Goyal2100c782016-08-22 16:00:03 -0700234 // Type: ActivityResultInfo
235 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700236 // Type: SparseArray<Parcelable>
237 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
Sunny Goyal17c72fb2019-10-14 14:06:38 -0700238
vadimt00d42552018-12-11 17:59:36 -0800239 public static final String ON_CREATE_EVT = "Launcher.onCreate";
Sunny Goyal17c72fb2019-10-14 14:06:38 -0700240 public static final String ON_START_EVT = "Launcher.onStart";
241 public static final String ON_RESUME_EVT = "Launcher.onResume";
242 public static final String ON_NEW_INTENT_EVT = "Launcher.onNewIntent";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800243
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700244 private LauncherStateManager mStateManager;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700245
Adam Cohenad4e15c2013-10-17 16:21:35 -0700246 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800247
Winson Chunga2413752012-04-03 14:22:34 -0700248 // How long to wait before the new-shortcut animation automatically pans the workspace
Jon Mirandaf5197fc2019-09-04 16:55:41 -0700249 @VisibleForTesting public static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700250 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Jon Mirandaf5197fc2019-09-04 16:55:41 -0700251 @Thunk @VisibleForTesting public static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700252
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400253 private static final int APPS_VIEW_ALPHA_CHANNEL_INDEX = 1;
254 private static final int SCRIM_VIEW_ALPHA_CHANNEL_INDEX = 0;
255
Jon Miranda54441f52018-01-24 15:38:25 -0800256 private LauncherAppTransitionManager mAppTransitionManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800257 private Configuration mOldConfig;
Jon Miranda54441f52018-01-24 15:38:25 -0800258
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700259 @Thunk
260 Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700261 private View mLauncherView;
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700262 @Thunk
263 DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800264 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700265
Sunny Goyal337c81f2019-12-10 12:19:13 -0800266 private WidgetManagerHelper mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700267 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700268
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700269 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700270
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700271 @Thunk
272 Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700273
Sunny Goyal47328fd2016-05-25 18:56:41 -0700274 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700275
276 // Main container view for the all apps screen.
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700277 @Thunk
278 AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700279 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700280
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400281 // Scrim view for the all apps and overview state.
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700282 @Thunk
283 ScrimView mScrimView;
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400284
Winson Chung8ae41982017-11-10 11:42:13 -0800285 // UI and state for the overview panel
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800286 private View mOverviewPanel;
Winson Chung8ae41982017-11-10 11:42:13 -0800287
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700288 @Thunk
289 boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400290
Winson Chung0b70cd42019-06-17 22:34:33 -0700291 private ArrayList<OnResumeCallback> mOnResumeCallbacks = new ArrayList<>();
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700292
Winson Chungc9bf6d42019-10-04 15:33:18 -0700293 // Used to notify when an activity launch has been deferred because launcher is not yet resumed
294 // TODO: See if we can remove this later
295 private Runnable mOnDeferredActivityLaunchCallback;
296
Sunny Goyal527c7d32015-08-28 15:19:36 -0700297 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700298
Joe Onorato9c1289c2009-08-17 11:03:03 -0400299 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800300 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800301 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700302 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400303
Tony Wickham010d2552017-01-20 08:15:28 -0800304 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700305
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800306 private int mSynchronouslyBoundPage = PagedView.INVALID_PAGE;
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530307 private int mPageToBindSynchronously = PagedView.INVALID_PAGE;
Adam Cohen1462de32012-07-24 22:34:36 -0700308
Winson Chung46353de2012-02-16 14:05:10 -0800309 // We only want to get the SharedPreferences once since it does an FS stat each time we get
310 // it from the context.
311 private SharedPreferences mSharedPrefs;
Tony Wickham133cd542020-04-08 17:48:41 -0700312 private OnboardingPrefs mOnboardingPrefs;
Winson Chung46353de2012-02-16 14:05:10 -0800313
Sunny Goyal2100c782016-08-22 16:00:03 -0700314 // Activity result which needs to be processed after workspace has loaded.
315 private ActivityResultInfo mPendingActivityResult;
316 /**
317 * Holds extra information required to handle a result from an external call, like
318 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
319 */
320 private PendingRequestArgs mPendingRequestArgs;
Sunny Goyaldedda052019-05-14 15:23:48 -0700321 // Request id for any pending activity result
Sunny Goyal210e1742019-10-17 12:05:38 -0700322 protected int mPendingActivityRequestCode = -1;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800323
Tracy Zhou4d7b2442019-12-09 13:42:57 -0800324 private ViewGroupFocusHelper mFocusHandler;
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700325
Sunny Goyal623eddd2018-03-02 12:24:41 -0800326 private RotationHelper mRotationHelper;
Sunny Goyal7779d622015-06-11 16:18:39 -0700327
Sunny Goyal2db53422019-03-01 16:06:12 -0800328 final Handler mHandler = new Handler();
Sunny Goyala002c6c2019-02-12 16:20:10 -0800329 private final Runnable mHandleDeferredResume = this::handleDeferredResume;
Sunny Goyale9c6f4c2019-05-30 11:35:25 -0700330 private boolean mDeferredResumePending;
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700331
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400332 private float mCurrentAssistantVisibility = 0f;
333
Sunny Goyalb1d7de22019-09-06 10:36:54 -0700334 protected LauncherOverlayManager mOverlayManager;
335 // If true, overlay callbacks are deferred
336 private boolean mDeferOverlayCallbacks;
337 private final Runnable mDeferredOverlayCallbacks = this::checkIfOverlayStillDeferred;
338
Sunny Goyal9b180102020-03-11 10:02:29 -0700339 private long mLastTouchUpTime = -1;
Sunny Goyala306e592020-03-24 01:51:41 -0700340 private boolean mTouchInProgress;
Sunny Goyal9b180102020-03-11 10:02:29 -0700341
Samuel Fufa41d909f2020-03-20 16:27:09 -0700342 private SafeCloseable mUserChangedCallbackCloseable;
343
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800344 @Override
345 protected void onCreate(Bundle savedInstanceState) {
Winson Chung5cc62c72019-10-16 11:32:41 -0700346 Object traceToken = TraceHelper.INSTANCE.beginSection(ON_CREATE_EVT,
347 TraceHelper.FLAG_UI_EVENT);
Winson Chunga2413752012-04-03 14:22:34 -0700348 if (DEBUG_STRICT_MODE) {
349 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
350 .detectDiskReads()
351 .detectDiskWrites()
352 .detectNetwork() // or .detectAll() for all detectable problems
353 .penaltyLog()
354 .build());
355 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
356 .detectLeakedSqlLiteObjects()
357 .detectLeakedClosableObjects()
358 .penaltyLog()
359 .penaltyDeath()
360 .build());
361 }
362
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800363 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400364
Sunny Goyal87f784c2017-01-11 10:48:34 -0800365 LauncherAppState app = LauncherAppState.getInstance(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800366 mOldConfig = new Configuration(getResources().getConfiguration());
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530367 mModel = app.getModel();
Sunny Goyal41d51a02019-05-14 09:44:34 -0700368 mRotationHelper = new RotationHelper(this);
Hyunyoung Song31971a32019-01-27 12:31:12 -0800369 InvariantDeviceProfile idp = app.getInvariantDeviceProfile();
370 initDeviceProfile(idp);
371 idp.addOnChangeListener(this);
Sunny Goyalf7258242015-10-19 16:59:07 -0700372 mSharedPrefs = Utilities.getPrefs(this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800373 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700374 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700375
Joe Onorato41a12d22009-10-31 18:30:00 -0400376 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700377 mAllAppsController = new AllAppsTransitionController(this);
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800378 mStateManager = new LauncherStateManager(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700379
Tony Wickham133cd542020-04-08 17:48:41 -0700380 mOnboardingPrefs = createOnboardingPrefs(mSharedPrefs, mStateManager);
381
Sunny Goyal337c81f2019-12-10 12:19:13 -0800382 mAppWidgetManager = new WidgetManagerHelper(this);
Samuel Fufa44043ec2019-08-26 15:00:30 -0700383 mAppWidgetHost = new LauncherAppWidgetHost(this,
384 appWidgetId -> getWorkspace().removeWidget(appWidgetId));
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700385 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700386
Sunny Goyal60820d72017-05-09 12:40:11 -0700387 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700388
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800389 setupViews();
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800390 mPopupDataProvider = new PopupDataProvider(this::updateNotificationDots);
Tony Wickham010d2552017-01-20 08:15:28 -0800391
Sunny Goyal90548432018-04-04 17:17:00 -0700392 mAppTransitionManager = LauncherAppTransitionManager.newInstance(this);
Winson Chung24ab40c2019-10-30 22:35:09 -0700393 mAppTransitionManager.registerRemoteAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800394
Sunny Goyale84c5b82019-09-26 17:05:31 -0700395 boolean internalStateHandled = ACTIVITY_TRACKER.handleCreate(this);
Sunny Goyald3864fa2017-11-14 15:06:36 -0800396 if (internalStateHandled) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800397 if (savedInstanceState != null) {
398 // InternalStateHandler has already set the appropriate state.
399 // We dont need to do anything.
400 savedInstanceState.remove(RUNTIME_STATE);
401 }
402 }
403 restoreState(savedInstanceState);
Sunny Goyalbbece862019-03-01 13:13:52 -0800404 mStateManager.reapplyState();
Winson Chungb63b44c2017-11-10 17:54:44 -0800405
Sunny Goyal2100c782016-08-22 16:00:03 -0700406 // We only load the page synchronously if the user rotates (or triggers a
407 // configuration change) while launcher is in the foreground
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530408 int currentScreen = PagedView.INVALID_PAGE;
Sunny Goyalfe770c92016-09-27 14:38:58 -0700409 if (savedInstanceState != null) {
410 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
411 }
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530412 mPageToBindSynchronously = currentScreen;
Sunny Goyald3864fa2017-11-14 15:06:36 -0800413
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530414 if (!mModel.addCallbacksAndLoad(this)) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800415 if (!internalStateHandled) {
416 // If we are not binding synchronously, show a fade in animation when
417 // the first page bind completes.
Sunny Goyal6001ea22018-05-10 16:31:00 -0700418 mDragLayer.getAlphaProperty(ALPHA_INDEX_LAUNCHER_LOAD).setValue(0);
Sunny Goyald3864fa2017-11-14 15:06:36 -0800419 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800420 }
421
422 // For handling default keys
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800423 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800424
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800425 setContentView(mLauncherView);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800426 getRootView().dispatchInsets();
Mario Bertschlera6936942017-05-31 14:48:19 -0700427
Jon Miranda7fda2852017-07-19 16:07:01 -0700428 // Listen for broadcasts
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700429 registerReceiver(mScreenOffReceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));
Sunny Goyala616d2b2017-06-12 13:54:01 -0700430
Sunny Goyal8392c822017-06-20 10:03:56 -0700431 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
432 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700433
434 if (mLauncherCallbacks != null) {
435 mLauncherCallbacks.onCreate(savedInstanceState);
436 }
Sunny Goyalb1d7de22019-09-06 10:36:54 -0700437 mOverlayManager = getDefaultOverlay();
438 PluginManagerWrapper.INSTANCE.get(this).addPluginListener(this,
439 OverlayPlugin.class, false /* allowedMultiple */);
440
Sunny Goyal623eddd2018-03-02 12:24:41 -0800441 mRotationHelper.initialize();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700442
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400443 mStateManager.addStateListener(new LauncherStateManager.StateListener() {
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400444
445 @Override
446 public void onStateTransitionComplete(LauncherState finalState) {
447 float alpha = 1f - mCurrentAssistantVisibility;
448 if (finalState == NORMAL) {
449 mAppsView.getAlphaProperty(APPS_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
450 } else if (finalState == OVERVIEW || finalState == OVERVIEW_PEEK) {
451 mAppsView.getAlphaProperty(APPS_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
452 mScrimView.getAlphaProperty(SCRIM_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
453 } else {
454 mAppsView.getAlphaProperty(APPS_VIEW_ALPHA_CHANNEL_INDEX).setValue(1f);
455 mScrimView.getAlphaProperty(SCRIM_VIEW_ALPHA_CHANNEL_INDEX).setValue(1f);
456 }
457 }
458 });
Sunny Goyal17c72fb2019-10-14 14:06:38 -0700459
Winson Chung5cc62c72019-10-16 11:32:41 -0700460 TraceHelper.INSTANCE.endSection(traceToken);
Samuel Fufa41d909f2020-03-20 16:27:09 -0700461
462 mUserChangedCallbackCloseable = UserCache.INSTANCE.get(this).addUserChangeListener(
463 () -> getStateManager().goToState(NORMAL));
Adam Cohen9211d422014-10-07 18:14:53 -0700464 }
465
Sunny Goyalb1d7de22019-09-06 10:36:54 -0700466 protected LauncherOverlayManager getDefaultOverlay() {
467 return new LauncherOverlayManager() { };
468 }
469
Tony Wickham133cd542020-04-08 17:48:41 -0700470 protected OnboardingPrefs createOnboardingPrefs(SharedPreferences sharedPrefs,
471 LauncherStateManager stateManager) {
472 return new OnboardingPrefs<>(this, sharedPrefs, stateManager);
473 }
474
475 public OnboardingPrefs getOnboardingPrefs() {
476 return mOnboardingPrefs;
477 }
478
Sunny Goyalb1d7de22019-09-06 10:36:54 -0700479 @Override
480 public void onPluginConnected(OverlayPlugin overlayManager, Context context) {
481 switchOverlay(() -> overlayManager.createOverlayManager(this, this));
482 }
483
484 @Override
485 public void onPluginDisconnected(OverlayPlugin plugin) {
486 switchOverlay(this::getDefaultOverlay);
487 }
488
489 private void switchOverlay(Supplier<LauncherOverlayManager> overlaySupplier) {
490 if (mOverlayManager != null) {
491 mOverlayManager.onActivityDestroyed(this);
492 }
493 mOverlayManager = overlaySupplier.get();
494 if (getRootView().isAttachedToWindow()) {
495 mOverlayManager.onAttachedToWindow();
496 }
497 mDeferOverlayCallbacks = true;
498 checkIfOverlayStillDeferred();
499 }
500
501 @Override
502 protected void dispatchDeviceProfileChanged() {
503 super.dispatchDeviceProfileChanged();
504 mOverlayManager.onDeviceProvideChanged();
505 }
506
Sunny Goyal7779d622015-06-11 16:18:39 -0700507 @Override
Winson Chung8eb49e02018-07-16 13:33:31 -0700508 public void onEnterAnimationComplete() {
509 super.onEnterAnimationComplete();
Sunny Goyalbe8c3402019-05-22 15:17:52 -0700510 mRotationHelper.setCurrentTransitionRequest(REQUEST_NONE);
Winson Chung8eb49e02018-07-16 13:33:31 -0700511 }
512
513 @Override
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800514 public void onConfigurationChanged(Configuration newConfig) {
515 int diff = newConfig.diff(mOldConfig);
Jon Mirandaaeb4dd02018-07-11 14:16:23 -0700516
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800517 if ((diff & (CONFIG_ORIENTATION | CONFIG_SCREEN_SIZE)) != 0) {
Sunny Goyal371ea052019-03-06 15:38:32 -0800518 onIdpChanged(mDeviceProfile.inv);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800519 }
520
521 mOldConfig.setTo(newConfig);
522 super.onConfigurationChanged(newConfig);
523 }
524
Sunny Goyal605bcf32018-03-02 15:16:12 -0800525 @Override
Sunny Goyal41d51a02019-05-14 09:44:34 -0700526 public void reapplyUi() {
Tony Wickham4df42f82019-11-13 15:38:28 -0800527 reapplyUi(true /* cancelCurrentAnimation */);
528 }
529
530 public void reapplyUi(boolean cancelCurrentAnimation) {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700531 getRootView().dispatchInsets();
Tony Wickham4df42f82019-11-13 15:38:28 -0800532 getStateManager().reapplyState(cancelCurrentAnimation);
Sunny Goyal59d086c2018-05-07 17:31:40 -0700533 }
534
535 @Override
Hyunyoung Song31971a32019-01-27 12:31:12 -0800536 public void onIdpChanged(int changeFlags, InvariantDeviceProfile idp) {
Sunny Goyal371ea052019-03-06 15:38:32 -0800537 onIdpChanged(idp);
538 }
539
540 private void onIdpChanged(InvariantDeviceProfile idp) {
541 mUserEventDispatcher = null;
542
Hyunyoung Song31971a32019-01-27 12:31:12 -0800543 initDeviceProfile(idp);
Sunny Goyal371ea052019-03-06 15:38:32 -0800544 dispatchDeviceProfileChanged();
545 reapplyUi();
546 mDragLayer.recreateControllers();
547
Sunny Goyal76ba0a32019-06-10 11:56:11 -0700548 // Calling onSaveInstanceState ensures that static cache used by listWidgets is
549 // initialized properly.
550 onSaveInstanceState(new Bundle());
Sunny Goyalc4d32012020-04-03 17:10:11 -0700551 mModel.rebindCallbacks();
Hyunyoung Song31971a32019-01-27 12:31:12 -0800552 }
553
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400554 public void onAssistantVisibilityChanged(float visibility) {
555 mCurrentAssistantVisibility = visibility;
556 float alpha = 1f - visibility;
557 LauncherState state = mStateManager.getState();
558 if (state == NORMAL) {
559 mAppsView.getAlphaProperty(APPS_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
560 } else if (state == OVERVIEW || state == OVERVIEW_PEEK) {
561 mAppsView.getAlphaProperty(APPS_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
562 mScrimView.getAlphaProperty(SCRIM_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
563 }
564 }
565
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800566 private void initDeviceProfile(InvariantDeviceProfile idp) {
567 // Load configuration-specific DeviceProfile
Sunny Goyal59d086c2018-05-07 17:31:40 -0700568 mDeviceProfile = idp.getDeviceProfile(this);
Sunny Goyal8c48d8b2019-01-25 15:10:18 -0800569 if (isInMultiWindowMode()) {
Sunny Goyal0addbf02020-04-28 14:17:35 -0700570 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(
571 this, getMultiWindowDisplaySize());
Sunny Goyal59d086c2018-05-07 17:31:40 -0700572 }
Sunny Goyalae6e3182019-04-30 12:04:37 -0700573
Sunny Goyal59d086c2018-05-07 17:31:40 -0700574 onDeviceProfileInitiated();
Sunny Goyalc4d32012020-04-03 17:10:11 -0700575 mModelWriter = mModel.getWriter(getDeviceProfile().isVerticalBarLayout(), true);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800576 }
577
Sunny Goyal623eddd2018-03-02 12:24:41 -0800578 public RotationHelper getRotationHelper() {
579 return mRotationHelper;
580 }
581
Tracy Zhou4d7b2442019-12-09 13:42:57 -0800582 public ViewGroupFocusHelper getFocusHandler() {
583 return mFocusHandler;
584 }
585
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700586 public LauncherStateManager getStateManager() {
587 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700588 }
589
Mario Bertschler27288382017-05-24 15:35:09 -0700590 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700591 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800592 return mLauncherView.findViewById(id);
593 }
594
Adam Cohen9211d422014-10-07 18:14:53 -0700595 private LauncherCallbacks mLauncherCallbacks;
596
Sunny Goyal32554d12015-12-03 15:31:25 -0800597 /**
598 * Call this after onCreate to set or clear overlay.
599 */
Sunny Goyalb1d7de22019-09-06 10:36:54 -0700600 @Override
Sunny Goyal32554d12015-12-03 15:31:25 -0800601 public void setLauncherOverlay(LauncherOverlay overlay) {
602 if (overlay != null) {
603 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
604 }
605 mWorkspace.setLauncherOverlay(overlay);
606 }
607
Sunny Goyalb1d7de22019-09-06 10:36:54 -0700608 @Override
609 public void runOnOverlayHidden(Runnable runnable) {
610 getWorkspace().runOnOverlayHidden(runnable);
611 }
612
Adam Cohen9211d422014-10-07 18:14:53 -0700613 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
614 mLauncherCallbacks = callbacks;
615 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700616 }
617
Hyunyoung Song3f471442015-04-08 19:01:34 -0700618 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700619 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
620 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700621 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700622 }
623
Tony Wickham010d2552017-01-20 08:15:28 -0800624 public PopupDataProvider getPopupDataProvider() {
625 return mPopupDataProvider;
626 }
627
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700628 @Override
Tony Wickhamf34bee82018-12-03 18:11:39 -0800629 public DotInfo getDotInfoForItem(ItemInfo info) {
630 return mPopupDataProvider.getDotInfoForItem(info);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700631 }
632
633 @Override
634 public void invalidateParent(ItemInfo info) {
Jon Mirandae6f3fa42019-03-04 09:14:40 -0800635 if (info.container >= 0) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700636 View folderIcon = getWorkspace().getHomescreenIconByItemId(info.container);
Jon Mirandae6f3fa42019-03-04 09:14:40 -0800637 if (folderIcon instanceof FolderIcon && folderIcon.getTag() instanceof FolderInfo) {
Sunny Goyal11a50742019-08-07 09:24:26 -0700638 if (new FolderGridOrganizer(getDeviceProfile().inv)
639 .setFolderInfo((FolderInfo) folderIcon.getTag())
640 .isItemInPreview(info.rank)) {
Jon Mirandae6f3fa42019-03-04 09:14:40 -0800641 folderIcon.invalidate();
642 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700643 }
644 }
645 }
646
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000647 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700648 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
649 * a configuration step, this allows the proper animations to run after other transitions.
650 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700651 private int completeAdd(
Sunny Goyal2100c782016-08-22 16:00:03 -0700652 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
Sunny Goyalefb7e842018-10-04 15:11:00 -0700653 int screenId = info.screenId;
Sunny Goyal2100c782016-08-22 16:00:03 -0700654 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700655 // When the screen id represents an actual screen (as opposed to a rank) we make sure
656 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700657 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700658 }
Adam Cohendb364c32014-05-20 14:23:40 -0700659
Sunny Goyal2100c782016-08-22 16:00:03 -0700660 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800661 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700662 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Pinyao Tingc9074272019-11-11 11:51:44 -0800663 announceForAccessibility(R.string.item_added_to_workspace);
Winson Chungb8472bb2011-08-05 13:49:21 -0700664 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800665 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700666 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700667 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700668 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700669 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700670 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700671 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700672 int widgetId = appWidgetId;
673 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700674 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700675 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700676 // Since the view was just bound, also launch the configure activity if needed
677 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
678 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800679 if (provider != null) {
680 new WidgetAddFlowHandler(provider)
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700681 .startConfigActivity(this, widgetInfo,
682 REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700683 }
684 }
685 break;
686 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800687 }
Adam Cohendb364c32014-05-20 14:23:40 -0700688 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800689 }
690
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800691 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700692 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700693 if (isWorkspaceLoading()) {
694 // process the result once the workspace has loaded.
695 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
696 return;
697 }
698 mPendingActivityResult = null;
699
Winson Chunge341d302013-08-16 14:31:00 -0700700 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700701 final PendingRequestArgs requestArgs = mPendingRequestArgs;
702 setWaitingForResult(null);
703 if (requestArgs == null) {
704 return;
705 }
706
707 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700708
Adam Cohenad4e15c2013-10-17 16:21:35 -0700709 Runnable exitSpringLoaded = new Runnable() {
710 @Override
711 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700712 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700713 }
714 };
715
Michael Jurka8b805b12012-04-18 14:23:14 -0700716 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700717 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700718 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700719 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
720 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700721 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Sunny Goyal6e379fc2020-03-19 16:08:14 -0700722 mWorkspace.removeExtraEmptyScreenDelayed(
723 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false, exitSpringLoaded);
Michael Jurka8b805b12012-04-18 14:23:14 -0700724 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700725 addAppWidgetImpl(
726 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800727 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700728 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700729 }
730 return;
731 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200732
Adam Cohened66b2b2012-01-23 17:28:51 -0800733 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700734 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700735
Adam Cohened66b2b2012-01-23 17:28:51 -0800736 // We have special handling for widgets
737 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800738 final int appWidgetId;
739 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
740 : -1;
741 if (widgetId < 0) {
742 appWidgetId = pendingAddWidgetId;
743 } else {
744 appWidgetId = widgetId;
745 }
746
Adam Cohenad4e15c2013-10-17 16:21:35 -0700747 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800748 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700749 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
750 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700751 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700752 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Sunny Goyal6e379fc2020-03-19 16:08:14 -0700753 mWorkspace.removeExtraEmptyScreenDelayed(
754 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false,
755 () -> getStateManager().goToState(NORMAL));
Sunny Goyal2100c782016-08-22 16:00:03 -0700756 } else {
757 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
758 // When the screen id represents an actual screen (as opposed to a rank)
759 // we make sure that the drop page actually exists.
760 requestArgs.screenId =
761 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700762 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700763 final CellLayout dropLayout =
764 mWorkspace.getScreenWithId(requestArgs.screenId);
765
766 dropLayout.setDropPending(true);
767 final Runnable onComplete = new Runnable() {
768 @Override
769 public void run() {
770 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
771 dropLayout.setDropPending(false);
772 }
773 };
Sunny Goyal6e379fc2020-03-19 16:08:14 -0700774 mWorkspace.removeExtraEmptyScreenDelayed(
775 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false, onComplete);
Winson Chung5aaab772012-04-27 15:24:02 -0700776 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800777 return;
778 }
779
Sunny Goyald478c832016-04-01 12:04:16 -0700780 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
781 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700782 if (resultCode == RESULT_OK) {
783 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700784 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700785 }
786 // Leave the widget in the pending state if the user canceled the configure.
787 return;
788 }
789
Sunny Goyalaad90582015-07-09 14:22:50 -0700790 if (requestCode == REQUEST_CREATE_SHORTCUT) {
791 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700792 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
793 completeAdd(requestCode, data, -1, requestArgs);
Sunny Goyal6e379fc2020-03-19 16:08:14 -0700794 mWorkspace.removeExtraEmptyScreenDelayed(
795 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false, exitSpringLoaded);
Sunny Goyal2100c782016-08-22 16:00:03 -0700796
Sunny Goyalaad90582015-07-09 14:22:50 -0700797 } else if (resultCode == RESULT_CANCELED) {
Sunny Goyal6e379fc2020-03-19 16:08:14 -0700798 mWorkspace.removeExtraEmptyScreenDelayed(
799 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false, exitSpringLoaded);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800800 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800801 }
Hongming Jind91cb6e2020-01-27 16:23:03 -0800802
Adam Cohened66b2b2012-01-23 17:28:51 -0800803 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800804 }
805
806 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700807 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800808 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyaldedda052019-05-14 15:23:48 -0700809 mPendingActivityRequestCode = -1;
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800810 handleActivityResult(requestCode, resultCode, data);
Adam Cohened66b2b2012-01-23 17:28:51 -0800811 }
812
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700813 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700814 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600815 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700816 PendingRequestArgs pendingArgs = mPendingRequestArgs;
817 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
818 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
819 setWaitingForResult(null);
820
Sunny Goyal28c6b962015-10-12 11:42:05 -0700821 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700822 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700823 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700824 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700825 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700826 Intent intent = pendingArgs.getPendingIntent();
827
Sunny Goyal28c6b962015-10-12 11:42:05 -0700828 if (grantResults.length > 0
829 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal369212a2019-03-26 15:03:57 -0700830 startActivitySafely(v, intent, null, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700831 } else {
832 // TODO: Show a snack bar with link to settings
833 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700834 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700835 }
836 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600837 }
838
Adam Cohendb364c32014-05-20 14:23:40 -0700839 /**
840 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
841 *
842 * @param screenId the screen id to check
843 * @return the new screen, or screenId if it exists
844 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700845 private int ensurePendingDropLayoutExists(int screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800846 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700847 if (dropLayout == null) {
848 // it's possible that the add screen was removed because it was
849 // empty and a re-bind occurred
850 mWorkspace.addExtraEmptyScreen();
851 return mWorkspace.commitExtraEmptyScreen();
852 } else {
853 return screenId;
854 }
855 }
856
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700857 @Thunk
858 void completeTwoStageWidgetDrop(
Sunny Goyal2100c782016-08-22 16:00:03 -0700859 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
860 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800861 Runnable onCompleteRunnable = null;
862 int animationType = 0;
863
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700864 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800865 if (resultCode == RESULT_OK) {
866 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
867 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800868 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700869 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800870 onCompleteRunnable = new Runnable() {
871 @Override
872 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700873 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700874 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800875 }
876 };
877 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800878 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800879 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800880 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700881 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700882 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700883 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
884 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700885 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700886 // The animated view may be null in the case of a rotation during widget configuration
887 onCompleteRunnable.run();
888 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800889 }
890
891 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700892 protected void onStop() {
Riddle Hsu7a7468f2019-11-21 01:32:58 +0800893 final boolean wasActive = isUserActive();
894 final LauncherState origState = getStateManager().getState();
895 final int origDragLayerChildCount = mDragLayer.getChildCount();
Michael Jurkacd496d72013-04-11 11:32:45 -0700896 super.onStop();
Winson Chung0b70cd42019-06-17 22:34:33 -0700897
Sunny Goyalb1d7de22019-09-06 10:36:54 -0700898 if (mDeferOverlayCallbacks) {
899 checkIfOverlayStillDeferred();
900 } else {
901 mOverlayManager.onActivityStopped(this);
Adam Cohen9211d422014-10-07 18:14:53 -0700902 }
Sunny Goyalb1d7de22019-09-06 10:36:54 -0700903
Becky Qiubfc2da12019-08-01 11:09:38 -0700904 logStopAndResume(Action.Command.STOP);
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700905
906 mAppWidgetHost.setListenIfResumed(false);
907
Tony Wickham010d2552017-01-20 08:15:28 -0800908 NotificationListener.removeNotificationsChangedListener();
Sunny Goyalaf161fd2018-02-05 16:34:57 -0800909 getStateManager().moveToRestState();
Winson Chungedb0d6b2018-03-21 17:32:55 -0700910
Winson Chungdea51352018-04-24 13:02:10 -0700911 // Workaround for b/78520668, explicitly trim memory once UI is hidden
Winson Chung473730f2018-04-26 11:54:22 -0700912 onTrimMemory(TRIM_MEMORY_UI_HIDDEN);
Riddle Hsu7a7468f2019-11-21 01:32:58 +0800913
914 if (wasActive) {
915 // The expected condition is that this activity is stopped because the device goes to
916 // sleep and the UI may have noticeable changes.
917 mDragLayer.post(() -> {
918 if ((!getStateManager().isInStableState(origState)
919 // The drag layer may be animating (e.g. dismissing QSB).
920 || mDragLayer.getAlpha() < 1
921 // Maybe an ArrowPopup is closed.
922 || mDragLayer.getChildCount() != origDragLayerChildCount)) {
923 onUiChangedWhileSleeping();
924 }
925 });
926 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700927 }
928
929 @Override
930 protected void onStart() {
Winson Chung5cc62c72019-10-16 11:32:41 -0700931 Object traceToken = TraceHelper.INSTANCE.beginSection(ON_START_EVT,
932 TraceHelper.FLAG_UI_EVENT);
Michael Jurkacd496d72013-04-11 11:32:45 -0700933 super.onStart();
Sunny Goyalb1d7de22019-09-06 10:36:54 -0700934 if (!mDeferOverlayCallbacks) {
935 mOverlayManager.onActivityStarted(this);
Adam Cohen9211d422014-10-07 18:14:53 -0700936 }
Sunny Goyalb1d7de22019-09-06 10:36:54 -0700937
Sunny Goyal67419a12017-11-14 16:55:22 -0800938 mAppWidgetHost.setListenIfResumed(true);
Winson Chung5cc62c72019-10-16 11:32:41 -0700939 TraceHelper.INSTANCE.endSection(traceToken);
Michael Jurkacd496d72013-04-11 11:32:45 -0700940 }
941
Sunny Goyala002c6c2019-02-12 16:20:10 -0800942 private void handleDeferredResume() {
Sunny Goyal8f965462020-04-23 19:00:36 -0700943 if (hasBeenResumed() && !mStateManager.getState().hasFlag(FLAG_NON_INTERACTIVE)) {
Becky Qiubfc2da12019-08-01 11:09:38 -0700944 logStopAndResume(Action.Command.RESUME);
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700945 getUserEventDispatcher().startSession();
Sunny Goyala002c6c2019-02-12 16:20:10 -0800946
Sunny Goyal369212a2019-03-26 15:03:57 -0700947 AppLaunchTracker.INSTANCE.get(this).onReturnedToHome();
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700948
Sunny Goyale9c6f4c2019-05-30 11:35:25 -0700949 // Process any items that were added while Launcher was away.
950 InstallShortcutReceiver.disableAndFlushInstallQueue(
951 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
952
953 // Refresh shortcuts if the permission changed.
954 mModel.refreshShortcutsIfRequired();
955
Winson Chung87583112019-06-10 14:20:58 -0700956 // Set the notification listener and fetch updated notifications when we resume
957 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
958
Sunny Goyale9c6f4c2019-05-30 11:35:25 -0700959 DiscoveryBounce.showForHomeIfNeeded(this);
960
Sunny Goyal210e1742019-10-17 12:05:38 -0700961 onDeferredResumed();
962 addActivityFlags(ACTIVITY_STATE_DEFERRED_RESUMED);
963
Sunny Goyale9c6f4c2019-05-30 11:35:25 -0700964 mDeferredResumePending = false;
965 } else {
966 mDeferredResumePending = true;
Sunny Goyaldedda052019-05-14 15:23:48 -0700967 }
968 }
969
Sunny Goyal210e1742019-10-17 12:05:38 -0700970 protected void onDeferredResumed() { }
971
Becky Qiubfc2da12019-08-01 11:09:38 -0700972 private void logStopAndResume(int command) {
973 int containerType = mStateManager.getState().containerType;
974 if (containerType == ContainerType.WORKSPACE && mWorkspace != null) {
975 getUserEventDispatcher().logActionCommand(command,
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700976 containerType, -1, mWorkspace.isOverlayShown() ? -1 : 0);
Becky Qiubfc2da12019-08-01 11:09:38 -0700977 } else {
978 getUserEventDispatcher().logActionCommand(command, containerType, -1);
979 }
Sunny Goyalb1d7de22019-09-06 10:36:54 -0700980 }
Becky Qiubfc2da12019-08-01 11:09:38 -0700981
Sunny Goyalb1d7de22019-09-06 10:36:54 -0700982 private void scheduleDeferredCheck() {
983 mHandler.removeCallbacks(mDeferredOverlayCallbacks);
984 postAsyncCallback(mHandler, mDeferredOverlayCallbacks);
985 }
986
987 private void checkIfOverlayStillDeferred() {
988 if (!mDeferOverlayCallbacks) {
989 return;
990 }
Sunny Goyal8f965462020-04-23 19:00:36 -0700991 if (isStarted() && (!hasBeenResumed()
992 || mStateManager.getState().hasFlag(FLAG_NON_INTERACTIVE))) {
Sunny Goyalb1d7de22019-09-06 10:36:54 -0700993 return;
994 }
995 mDeferOverlayCallbacks = false;
996
997 // Move the client to the correct state. Calling the same method twice is no-op.
998 if (isStarted()) {
999 mOverlayManager.onActivityStarted(this);
1000 }
1001 if (hasBeenResumed()) {
1002 mOverlayManager.onActivityResumed(this);
1003 } else {
1004 mOverlayManager.onActivityPaused(this);
1005 }
1006 if (!isStarted()) {
1007 mOverlayManager.onActivityStopped(this);
1008 }
1009 }
1010
1011 public void deferOverlayCallbacksUntilNextResumeOrStop() {
1012 mDeferOverlayCallbacks = true;
1013 }
1014
1015 public LauncherOverlayManager getOverlayManager() {
1016 return mOverlayManager;
Becky Qiubfc2da12019-08-01 11:09:38 -07001017 }
1018
Winson Chungef528762019-09-06 12:05:52 -07001019 public void onStateSetStart(LauncherState state) {
Sunny Goyale9c6f4c2019-05-30 11:35:25 -07001020 if (mDeferredResumePending) {
1021 handleDeferredResume();
1022 }
Sunny Goyalb1d7de22019-09-06 10:36:54 -07001023 if (mDeferOverlayCallbacks) {
1024 scheduleDeferredCheck();
Sunny Goyale9c6f4c2019-05-30 11:35:25 -07001025 }
Sunny Goyal210e1742019-10-17 12:05:38 -07001026 addActivityFlags(ACTIVITY_STATE_TRANSITION_ACTIVE);
Sunny Goyal8f965462020-04-23 19:00:36 -07001027
1028 if (state.hasFlag(FLAG_CLOSE_POPUPS)) {
1029 AbstractFloatingView.closeAllOpenViews(this, !state.hasFlag(FLAG_NON_INTERACTIVE));
1030 }
1031
1032 if (state == SPRING_LOADED) {
1033 // Prevent any Un/InstallShortcutReceivers from updating the db while we are
1034 // not on homescreen
1035 InstallShortcutReceiver.enableInstallQueue(FLAG_DRAG_AND_DROP);
1036 getRotationHelper().setCurrentStateRequest(REQUEST_LOCK);
1037
1038 mWorkspace.showPageIndicatorAtCurrentScroll();
1039 mWorkspace.setClipChildren(false);
1040 }
1041 // When multiple pages are visible, show persistent page indicator
1042 mWorkspace.getPageIndicator().setShouldAutoHide(!state.hasFlag(FLAG_MULTI_PAGE));
Sunny Goyaldedda052019-05-14 15:23:48 -07001043 }
1044
Winson Chungef528762019-09-06 12:05:52 -07001045 public void onStateSetEnd(LauncherState state) {
1046 getAppWidgetHost().setResumed(state == LauncherState.NORMAL);
Sunny Goyal8f965462020-04-23 19:00:36 -07001047 getWorkspace().setClipChildren(!state.hasFlag(FLAG_MULTI_PAGE));
1048
Winson Chungef528762019-09-06 12:05:52 -07001049 finishAutoCancelActionMode();
Sunny Goyal210e1742019-10-17 12:05:38 -07001050 removeActivityFlags(ACTIVITY_STATE_TRANSITION_ACTIVE);
Sunny Goyal8f965462020-04-23 19:00:36 -07001051
1052 // dispatch window state changed
1053 getWindow().getDecorView().sendAccessibilityEvent(TYPE_WINDOW_STATE_CHANGED);
1054 AccessibilityManagerCompat.sendStateEventToTest(this, state.ordinal);
1055
1056 if (state == NORMAL) {
1057 // Re-enable any Un/InstallShortcutReceiver and now process any queued items
1058 InstallShortcutReceiver.disableAndFlushInstallQueue(FLAG_DRAG_AND_DROP, this);
1059
1060 // Clear any rotation locks when going to normal state
1061 getRotationHelper().setCurrentStateRequest(REQUEST_NONE);
1062 }
Winson Chungef528762019-09-06 12:05:52 -07001063 }
1064
Michael Jurkacd496d72013-04-11 11:32:45 -07001065 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001066 protected void onResume() {
Winson Chung5cc62c72019-10-16 11:32:41 -07001067 Object traceToken = TraceHelper.INSTANCE.beginSection(ON_RESUME_EVT,
1068 TraceHelper.FLAG_UI_EVENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001069 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001070
Sunny Goyala002c6c2019-02-12 16:20:10 -08001071 mHandler.removeCallbacks(mHandleDeferredResume);
1072 Utilities.postAsyncCallback(mHandler, mHandleDeferredResume);
Hyunyoung Songbd6fba92018-05-16 15:54:31 -07001073
Winson Chung940b0ac2019-07-09 11:33:50 -07001074 if (!mOnResumeCallbacks.isEmpty()) {
1075 final ArrayList<OnResumeCallback> resumeCallbacks = new ArrayList<>(mOnResumeCallbacks);
1076 mOnResumeCallbacks.clear();
1077 for (int i = resumeCallbacks.size() - 1; i >= 0; i--) {
1078 resumeCallbacks.get(i).onLauncherResume();
1079 }
1080 resumeCallbacks.clear();
Winson Chung0b70cd42019-06-17 22:34:33 -07001081 }
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001082
Sunny Goyalb1d7de22019-09-06 10:36:54 -07001083 if (mDeferOverlayCallbacks) {
1084 scheduleDeferredCheck();
1085 } else {
1086 mOverlayManager.onActivityResumed(this);
Adam Cohen9211d422014-10-07 18:14:53 -07001087 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -08001088
Winson Chung5cc62c72019-10-16 11:32:41 -07001089 TraceHelper.INSTANCE.endSection(traceToken);
Adam Cohen06dff352012-06-01 17:17:08 -07001090 }
1091
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001092 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001093 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001094 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -07001095 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -07001096
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001097 super.onPause();
Joe Onorato24b6fd82009-11-12 13:47:09 -08001098 mDragController.cancelDrag();
Sunny Goyal9b180102020-03-11 10:02:29 -07001099 mLastTouchUpTime = -1;
Hyunyoung Song497708c2019-05-01 16:16:53 -07001100 mDropTargetBar.animateToVisibility(false);
Sunny Goyalb1d7de22019-09-06 10:36:54 -07001101
1102 if (!mDeferOverlayCallbacks) {
1103 mOverlayManager.onActivityPaused(this);
Adam Cohenbffe7452013-07-22 18:21:45 -07001104 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001105 }
1106
Adam Cohenc2d6e892014-10-16 09:49:24 -07001107 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1108
Sunny Goyalc86df472016-02-25 09:19:38 -08001109 public void onScrollChanged(float progress) {
1110 if (mWorkspace != null) {
1111 mWorkspace.onOverlayScrollChanged(progress);
1112 }
1113 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001114 }
1115
Sunny Goyalf9403d92017-10-18 10:55:56 -07001116 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -07001117 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -07001118 }
1119
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001120 /**
1121 * Restores the previous state, if it exists.
1122 *
1123 * @param savedState The previous state.
1124 */
1125 private void restoreState(Bundle savedState) {
1126 if (savedState == null) {
1127 return;
1128 }
1129
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001130 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -07001131 LauncherState[] stateValues = LauncherState.values();
1132 LauncherState state = stateValues[stateOrdinal];
Sunny Goyal8f965462020-04-23 19:00:36 -07001133 if (!state.shouldDisableRestore()) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001134 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001135 }
1136
Samuel Fufaca37b8a2019-08-19 17:04:36 -07001137 PendingRequestArgs requestArgs = savedState.getParcelable(
1138 RUNTIME_STATE_PENDING_REQUEST_ARGS);
Sunny Goyal2100c782016-08-22 16:00:03 -07001139 if (requestArgs != null) {
1140 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001141 }
Sunny Goyaldedda052019-05-14 15:23:48 -07001142 mPendingActivityRequestCode = savedState.getInt(RUNTIME_STATE_PENDING_REQUEST_CODE);
Sunny Goyal2100c782016-08-22 16:00:03 -07001143
1144 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001145
1146 SparseArray<Parcelable> widgetsState =
1147 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
1148 if (widgetsState != null) {
1149 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
1150 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001151 }
1152
1153 /**
1154 * Finds all the views we need and configure them properly.
1155 */
Tony Wickham04fca162020-02-05 15:06:52 -08001156 protected void setupViews() {
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001157 mDragLayer = findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001158 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -07001159 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001160 mWorkspace.initParentViews(mDragLayer);
Sreyas321bfae2020-02-21 17:53:02 -08001161 mOverviewPanel = findViewById(R.id.overview_panel);
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001162 mHotseat = findViewById(R.id.hotseat);
Craig Mautner360310b2012-10-26 15:13:08 -07001163
Sunny Goyal784f9c32016-03-23 16:56:54 -07001164 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1165 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1166 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001167
Winson Chung4c98d922011-05-31 16:50:48 -07001168 // Setup the drag layer
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001169 mDragLayer.setup(mDragController, mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001170
Hyunyoung Song645764e2016-06-06 14:19:02 -07001171 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001172 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1173 // default state, otherwise we will update to the wrong offsets in RTL
1174 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001175 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001176 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001177
Tony Wickham34d2c912015-09-11 09:27:58 -07001178 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001179 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001180
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001181 // Setup Apps
1182 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001183
Govinda Wassermanc06e1512019-04-09 09:56:53 -04001184 // Setup Scrim
1185 mScrimView = findViewById(R.id.scrim_view);
1186
Winson Chung3d503fb2011-07-13 17:25:49 -07001187 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Sunny Goyal47328fd2016-05-25 18:56:41 -07001188 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001189
Tony Wickham490a8ab2020-04-01 17:03:58 -07001190 mAllAppsController.setupViews(mAppsView, mScrimView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001191 }
1192
1193 /**
1194 * Creates a view representing a shortcut.
1195 *
1196 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001197 */
Sunny Goyal95899162019-03-27 16:03:06 -07001198 View createShortcut(WorkspaceItemInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001199 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001200 }
1201
1202 /**
1203 * Creates a view representing a shortcut inflated from the specified resource.
1204 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001205 * @param parent The group the shortcut belongs to.
Samuel Fufaca37b8a2019-08-19 17:04:36 -07001206 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001207 * @return A View inflated from layoutResId.
1208 */
Sunny Goyal95899162019-03-27 16:03:06 -07001209 public View createShortcut(ViewGroup parent, WorkspaceItemInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -08001210 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
1211 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal95899162019-03-27 16:03:06 -07001212 favorite.applyFromWorkspaceItem(info);
Sunny Goyal8a2a63b2018-03-06 22:15:18 -08001213 favorite.setOnClickListener(ItemClickHandler.INSTANCE);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001214 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001215 return favorite;
1216 }
1217
1218 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -07001219 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001220 *
1221 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001222 */
Sunny Goyalefb7e842018-10-04 15:11:00 -07001223 private void completeAddShortcut(Intent data, int container, int screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001224 int cellY, PendingRequestArgs args) {
Pinyao Ting5eee5f42019-10-31 16:47:35 -07001225 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
Jon Mirandac476d6e2017-05-04 16:30:01 -07001226 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001227 return;
1228 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001229
Jon Mirandac476d6e2017-05-04 16:30:01 -07001230 int[] cellXY = mTmpAddItemCellCoordinates;
1231 CellLayout layout = getCellLayout(container, screenId);
1232
Sunny Goyal95899162019-03-27 16:03:06 -07001233 WorkspaceItemInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001234 if (Utilities.ATLEAST_OREO) {
Sunny Goyale7b00122019-10-02 16:13:34 -07001235 info = PinRequestHelper.createWorkspaceItemFromPinItemRequest(
1236 this, PinRequestHelper.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001237 }
1238
1239 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001240 // Legacy shortcuts are only supported for primary profile.
1241 info = Process.myUserHandle().equals(args.user)
1242 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001243
Sunny Goyalb57645f2017-03-20 13:57:28 -07001244 if (info == null) {
1245 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1246 return;
1247 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001248 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001249 // The app is trying to add a shortcut without sufficient permissions
1250 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1251 return;
1252 }
1253 }
1254
Jon Mirandac476d6e2017-05-04 16:30:01 -07001255 if (container < 0) {
1256 // Adding a shortcut to the Workspace.
1257 final View view = createShortcut(info);
1258 boolean foundCellSpan = false;
1259 // First we check if we already know the exact location where we want to add this item.
1260 if (cellX >= 0 && cellY >= 0) {
1261 cellXY[0] = cellX;
1262 cellXY[1] = cellY;
1263 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001264
Hyunyoung Song1ae42422020-01-26 23:25:14 -08001265 DragObject dragObject = new DragObject(getApplicationContext());
1266 dragObject.dragInfo = info;
Jon Mirandac476d6e2017-05-04 16:30:01 -07001267 // If appropriate, either create a folder or add to an existing folder
1268 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Hyunyoung Song1ae42422020-01-26 23:25:14 -08001269 true, dragObject)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001270 return;
1271 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001272 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1273 true)) {
1274 return;
1275 }
1276 } else {
1277 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1278 }
1279
1280 if (!foundCellSpan) {
1281 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001282 return;
1283 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001284
1285 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1286 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001287 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001288 // Adding a shortcut to a Folder.
Sunny Goyale29897f52017-07-20 10:09:42 -07001289 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001290 if (folderIcon != null) {
1291 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1292 folderInfo.add(info, args.rank, false);
1293 } else {
Sunny Goyale29897f52017-07-20 10:09:42 -07001294 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001295 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001296 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001297 }
1298
Sunny Goyalefb7e842018-10-04 15:11:00 -07001299 public FolderIcon findFolderIcon(final int folderIconId) {
Sunny Goyal83fd25e2018-07-09 16:47:01 -07001300 return (FolderIcon) mWorkspace.getHomescreenIconByItemId(folderIconId);
Sunny Goyale29897f52017-07-20 10:09:42 -07001301 }
1302
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001303 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001304 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001305 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001306 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001307 */
Samuel Fufaca37b8a2019-08-19 17:04:36 -07001308 @Thunk
1309 void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001310 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1311
Adam Cohened66b2b2012-01-23 17:28:51 -08001312 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001313 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001314 }
Romain Guycbb89e42009-06-08 15:52:54 -07001315
Adam Cohen59400422014-03-05 18:07:04 -08001316 LauncherAppWidgetInfo launcherInfo;
1317 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001318 launcherInfo.spanX = itemInfo.spanX;
1319 launcherInfo.spanY = itemInfo.spanY;
1320 launcherInfo.minSpanX = itemInfo.minSpanX;
1321 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001322 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001323
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001324 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001325 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001326
Sunny Goyal2100c782016-08-22 16:00:03 -07001327 if (hostView == null) {
1328 // Perform actual inflation because we're live
1329 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001330 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001331 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301332 prepareAppWidget(hostView, launcherInfo);
1333 mWorkspace.addInScreen(hostView, launcherInfo);
Pinyao Tingc9074272019-11-11 11:51:44 -08001334 announceForAccessibility(R.string.item_added_to_workspace);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001335 }
Romain Guycbb89e42009-06-08 15:52:54 -07001336
Sunny Goyald5462aa2016-11-22 16:52:39 +05301337 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001338 hostView.setTag(item);
1339 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001340 hostView.setFocusable(true);
1341 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001342 }
1343
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001344 private final BroadcastReceiver mScreenOffReceiver = new BroadcastReceiver() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001345 @Override
1346 public void onReceive(Context context, Intent intent) {
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001347 // Reset AllApps to its initial state only if we are not in the middle of
1348 // processing a multi-step drop
1349 if (mPendingRequestArgs == null) {
Riddle Hsu7a7468f2019-11-21 01:32:58 +08001350 if (!isInState(NORMAL)) {
1351 onUiChangedWhileSleeping();
1352 }
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001353 mStateManager.goToState(NORMAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001354 }
1355 }
1356 };
1357
Riddle Hsu7a7468f2019-11-21 01:32:58 +08001358 protected void onUiChangedWhileSleeping() { }
1359
Sunny Goyal9c2b9602020-01-07 13:07:55 -08001360 private void updateNotificationDots(Predicate<PackageUserKey> updatedDots) {
Tony Wickhamf34bee82018-12-03 18:11:39 -08001361 mWorkspace.updateNotificationDots(updatedDots);
1362 mAppsView.getAppsStore().updateNotificationDots(updatedDots);
Tony Wickham010d2552017-01-20 08:15:28 -08001363 }
1364
Adam Cohended9f8d2010-11-03 13:25:16 -07001365 @Override
1366 public void onAttachedToWindow() {
1367 super.onAttachedToWindow();
Sunny Goyalb1d7de22019-09-06 10:36:54 -07001368 mOverlayManager.onAttachedToWindow();
Adam Cohended9f8d2010-11-03 13:25:16 -07001369 }
1370
1371 @Override
1372 public void onDetachedFromWindow() {
1373 super.onDetachedFromWindow();
Sunny Goyalb1d7de22019-09-06 10:36:54 -07001374 mOverlayManager.onDetachedFromWindow();
1375 closeContextMenu();
Adam Cohended9f8d2010-11-03 13:25:16 -07001376 }
1377
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001378 public AllAppsTransitionController getAllAppsController() {
1379 return mAllAppsController;
1380 }
1381
Sunny Goyal9d69c8d2018-03-19 13:41:31 -07001382 @Override
Winson Chung1a341002018-01-17 10:00:23 -08001383 public LauncherRootView getRootView() {
1384 return (LauncherRootView) mLauncherView;
1385 }
1386
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001387 @Override
Winson Chunga6945242014-01-08 14:04:34 -08001388 public DragLayer getDragLayer() {
1389 return mDragLayer;
1390 }
1391
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001392 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001393 return mAppsView;
1394 }
1395
Winson Chunga6945242014-01-08 14:04:34 -08001396 public Workspace getWorkspace() {
1397 return mWorkspace;
1398 }
1399
1400 public Hotseat getHotseat() {
1401 return mHotseat;
1402 }
1403
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001404 public <T extends View> T getOverviewPanel() {
Winson Chung8ae41982017-11-10 11:42:13 -08001405 return (T) mOverviewPanel;
Winson Chunga6945242014-01-08 14:04:34 -08001406 }
1407
Sunny Goyal47328fd2016-05-25 18:56:41 -07001408 public DropTargetBar getDropTargetBar() {
1409 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001410 }
1411
Tony Wickham490a8ab2020-04-01 17:03:58 -07001412 public ScrimView getScrimView() {
1413 return mScrimView;
1414 }
1415
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001416 public LauncherAppWidgetHost getAppWidgetHost() {
1417 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001418 }
Romain Guycbb89e42009-06-08 15:52:54 -07001419
Winson Chunga9abd0e2010-10-27 17:18:37 -07001420 public LauncherModel getModel() {
1421 return mModel;
1422 }
1423
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001424 public ModelWriter getModelWriter() {
1425 return mModelWriter;
1426 }
1427
Sunny Goyalb1d7de22019-09-06 10:36:54 -07001428 @Override
Adam Cohen79d90c52016-04-22 13:29:20 -07001429 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001430 return mSharedPrefs;
1431 }
1432
Sunny Goyalb1d7de22019-09-06 10:36:54 -07001433 @Override
1434 public SharedPreferences getDevicePrefs() {
1435 return Utilities.getDevicePrefs(this);
1436 }
1437
Samuel Fufaca37b8a2019-08-19 17:04:36 -07001438 public int getOrientation() {
1439 return mOldConfig.orientation;
1440 }
Jon Miranda6bed3502017-09-19 14:43:17 -07001441
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001442 @Override
1443 protected void onNewIntent(Intent intent) {
vadimtbaf5fbb2019-12-16 18:56:09 -08001444 if (Utilities.IS_RUNNING_IN_TEST_HARNESS) {
1445 Log.d(TestProtocol.PERMANENT_DIAG_TAG, "Launcher.onNewIntent: " + intent);
1446 }
Winson Chung5cc62c72019-10-16 11:32:41 -07001447 Object traceToken = TraceHelper.INSTANCE.beginSection(ON_NEW_INTENT_EVT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001448 super.onNewIntent(intent);
1449
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001450 boolean alreadyOnHome = hasWindowFocus() && ((intent.getFlags() &
Winson15f8b172015-08-19 11:02:39 -07001451 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1452 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001453
1454 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001455 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001456 && AbstractFloatingView.getTopOpenView(this) == null;
Winson15f8b172015-08-19 11:02:39 -07001457 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
Sunny Goyale84c5b82019-09-26 17:05:31 -07001458 boolean internalStateHandled = ACTIVITY_TRACKER.handleNewIntent(this, intent);
Sunny Goyald3864fa2017-11-14 15:06:36 -08001459
Winson15f8b172015-08-19 11:02:39 -07001460 if (isActionMain) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001461 if (!internalStateHandled) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001462 // In all these cases, only animate if we're already on home
Andy Wickham65ced1b2020-03-07 02:14:19 +00001463 closeOpenViews(isStarted());
Jon Mirandafeba90f2016-10-06 10:53:29 -07001464
Sunny Goyala2467272018-03-16 14:53:05 -07001465 if (!isInState(NORMAL)) {
1466 // Only change state, if not already the same. This prevents cancelling any
1467 // animations running as part of resume
1468 mStateManager.goToState(NORMAL);
1469 }
Sunny Goyald3864fa2017-11-14 15:06:36 -08001470
1471 // Reset the apps view
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001472 if (!alreadyOnHome) {
Sunny Goyal7185dd62018-03-14 17:51:49 -07001473 mAppsView.reset(isStarted() /* animate */);
Sunny Goyald3864fa2017-11-14 15:06:36 -08001474 }
1475
Sunny Goyalaad33592018-08-07 17:20:35 -07001476 if (shouldMoveToDefaultScreen && !mWorkspace.isHandlingTouch()) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001477 mWorkspace.post(mWorkspace::moveToDefaultScreen);
1478 }
1479 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001480
Hyunyoung Song2a70b3d2019-06-11 12:47:07 -07001481 // Handle HOME_INTENT
1482 UserEventDispatcher ued = getUserEventDispatcher();
1483 Target target = newContainerTarget(mStateManager.getState().containerType);
1484 target.pageIndex = mWorkspace.getCurrentPage();
1485 ued.logActionCommand(Action.Command.HOME_INTENT, target,
1486 newContainerTarget(ContainerType.WORKSPACE));
Sunny Goyaldeb91c42020-03-24 02:17:59 -07001487 hideKeyboard();
Adam Cohen6fecd412013-10-02 17:41:50 -07001488
Adam Cohen9211d422014-10-07 18:14:53 -07001489 if (mLauncherCallbacks != null) {
Sunny Goyal85462132018-04-24 11:39:37 -07001490 mLauncherCallbacks.onHomeIntent(internalStateHandled);
Adam Cohen9211d422014-10-07 18:14:53 -07001491 }
Sunny Goyalb1d7de22019-09-06 10:36:54 -07001492 mOverlayManager.hideOverlay(isStarted() && !isForceInvisible());
Sunny Goyal512a2c12020-04-08 12:52:45 -07001493 } else if (Intent.ACTION_ALL_APPS.equals(intent.getAction())) {
1494 getStateManager().goToState(ALL_APPS, alreadyOnHome);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001495 }
Winson15f8b172015-08-19 11:02:39 -07001496
Winson Chung5cc62c72019-10-16 11:32:41 -07001497 TraceHelper.INSTANCE.endSection(traceToken);
Adam Coppa120b8e2013-11-12 16:26:04 +00001498 }
1499
Sunny Goyaldeb91c42020-03-24 02:17:59 -07001500 /**
1501 * Hides the keyboard if visible
1502 */
1503 public void hideKeyboard() {
1504 final View v = getWindow().peekDecorView();
1505 if (v != null && v.getWindowToken() != null) {
1506 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
1507 }
1508 }
1509
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001510 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001511 public void onRestoreInstanceState(Bundle state) {
1512 super.onRestoreInstanceState(state);
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001513 mWorkspace.restoreInstanceStateForChild(mSynchronouslyBoundPage);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001514 }
1515
1516 @Override
1517 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001518 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001519 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001520
Adam Cohen21cd0022013-10-09 18:57:02 -07001521 }
Sunny Goyalea609262017-10-25 15:47:38 -07001522 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001523
1524
1525 AbstractFloatingView widgets = AbstractFloatingView
1526 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1527 if (widgets != null) {
1528 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1529 widgets.saveHierarchyState(widgetsState);
1530 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1531 } else {
1532 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1533 }
1534
Pinyao Tinga74b63a2019-07-17 23:26:06 -07001535 // We close any open folders and shortcut containers that are not safe for rebind,
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001536 // and we need to make sure this state is reflected.
Pinyao Tinga74b63a2019-07-17 23:26:06 -07001537 AbstractFloatingView.closeOpenViews(this, false, TYPE_ALL & ~TYPE_REBIND_SAFE);
1538 finishAutoCancelActionMode();
Tony Wickham49c8d292016-07-01 11:44:34 -07001539
Sunny Goyal2100c782016-08-22 16:00:03 -07001540 if (mPendingRequestArgs != null) {
1541 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1542 }
Sunny Goyaldedda052019-05-14 15:23:48 -07001543 outState.putInt(RUNTIME_STATE_PENDING_REQUEST_CODE, mPendingActivityRequestCode);
1544
Sunny Goyal2100c782016-08-22 16:00:03 -07001545 if (mPendingActivityResult != null) {
1546 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001547 }
1548
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001549 super.onSaveInstanceState(outState);
Sunny Goyalb1d7de22019-09-06 10:36:54 -07001550 mOverlayManager.onActivitySaveInstanceState(this, outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001551 }
1552
1553 @Override
1554 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001555 super.onDestroy();
Sunny Goyale84c5b82019-09-26 17:05:31 -07001556 ACTIVITY_TRACKER.onActivityDestroyed(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001557
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001558 unregisterReceiver(mScreenOffReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001559 mWorkspace.removeFolderListeners();
Sunny Goyalb1d7de22019-09-06 10:36:54 -07001560 PluginManagerWrapper.INSTANCE.get(this).removePluginListener(this);
Winson Chunge7a03942011-08-05 15:05:12 -07001561
Sunny Goyala7a5bf32020-01-05 15:35:29 +05301562 mModel.removeCallbacks(this);
Sunny Goyal623eddd2018-03-02 12:24:41 -08001563 mRotationHelper.destroy();
Sunny Goyal745bad92016-05-02 10:54:12 -07001564
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001565 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001566 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001567 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001568 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001569 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001570
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001571 TextKeyListener.getInstance().release();
Tony2917a8b2017-07-31 23:29:42 -07001572 clearPendingBinds();
Hyunyoung Song31971a32019-01-27 12:31:12 -08001573 LauncherAppState.getIDP(this).removeOnChangeListener(this);
Sunny Goyalb1d7de22019-09-06 10:36:54 -07001574
1575 mOverlayManager.onActivityDestroyed(this);
Winson Chung24ab40c2019-10-30 22:35:09 -07001576 mAppTransitionManager.unregisterRemoteAnimations();
Samuel Fufa41d909f2020-03-20 16:27:09 -07001577 mUserChangedCallbackCloseable.close();
Hyunyoung Song3f821352020-04-09 20:14:58 -07001578 mAllAppsController.onActivityDestroyed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001579 }
1580
Sunny Goyalae502842016-06-17 08:43:56 -07001581 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1582 return mAccessibilityDelegate;
1583 }
1584
Adam Cohena9cf38f2011-05-02 15:36:58 -07001585 public DragController getDragController() {
1586 return mDragController;
1587 }
1588
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001589 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001590 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyaldedda052019-05-14 15:23:48 -07001591 if (requestCode != -1) {
1592 mPendingActivityRequestCode = requestCode;
1593 }
Sunny Goyal210e1742019-10-17 12:05:38 -07001594 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001595 }
1596
1597 @Override
Sunny Goyaldedda052019-05-14 15:23:48 -07001598 public void startIntentSenderForResult(IntentSender intent, int requestCode,
Adam Cohen173f7112015-03-27 15:14:00 -07001599 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Sunny Goyaldedda052019-05-14 15:23:48 -07001600 if (requestCode != -1) {
1601 mPendingActivityRequestCode = requestCode;
1602 }
Sunny Goyal210e1742019-10-17 12:05:38 -07001603 try {
1604 super.startIntentSenderForResult(intent, requestCode,
1605 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1606 } catch (IntentSender.SendIntentException e) {
1607 throw new ActivityNotFoundException();
Adam Cohen173f7112015-03-27 15:14:00 -07001608 }
1609 }
1610
Winson Chung70d72102011-08-12 11:24:35 -07001611 /**
1612 * Indicates that we want global search for this activity by setting the globalSearch
1613 * argument for {@link #startSearch} to true.
1614 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001615 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001616 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001617 Bundle appSearchData, boolean globalSearch) {
1618 if (appSearchData == null) {
1619 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001620 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001621 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001622
Sunny Goyal6f28e712016-09-13 14:19:29 -07001623 if (mLauncherCallbacks == null ||
1624 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1625 // Starting search from the callbacks failed. Start the default global search.
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001626 super.startSearch(initialQuery, selectInitialQuery, appSearchData, true);
Ian Parkinson047036e2014-09-01 15:40:53 +01001627 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001628
1629 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001630 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001631 }
1632
Joe Onorato9c1289c2009-08-17 11:03:03 -04001633 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001634 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001635 }
1636
Adam Cohen517a7f52014-03-01 12:12:59 -08001637 public boolean isWorkspaceLoading() {
1638 return mWorkspaceLoading;
1639 }
1640
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001641 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001642 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001643 }
1644
Sunny Goyal04a324a2017-01-20 21:08:59 -08001645 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001646 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001647 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001648
Sunny Goyal2100c782016-08-22 16:00:03 -07001649 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001650 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001651 if (LOGD) {
1652 Log.d(TAG, "Adding widget from drop");
1653 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001654 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001655 }
1656
Sunny Goyal2100c782016-08-22 16:00:03 -07001657 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001658 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
Samuel Fufaca37b8a2019-08-19 17:04:36 -07001659 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info,
1660 REQUEST_CREATE_APPWIDGET)) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001661 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001662
Adam Cohenad4e15c2013-10-17 16:21:35 -07001663 Runnable onComplete = new Runnable() {
1664 @Override
1665 public void run() {
1666 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001667 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001668 }
1669 };
Samuel Fufaca37b8a2019-08-19 17:04:36 -07001670 completeAddAppWidget(appWidgetId, info, boundWidget,
1671 addFlowHandler.getProviderInfo(this));
Sunny Goyal6e379fc2020-03-19 16:08:14 -07001672 mWorkspace.removeExtraEmptyScreenDelayed(delay, false, onComplete);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001673 }
1674 }
Romain Guycbb89e42009-06-08 15:52:54 -07001675
Sunny Goyalefb7e842018-10-04 15:11:00 -07001676 public void addPendingItem(PendingAddItemInfo info, int container, int screenId,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001677 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001678 info.container = container;
1679 info.screenId = screenId;
1680 if (cell != null) {
1681 info.cellX = cell[0];
1682 info.cellY = cell[1];
1683 }
1684 info.spanX = spanX;
1685 info.spanY = spanY;
1686
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001687 switch (info.itemType) {
1688 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1689 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001690 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001691 break;
1692 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001693 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001694 break;
1695 default:
1696 throw new IllegalStateException("Unknown item type: " + info.itemType);
Samuel Fufaca37b8a2019-08-19 17:04:36 -07001697 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001698 }
1699
Adam Cohenfbba09b2011-07-18 21:43:05 -07001700 /**
1701 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001702 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001703 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001704 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1705 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
vadimtcf275c12020-04-15 12:43:01 -07001706 TestLogging.recordEvent(TestProtocol.SEQUENCE_MAIN, "start: processShortcutFromDrop");
Sunny Goyal782f0c92017-01-19 10:27:54 -08001707 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1708 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1709 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001710 }
1711
Adam Cohenfbba09b2011-07-18 21:43:05 -07001712 /**
1713 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001714 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001715 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001716 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001717 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001718 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001719 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001720 // In the case where we've prebound the widget, we remove it from the DragLayer
1721 if (LOGD) {
1722 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1723 }
1724 getDragLayer().removeView(hostView);
1725
Adam Cohened66b2b2012-01-23 17:28:51 -08001726 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001727 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001728
1729 // Clear the boundWidget so that it doesn't get destroyed.
1730 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001731 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001732 // In this case, we either need to start an activity to get permission to bind
1733 // the widget, or we need to start an activity to configure the widget, or both.
Pinyao Tingc7a6c292019-08-26 14:36:02 -07001734 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1735 appWidgetId = CustomWidgetManager.INSTANCE.get(this).getWidgetIdForCustomProvider(
1736 info.componentName);
Sunny Goyal952e63d2017-08-16 04:59:08 -07001737 } else {
1738 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1739 }
Adam Cohendd70d662012-10-04 16:53:44 -07001740 Bundle options = info.bindOptions;
1741
Sunny Goyalffe83f12014-08-14 17:39:34 -07001742 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1743 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001744 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001745 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001746 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001747 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001748 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001749 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001750 }
1751
Samuel Fufa746d4202020-02-02 18:55:44 -08001752 /**
1753 * Creates and adds new folder to CellLayout
1754 */
Samuel Fufa28c3e452020-02-08 16:07:16 -08001755 public FolderIcon addFolder(CellLayout layout, int container, final int screenId, int cellX,
1756 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001757 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001758
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001759 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001760 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001761
1762 // Create the view
Samuel Fufa746d4202020-02-02 18:55:44 -08001763 FolderIcon newFolder = FolderIcon.inflateFolderAndIcon(R.layout.folder_icon, this, layout,
1764 folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301765 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001766 // Force measure the new folder icon
1767 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1768 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001769 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001770 }
Romain Guycbb89e42009-06-08 15:52:54 -07001771
Winsonc0b52fe2015-09-09 16:38:15 -07001772 /**
Samuel Fufa28c3e452020-02-08 16:07:16 -08001773 * Called when a workspace item is converted into a folder
1774 */
1775 public void folderCreatedFromItem(Folder folder, WorkspaceItemInfo itemInfo){}
1776
1777 /**
1778 * Called when a folder is converted into a workspace item
1779 */
1780 public void folderConvertedToItem(Folder folder, WorkspaceItemInfo itemInfo) {}
1781
1782 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001783 * Unbinds the view for the specified item, and removes the item and all its children.
1784 *
1785 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001786 * @param itemInfo the {@link ItemInfo} for this view.
1787 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001788 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001789 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Sunny Goyal95899162019-03-27 16:03:06 -07001790 if (itemInfo instanceof WorkspaceItemInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001791 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001792 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1793 if (folderIcon instanceof FolderIcon) {
Sunny Goyal95899162019-03-27 16:03:06 -07001794 ((FolderInfo) folderIcon.getTag()).remove((WorkspaceItemInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001795 } else {
1796 mWorkspace.removeWorkspaceItem(v);
1797 }
Winsonc0b52fe2015-09-09 16:38:15 -07001798 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001799 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001800 }
1801 } else if (itemInfo instanceof FolderInfo) {
1802 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001803 if (v instanceof FolderIcon) {
1804 ((FolderIcon) v).removeListeners();
1805 }
Winsonc0b52fe2015-09-09 16:38:15 -07001806 mWorkspace.removeWorkspaceItem(v);
1807 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001808 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001809 }
1810 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1811 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001812 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001813 if (deleteFromDb) {
Tony Wickham6a71a5b2018-08-21 11:40:23 -07001814 getModelWriter().deleteWidgetInfo(widgetInfo, getAppWidgetHost());
Winsonc0b52fe2015-09-09 16:38:15 -07001815 }
1816 } else {
1817 return false;
1818 }
1819 return true;
1820 }
1821
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001822 @Override
1823 public boolean dispatchKeyEvent(KeyEvent event) {
vadimtd633c9c2020-01-22 18:00:37 -08001824 TestLogging.recordEvent(TestProtocol.SEQUENCE_MAIN, "Key event", event);
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001825 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001826 }
1827
Joe Onorato88ec0992009-11-19 13:16:06 -08001828 @Override
vadimt9af34a72020-01-21 11:19:13 -08001829 public boolean dispatchTouchEvent(MotionEvent ev) {
Sunny Goyala306e592020-03-24 01:51:41 -07001830 switch (ev.getAction()) {
1831 case MotionEvent.ACTION_DOWN:
1832 mTouchInProgress = true;
1833 break;
1834 case MotionEvent.ACTION_UP:
1835 mLastTouchUpTime = System.currentTimeMillis();
1836 // Follow through
1837 case MotionEvent.ACTION_CANCEL:
1838 mTouchInProgress = false;
1839 break;
Sunny Goyal9b180102020-03-11 10:02:29 -07001840 }
vadimtd633c9c2020-01-22 18:00:37 -08001841 TestLogging.recordMotionEvent(TestProtocol.SEQUENCE_MAIN, "Touch event", ev);
vadimt9af34a72020-01-21 11:19:13 -08001842 return super.dispatchTouchEvent(ev);
1843 }
1844
Sunny Goyala306e592020-03-24 01:51:41 -07001845 /**
1846 * Returns true if a touch interaction is in progress
1847 */
1848 public boolean isTouchInProgress() {
1849 return mTouchInProgress;
1850 }
1851
vadimt9af34a72020-01-21 11:19:13 -08001852 @Override
Joe Onorato88ec0992009-11-19 13:16:06 -08001853 public void onBackPressed() {
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08001854 if (finishAutoCancelActionMode()) {
1855 return;
1856 }
Adam Cohen9211d422014-10-07 18:14:53 -07001857
Sunny Goyal45478022015-06-08 16:52:41 -07001858 if (mDragController.isDragging()) {
1859 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001860 return;
1861 }
1862
Jon Mirandafeba90f2016-10-06 10:53:29 -07001863 // Note: There should be at most one log per method call. This is enforced implicitly
1864 // by using if-else statements.
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001865 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
Tony Wickham52c1b662018-05-21 13:13:58 -07001866 if (topView != null && topView.onBackPressed()) {
1867 // Handled by the floating view.
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001868 } else {
Sunny Goyale39690b2018-08-02 13:35:07 -07001869 mStateManager.getState().onBackPressed(this);
Michael Jurkaaf442092010-06-10 17:01:57 -07001870 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001871 }
1872
Sunny Goyalfe770c92016-09-27 14:38:58 -07001873 @TargetApi(Build.VERSION_CODES.M)
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001874 @Override
Jon Miranda73c27ec2018-05-16 18:06:23 -07001875 public ActivityOptions getActivityLaunchOptions(View v) {
1876 return mAppTransitionManager.getActivityLaunchOptions(this, v);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001877 }
1878
Tony Wickham3a6746a2018-03-29 09:39:56 -07001879 public LauncherAppTransitionManager getAppTransitionManager() {
1880 return mAppTransitionManager;
1881 }
1882
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001883 @TargetApi(Build.VERSION_CODES.M)
1884 @Override
1885 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
1886 // Due to legacy reasons, direct call shortcuts require Launchers to have the
1887 // corresponding permission. Show the appropriate permission prompt if that
1888 // is the case.
1889 if (intent.getComponent() == null
1890 && Intent.ACTION_CALL.equals(intent.getAction())
1891 && checkSelfPermission(android.Manifest.permission.CALL_PHONE) !=
1892 PackageManager.PERMISSION_GRANTED) {
1893
1894 setWaitingForResult(PendingRequestArgs
1895 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
1896 requestPermissions(new String[]{android.Manifest.permission.CALL_PHONE},
1897 REQUEST_PERMISSION_CALL_PHONE);
1898 return true;
1899 } else {
1900 return false;
1901 }
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001902 }
1903
Hyunyoung Song46d07f72018-05-22 15:41:25 -07001904 @Override
Hyunyoung Songfc007472018-10-25 14:09:50 -07001905 public int getCurrentState() {
Samuel Fufaca37b8a2019-08-19 17:04:36 -07001906 if (mStateManager.getState() == LauncherState.ALL_APPS) {
Hyunyoung Songfc007472018-10-25 14:09:50 -07001907 return StatsLogUtils.LAUNCHER_STATE_ALLAPPS;
Govinda Wassermanc06e1512019-04-09 09:56:53 -04001908 } else if (mStateManager.getState() == OVERVIEW) {
Hyunyoung Songfc007472018-10-25 14:09:50 -07001909 return StatsLogUtils.LAUNCHER_STATE_OVERVIEW;
1910 }
Hyunyoung Song0ae38882018-11-05 14:45:19 -08001911 return StatsLogUtils.LAUNCHER_STATE_HOME;
Hyunyoung Songfc007472018-10-25 14:09:50 -07001912 }
1913
1914 @Override
Sunny Goyal369212a2019-03-26 15:03:57 -07001915 public boolean startActivitySafely(View v, Intent intent, ItemInfo item,
1916 @Nullable String sourceContainer) {
Winson Chung0b70cd42019-06-17 22:34:33 -07001917 if (!hasBeenResumed()) {
1918 // Workaround an issue where the WM launch animation is clobbered when finishing the
1919 // recents animation into launcher. Defer launching the activity until Launcher is
1920 // next resumed.
1921 addOnResumeCallback(() -> startActivitySafely(v, intent, item, sourceContainer));
Winson Chungc9bf6d42019-10-04 15:33:18 -07001922 if (mOnDeferredActivityLaunchCallback != null) {
1923 mOnDeferredActivityLaunchCallback.run();
1924 mOnDeferredActivityLaunchCallback = null;
1925 }
Winson Chung0b70cd42019-06-17 22:34:33 -07001926 return true;
1927 }
1928
Sunny Goyal369212a2019-03-26 15:03:57 -07001929 boolean success = super.startActivitySafely(v, intent, item, sourceContainer);
Hyunyoung Song1241e612018-05-15 21:46:51 -07001930 if (success && v instanceof BubbleTextView) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001931 // This is set to the view that launched the activity that navigated the user away
1932 // from launcher. Since there is no callback for when the activity has finished
1933 // launching, enable the press state and keep this reference to reset the press
1934 // state when we return to launcher.
1935 BubbleTextView btv = (BubbleTextView) v;
1936 btv.setStayPressed(true);
Winson Chung0b70cd42019-06-17 22:34:33 -07001937 addOnResumeCallback(btv);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001938 }
Hyunyoung Song1241e612018-05-15 21:46:51 -07001939 return success;
Michael Jurka86a720e2012-05-09 11:23:23 -07001940 }
1941
Winson Chung3d503fb2011-07-13 17:25:49 -07001942 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001943 // TODO: Remove this method
Sunny Goyal876e4622018-11-02 13:50:40 -07001944 return mHotseat != null && (layout == mHotseat);
Winson Chung3d503fb2011-07-13 17:25:49 -07001945 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001946
Winson Chung3d503fb2011-07-13 17:25:49 -07001947 /**
1948 * Returns the CellLayout of the specified container at the specified screen.
1949 */
Sunny Goyalefb7e842018-10-04 15:11:00 -07001950 public CellLayout getCellLayout(int container, int screenId) {
Sunny Goyal876e4622018-11-02 13:50:40 -07001951 return (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1952 ? mHotseat : mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08001953 }
1954
Michael Jurkae326f182011-11-21 14:05:46 -08001955 @Override
1956 public void onTrimMemory(int level) {
1957 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07001958 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
1959 // The widget preview db can result in holding onto over
1960 // 3MB of memory for caching which isn't necessary.
1961 SQLiteDatabase.releaseMemory();
1962
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08001963 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07001964 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08001965 }
1966 }
1967
Michael Jurkad7c28052012-04-27 15:43:36 -07001968 @Override
1969 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07001970 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07001971 final List<CharSequence> text = event.getText();
1972 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07001973 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001974 // TODO: When can workspace be null?
1975 text.add(mWorkspace == null
1976 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07001977 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07001978 return result;
1979 }
1980
Winson Chung0b70cd42019-06-17 22:34:33 -07001981 public void addOnResumeCallback(OnResumeCallback callback) {
1982 mOnResumeCallbacks.add(callback);
Michael Jurka1e2f4652013-07-08 18:03:46 -07001983 }
1984
Michael Jurka7607c2f2013-04-03 14:33:19 -07001985 /**
Winson Chungc9bf6d42019-10-04 15:33:18 -07001986 * Persistant callback which notifies when an activity launch is deferred because the activity
1987 * was not yet resumed.
1988 */
1989 public void setOnDeferredActivityLaunchCallback(Runnable callback) {
1990 mOnDeferredActivityLaunchCallback = callback;
1991 }
1992
1993 /**
Sunny Goyala7a5bf32020-01-05 15:35:29 +05301994 * Sets the next page to bind synchronously on next bind.
1995 * @param page
1996 */
1997 public void setPageToBindSynchronously(int page) {
1998 mPageToBindSynchronously = page;
1999 }
2000
2001 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002002 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002003 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002004 @Override
Sunny Goyala7a5bf32020-01-05 15:35:29 +05302005 public int getPageToBindSynchronously() {
2006 if (mPageToBindSynchronously != PagedView.INVALID_PAGE) {
2007 return mPageToBindSynchronously;
2008 } else if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002009 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002010 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07002011 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07002012 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002013 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002014
Joe Onorato9c1289c2009-08-17 11:03:03 -04002015 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07002016 * Clear any pending bind callbacks. This is called when is loader is planning to
2017 * perform a full rebind from scratch.
2018 */
2019 @Override
2020 public void clearPendingBinds() {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002021 if (mPendingExecutor != null) {
2022 mPendingExecutor.markCompleted();
2023 mPendingExecutor = null;
Sunny Goyal6b6d0a32019-08-05 17:19:32 -07002024
2025 // We might have set this flag previously and forgot to clear it.
2026 mAppsView.getAppsStore()
2027 .disableDeferUpdatesSilently(AllAppsStore.DEFER_UPDATES_NEXT_DRAW);
Sunny Goyal527c7d32015-08-28 15:19:36 -07002028 }
2029 }
2030
2031 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002032 * Refreshes the shortcuts shown on the workspace.
2033 *
2034 * Implementation of the method from LauncherModel.Callbacks.
2035 */
2036 public void startBinding() {
Winson Chung5cc62c72019-10-16 11:32:41 -07002037 Object traceToken = TraceHelper.INSTANCE.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002038 // Floating panels (except the full widget sheet) are associated with individual icons. If
2039 // we are starting a fresh bind, close all such panels as all the icons are about
2040 // to go away.
Pinyao Tinga74b63a2019-07-17 23:26:06 -07002041 AbstractFloatingView.closeOpenViews(this, true, TYPE_ALL & ~TYPE_REBIND_SAFE);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08002042
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002043 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08002044
Winson Chungd64d1762013-08-20 14:37:16 -07002045 // Clear the workspace because it's going to be rebound
Sunny Goyal2db53422019-03-01 16:06:12 -08002046 mDragController.cancelDrag();
2047
Adam Cohendf035382011-04-11 17:22:04 -07002048 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07002049 mWorkspace.removeAllWorkspaceScreens();
Sunny Goyalc11fac32018-02-27 19:20:15 -08002050 mAppWidgetHost.clearViews();
Winson Chungd64d1762013-08-20 14:37:16 -07002051
Winson Chung3d503fb2011-07-13 17:25:49 -07002052 if (mHotseat != null) {
Sunny Goyalc4d32012020-04-03 17:10:11 -07002053 mHotseat.resetLayout(getDeviceProfile().isVerticalBarLayout());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002054 }
Winson Chung5cc62c72019-10-16 11:32:41 -07002055 TraceHelper.INSTANCE.endSection(traceToken);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002056 }
2057
Adam Cohendcd297f2013-06-18 13:13:40 -07002058 @Override
Sunny Goyalefb7e842018-10-04 15:11:00 -07002059 public void bindScreens(IntArray orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002060 // Make sure the first screen is always at the start.
Jon Miranda7143ba62019-03-15 09:00:05 -07002061 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002062 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalefb7e842018-10-04 15:11:00 -07002063 orderedScreenIds.removeValue(Workspace.FIRST_SCREEN_ID);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002064 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Jon Miranda7143ba62019-03-15 09:00:05 -07002065 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002066 // If there are no screens, we need to have an empty screen
2067 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07002068 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002069 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07002070
Winsonc7d2e832016-07-28 12:24:55 -07002071 // After we have added all the screens, if the wallpaper was locked to the default state,
2072 // then notify to indicate that it can be released and a proper wallpaper offset can be
2073 // computed before the next layout
2074 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07002075 }
2076
Sunny Goyalefb7e842018-10-04 15:11:00 -07002077 private void bindAddScreens(IntArray orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002078 int count = orderedScreenIds.size();
2079 for (int i = 0; i < count; i++) {
Sunny Goyalefb7e842018-10-04 15:11:00 -07002080 int screenId = orderedScreenIds.get(i);
Jon Miranda7143ba62019-03-15 09:00:05 -07002081 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002082 // No need to bind the first screen, as its always bound.
2083 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
2084 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002085 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002086 }
2087
Sunny Goyalb23980c2017-08-17 07:45:25 -07002088 @Override
Tony Wickham6a71a5b2018-08-21 11:40:23 -07002089 public void preAddApps() {
2090 // If there's an undo snackbar, force it to complete to ensure empty screens are removed
2091 // before trying to add new items.
2092 mModelWriter.commitDelete();
2093 AbstractFloatingView snackbar = AbstractFloatingView.getOpenView(this, TYPE_SNACKBAR);
2094 if (snackbar != null) {
2095 snackbar.post(() -> snackbar.close(true));
2096 }
2097 }
2098
2099 @Override
Sunny Goyalefb7e842018-10-04 15:11:00 -07002100 public void bindAppsAdded(IntArray newScreens, ArrayList<ItemInfo> addNotAnimated,
Sunny Goyal29947f02017-12-18 13:49:44 -08002101 ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07002102 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08002103 if (newScreens != null) {
2104 bindAddScreens(newScreens);
2105 }
Winson Chungd64d1762013-08-20 14:37:16 -07002106
2107 // We add the items without animation on non-visible pages, and with
2108 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08002109 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002110 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002111 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002112 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002113 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07002114 }
Winson Chungc58497e2013-09-03 17:48:37 -07002115
Winson Chung87412982013-10-03 18:34:14 -07002116 // Remove the extra empty screen
Sunny Goyal6e379fc2020-03-19 16:08:14 -07002117 mWorkspace.removeExtraEmptyScreen(false);
Winson Chungd64d1762013-08-20 14:37:16 -07002118 }
2119
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002120 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002121 * Bind the items start-end from the list.
2122 *
2123 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002124 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07002125 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07002126 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002127 // Get the list of added items and intersect them with the set of items here
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002128 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07002129 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07002130 Workspace workspace = mWorkspace;
Sunny Goyalefb7e842018-10-04 15:11:00 -07002131 int newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07002132 int end = items.size();
2133 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002134 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002135
2136 // Short circuit if we are loading dock items for a configuration which has no dock
2137 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2138 mHotseat == null) {
2139 continue;
2140 }
2141
Sunny Goyal639e9062015-08-19 19:17:06 -07002142 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002143 switch (item.itemType) {
2144 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2145 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08002146 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Sunny Goyal95899162019-03-27 16:03:06 -07002147 WorkspaceItemInfo info = (WorkspaceItemInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07002148 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002149 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002150 }
2151 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Tracy Zhou4d7b2442019-12-09 13:42:57 -08002152 view = FolderIcon.inflateFolderAndIcon(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002153 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002154 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002155 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002156 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07002157 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2158 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07002159 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002160 if (view == null) {
2161 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002162 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08002163 break;
2164 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002165 default:
2166 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002167 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002168
Samuel Fufaca37b8a2019-08-19 17:04:36 -07002169 /*
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002170 * Remove colliding items.
2171 */
2172 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
2173 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
2174 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
2175 View v = cl.getChildAt(item.cellX, item.cellY);
2176 Object tag = v.getTag();
2177 String desc = "Collision while binding workspace item: " + item
2178 + ". Collides with " + tag;
Zak Cohen3eeb41d2020-02-14 14:15:13 -08002179 if (FeatureFlags.IS_STUDIO_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002180 throw (new RuntimeException(desc));
2181 } else {
2182 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002183 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002184 continue;
2185 }
2186 }
2187 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302188 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07002189 if (animateIcons) {
2190 // Animate all the applications up now
2191 view.setAlpha(0f);
2192 view.setScaleX(0f);
2193 view.setScaleY(0f);
2194 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08002195 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07002196 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002197 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07002198
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002199 // Animate to the correct page
2200 if (animateIcons && newItemsScreenId > -1) {
2201 AnimatorSet anim = new AnimatorSet();
2202 anim.playTogether(bounceAnims);
Jon Miranda64eb8ea2018-03-29 11:43:58 -07002203
Sunny Goyalefb7e842018-10-04 15:11:00 -07002204 int currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002205 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
2206 final Runnable startBounceAnimRunnable = anim::start;
2207
2208 if (newItemsScreenId != currentScreenId) {
2209 // We post the animation slightly delayed to prevent slowdowns
2210 // when we are loading right after we return to launcher.
2211 mWorkspace.postDelayed(new Runnable() {
2212 public void run() {
2213 if (mWorkspace != null) {
Andy Wickham65ced1b2020-03-07 02:14:19 +00002214 closeOpenViews(false);
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002215
2216 mWorkspace.snapToPage(newScreenIndex);
2217 mWorkspace.postDelayed(startBounceAnimRunnable,
2218 NEW_APPS_ANIMATION_DELAY);
Winson Chung94d67682013-09-25 16:29:40 -07002219 }
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002220 }
2221 }, NEW_APPS_PAGE_MOVE_DELAY);
2222 } else {
2223 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07002224 }
Winson Chung64359a52013-07-08 17:17:08 -07002225 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002226 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002227 }
2228
Samuel Fufa385e9322020-05-04 11:14:52 -07002229 @Override
2230 public void bindPredictedItems(List<AppInfo> appInfos, IntArray ranks) { }
2231
Joe Onorato9c1289c2009-08-17 11:03:03 -04002232 /**
2233 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002234 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07002235 public void bindAppWidget(LauncherAppWidgetInfo item) {
2236 View view = inflateAppWidget(item);
2237 if (view != null) {
2238 mWorkspace.addInScreen(view, item);
2239 mWorkspace.requestLayout();
2240 }
2241 }
2242
2243 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Samuel Fufaca37b8a2019-08-19 17:04:36 -07002244 if (item.hasOptionFlag(LauncherAppWidgetInfo.OPTION_SEARCH_WIDGET)) {
2245 item.providerName = QsbContainerView.getSearchComponentName(this);
2246 if (item.providerName == null) {
2247 getModelWriter().deleteItemFromDatabase(item);
2248 return null;
2249 }
2250 }
Sunny Goyal17c72fb2019-10-14 14:06:38 -07002251 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002252 if (mIsSafeModeEnabled) {
Sunny Goyal17c72fb2019-10-14 14:06:38 -07002253 view = new PendingAppWidgetHostView(this, item, mIconCache, true);
Sunny Goyald5462aa2016-11-22 16:52:39 +05302254 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002255 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002256 }
2257
Winson Chung5cc62c72019-10-16 11:32:41 -07002258 Object traceToken = TraceHelper.INSTANCE.beginSection("BIND_WIDGET_id=" + item.appWidgetId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002259
Sunny Goyal17c72fb2019-10-14 14:06:38 -07002260 try {
2261 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08002262
Sunny Goyal17c72fb2019-10-14 14:06:38 -07002263 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2264 // If the provider is not ready, bind as a pending widget.
2265 appWidgetInfo = null;
2266 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2267 // The widget id is not valid. Try to find the widget based on the provider info.
2268 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
2269 } else {
2270 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
Sunny Goyalff572272014-07-23 13:58:07 -07002271 }
Sunny Goyalff572272014-07-23 13:58:07 -07002272
Sunny Goyal17c72fb2019-10-14 14:06:38 -07002273 // If the provider is ready, but the width is not yet restored, try to restore it.
2274 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
2275 && (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
2276 if (appWidgetInfo == null) {
2277 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
2278 + " belongs to component " + item.providerName
2279 + ", as the provider is null");
2280 getModelWriter().deleteItemFromDatabase(item);
2281 return null;
2282 }
Sunny Goyalff572272014-07-23 13:58:07 -07002283
Sunny Goyal17c72fb2019-10-14 14:06:38 -07002284 // If we do not have a valid id, try to bind an id.
2285 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2286 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2287 // Id has not been allocated yet. Allocate a new id.
2288 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
2289 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyal86df1382016-08-10 15:03:22 -07002290
Sunny Goyal17c72fb2019-10-14 14:06:38 -07002291 // Also try to bind the widget. If the bind fails, the user will be shown
2292 // a click to setup UI, which will ask for the bind permission.
2293 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
2294 pendingInfo.spanX = item.spanX;
2295 pendingInfo.spanY = item.spanY;
2296 pendingInfo.minSpanX = item.minSpanX;
2297 pendingInfo.minSpanY = item.minSpanY;
2298 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this,
2299 pendingInfo);
2300
2301 boolean isDirectConfig =
2302 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2303 if (isDirectConfig && item.bindOptions != null) {
2304 Bundle newOptions = item.bindOptions.getExtras();
2305 if (options != null) {
2306 newOptions.putAll(options);
2307 }
2308 options = newOptions;
Sunny Goyal86df1382016-08-10 15:03:22 -07002309 }
Sunny Goyal17c72fb2019-10-14 14:06:38 -07002310 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2311 item.appWidgetId, appWidgetInfo, options);
2312
2313 // We tried to bind once. If we were not able to bind, we would need to
2314 // go through the permission dialog, which means we cannot skip the config
2315 // activity.
2316 item.bindOptions = null;
2317 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2318
2319 // Bind succeeded
2320 if (success) {
2321 // If the widget has a configure activity, it is still needs to set it
2322 // up, otherwise the widget is ready to go.
2323 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
2324 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2325 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
2326 }
2327
2328 getModelWriter().updateItemInDatabase(item);
Sunny Goyal86df1382016-08-10 15:03:22 -07002329 }
Sunny Goyal17c72fb2019-10-14 14:06:38 -07002330 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
2331 && (appWidgetInfo.configure == null)) {
2332 // The widget was marked as UI not ready, but there is no configure activity to
2333 // update the UI.
2334 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002335 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002336 }
Samuel Fufa9151c012020-02-24 17:06:28 -08002337 else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
2338 && appWidgetInfo.configure != null) {
2339 if (mAppWidgetManager.isAppWidgetRestored(item.appWidgetId)) {
2340 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
2341 getModelWriter().updateItemInDatabase(item);
2342 }
2343 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002344 }
2345
Sunny Goyal17c72fb2019-10-14 14:06:38 -07002346 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2347 // Verify that we own the widget
2348 if (appWidgetInfo == null) {
2349 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
2350 getModelWriter().deleteWidgetInfo(item, getAppWidgetHost());
2351 return null;
2352 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002353
Sunny Goyal17c72fb2019-10-14 14:06:38 -07002354 item.minSpanX = appWidgetInfo.minSpanX;
2355 item.minSpanY = appWidgetInfo.minSpanY;
2356 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Samuel Fufa9151c012020-02-24 17:06:28 -08002357 } else if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)
2358 && appWidgetInfo != null) {
2359 mAppWidgetHost.addPendingView(item.appWidgetId,
2360 new PendingAppWidgetHostView(this, item, mIconCache, false));
2361 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal17c72fb2019-10-14 14:06:38 -07002362 } else {
2363 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
2364 }
2365 prepareAppWidget(view, item);
2366 } finally {
Winson Chung5cc62c72019-10-16 11:32:41 -07002367 TraceHelper.INSTANCE.endSection(traceToken);
Sunny Goyal17c72fb2019-10-14 14:06:38 -07002368 }
2369
Sunny Goyalb23980c2017-08-17 07:45:25 -07002370 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002371 }
2372
Sunny Goyalff572272014-07-23 13:58:07 -07002373 /**
2374 * Restores a pending widget.
2375 *
2376 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002377 */
Sunny Goyald478c832016-04-01 12:04:16 -07002378 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002379 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2380 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2381 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002382 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002383 }
2384
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002385 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002386 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002387 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2388 info.pendingItemInfo = null;
2389 }
Sunny Goyalff572272014-07-23 13:58:07 -07002390
Sunny Goyalba47faa2017-10-04 16:50:57 -07002391 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002392 view.reInflate();
Sunny Goyalba47faa2017-10-04 16:50:57 -07002393 }
2394
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002395 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002396 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002397 }
2398
Adam Cohen1462de32012-07-24 22:34:36 -07002399 public void onPageBoundSynchronously(int page) {
Sunny Goyalf4aceab2017-12-06 16:16:31 -08002400 mSynchronouslyBoundPage = page;
Sunny Goyala7a5bf32020-01-05 15:35:29 +05302401 mWorkspace.setCurrentPage(page);
2402 mPageToBindSynchronously = PagedView.INVALID_PAGE;
Adam Cohen1462de32012-07-24 22:34:36 -07002403 }
2404
Sunny Goyal527c7d32015-08-28 15:19:36 -07002405 @Override
2406 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
Sunny Goyal6b6d0a32019-08-05 17:19:32 -07002407 clearPendingBinds();
Sunny Goyal527c7d32015-08-28 15:19:36 -07002408 mPendingExecutor = executor;
Sunny Goyal60180b02018-02-07 12:56:30 -08002409 if (!isInState(ALL_APPS)) {
vadimtd4c90e12019-06-04 13:59:43 -07002410 mAppsView.getAppsStore().enableDeferUpdates(AllAppsStore.DEFER_UPDATES_NEXT_DRAW);
2411 mPendingExecutor.execute(() -> mAppsView.getAppsStore().disableDeferUpdates(
2412 AllAppsStore.DEFER_UPDATES_NEXT_DRAW));
Sunny Goyal60180b02018-02-07 12:56:30 -08002413 }
2414
Sunny Goyal527c7d32015-08-28 15:19:36 -07002415 executor.attachTo(this);
2416 }
2417
2418 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2419 if (mPendingExecutor == executor) {
2420 mPendingExecutor = null;
2421 }
2422 }
2423
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002424 @Override
2425 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
Sunny Goyal6001ea22018-05-10 16:31:00 -07002426 AlphaProperty property = mDragLayer.getAlphaProperty(ALPHA_INDEX_LAUNCHER_LOAD);
2427 if (property.getValue() < 1) {
2428 ObjectAnimator anim = ObjectAnimator.ofFloat(property, MultiValueAlpha.VALUE, 1);
2429 if (executor != null) {
2430 anim.addListener(new AnimatorListenerAdapter() {
2431 @Override
2432 public void onAnimationEnd(Animator animation) {
2433 executor.onLoadAnimationCompleted();
2434 }
2435 });
2436 }
2437 anim.start();
Sunny Goyal29947f02017-12-18 13:49:44 -08002438 } else if (executor != null) {
2439 executor.onLoadAnimationCompleted();
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002440 }
2441 }
2442
Joe Onorato9c1289c2009-08-17 11:03:03 -04002443 /**
2444 * Callback saying that there aren't any more items to bind.
2445 *
2446 * Implementation of the method from LauncherModel.Callbacks.
2447 */
Tonyf80e8932018-12-21 11:58:04 -08002448 public void finishBindingItems(int pageBoundFirst) {
Winson Chung5cc62c72019-10-16 11:32:41 -07002449 Object traceToken = TraceHelper.INSTANCE.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002450 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002451
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002452 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002453
Sunny Goyal2100c782016-08-22 16:00:03 -07002454 if (mPendingActivityResult != null) {
2455 handleActivityResult(mPendingActivityResult.requestCode,
2456 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2457 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002458 }
2459
Sunny Goyala474a9b2017-05-04 16:47:11 -07002460 InstallShortcutReceiver.disableAndFlushInstallQueue(
2461 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002462
Tonyf80e8932018-12-21 11:58:04 -08002463 // When undoing the removal of the last item on a page, return to that page.
Tony Wickham03f27012019-04-25 14:22:54 -07002464 // Since we are just resetting the current page without user interaction,
2465 // override the previous page so we don't log the page switch.
2466 mWorkspace.setCurrentPage(pageBoundFirst, pageBoundFirst /* overridePrevPage */);
Sunny Goyala7a5bf32020-01-05 15:35:29 +05302467 mPageToBindSynchronously = PagedView.INVALID_PAGE;
Tony Wickham6a71a5b2018-08-21 11:40:23 -07002468
Sunny Goyalac6f69f2019-08-16 11:59:55 -07002469 // Cache one page worth of icons
2470 getViewCache().setCacheSize(R.layout.folder_application,
2471 mDeviceProfile.inv.numFolderColumns * mDeviceProfile.inv.numFolderRows);
2472 getViewCache().setCacheSize(R.layout.folder_page, 2);
2473
Winson Chung5cc62c72019-10-16 11:32:41 -07002474 TraceHelper.INSTANCE.endSection(traceToken);
Winson Chungf0c6ae02012-03-21 16:10:31 -07002475 }
2476
Winson Chunga2413752012-04-03 14:22:34 -07002477 private boolean canRunNewAppsAnimation() {
Sunny Goyal9b180102020-03-11 10:02:29 -07002478 if (mDragController.isDragging()) {
2479 return false;
2480 } else {
2481 return (System.currentTimeMillis() - mLastTouchUpTime)
2482 > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
2483 }
Winson Chunga2413752012-04-03 14:22:34 -07002484 }
2485
Winson Chungc9168342013-06-26 14:54:55 -07002486 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002487 ValueAnimator bounceAnim = new PropertyListBuilder().alpha(1).scale(1).build(v)
2488 .setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
Winson Chungc9168342013-06-26 14:54:55 -07002489 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002490 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002491 return bounceAnim;
2492 }
2493
Pinyao Tingc9074272019-11-11 11:51:44 -08002494 private void announceForAccessibility(@StringRes int stringResId) {
2495 getDragLayer().announceForAccessibility(getString(stringResId));
2496 }
2497
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002498 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002499 * Add the icons for all apps.
2500 *
2501 * Implementation of the method from LauncherModel.Callbacks.
2502 */
Sunny Goyal87dcde62019-07-17 20:35:56 -07002503 public void bindAllApplications(AppInfo[] apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002504 mAppsView.getAppsStore().setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002505 }
2506
2507 /**
Zak Cohen658c67a2018-10-19 14:21:05 -07002508 * Copies LauncherModel's map of activities to shortcut counts to Launcher's. This is necessary
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002509 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2510 */
2511 @Override
Zak Cohen658c67a2018-10-19 14:21:05 -07002512 public void bindDeepShortcutMap(HashMap<ComponentKey, Integer> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002513 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002514 }
2515
Sunny Goyal4390ace2014-10-13 11:33:11 -07002516 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002517 public void bindPromiseAppProgressUpdated(PromiseAppInfo app) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002518 mAppsView.getAppsStore().updatePromiseAppProgress(app);
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002519 }
2520
2521 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002522 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002523 mWorkspace.widgetsRestored(widgets);
2524 }
2525
Joe Onorato9c1289c2009-08-17 11:03:03 -04002526 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002527 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002528 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002529 *
2530 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002531 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002532 @Override
Sunny Goyal95899162019-03-27 16:03:06 -07002533 public void bindWorkspaceItemsChanged(ArrayList<WorkspaceItemInfo> updated) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002534 if (!updated.isEmpty()) {
2535 mWorkspace.updateShortcuts(updated);
2536 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002537 }
2538
2539 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002540 * Update the state of a package, typically related to install state.
2541 *
2542 * Implementation of the method from LauncherModel.Callbacks.
2543 */
Sunny Goyale755d462014-07-22 13:48:29 -07002544 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002545 public void bindRestoreItemsChange(HashSet<ItemInfo> updates) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07002546 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07002547 }
2548
2549 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002550 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07002551 * in addition to specific applications to remove, the reason being that
2552 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002553 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07002554 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002555 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07002556 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002557 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002558 mWorkspace.removeItemsByMatcher(matcher);
2559 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002560 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002561
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002562 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002563 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
2564 mPopupDataProvider.setAllWidgets(allWidgets);
Tony Wickham26b17462017-03-20 17:12:24 -07002565 }
2566
Tony Wickham86222d22017-03-29 15:30:43 -07002567 /**
2568 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
2569 * refreshes the widgets and shortcuts associated with the given package/user
2570 */
2571 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07002572 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002573 }
2574
Winson Chung80baf5a2010-08-09 16:03:15 -07002575 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002576 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08002577 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002578 @Override
2579 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
2580 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002581
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002582 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
2583 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07002584 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002585 writer.println(prefix + " Homescreen " + i);
2586
2587 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
2588 for (int j = 0; j < layout.getChildCount(); j++) {
2589 Object tag = layout.getChildAt(j).getTag();
2590 if (tag != null) {
2591 writer.println(prefix + " " + tag.toString());
2592 }
2593 }
2594 }
2595
2596 writer.println(prefix + " Hotseat");
Sunny Goyal876e4622018-11-02 13:50:40 -07002597 ViewGroup layout = mHotseat.getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07002598 for (int j = 0; j < layout.getChildCount(); j++) {
2599 Object tag = layout.getChildAt(j).getTag();
2600 if (tag != null) {
2601 writer.println(prefix + " " + tag.toString());
2602 }
Adam Cohen4caf2982013-08-20 18:54:31 -07002603 }
Sunny Goyala1365452015-10-01 15:46:24 -07002604 }
2605
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002606 writer.println(prefix + "Misc:");
Winson Chungef528762019-09-06 12:05:52 -07002607 dumpMisc(prefix + "\t", writer);
2608 writer.println(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
2609 writer.println(prefix + "\tmPendingRequestArgs=" + mPendingRequestArgs
2610 + " mPendingActivityResult=" + mPendingActivityResult);
2611 writer.println(prefix + "\tmRotationHelper: " + mRotationHelper);
2612 writer.println(prefix + "\tmAppWidgetHost.isListening: " + mAppWidgetHost.isListening());
2613
Tony Wickham646e4482020-04-06 17:25:53 -07002614 // Extra logging for general debugging
Hyunyoung Songa310a802019-04-25 13:57:16 -07002615 mDragLayer.dump(prefix, writer);
2616 mStateManager.dump(prefix, writer);
Tony Wickham646e4482020-04-06 17:25:53 -07002617 mPopupDataProvider.dump(prefix, writer);
Sunny Goyal2bba1902018-02-27 12:20:50 -08002618
2619 try {
2620 FileLog.flushAll(writer);
2621 } catch (Exception e) {
2622 // Ignore
2623 }
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002624
2625 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002626
Adam Cohen9211d422014-10-07 18:14:53 -07002627 if (mLauncherCallbacks != null) {
2628 mLauncherCallbacks.dump(prefix, fd, writer, args);
2629 }
Sunny Goyalb1d7de22019-09-06 10:36:54 -07002630 mOverlayManager.dump(prefix, writer);
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002631 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002632
Sunny Goyal66b24572016-09-21 15:57:55 -07002633 @Override
Sunny Goyal66b24572016-09-21 15:57:55 -07002634 public void onProvideKeyboardShortcuts(
2635 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
2636
2637 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002638 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002639 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
2640 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
Vadim Tryshev74c261c2018-06-12 13:44:43 -07002641 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.widget_button_text),
2642 KeyEvent.KEYCODE_W, KeyEvent.META_CTRL_ON));
Sunny Goyal66b24572016-09-21 15:57:55 -07002643 }
Vadim Tryshev3455f8d2018-05-11 20:02:20 -07002644 final View currentFocus = getCurrentFocus();
2645 if (currentFocus != null) {
2646 if (new CustomActionsPopup(this, currentFocus).canShow()) {
2647 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
2648 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
2649 }
2650 if (currentFocus.getTag() instanceof ItemInfo
Pinyao Ting49a3e692019-07-26 12:28:38 -07002651 && ShortcutUtil.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
Vadim Tryshev3455f8d2018-05-11 20:02:20 -07002652 shortcutInfos.add(new KeyboardShortcutInfo(
2653 getString(R.string.shortcuts_menu_with_notifications_description),
2654 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
2655 }
Sunny Goyal66b24572016-09-21 15:57:55 -07002656 }
2657 if (!shortcutInfos.isEmpty()) {
2658 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
2659 }
2660
2661 super.onProvideKeyboardShortcuts(data, menu, deviceId);
2662 }
2663
2664 @Override
2665 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
2666 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
2667 switch (keyCode) {
2668 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002669 if (isInState(NORMAL)) {
2670 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07002671 return true;
2672 }
2673 break;
2674 case KeyEvent.KEYCODE_S: {
2675 View focusedView = getCurrentFocus();
2676 if (focusedView instanceof BubbleTextView
2677 && focusedView.getTag() instanceof ItemInfo
2678 && mAccessibilityDelegate.performAction(focusedView,
Samuel Fufaca37b8a2019-08-19 17:04:36 -07002679 (ItemInfo) focusedView.getTag(),
2680 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal00ac9202017-11-09 15:24:06 -08002681 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07002682 return true;
2683 }
2684 break;
2685 }
2686 case KeyEvent.KEYCODE_O:
2687 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
2688 return true;
2689 }
2690 break;
Vadim Tryshev74c261c2018-06-12 13:44:43 -07002691 case KeyEvent.KEYCODE_W:
2692 if (isInState(NORMAL)) {
2693 OptionsPopupView.openWidgets(this);
2694 return true;
2695 }
2696 break;
Sunny Goyal66b24572016-09-21 15:57:55 -07002697 }
2698 }
2699 return super.onKeyShortcut(keyCode, event);
2700 }
2701
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002702 @Override
2703 public boolean onKeyUp(int keyCode, KeyEvent event) {
2704 if (keyCode == KeyEvent.KEYCODE_MENU) {
2705 // KEYCODE_MENU is sent by some tests, for example
2706 // LauncherJankTests#testWidgetsContainerFling. Don't just remove its handling.
2707 if (!mDragController.isDragging() && !mWorkspace.isSwitchingState() &&
2708 isInState(NORMAL)) {
2709 // Close any open floating views.
Andy Wickham65ced1b2020-03-07 02:14:19 +00002710 closeOpenViews();
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002711
2712 // Setting the touch point to (-1, -1) will show the options popup in the center of
2713 // the screen.
vadimtbc5d7e62019-12-09 13:53:05 -08002714 if (Utilities.IS_RUNNING_IN_TEST_HARNESS) {
2715 Log.d(TestProtocol.PERMANENT_DIAG_TAG, "Opening options popup on key up");
2716 }
Sunny Goyal2fd7a8b2018-03-30 17:10:13 -07002717 OptionsPopupView.showDefaultOptions(this, -1, -1);
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002718 }
2719 return true;
2720 }
2721 return super.onKeyUp(keyCode, event);
2722 }
2723
Sunny Goyal210e1742019-10-17 12:05:38 -07002724 protected StateHandler[] createStateHandlers() {
Sunny Goyal284a6cf2020-03-24 12:41:56 -07002725 return new StateHandler[] { getAllAppsController(), getWorkspace() };
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002726 }
2727
Sunny Goyalf67db352020-04-24 00:38:11 -07002728 /**
2729 * Creates a factory for atomic state animations
2730 */
2731 public AtomicAnimationFactory createAtomicAnimationFactory() {
2732 return new AtomicAnimationFactory(0);
2733 }
2734
Sunny Goyal210e1742019-10-17 12:05:38 -07002735 public TouchController[] createTouchControllers() {
2736 return new TouchController[] {getDragController(), new AllAppsSwipeController(this)};
2737 }
2738
Sunny Goyal210e1742019-10-17 12:05:38 -07002739 public void useFadeOutAnimationForLauncherStart(CancellationSignal signal) { }
2740
2741 public void onDragLayerHierarchyChanged() { }
2742
Vinit Nayakf9b585b2019-07-10 14:25:32 -07002743 @Override
2744 public void returnToHomescreen() {
2745 super.returnToHomescreen();
2746 getStateManager().goToState(LauncherState.NORMAL);
2747 }
2748
Andy Wickham65ced1b2020-03-07 02:14:19 +00002749 private void closeOpenViews() {
2750 closeOpenViews(true);
2751 }
2752
2753 protected void closeOpenViews(boolean animate) {
2754 AbstractFloatingView.closeAllOpenViews(this, animate);
2755 }
2756
Sunny Goyal3a1eadd2019-10-23 17:44:27 -07002757 public Stream<SystemShortcut.Factory> getSupportedShortcuts() {
Samuel Fufa5cf3e862020-02-03 20:22:54 -08002758 return Stream.of(APP_INFO, WIDGETS, INSTALL);
Sunny Goyal3a1eadd2019-10-23 17:44:27 -07002759 }
2760
Sunny Goyalf2393f12020-04-01 20:13:12 -07002761
2762 /**
2763 * @see LauncherState#getOverviewScaleAndOffset(Launcher)
2764 */
2765 public float[] getNormalOverviewScaleAndOffset() {
2766 return new float[] {NO_SCALE, NO_OFFSET};
2767 }
2768
Sunny Goyal210e1742019-10-17 12:05:38 -07002769 public static Launcher getLauncher(Context context) {
Winson Chung8687bc22020-02-27 23:34:24 -08002770 return fromContext(context);
Sunny Goyal210e1742019-10-17 12:05:38 -07002771 }
2772
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002773 /**
Sunny Goyalab770a12018-11-14 15:17:26 -08002774 * Just a wrapper around the type cast to allow easier tracking of calls.
2775 */
2776 public static <T extends Launcher> T cast(ActivityContext activityContext) {
2777 return (T) activityContext;
2778 }
2779
Sunny Goyalf2393f12020-04-01 20:13:12 -07002780
Sunny Goyalab770a12018-11-14 15:17:26 -08002781 /**
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002782 * Callback for listening for onResume
2783 */
2784 public interface OnResumeCallback {
2785
2786 void onLauncherResume();
2787 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002788}