blob: b6e301863ee4b817f130290e5bd3c1d1f0838ef5 [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;
Pinyao Tinga74b63a2019-07-17 23:26:06 -070021import static com.android.launcher3.AbstractFloatingView.TYPE_ALL;
22import static com.android.launcher3.AbstractFloatingView.TYPE_REBIND_SAFE;
Tony Wickham6a71a5b2018-08-21 11:40:23 -070023import static com.android.launcher3.AbstractFloatingView.TYPE_SNACKBAR;
Sunny Goyal4c7f2152017-10-17 17:17:16 -070024import static com.android.launcher3.LauncherAnimUtils.SPRING_LOADED_EXIT_DELAY;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -070025import static com.android.launcher3.LauncherState.ALL_APPS;
26import static com.android.launcher3.LauncherState.NORMAL;
Sunny Goyalf2393f12020-04-01 20:13:12 -070027import static com.android.launcher3.LauncherState.NO_OFFSET;
28import static com.android.launcher3.LauncherState.NO_SCALE;
Govinda Wassermanc06e1512019-04-09 09:56:53 -040029import static com.android.launcher3.LauncherState.OVERVIEW;
30import static com.android.launcher3.LauncherState.OVERVIEW_PEEK;
Sunny Goyalb1d7de22019-09-06 10:36:54 -070031import static com.android.launcher3.Utilities.postAsyncCallback;
Sunny Goyal6001ea22018-05-10 16:31:00 -070032import static com.android.launcher3.dragndrop.DragLayer.ALPHA_INDEX_LAUNCHER_LOAD;
Sunny Goyalaeb16432017-10-16 11:46:41 -070033import static com.android.launcher3.logging.LoggerUtils.newContainerTarget;
Sunny Goyal3a1eadd2019-10-23 17:44:27 -070034import static com.android.launcher3.popup.SystemShortcut.APP_INFO;
Sunny Goyal3a1eadd2019-10-23 17:44:27 -070035import static com.android.launcher3.popup.SystemShortcut.INSTALL;
36import static com.android.launcher3.popup.SystemShortcut.WIDGETS;
Sunny Goyalbe8c3402019-05-22 15:17:52 -070037import static com.android.launcher3.states.RotationHelper.REQUEST_NONE;
Sunny Goyal326403e2017-10-02 12:45:10 -070038
Gilles Debunnedd6c9922010-10-25 11:23:41 -070039import android.animation.Animator;
Sunny Goyal6001ea22018-05-10 16:31:00 -070040import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070041import android.animation.AnimatorSet;
Sunny Goyal6001ea22018-05-10 16:31:00 -070042import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070043import android.animation.ValueAnimator;
Adam Cohen0b395352014-06-09 22:54:36 +000044import android.annotation.TargetApi;
Tony Wickham005df0b2018-02-13 11:28:12 -080045import android.app.ActivityOptions;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080046import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070047import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080049import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080050import android.content.ComponentCallbacks2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070053import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070054import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070055import android.content.SharedPreferences;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056import android.content.pm.PackageManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080057import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070058import android.database.sqlite.SQLiteDatabase;
Sunny Goyal59d086c2018-05-07 17:31:40 -070059import android.graphics.Point;
Adam Cohen0f668f32014-09-08 19:54:17 +020060import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061import android.os.Bundle;
Sunny Goyal210e1742019-10-17 12:05:38 -070062import android.os.CancellationSignal;
Hyunyoung Songbd6fba92018-05-16 15:54:31 -070063import android.os.Handler;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070064import android.os.Parcelable;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080065import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070066import android.os.StrictMode;
Michael Jurkaa33411c2012-06-14 16:18:21 -070067import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070069import android.util.Log;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070070import android.util.SparseArray;
Sunny Goyal59d086c2018-05-07 17:31:40 -070071import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070072import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070073import android.view.KeyboardShortcutGroup;
74import android.view.KeyboardShortcutInfo;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080075import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -070076import android.view.Menu;
vadimt9af34a72020-01-21 11:19:13 -080077import android.view.MotionEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070078import android.view.View;
Adam Cohen96d30a12013-07-16 18:13:21 -070079import android.view.ViewGroup;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080080import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070081import android.view.animation.OvershootInterpolator;
Adam Cohen96d30a12013-07-16 18:13:21 -070082import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070083
Sunny Goyal11a50742019-08-07 09:24:26 -070084import androidx.annotation.Nullable;
Pinyao Tingc9074272019-11-11 11:51:44 -080085import androidx.annotation.StringRes;
Sunny Goyalb1d7de22019-09-06 10:36:54 -070086import androidx.annotation.VisibleForTesting;
Sunny Goyal11a50742019-08-07 09:24:26 -070087
Sunny Goyal651077b2014-06-30 14:15:31 -070088import com.android.launcher3.DropTarget.DragObject;
Sunny Goyal210e1742019-10-17 12:05:38 -070089import com.android.launcher3.LauncherStateManager.StateHandler;
Sunny Goyalae502842016-06-17 08:43:56 -070090import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070091import com.android.launcher3.allapps.AllAppsContainerView;
vadimtd4c90e12019-06-04 13:59:43 -070092import com.android.launcher3.allapps.AllAppsStore;
Hyunyoung Song645764e2016-06-06 14:19:02 -070093import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal7ede6112017-12-05 15:11:21 -080094import com.android.launcher3.allapps.DiscoveryBounce;
Sunny Goyalf0b6db72018-08-13 16:10:14 -070095import com.android.launcher3.anim.PropertyListBuilder;
Tony Wickhame0c33232016-02-08 11:37:04 -080096import com.android.launcher3.config.FeatureFlags;
vadimt00d42552018-12-11 17:59:36 -080097import com.android.launcher3.dot.DotInfo;
Vadim Tryshevfedca432015-08-19 17:55:02 -070098import com.android.launcher3.dragndrop.DragController;
99import com.android.launcher3.dragndrop.DragLayer;
100import com.android.launcher3.dragndrop.DragView;
Samuel Fufa28c3e452020-02-08 16:07:16 -0800101import com.android.launcher3.folder.Folder;
Sunny Goyal11a50742019-08-07 09:24:26 -0700102import com.android.launcher3.folder.FolderGridOrganizer;
Sunny Goyal26119432016-02-18 22:09:23 +0000103import com.android.launcher3.folder.FolderIcon;
Sunny Goyalf840f102018-09-21 14:41:05 -0700104import com.android.launcher3.icons.IconCache;
Sunny Goyal66b24572016-09-21 15:57:55 -0700105import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700106import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700107import com.android.launcher3.logging.FileLog;
Hyunyoung Songfc007472018-10-25 14:09:50 -0700108import com.android.launcher3.logging.StatsLogUtils;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700109import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyal369212a2019-03-26 15:03:57 -0700110import com.android.launcher3.model.AppLaunchTracker;
Sunny Goyal87dcde62019-07-17 20:35:56 -0700111import com.android.launcher3.model.BgDataModel.Callbacks;
Sunny Goyala535ae42017-02-27 10:07:13 -0800112import com.android.launcher3.model.ModelWriter;
Sunny Goyale396abf2020-04-06 15:11:17 -0700113import com.android.launcher3.model.data.AppInfo;
114import com.android.launcher3.model.data.FolderInfo;
115import com.android.launcher3.model.data.ItemInfo;
116import com.android.launcher3.model.data.LauncherAppWidgetInfo;
117import com.android.launcher3.model.data.PromiseAppInfo;
118import com.android.launcher3.model.data.WorkspaceItemInfo;
Sunny Goyala535ae42017-02-27 10:07:13 -0800119import com.android.launcher3.notification.NotificationListener;
Sunny Goyale7b00122019-10-02 16:13:34 -0700120import com.android.launcher3.pm.PinRequestHelper;
Samuel Fufa41d909f2020-03-20 16:27:09 -0700121import com.android.launcher3.pm.UserCache;
Tony Wickham540913e2017-01-23 11:47:51 -0800122import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -0800123import com.android.launcher3.popup.PopupDataProvider;
Sunny Goyal3a1eadd2019-10-23 17:44:27 -0700124import com.android.launcher3.popup.SystemShortcut;
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700125import com.android.launcher3.qsb.QsbContainerView;
Sunny Goyal623eddd2018-03-02 12:24:41 -0800126import com.android.launcher3.states.RotationHelper;
vadimt9af34a72020-01-21 11:19:13 -0800127import com.android.launcher3.testing.TestLogging;
vadimtbc5d7e62019-12-09 13:53:05 -0800128import com.android.launcher3.testing.TestProtocol;
Sunny Goyal210e1742019-10-17 12:05:38 -0700129import com.android.launcher3.touch.AllAppsSwipeController;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800130import com.android.launcher3.touch.ItemClickHandler;
Sunny Goyalb1d7de22019-09-06 10:36:54 -0700131import com.android.launcher3.uioverrides.plugins.PluginManagerWrapper;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530132import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
133import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
Sunny Goyalaeb16432017-10-16 11:46:41 -0700134import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyal2100c782016-08-22 16:00:03 -0700135import com.android.launcher3.util.ActivityResultInfo;
Sunny Goyale84c5b82019-09-26 17:05:31 -0700136import com.android.launcher3.util.ActivityTracker;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700137import com.android.launcher3.util.ComponentKey;
Sunny Goyalefb7e842018-10-04 15:11:00 -0700138import com.android.launcher3.util.IntArray;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700139import com.android.launcher3.util.ItemInfoMatcher;
Sunny Goyal6001ea22018-05-10 16:31:00 -0700140import com.android.launcher3.util.MultiValueAlpha;
141import com.android.launcher3.util.MultiValueAlpha.AlphaProperty;
Tony Wickham133cd542020-04-08 17:48:41 -0700142import com.android.launcher3.util.OnboardingPrefs;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700143import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800144import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700145import com.android.launcher3.util.PendingRequestArgs;
Samuel Fufa41d909f2020-03-20 16:27:09 -0700146import com.android.launcher3.util.SafeCloseable;
Pinyao Ting49a3e692019-07-26 12:28:38 -0700147import com.android.launcher3.util.ShortcutUtil;
Sunny Goyal8392c822017-06-20 10:03:56 -0700148import com.android.launcher3.util.SystemUiController;
Mario Bertschlera6936942017-05-31 14:48:19 -0700149import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700150import com.android.launcher3.util.Thunk;
Sunny Goyal210e1742019-10-17 12:05:38 -0700151import com.android.launcher3.util.TouchController;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700152import com.android.launcher3.util.TraceHelper;
Sunny Goyal326403e2017-10-02 12:45:10 -0700153import com.android.launcher3.util.UiThreadHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700154import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyalab770a12018-11-14 15:17:26 -0800155import com.android.launcher3.views.ActivityContext;
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800156import com.android.launcher3.views.OptionsPopupView;
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400157import com.android.launcher3.views.ScrimView;
Sunny Goyal29947f02017-12-18 13:49:44 -0800158import com.android.launcher3.widget.LauncherAppWidgetHostView;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800159import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700160import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal29947f02017-12-18 13:49:44 -0800161import com.android.launcher3.widget.PendingAppWidgetHostView;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800162import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700163import com.android.launcher3.widget.WidgetHostViewLoader;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700164import com.android.launcher3.widget.WidgetListRowEntry;
Sunny Goyal337c81f2019-12-10 12:19:13 -0800165import com.android.launcher3.widget.WidgetManagerHelper;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700166import com.android.launcher3.widget.WidgetsFullSheet;
Pinyao Tingc7a6c292019-08-26 14:36:02 -0700167import com.android.launcher3.widget.custom.CustomWidgetManager;
Sunny Goyalb1d7de22019-09-06 10:36:54 -0700168import com.android.systemui.plugins.OverlayPlugin;
169import com.android.systemui.plugins.PluginListener;
170import com.android.systemui.plugins.shared.LauncherExterns;
171import com.android.systemui.plugins.shared.LauncherOverlayManager;
172import com.android.systemui.plugins.shared.LauncherOverlayManager.LauncherOverlay;
173import com.android.systemui.plugins.shared.LauncherOverlayManager.LauncherOverlayCallbacks;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700174
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700175import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700176import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700177import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700178import java.util.Collection;
Zak Cohen658c67a2018-10-19 14:21:05 -0700179import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700180import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700181import java.util.List;
Sunny Goyalaae6fbb2019-01-31 16:05:58 -0800182import java.util.function.Predicate;
Sunny Goyalb1d7de22019-09-06 10:36:54 -0700183import java.util.function.Supplier;
Sunny Goyal3a1eadd2019-10-23 17:44:27 -0700184import java.util.stream.Stream;
Jon Mirandaf5197fc2019-09-04 16:55:41 -0700185
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186/**
187 * Default launcher application.
188 */
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700189public class Launcher extends BaseDraggingActivity implements LauncherExterns,
Samuel Fufaa579ddc2020-02-27 16:59:19 -0800190 Callbacks, InvariantDeviceProfile.OnIDPChangeListener, PluginListener<OverlayPlugin> {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700191 public static final String TAG = "Launcher";
Sunny Goyale84c5b82019-09-26 17:05:31 -0700192
193 public static final ActivityTracker<Launcher> ACTIVITY_TRACKER = new ActivityTracker<>();
194
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700195 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196
Winson Chunga2413752012-04-03 14:22:34 -0700197 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700198
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700200 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700201
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700202 private static final int REQUEST_PICK_APPWIDGET = 9;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800203
Michael Jurka8b805b12012-04-18 14:23:14 -0700204 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800205 public static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
206 public static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700207
Jon Mirandac476d6e2017-05-04 16:30:01 -0700208 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700209
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700210 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
211
Mathew Inwood876a8462013-06-14 14:12:41 +0100212 /**
213 * IntentStarter uses request codes starting with this. This must be greater than all activity
214 * request codes used internally.
215 */
216 protected static final int REQUEST_LAST = 100;
217
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218 // Type: int
219 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700220 // Type: int
221 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700222 // Type: PendingRequestArgs
223 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
Sunny Goyaldedda052019-05-14 15:23:48 -0700224 // Type: int
225 private static final String RUNTIME_STATE_PENDING_REQUEST_CODE = "launcher.request_code";
Sunny Goyal2100c782016-08-22 16:00:03 -0700226 // Type: ActivityResultInfo
227 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700228 // Type: SparseArray<Parcelable>
229 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
Sunny Goyal17c72fb2019-10-14 14:06:38 -0700230
vadimt00d42552018-12-11 17:59:36 -0800231 public static final String ON_CREATE_EVT = "Launcher.onCreate";
Sunny Goyal17c72fb2019-10-14 14:06:38 -0700232 public static final String ON_START_EVT = "Launcher.onStart";
233 public static final String ON_RESUME_EVT = "Launcher.onResume";
234 public static final String ON_NEW_INTENT_EVT = "Launcher.onNewIntent";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800235
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700236 private LauncherStateManager mStateManager;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700237
Adam Cohenad4e15c2013-10-17 16:21:35 -0700238 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800239
Winson Chunga2413752012-04-03 14:22:34 -0700240 // How long to wait before the new-shortcut animation automatically pans the workspace
Jon Mirandaf5197fc2019-09-04 16:55:41 -0700241 @VisibleForTesting public static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700242 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Jon Mirandaf5197fc2019-09-04 16:55:41 -0700243 @Thunk @VisibleForTesting public static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700244
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400245 private static final int APPS_VIEW_ALPHA_CHANNEL_INDEX = 1;
246 private static final int SCRIM_VIEW_ALPHA_CHANNEL_INDEX = 0;
247
Jon Miranda54441f52018-01-24 15:38:25 -0800248 private LauncherAppTransitionManager mAppTransitionManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800249 private Configuration mOldConfig;
Jon Miranda54441f52018-01-24 15:38:25 -0800250
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700251 @Thunk
252 Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700253 private View mLauncherView;
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700254 @Thunk
255 DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800256 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700257
Sunny Goyal337c81f2019-12-10 12:19:13 -0800258 private WidgetManagerHelper mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700259 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700260
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700261 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700262
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700263 @Thunk
264 Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700265
Sunny Goyal47328fd2016-05-25 18:56:41 -0700266 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700267
268 // Main container view for the all apps screen.
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700269 @Thunk
270 AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700271 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700272
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400273 // Scrim view for the all apps and overview state.
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700274 @Thunk
275 ScrimView mScrimView;
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400276
Winson Chung8ae41982017-11-10 11:42:13 -0800277 // UI and state for the overview panel
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800278 private View mOverviewPanel;
Winson Chung8ae41982017-11-10 11:42:13 -0800279
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700280 @Thunk
281 boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400282
Winson Chung0b70cd42019-06-17 22:34:33 -0700283 private ArrayList<OnResumeCallback> mOnResumeCallbacks = new ArrayList<>();
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700284
Winson Chungc9bf6d42019-10-04 15:33:18 -0700285 // Used to notify when an activity launch has been deferred because launcher is not yet resumed
286 // TODO: See if we can remove this later
287 private Runnable mOnDeferredActivityLaunchCallback;
288
Sunny Goyal527c7d32015-08-28 15:19:36 -0700289 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700290
Joe Onorato9c1289c2009-08-17 11:03:03 -0400291 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800292 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800293 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700294 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400295
Tony Wickham010d2552017-01-20 08:15:28 -0800296 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700297
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800298 private int mSynchronouslyBoundPage = PagedView.INVALID_PAGE;
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530299 private int mPageToBindSynchronously = PagedView.INVALID_PAGE;
Adam Cohen1462de32012-07-24 22:34:36 -0700300
Winson Chung46353de2012-02-16 14:05:10 -0800301 // We only want to get the SharedPreferences once since it does an FS stat each time we get
302 // it from the context.
303 private SharedPreferences mSharedPrefs;
Tony Wickham133cd542020-04-08 17:48:41 -0700304 private OnboardingPrefs mOnboardingPrefs;
Winson Chung46353de2012-02-16 14:05:10 -0800305
Sunny Goyal2100c782016-08-22 16:00:03 -0700306 // Activity result which needs to be processed after workspace has loaded.
307 private ActivityResultInfo mPendingActivityResult;
308 /**
309 * Holds extra information required to handle a result from an external call, like
310 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
311 */
312 private PendingRequestArgs mPendingRequestArgs;
Sunny Goyaldedda052019-05-14 15:23:48 -0700313 // Request id for any pending activity result
Sunny Goyal210e1742019-10-17 12:05:38 -0700314 protected int mPendingActivityRequestCode = -1;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800315
Tracy Zhou4d7b2442019-12-09 13:42:57 -0800316 private ViewGroupFocusHelper mFocusHandler;
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700317
Sunny Goyal623eddd2018-03-02 12:24:41 -0800318 private RotationHelper mRotationHelper;
Sunny Goyal7779d622015-06-11 16:18:39 -0700319
Sunny Goyal2db53422019-03-01 16:06:12 -0800320 final Handler mHandler = new Handler();
Sunny Goyala002c6c2019-02-12 16:20:10 -0800321 private final Runnable mHandleDeferredResume = this::handleDeferredResume;
Sunny Goyale9c6f4c2019-05-30 11:35:25 -0700322 private boolean mDeferredResumePending;
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700323
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400324 private float mCurrentAssistantVisibility = 0f;
325
Sunny Goyalb1d7de22019-09-06 10:36:54 -0700326 protected LauncherOverlayManager mOverlayManager;
327 // If true, overlay callbacks are deferred
328 private boolean mDeferOverlayCallbacks;
329 private final Runnable mDeferredOverlayCallbacks = this::checkIfOverlayStillDeferred;
330
Sunny Goyal9b180102020-03-11 10:02:29 -0700331 private long mLastTouchUpTime = -1;
Sunny Goyala306e592020-03-24 01:51:41 -0700332 private boolean mTouchInProgress;
Sunny Goyal9b180102020-03-11 10:02:29 -0700333
Samuel Fufa41d909f2020-03-20 16:27:09 -0700334 private SafeCloseable mUserChangedCallbackCloseable;
335
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800336 @Override
337 protected void onCreate(Bundle savedInstanceState) {
Winson Chung5cc62c72019-10-16 11:32:41 -0700338 Object traceToken = TraceHelper.INSTANCE.beginSection(ON_CREATE_EVT,
339 TraceHelper.FLAG_UI_EVENT);
Winson Chunga2413752012-04-03 14:22:34 -0700340 if (DEBUG_STRICT_MODE) {
341 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
342 .detectDiskReads()
343 .detectDiskWrites()
344 .detectNetwork() // or .detectAll() for all detectable problems
345 .penaltyLog()
346 .build());
347 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
348 .detectLeakedSqlLiteObjects()
349 .detectLeakedClosableObjects()
350 .penaltyLog()
351 .penaltyDeath()
352 .build());
353 }
354
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800355 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400356
Sunny Goyal87f784c2017-01-11 10:48:34 -0800357 LauncherAppState app = LauncherAppState.getInstance(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800358 mOldConfig = new Configuration(getResources().getConfiguration());
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530359 mModel = app.getModel();
Sunny Goyal41d51a02019-05-14 09:44:34 -0700360 mRotationHelper = new RotationHelper(this);
Hyunyoung Song31971a32019-01-27 12:31:12 -0800361 InvariantDeviceProfile idp = app.getInvariantDeviceProfile();
362 initDeviceProfile(idp);
363 idp.addOnChangeListener(this);
Sunny Goyalf7258242015-10-19 16:59:07 -0700364 mSharedPrefs = Utilities.getPrefs(this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800365 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700366 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700367
Joe Onorato41a12d22009-10-31 18:30:00 -0400368 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700369 mAllAppsController = new AllAppsTransitionController(this);
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800370 mStateManager = new LauncherStateManager(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700371
Tony Wickham133cd542020-04-08 17:48:41 -0700372 mOnboardingPrefs = createOnboardingPrefs(mSharedPrefs, mStateManager);
373
Sunny Goyal337c81f2019-12-10 12:19:13 -0800374 mAppWidgetManager = new WidgetManagerHelper(this);
Samuel Fufa44043ec2019-08-26 15:00:30 -0700375 mAppWidgetHost = new LauncherAppWidgetHost(this,
376 appWidgetId -> getWorkspace().removeWidget(appWidgetId));
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700377 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700378
Sunny Goyal60820d72017-05-09 12:40:11 -0700379 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700380
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800381 setupViews();
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800382 mPopupDataProvider = new PopupDataProvider(this::updateNotificationDots);
Tony Wickham010d2552017-01-20 08:15:28 -0800383
Sunny Goyal90548432018-04-04 17:17:00 -0700384 mAppTransitionManager = LauncherAppTransitionManager.newInstance(this);
Winson Chung24ab40c2019-10-30 22:35:09 -0700385 mAppTransitionManager.registerRemoteAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800386
Sunny Goyale84c5b82019-09-26 17:05:31 -0700387 boolean internalStateHandled = ACTIVITY_TRACKER.handleCreate(this);
Sunny Goyald3864fa2017-11-14 15:06:36 -0800388 if (internalStateHandled) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800389 if (savedInstanceState != null) {
390 // InternalStateHandler has already set the appropriate state.
391 // We dont need to do anything.
392 savedInstanceState.remove(RUNTIME_STATE);
393 }
394 }
395 restoreState(savedInstanceState);
Sunny Goyalbbece862019-03-01 13:13:52 -0800396 mStateManager.reapplyState();
Winson Chungb63b44c2017-11-10 17:54:44 -0800397
Sunny Goyal2100c782016-08-22 16:00:03 -0700398 // We only load the page synchronously if the user rotates (or triggers a
399 // configuration change) while launcher is in the foreground
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530400 int currentScreen = PagedView.INVALID_PAGE;
Sunny Goyalfe770c92016-09-27 14:38:58 -0700401 if (savedInstanceState != null) {
402 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
403 }
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530404 mPageToBindSynchronously = currentScreen;
Sunny Goyald3864fa2017-11-14 15:06:36 -0800405
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530406 if (!mModel.addCallbacksAndLoad(this)) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800407 if (!internalStateHandled) {
408 // If we are not binding synchronously, show a fade in animation when
409 // the first page bind completes.
Sunny Goyal6001ea22018-05-10 16:31:00 -0700410 mDragLayer.getAlphaProperty(ALPHA_INDEX_LAUNCHER_LOAD).setValue(0);
Sunny Goyald3864fa2017-11-14 15:06:36 -0800411 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800412 }
413
414 // For handling default keys
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800415 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800416
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800417 setContentView(mLauncherView);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800418 getRootView().dispatchInsets();
Mario Bertschlera6936942017-05-31 14:48:19 -0700419
Jon Miranda7fda2852017-07-19 16:07:01 -0700420 // Listen for broadcasts
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700421 registerReceiver(mScreenOffReceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));
Sunny Goyala616d2b2017-06-12 13:54:01 -0700422
Sunny Goyal8392c822017-06-20 10:03:56 -0700423 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
424 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700425
426 if (mLauncherCallbacks != null) {
427 mLauncherCallbacks.onCreate(savedInstanceState);
428 }
Sunny Goyalb1d7de22019-09-06 10:36:54 -0700429 mOverlayManager = getDefaultOverlay();
430 PluginManagerWrapper.INSTANCE.get(this).addPluginListener(this,
431 OverlayPlugin.class, false /* allowedMultiple */);
432
Sunny Goyal623eddd2018-03-02 12:24:41 -0800433 mRotationHelper.initialize();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700434
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400435 mStateManager.addStateListener(new LauncherStateManager.StateListener() {
436 @Override
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700437 public void onStateTransitionStart(LauncherState toState) {
438 }
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400439
440 @Override
441 public void onStateTransitionComplete(LauncherState finalState) {
442 float alpha = 1f - mCurrentAssistantVisibility;
443 if (finalState == NORMAL) {
444 mAppsView.getAlphaProperty(APPS_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
445 } else if (finalState == OVERVIEW || finalState == OVERVIEW_PEEK) {
446 mAppsView.getAlphaProperty(APPS_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
447 mScrimView.getAlphaProperty(SCRIM_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
448 } else {
449 mAppsView.getAlphaProperty(APPS_VIEW_ALPHA_CHANNEL_INDEX).setValue(1f);
450 mScrimView.getAlphaProperty(SCRIM_VIEW_ALPHA_CHANNEL_INDEX).setValue(1f);
451 }
452 }
453 });
Sunny Goyal17c72fb2019-10-14 14:06:38 -0700454
Winson Chung5cc62c72019-10-16 11:32:41 -0700455 TraceHelper.INSTANCE.endSection(traceToken);
Samuel Fufa41d909f2020-03-20 16:27:09 -0700456
457 mUserChangedCallbackCloseable = UserCache.INSTANCE.get(this).addUserChangeListener(
458 () -> getStateManager().goToState(NORMAL));
Adam Cohen9211d422014-10-07 18:14:53 -0700459 }
460
Sunny Goyalb1d7de22019-09-06 10:36:54 -0700461 protected LauncherOverlayManager getDefaultOverlay() {
462 return new LauncherOverlayManager() { };
463 }
464
Tony Wickham133cd542020-04-08 17:48:41 -0700465 protected OnboardingPrefs createOnboardingPrefs(SharedPreferences sharedPrefs,
466 LauncherStateManager stateManager) {
467 return new OnboardingPrefs<>(this, sharedPrefs, stateManager);
468 }
469
470 public OnboardingPrefs getOnboardingPrefs() {
471 return mOnboardingPrefs;
472 }
473
Sunny Goyalb1d7de22019-09-06 10:36:54 -0700474 @Override
475 public void onPluginConnected(OverlayPlugin overlayManager, Context context) {
476 switchOverlay(() -> overlayManager.createOverlayManager(this, this));
477 }
478
479 @Override
480 public void onPluginDisconnected(OverlayPlugin plugin) {
481 switchOverlay(this::getDefaultOverlay);
482 }
483
484 private void switchOverlay(Supplier<LauncherOverlayManager> overlaySupplier) {
485 if (mOverlayManager != null) {
486 mOverlayManager.onActivityDestroyed(this);
487 }
488 mOverlayManager = overlaySupplier.get();
489 if (getRootView().isAttachedToWindow()) {
490 mOverlayManager.onAttachedToWindow();
491 }
492 mDeferOverlayCallbacks = true;
493 checkIfOverlayStillDeferred();
494 }
495
496 @Override
497 protected void dispatchDeviceProfileChanged() {
498 super.dispatchDeviceProfileChanged();
499 mOverlayManager.onDeviceProvideChanged();
500 }
501
Sunny Goyal7779d622015-06-11 16:18:39 -0700502 @Override
Winson Chung8eb49e02018-07-16 13:33:31 -0700503 public void onEnterAnimationComplete() {
504 super.onEnterAnimationComplete();
Sunny Goyalbe8c3402019-05-22 15:17:52 -0700505 mRotationHelper.setCurrentTransitionRequest(REQUEST_NONE);
Winson Chung8eb49e02018-07-16 13:33:31 -0700506 }
507
508 @Override
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800509 public void onConfigurationChanged(Configuration newConfig) {
510 int diff = newConfig.diff(mOldConfig);
Jon Mirandaaeb4dd02018-07-11 14:16:23 -0700511
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800512 if ((diff & (CONFIG_ORIENTATION | CONFIG_SCREEN_SIZE)) != 0) {
Sunny Goyal371ea052019-03-06 15:38:32 -0800513 onIdpChanged(mDeviceProfile.inv);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800514 }
515
516 mOldConfig.setTo(newConfig);
517 super.onConfigurationChanged(newConfig);
518 }
519
Sunny Goyal605bcf32018-03-02 15:16:12 -0800520 @Override
Sunny Goyal41d51a02019-05-14 09:44:34 -0700521 public void reapplyUi() {
Tony Wickham4df42f82019-11-13 15:38:28 -0800522 reapplyUi(true /* cancelCurrentAnimation */);
523 }
524
525 public void reapplyUi(boolean cancelCurrentAnimation) {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700526 getRootView().dispatchInsets();
Tony Wickham4df42f82019-11-13 15:38:28 -0800527 getStateManager().reapplyState(cancelCurrentAnimation);
Sunny Goyal59d086c2018-05-07 17:31:40 -0700528 }
529
530 @Override
Hyunyoung Song31971a32019-01-27 12:31:12 -0800531 public void onIdpChanged(int changeFlags, InvariantDeviceProfile idp) {
Sunny Goyal371ea052019-03-06 15:38:32 -0800532 onIdpChanged(idp);
533 }
534
535 private void onIdpChanged(InvariantDeviceProfile idp) {
536 mUserEventDispatcher = null;
537
Hyunyoung Song31971a32019-01-27 12:31:12 -0800538 initDeviceProfile(idp);
Sunny Goyal371ea052019-03-06 15:38:32 -0800539 dispatchDeviceProfileChanged();
540 reapplyUi();
541 mDragLayer.recreateControllers();
542
Sunny Goyal76ba0a32019-06-10 11:56:11 -0700543 // Calling onSaveInstanceState ensures that static cache used by listWidgets is
544 // initialized properly.
545 onSaveInstanceState(new Bundle());
Sunny Goyalc4d32012020-04-03 17:10:11 -0700546 mModel.rebindCallbacks();
Hyunyoung Song31971a32019-01-27 12:31:12 -0800547 }
548
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400549 public void onAssistantVisibilityChanged(float visibility) {
550 mCurrentAssistantVisibility = visibility;
551 float alpha = 1f - visibility;
552 LauncherState state = mStateManager.getState();
553 if (state == NORMAL) {
554 mAppsView.getAlphaProperty(APPS_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
555 } else if (state == OVERVIEW || state == OVERVIEW_PEEK) {
556 mAppsView.getAlphaProperty(APPS_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
557 mScrimView.getAlphaProperty(SCRIM_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
558 }
559 }
560
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800561 private void initDeviceProfile(InvariantDeviceProfile idp) {
562 // Load configuration-specific DeviceProfile
Sunny Goyal59d086c2018-05-07 17:31:40 -0700563 mDeviceProfile = idp.getDeviceProfile(this);
Sunny Goyal8c48d8b2019-01-25 15:10:18 -0800564 if (isInMultiWindowMode()) {
Winson Chung13c1c2c2019-09-06 11:46:19 -0700565 // Note: Calls to getSize() can't rely on our cached DefaultDisplay since it can return
566 // the app window size
Sunny Goyal59d086c2018-05-07 17:31:40 -0700567 Display display = getWindowManager().getDefaultDisplay();
568 Point mwSize = new Point();
569 display.getSize(mwSize);
570 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
571 }
Sunny Goyalae6e3182019-04-30 12:04:37 -0700572
Sunny Goyal59d086c2018-05-07 17:31:40 -0700573 onDeviceProfileInitiated();
Sunny Goyalc4d32012020-04-03 17:10:11 -0700574 mModelWriter = mModel.getWriter(getDeviceProfile().isVerticalBarLayout(), true);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800575 }
576
Sunny Goyal623eddd2018-03-02 12:24:41 -0800577 public RotationHelper getRotationHelper() {
578 return mRotationHelper;
579 }
580
Tracy Zhou4d7b2442019-12-09 13:42:57 -0800581 public ViewGroupFocusHelper getFocusHandler() {
582 return mFocusHandler;
583 }
584
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700585 public LauncherStateManager getStateManager() {
586 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700587 }
588
Mario Bertschler27288382017-05-24 15:35:09 -0700589 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700590 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800591 return mLauncherView.findViewById(id);
592 }
593
Adam Cohen9211d422014-10-07 18:14:53 -0700594 private LauncherCallbacks mLauncherCallbacks;
595
Sunny Goyal32554d12015-12-03 15:31:25 -0800596 /**
597 * Call this after onCreate to set or clear overlay.
598 */
Sunny Goyalb1d7de22019-09-06 10:36:54 -0700599 @Override
Sunny Goyal32554d12015-12-03 15:31:25 -0800600 public void setLauncherOverlay(LauncherOverlay overlay) {
601 if (overlay != null) {
602 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
603 }
604 mWorkspace.setLauncherOverlay(overlay);
605 }
606
Sunny Goyalb1d7de22019-09-06 10:36:54 -0700607 @Override
608 public void runOnOverlayHidden(Runnable runnable) {
609 getWorkspace().runOnOverlayHidden(runnable);
610 }
611
Adam Cohen9211d422014-10-07 18:14:53 -0700612 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
613 mLauncherCallbacks = callbacks;
614 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700615 }
616
Hyunyoung Song3f471442015-04-08 19:01:34 -0700617 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700618 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
619 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700620 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700621 }
622
Tony Wickham010d2552017-01-20 08:15:28 -0800623 public PopupDataProvider getPopupDataProvider() {
624 return mPopupDataProvider;
625 }
626
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700627 @Override
Tony Wickhamf34bee82018-12-03 18:11:39 -0800628 public DotInfo getDotInfoForItem(ItemInfo info) {
629 return mPopupDataProvider.getDotInfoForItem(info);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700630 }
631
632 @Override
633 public void invalidateParent(ItemInfo info) {
Jon Mirandae6f3fa42019-03-04 09:14:40 -0800634 if (info.container >= 0) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700635 View folderIcon = getWorkspace().getHomescreenIconByItemId(info.container);
Jon Mirandae6f3fa42019-03-04 09:14:40 -0800636 if (folderIcon instanceof FolderIcon && folderIcon.getTag() instanceof FolderInfo) {
Sunny Goyal11a50742019-08-07 09:24:26 -0700637 if (new FolderGridOrganizer(getDeviceProfile().inv)
638 .setFolderInfo((FolderInfo) folderIcon.getTag())
639 .isItemInPreview(info.rank)) {
Jon Mirandae6f3fa42019-03-04 09:14:40 -0800640 folderIcon.invalidate();
641 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700642 }
643 }
644 }
645
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000646 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700647 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
648 * a configuration step, this allows the proper animations to run after other transitions.
649 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700650 private int completeAdd(
Sunny Goyal2100c782016-08-22 16:00:03 -0700651 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
Sunny Goyalefb7e842018-10-04 15:11:00 -0700652 int screenId = info.screenId;
Sunny Goyal2100c782016-08-22 16:00:03 -0700653 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700654 // When the screen id represents an actual screen (as opposed to a rank) we make sure
655 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700656 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700657 }
Adam Cohendb364c32014-05-20 14:23:40 -0700658
Sunny Goyal2100c782016-08-22 16:00:03 -0700659 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800660 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700661 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Pinyao Tingc9074272019-11-11 11:51:44 -0800662 announceForAccessibility(R.string.item_added_to_workspace);
Winson Chungb8472bb2011-08-05 13:49:21 -0700663 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800664 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700665 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700666 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700667 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700668 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700669 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700670 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700671 int widgetId = appWidgetId;
672 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700673 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700674 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700675 // Since the view was just bound, also launch the configure activity if needed
676 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
677 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800678 if (provider != null) {
679 new WidgetAddFlowHandler(provider)
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700680 .startConfigActivity(this, widgetInfo,
681 REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700682 }
683 }
684 break;
685 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800686 }
Adam Cohendb364c32014-05-20 14:23:40 -0700687 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800688 }
689
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800690 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700691 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700692 if (isWorkspaceLoading()) {
693 // process the result once the workspace has loaded.
694 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
695 return;
696 }
697 mPendingActivityResult = null;
698
Winson Chunge341d302013-08-16 14:31:00 -0700699 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700700 final PendingRequestArgs requestArgs = mPendingRequestArgs;
701 setWaitingForResult(null);
702 if (requestArgs == null) {
703 return;
704 }
705
706 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700707
Adam Cohenad4e15c2013-10-17 16:21:35 -0700708 Runnable exitSpringLoaded = new Runnable() {
709 @Override
710 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700711 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700712 }
713 };
714
Michael Jurka8b805b12012-04-18 14:23:14 -0700715 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700716 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700717 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700718 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
719 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700720 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Sunny Goyal6e379fc2020-03-19 16:08:14 -0700721 mWorkspace.removeExtraEmptyScreenDelayed(
722 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false, exitSpringLoaded);
Michael Jurka8b805b12012-04-18 14:23:14 -0700723 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700724 addAppWidgetImpl(
725 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800726 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700727 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700728 }
729 return;
730 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200731
Adam Cohened66b2b2012-01-23 17:28:51 -0800732 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700733 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700734
Adam Cohened66b2b2012-01-23 17:28:51 -0800735 // We have special handling for widgets
736 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800737 final int appWidgetId;
738 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
739 : -1;
740 if (widgetId < 0) {
741 appWidgetId = pendingAddWidgetId;
742 } else {
743 appWidgetId = widgetId;
744 }
745
Adam Cohenad4e15c2013-10-17 16:21:35 -0700746 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800747 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700748 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
749 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700750 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700751 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Sunny Goyal6e379fc2020-03-19 16:08:14 -0700752 mWorkspace.removeExtraEmptyScreenDelayed(
753 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false,
754 () -> getStateManager().goToState(NORMAL));
Sunny Goyal2100c782016-08-22 16:00:03 -0700755 } else {
756 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
757 // When the screen id represents an actual screen (as opposed to a rank)
758 // we make sure that the drop page actually exists.
759 requestArgs.screenId =
760 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700761 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700762 final CellLayout dropLayout =
763 mWorkspace.getScreenWithId(requestArgs.screenId);
764
765 dropLayout.setDropPending(true);
766 final Runnable onComplete = new Runnable() {
767 @Override
768 public void run() {
769 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
770 dropLayout.setDropPending(false);
771 }
772 };
Sunny Goyal6e379fc2020-03-19 16:08:14 -0700773 mWorkspace.removeExtraEmptyScreenDelayed(
774 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false, onComplete);
Winson Chung5aaab772012-04-27 15:24:02 -0700775 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800776 return;
777 }
778
Sunny Goyald478c832016-04-01 12:04:16 -0700779 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
780 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700781 if (resultCode == RESULT_OK) {
782 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700783 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700784 }
785 // Leave the widget in the pending state if the user canceled the configure.
786 return;
787 }
788
Sunny Goyalaad90582015-07-09 14:22:50 -0700789 if (requestCode == REQUEST_CREATE_SHORTCUT) {
790 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700791 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
792 completeAdd(requestCode, data, -1, requestArgs);
Sunny Goyal6e379fc2020-03-19 16:08:14 -0700793 mWorkspace.removeExtraEmptyScreenDelayed(
794 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false, exitSpringLoaded);
Sunny Goyal2100c782016-08-22 16:00:03 -0700795
Sunny Goyalaad90582015-07-09 14:22:50 -0700796 } else if (resultCode == RESULT_CANCELED) {
Sunny Goyal6e379fc2020-03-19 16:08:14 -0700797 mWorkspace.removeExtraEmptyScreenDelayed(
798 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false, exitSpringLoaded);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800799 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800800 }
Hongming Jind91cb6e2020-01-27 16:23:03 -0800801
Adam Cohened66b2b2012-01-23 17:28:51 -0800802 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800803 }
804
805 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700806 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800807 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyaldedda052019-05-14 15:23:48 -0700808 mPendingActivityRequestCode = -1;
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800809 handleActivityResult(requestCode, resultCode, data);
Adam Cohened66b2b2012-01-23 17:28:51 -0800810 }
811
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700812 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700813 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600814 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700815 PendingRequestArgs pendingArgs = mPendingRequestArgs;
816 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
817 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
818 setWaitingForResult(null);
819
Sunny Goyal28c6b962015-10-12 11:42:05 -0700820 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700821 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700822 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700823 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700824 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700825 Intent intent = pendingArgs.getPendingIntent();
826
Sunny Goyal28c6b962015-10-12 11:42:05 -0700827 if (grantResults.length > 0
828 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal369212a2019-03-26 15:03:57 -0700829 startActivitySafely(v, intent, null, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700830 } else {
831 // TODO: Show a snack bar with link to settings
832 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700833 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700834 }
835 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600836 }
837
Adam Cohendb364c32014-05-20 14:23:40 -0700838 /**
839 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
840 *
841 * @param screenId the screen id to check
842 * @return the new screen, or screenId if it exists
843 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700844 private int ensurePendingDropLayoutExists(int screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800845 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700846 if (dropLayout == null) {
847 // it's possible that the add screen was removed because it was
848 // empty and a re-bind occurred
849 mWorkspace.addExtraEmptyScreen();
850 return mWorkspace.commitExtraEmptyScreen();
851 } else {
852 return screenId;
853 }
854 }
855
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700856 @Thunk
857 void completeTwoStageWidgetDrop(
Sunny Goyal2100c782016-08-22 16:00:03 -0700858 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
859 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800860 Runnable onCompleteRunnable = null;
861 int animationType = 0;
862
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700863 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800864 if (resultCode == RESULT_OK) {
865 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
866 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800867 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700868 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800869 onCompleteRunnable = new Runnable() {
870 @Override
871 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700872 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700873 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800874 }
875 };
876 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800877 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800878 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800879 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700880 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700881 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700882 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
883 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700884 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700885 // The animated view may be null in the case of a rotation during widget configuration
886 onCompleteRunnable.run();
887 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800888 }
889
890 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700891 protected void onStop() {
Riddle Hsu7a7468f2019-11-21 01:32:58 +0800892 final boolean wasActive = isUserActive();
893 final LauncherState origState = getStateManager().getState();
894 final int origDragLayerChildCount = mDragLayer.getChildCount();
Michael Jurkacd496d72013-04-11 11:32:45 -0700895 super.onStop();
Winson Chung0b70cd42019-06-17 22:34:33 -0700896
Sunny Goyalb1d7de22019-09-06 10:36:54 -0700897 if (mDeferOverlayCallbacks) {
898 checkIfOverlayStillDeferred();
899 } else {
900 mOverlayManager.onActivityStopped(this);
Adam Cohen9211d422014-10-07 18:14:53 -0700901 }
Sunny Goyalb1d7de22019-09-06 10:36:54 -0700902
Becky Qiubfc2da12019-08-01 11:09:38 -0700903 logStopAndResume(Action.Command.STOP);
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700904
905 mAppWidgetHost.setListenIfResumed(false);
906
Tony Wickham010d2552017-01-20 08:15:28 -0800907 NotificationListener.removeNotificationsChangedListener();
Sunny Goyalaf161fd2018-02-05 16:34:57 -0800908 getStateManager().moveToRestState();
Winson Chungedb0d6b2018-03-21 17:32:55 -0700909
Winson Chungdea51352018-04-24 13:02:10 -0700910 // Workaround for b/78520668, explicitly trim memory once UI is hidden
Winson Chung473730f2018-04-26 11:54:22 -0700911 onTrimMemory(TRIM_MEMORY_UI_HIDDEN);
Riddle Hsu7a7468f2019-11-21 01:32:58 +0800912
913 if (wasActive) {
914 // The expected condition is that this activity is stopped because the device goes to
915 // sleep and the UI may have noticeable changes.
916 mDragLayer.post(() -> {
917 if ((!getStateManager().isInStableState(origState)
918 // The drag layer may be animating (e.g. dismissing QSB).
919 || mDragLayer.getAlpha() < 1
920 // Maybe an ArrowPopup is closed.
921 || mDragLayer.getChildCount() != origDragLayerChildCount)) {
922 onUiChangedWhileSleeping();
923 }
924 });
925 }
vadimt0dd1bd12020-02-14 14:42:35 -0800926
927 TestLogging.recordEvent(TestProtocol.SEQUENCE_MAIN, "Activity.onStop");
Michael Jurkacd496d72013-04-11 11:32:45 -0700928 }
929
930 @Override
931 protected void onStart() {
Winson Chung5cc62c72019-10-16 11:32:41 -0700932 Object traceToken = TraceHelper.INSTANCE.beginSection(ON_START_EVT,
933 TraceHelper.FLAG_UI_EVENT);
Michael Jurkacd496d72013-04-11 11:32:45 -0700934 super.onStart();
Sunny Goyalb1d7de22019-09-06 10:36:54 -0700935 if (!mDeferOverlayCallbacks) {
936 mOverlayManager.onActivityStarted(this);
Adam Cohen9211d422014-10-07 18:14:53 -0700937 }
Sunny Goyalb1d7de22019-09-06 10:36:54 -0700938
Sunny Goyal67419a12017-11-14 16:55:22 -0800939 mAppWidgetHost.setListenIfResumed(true);
Winson Chung5cc62c72019-10-16 11:32:41 -0700940 TraceHelper.INSTANCE.endSection(traceToken);
vadimt0dd1bd12020-02-14 14:42:35 -0800941 TestLogging.recordEvent(TestProtocol.SEQUENCE_MAIN, "Activity.onStart");
Michael Jurkacd496d72013-04-11 11:32:45 -0700942 }
943
Sunny Goyala002c6c2019-02-12 16:20:10 -0800944 private void handleDeferredResume() {
Sunny Goyale9c6f4c2019-05-30 11:35:25 -0700945 if (hasBeenResumed() && !mStateManager.getState().disableInteraction) {
Becky Qiubfc2da12019-08-01 11:09:38 -0700946 logStopAndResume(Action.Command.RESUME);
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700947 getUserEventDispatcher().startSession();
Sunny Goyala002c6c2019-02-12 16:20:10 -0800948
Sunny Goyal369212a2019-03-26 15:03:57 -0700949 AppLaunchTracker.INSTANCE.get(this).onReturnedToHome();
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700950
Sunny Goyale9c6f4c2019-05-30 11:35:25 -0700951 // Process any items that were added while Launcher was away.
952 InstallShortcutReceiver.disableAndFlushInstallQueue(
953 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
954
955 // Refresh shortcuts if the permission changed.
956 mModel.refreshShortcutsIfRequired();
957
Winson Chung87583112019-06-10 14:20:58 -0700958 // Set the notification listener and fetch updated notifications when we resume
959 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
960
Sunny Goyale9c6f4c2019-05-30 11:35:25 -0700961 DiscoveryBounce.showForHomeIfNeeded(this);
962
Sunny Goyal210e1742019-10-17 12:05:38 -0700963 onDeferredResumed();
964 addActivityFlags(ACTIVITY_STATE_DEFERRED_RESUMED);
965
Sunny Goyale9c6f4c2019-05-30 11:35:25 -0700966 mDeferredResumePending = false;
967 } else {
968 mDeferredResumePending = true;
Sunny Goyaldedda052019-05-14 15:23:48 -0700969 }
970 }
971
Sunny Goyal210e1742019-10-17 12:05:38 -0700972 protected void onDeferredResumed() { }
973
Becky Qiubfc2da12019-08-01 11:09:38 -0700974 private void logStopAndResume(int command) {
975 int containerType = mStateManager.getState().containerType;
976 if (containerType == ContainerType.WORKSPACE && mWorkspace != null) {
977 getUserEventDispatcher().logActionCommand(command,
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700978 containerType, -1, mWorkspace.isOverlayShown() ? -1 : 0);
Becky Qiubfc2da12019-08-01 11:09:38 -0700979 } else {
980 getUserEventDispatcher().logActionCommand(command, containerType, -1);
981 }
Sunny Goyalb1d7de22019-09-06 10:36:54 -0700982 }
Becky Qiubfc2da12019-08-01 11:09:38 -0700983
Sunny Goyalb1d7de22019-09-06 10:36:54 -0700984 private void scheduleDeferredCheck() {
985 mHandler.removeCallbacks(mDeferredOverlayCallbacks);
986 postAsyncCallback(mHandler, mDeferredOverlayCallbacks);
987 }
988
989 private void checkIfOverlayStillDeferred() {
990 if (!mDeferOverlayCallbacks) {
991 return;
992 }
993 if (isStarted() && (!hasBeenResumed() || mStateManager.getState().disableInteraction)) {
994 return;
995 }
996 mDeferOverlayCallbacks = false;
997
998 // Move the client to the correct state. Calling the same method twice is no-op.
999 if (isStarted()) {
1000 mOverlayManager.onActivityStarted(this);
1001 }
1002 if (hasBeenResumed()) {
1003 mOverlayManager.onActivityResumed(this);
1004 } else {
1005 mOverlayManager.onActivityPaused(this);
1006 }
1007 if (!isStarted()) {
1008 mOverlayManager.onActivityStopped(this);
1009 }
1010 }
1011
1012 public void deferOverlayCallbacksUntilNextResumeOrStop() {
1013 mDeferOverlayCallbacks = true;
1014 }
1015
1016 public LauncherOverlayManager getOverlayManager() {
1017 return mOverlayManager;
Becky Qiubfc2da12019-08-01 11:09:38 -07001018 }
1019
Winson Chungef528762019-09-06 12:05:52 -07001020 public void onStateSetStart(LauncherState state) {
Sunny Goyale9c6f4c2019-05-30 11:35:25 -07001021 if (mDeferredResumePending) {
1022 handleDeferredResume();
1023 }
Sunny Goyalb1d7de22019-09-06 10:36:54 -07001024 if (mDeferOverlayCallbacks) {
1025 scheduleDeferredCheck();
Sunny Goyale9c6f4c2019-05-30 11:35:25 -07001026 }
Sunny Goyal210e1742019-10-17 12:05:38 -07001027 addActivityFlags(ACTIVITY_STATE_TRANSITION_ACTIVE);
Sunny Goyaldedda052019-05-14 15:23:48 -07001028 }
1029
Winson Chungef528762019-09-06 12:05:52 -07001030 public void onStateSetEnd(LauncherState state) {
1031 getAppWidgetHost().setResumed(state == LauncherState.NORMAL);
1032 getWorkspace().setClipChildren(!state.disablePageClipping);
1033 finishAutoCancelActionMode();
Sunny Goyal210e1742019-10-17 12:05:38 -07001034 removeActivityFlags(ACTIVITY_STATE_TRANSITION_ACTIVE);
Winson Chungef528762019-09-06 12:05:52 -07001035 }
1036
Michael Jurkacd496d72013-04-11 11:32:45 -07001037 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001038 protected void onResume() {
Winson Chung5cc62c72019-10-16 11:32:41 -07001039 Object traceToken = TraceHelper.INSTANCE.beginSection(ON_RESUME_EVT,
1040 TraceHelper.FLAG_UI_EVENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001041 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001042
Sunny Goyala002c6c2019-02-12 16:20:10 -08001043 mHandler.removeCallbacks(mHandleDeferredResume);
1044 Utilities.postAsyncCallback(mHandler, mHandleDeferredResume);
Hyunyoung Songbd6fba92018-05-16 15:54:31 -07001045
Winson Chung940b0ac2019-07-09 11:33:50 -07001046 if (!mOnResumeCallbacks.isEmpty()) {
1047 final ArrayList<OnResumeCallback> resumeCallbacks = new ArrayList<>(mOnResumeCallbacks);
1048 mOnResumeCallbacks.clear();
1049 for (int i = resumeCallbacks.size() - 1; i >= 0; i--) {
1050 resumeCallbacks.get(i).onLauncherResume();
1051 }
1052 resumeCallbacks.clear();
Winson Chung0b70cd42019-06-17 22:34:33 -07001053 }
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001054
Sunny Goyalb1d7de22019-09-06 10:36:54 -07001055 if (mDeferOverlayCallbacks) {
1056 scheduleDeferredCheck();
1057 } else {
1058 mOverlayManager.onActivityResumed(this);
Adam Cohen9211d422014-10-07 18:14:53 -07001059 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -08001060
Winson Chung5cc62c72019-10-16 11:32:41 -07001061 TraceHelper.INSTANCE.endSection(traceToken);
Adam Cohen06dff352012-06-01 17:17:08 -07001062 }
1063
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001064 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001065 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001066 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -07001067 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -07001068
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001069 super.onPause();
Joe Onorato24b6fd82009-11-12 13:47:09 -08001070 mDragController.cancelDrag();
Sunny Goyal9b180102020-03-11 10:02:29 -07001071 mLastTouchUpTime = -1;
Hyunyoung Song497708c2019-05-01 16:16:53 -07001072 mDropTargetBar.animateToVisibility(false);
Sunny Goyalb1d7de22019-09-06 10:36:54 -07001073
1074 if (!mDeferOverlayCallbacks) {
1075 mOverlayManager.onActivityPaused(this);
Adam Cohenbffe7452013-07-22 18:21:45 -07001076 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001077 }
1078
Adam Cohenc2d6e892014-10-16 09:49:24 -07001079 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1080
Sunny Goyalc86df472016-02-25 09:19:38 -08001081 public void onScrollChanged(float progress) {
1082 if (mWorkspace != null) {
1083 mWorkspace.onOverlayScrollChanged(progress);
1084 }
1085 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001086 }
1087
Sunny Goyalf9403d92017-10-18 10:55:56 -07001088 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -07001089 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -07001090 }
1091
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001092 /**
1093 * Restores the previous state, if it exists.
1094 *
1095 * @param savedState The previous state.
1096 */
1097 private void restoreState(Bundle savedState) {
1098 if (savedState == null) {
1099 return;
1100 }
1101
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001102 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -07001103 LauncherState[] stateValues = LauncherState.values();
1104 LauncherState state = stateValues[stateOrdinal];
Sunny Goyalbc683e92017-12-06 10:25:07 -08001105 if (!state.disableRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001106 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001107 }
1108
Samuel Fufaca37b8a2019-08-19 17:04:36 -07001109 PendingRequestArgs requestArgs = savedState.getParcelable(
1110 RUNTIME_STATE_PENDING_REQUEST_ARGS);
Sunny Goyal2100c782016-08-22 16:00:03 -07001111 if (requestArgs != null) {
1112 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001113 }
Sunny Goyaldedda052019-05-14 15:23:48 -07001114 mPendingActivityRequestCode = savedState.getInt(RUNTIME_STATE_PENDING_REQUEST_CODE);
Sunny Goyal2100c782016-08-22 16:00:03 -07001115
1116 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001117
1118 SparseArray<Parcelable> widgetsState =
1119 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
1120 if (widgetsState != null) {
1121 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
1122 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001123 }
1124
1125 /**
1126 * Finds all the views we need and configure them properly.
1127 */
Tony Wickham04fca162020-02-05 15:06:52 -08001128 protected void setupViews() {
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001129 mDragLayer = findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001130 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -07001131 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001132 mWorkspace.initParentViews(mDragLayer);
Sreyas321bfae2020-02-21 17:53:02 -08001133 mOverviewPanel = findViewById(R.id.overview_panel);
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001134 mHotseat = findViewById(R.id.hotseat);
Craig Mautner360310b2012-10-26 15:13:08 -07001135
Sunny Goyal784f9c32016-03-23 16:56:54 -07001136 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1137 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1138 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001139
Winson Chung4c98d922011-05-31 16:50:48 -07001140 // Setup the drag layer
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001141 mDragLayer.setup(mDragController, mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001142
Hyunyoung Song645764e2016-06-06 14:19:02 -07001143 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001144 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1145 // default state, otherwise we will update to the wrong offsets in RTL
1146 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001147 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001148 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001149
Tony Wickham34d2c912015-09-11 09:27:58 -07001150 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001151 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001152
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001153 // Setup Apps
1154 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001155
Govinda Wassermanc06e1512019-04-09 09:56:53 -04001156 // Setup Scrim
1157 mScrimView = findViewById(R.id.scrim_view);
1158
Winson Chung3d503fb2011-07-13 17:25:49 -07001159 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Sunny Goyal47328fd2016-05-25 18:56:41 -07001160 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001161
Tony Wickham490a8ab2020-04-01 17:03:58 -07001162 mAllAppsController.setupViews(mAppsView, mScrimView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001163 }
1164
1165 /**
1166 * Creates a view representing a shortcut.
1167 *
1168 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001169 */
Sunny Goyal95899162019-03-27 16:03:06 -07001170 View createShortcut(WorkspaceItemInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001171 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001172 }
1173
1174 /**
1175 * Creates a view representing a shortcut inflated from the specified resource.
1176 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001177 * @param parent The group the shortcut belongs to.
Samuel Fufaca37b8a2019-08-19 17:04:36 -07001178 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001179 * @return A View inflated from layoutResId.
1180 */
Sunny Goyal95899162019-03-27 16:03:06 -07001181 public View createShortcut(ViewGroup parent, WorkspaceItemInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -08001182 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
1183 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal95899162019-03-27 16:03:06 -07001184 favorite.applyFromWorkspaceItem(info);
Sunny Goyal8a2a63b2018-03-06 22:15:18 -08001185 favorite.setOnClickListener(ItemClickHandler.INSTANCE);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001186 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001187 return favorite;
1188 }
1189
1190 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -07001191 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001192 *
1193 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001194 */
Sunny Goyalefb7e842018-10-04 15:11:00 -07001195 private void completeAddShortcut(Intent data, int container, int screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001196 int cellY, PendingRequestArgs args) {
Pinyao Ting5eee5f42019-10-31 16:47:35 -07001197 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
Jon Mirandac476d6e2017-05-04 16:30:01 -07001198 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001199 return;
1200 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001201
Jon Mirandac476d6e2017-05-04 16:30:01 -07001202 int[] cellXY = mTmpAddItemCellCoordinates;
1203 CellLayout layout = getCellLayout(container, screenId);
1204
Sunny Goyal95899162019-03-27 16:03:06 -07001205 WorkspaceItemInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001206 if (Utilities.ATLEAST_OREO) {
Sunny Goyale7b00122019-10-02 16:13:34 -07001207 info = PinRequestHelper.createWorkspaceItemFromPinItemRequest(
1208 this, PinRequestHelper.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001209 }
1210
1211 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001212 // Legacy shortcuts are only supported for primary profile.
1213 info = Process.myUserHandle().equals(args.user)
1214 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001215
Sunny Goyalb57645f2017-03-20 13:57:28 -07001216 if (info == null) {
1217 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1218 return;
1219 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001220 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001221 // The app is trying to add a shortcut without sufficient permissions
1222 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1223 return;
1224 }
1225 }
1226
Jon Mirandac476d6e2017-05-04 16:30:01 -07001227 if (container < 0) {
1228 // Adding a shortcut to the Workspace.
1229 final View view = createShortcut(info);
1230 boolean foundCellSpan = false;
1231 // First we check if we already know the exact location where we want to add this item.
1232 if (cellX >= 0 && cellY >= 0) {
1233 cellXY[0] = cellX;
1234 cellXY[1] = cellY;
1235 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001236
Hyunyoung Song1ae42422020-01-26 23:25:14 -08001237 DragObject dragObject = new DragObject(getApplicationContext());
1238 dragObject.dragInfo = info;
Jon Mirandac476d6e2017-05-04 16:30:01 -07001239 // If appropriate, either create a folder or add to an existing folder
1240 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Hyunyoung Song1ae42422020-01-26 23:25:14 -08001241 true, dragObject)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001242 return;
1243 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001244 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1245 true)) {
1246 return;
1247 }
1248 } else {
1249 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1250 }
1251
1252 if (!foundCellSpan) {
1253 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001254 return;
1255 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001256
1257 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1258 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001259 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001260 // Adding a shortcut to a Folder.
Sunny Goyale29897f52017-07-20 10:09:42 -07001261 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001262 if (folderIcon != null) {
1263 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1264 folderInfo.add(info, args.rank, false);
1265 } else {
Sunny Goyale29897f52017-07-20 10:09:42 -07001266 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001267 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001268 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001269 }
1270
Sunny Goyalefb7e842018-10-04 15:11:00 -07001271 public FolderIcon findFolderIcon(final int folderIconId) {
Sunny Goyal83fd25e2018-07-09 16:47:01 -07001272 return (FolderIcon) mWorkspace.getHomescreenIconByItemId(folderIconId);
Sunny Goyale29897f52017-07-20 10:09:42 -07001273 }
1274
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001275 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001276 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001277 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001278 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001279 */
Samuel Fufaca37b8a2019-08-19 17:04:36 -07001280 @Thunk
1281 void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001282 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1283
Adam Cohened66b2b2012-01-23 17:28:51 -08001284 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001285 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001286 }
Romain Guycbb89e42009-06-08 15:52:54 -07001287
Adam Cohen59400422014-03-05 18:07:04 -08001288 LauncherAppWidgetInfo launcherInfo;
1289 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001290 launcherInfo.spanX = itemInfo.spanX;
1291 launcherInfo.spanY = itemInfo.spanY;
1292 launcherInfo.minSpanX = itemInfo.minSpanX;
1293 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001294 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001295
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001296 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001297 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001298
Sunny Goyal2100c782016-08-22 16:00:03 -07001299 if (hostView == null) {
1300 // Perform actual inflation because we're live
1301 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001302 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001303 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301304 prepareAppWidget(hostView, launcherInfo);
1305 mWorkspace.addInScreen(hostView, launcherInfo);
Pinyao Tingc9074272019-11-11 11:51:44 -08001306 announceForAccessibility(R.string.item_added_to_workspace);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001307 }
Romain Guycbb89e42009-06-08 15:52:54 -07001308
Sunny Goyald5462aa2016-11-22 16:52:39 +05301309 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001310 hostView.setTag(item);
1311 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001312 hostView.setFocusable(true);
1313 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001314 }
1315
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001316 private final BroadcastReceiver mScreenOffReceiver = new BroadcastReceiver() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001317 @Override
1318 public void onReceive(Context context, Intent intent) {
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001319 // Reset AllApps to its initial state only if we are not in the middle of
1320 // processing a multi-step drop
1321 if (mPendingRequestArgs == null) {
Riddle Hsu7a7468f2019-11-21 01:32:58 +08001322 if (!isInState(NORMAL)) {
1323 onUiChangedWhileSleeping();
1324 }
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001325 mStateManager.goToState(NORMAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001326 }
1327 }
1328 };
1329
Riddle Hsu7a7468f2019-11-21 01:32:58 +08001330 protected void onUiChangedWhileSleeping() { }
1331
Sunny Goyal9c2b9602020-01-07 13:07:55 -08001332 private void updateNotificationDots(Predicate<PackageUserKey> updatedDots) {
Tony Wickhamf34bee82018-12-03 18:11:39 -08001333 mWorkspace.updateNotificationDots(updatedDots);
1334 mAppsView.getAppsStore().updateNotificationDots(updatedDots);
Tony Wickham010d2552017-01-20 08:15:28 -08001335 }
1336
Adam Cohended9f8d2010-11-03 13:25:16 -07001337 @Override
1338 public void onAttachedToWindow() {
1339 super.onAttachedToWindow();
Sunny Goyalb1d7de22019-09-06 10:36:54 -07001340 mOverlayManager.onAttachedToWindow();
Adam Cohended9f8d2010-11-03 13:25:16 -07001341 }
1342
1343 @Override
1344 public void onDetachedFromWindow() {
1345 super.onDetachedFromWindow();
Sunny Goyalb1d7de22019-09-06 10:36:54 -07001346 mOverlayManager.onDetachedFromWindow();
1347 closeContextMenu();
Adam Cohended9f8d2010-11-03 13:25:16 -07001348 }
1349
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001350 public AllAppsTransitionController getAllAppsController() {
1351 return mAllAppsController;
1352 }
1353
Sunny Goyal9d69c8d2018-03-19 13:41:31 -07001354 @Override
Winson Chung1a341002018-01-17 10:00:23 -08001355 public LauncherRootView getRootView() {
1356 return (LauncherRootView) mLauncherView;
1357 }
1358
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001359 @Override
Winson Chunga6945242014-01-08 14:04:34 -08001360 public DragLayer getDragLayer() {
1361 return mDragLayer;
1362 }
1363
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001364 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001365 return mAppsView;
1366 }
1367
Winson Chunga6945242014-01-08 14:04:34 -08001368 public Workspace getWorkspace() {
1369 return mWorkspace;
1370 }
1371
1372 public Hotseat getHotseat() {
1373 return mHotseat;
1374 }
1375
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001376 public <T extends View> T getOverviewPanel() {
Winson Chung8ae41982017-11-10 11:42:13 -08001377 return (T) mOverviewPanel;
Winson Chunga6945242014-01-08 14:04:34 -08001378 }
1379
Sunny Goyal47328fd2016-05-25 18:56:41 -07001380 public DropTargetBar getDropTargetBar() {
1381 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001382 }
1383
Tony Wickham490a8ab2020-04-01 17:03:58 -07001384 public ScrimView getScrimView() {
1385 return mScrimView;
1386 }
1387
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001388 public LauncherAppWidgetHost getAppWidgetHost() {
1389 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001390 }
Romain Guycbb89e42009-06-08 15:52:54 -07001391
Winson Chunga9abd0e2010-10-27 17:18:37 -07001392 public LauncherModel getModel() {
1393 return mModel;
1394 }
1395
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001396 public ModelWriter getModelWriter() {
1397 return mModelWriter;
1398 }
1399
Sunny Goyalb1d7de22019-09-06 10:36:54 -07001400 @Override
Adam Cohen79d90c52016-04-22 13:29:20 -07001401 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001402 return mSharedPrefs;
1403 }
1404
Sunny Goyalb1d7de22019-09-06 10:36:54 -07001405 @Override
1406 public SharedPreferences getDevicePrefs() {
1407 return Utilities.getDevicePrefs(this);
1408 }
1409
Samuel Fufaca37b8a2019-08-19 17:04:36 -07001410 public int getOrientation() {
1411 return mOldConfig.orientation;
1412 }
Jon Miranda6bed3502017-09-19 14:43:17 -07001413
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001414 @Override
1415 protected void onNewIntent(Intent intent) {
vadimtbaf5fbb2019-12-16 18:56:09 -08001416 if (Utilities.IS_RUNNING_IN_TEST_HARNESS) {
1417 Log.d(TestProtocol.PERMANENT_DIAG_TAG, "Launcher.onNewIntent: " + intent);
1418 }
Winson Chung5cc62c72019-10-16 11:32:41 -07001419 Object traceToken = TraceHelper.INSTANCE.beginSection(ON_NEW_INTENT_EVT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001420 super.onNewIntent(intent);
1421
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001422 boolean alreadyOnHome = hasWindowFocus() && ((intent.getFlags() &
Winson15f8b172015-08-19 11:02:39 -07001423 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1424 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001425
1426 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001427 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001428 && AbstractFloatingView.getTopOpenView(this) == null;
Winson15f8b172015-08-19 11:02:39 -07001429 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
Sunny Goyale84c5b82019-09-26 17:05:31 -07001430 boolean internalStateHandled = ACTIVITY_TRACKER.handleNewIntent(this, intent);
Sunny Goyald3864fa2017-11-14 15:06:36 -08001431
Winson15f8b172015-08-19 11:02:39 -07001432 if (isActionMain) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001433 if (!internalStateHandled) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001434 // In all these cases, only animate if we're already on home
Andy Wickham65ced1b2020-03-07 02:14:19 +00001435 closeOpenViews(isStarted());
Jon Mirandafeba90f2016-10-06 10:53:29 -07001436
Sunny Goyala2467272018-03-16 14:53:05 -07001437 if (!isInState(NORMAL)) {
1438 // Only change state, if not already the same. This prevents cancelling any
1439 // animations running as part of resume
1440 mStateManager.goToState(NORMAL);
1441 }
Sunny Goyald3864fa2017-11-14 15:06:36 -08001442
1443 // Reset the apps view
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001444 if (!alreadyOnHome) {
Sunny Goyal7185dd62018-03-14 17:51:49 -07001445 mAppsView.reset(isStarted() /* animate */);
Sunny Goyald3864fa2017-11-14 15:06:36 -08001446 }
1447
Sunny Goyalaad33592018-08-07 17:20:35 -07001448 if (shouldMoveToDefaultScreen && !mWorkspace.isHandlingTouch()) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001449 mWorkspace.post(mWorkspace::moveToDefaultScreen);
1450 }
1451 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001452
Hyunyoung Song2a70b3d2019-06-11 12:47:07 -07001453 // Handle HOME_INTENT
1454 UserEventDispatcher ued = getUserEventDispatcher();
1455 Target target = newContainerTarget(mStateManager.getState().containerType);
1456 target.pageIndex = mWorkspace.getCurrentPage();
1457 ued.logActionCommand(Action.Command.HOME_INTENT, target,
1458 newContainerTarget(ContainerType.WORKSPACE));
Sunny Goyaldeb91c42020-03-24 02:17:59 -07001459 hideKeyboard();
Adam Cohen6fecd412013-10-02 17:41:50 -07001460
Adam Cohen9211d422014-10-07 18:14:53 -07001461 if (mLauncherCallbacks != null) {
Sunny Goyal85462132018-04-24 11:39:37 -07001462 mLauncherCallbacks.onHomeIntent(internalStateHandled);
Adam Cohen9211d422014-10-07 18:14:53 -07001463 }
Sunny Goyalb1d7de22019-09-06 10:36:54 -07001464 mOverlayManager.hideOverlay(isStarted() && !isForceInvisible());
Sunny Goyal512a2c12020-04-08 12:52:45 -07001465 } else if (Intent.ACTION_ALL_APPS.equals(intent.getAction())) {
1466 getStateManager().goToState(ALL_APPS, alreadyOnHome);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001467 }
Winson15f8b172015-08-19 11:02:39 -07001468
Winson Chung5cc62c72019-10-16 11:32:41 -07001469 TraceHelper.INSTANCE.endSection(traceToken);
Adam Coppa120b8e2013-11-12 16:26:04 +00001470 }
1471
Sunny Goyaldeb91c42020-03-24 02:17:59 -07001472 /**
1473 * Hides the keyboard if visible
1474 */
1475 public void hideKeyboard() {
1476 final View v = getWindow().peekDecorView();
1477 if (v != null && v.getWindowToken() != null) {
1478 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
1479 }
1480 }
1481
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001482 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001483 public void onRestoreInstanceState(Bundle state) {
1484 super.onRestoreInstanceState(state);
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001485 mWorkspace.restoreInstanceStateForChild(mSynchronouslyBoundPage);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001486 }
1487
1488 @Override
1489 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001490 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001491 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001492
Adam Cohen21cd0022013-10-09 18:57:02 -07001493 }
Sunny Goyalea609262017-10-25 15:47:38 -07001494 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001495
1496
1497 AbstractFloatingView widgets = AbstractFloatingView
1498 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1499 if (widgets != null) {
1500 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1501 widgets.saveHierarchyState(widgetsState);
1502 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1503 } else {
1504 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1505 }
1506
Pinyao Tinga74b63a2019-07-17 23:26:06 -07001507 // We close any open folders and shortcut containers that are not safe for rebind,
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001508 // and we need to make sure this state is reflected.
Pinyao Tinga74b63a2019-07-17 23:26:06 -07001509 AbstractFloatingView.closeOpenViews(this, false, TYPE_ALL & ~TYPE_REBIND_SAFE);
1510 finishAutoCancelActionMode();
Tony Wickham49c8d292016-07-01 11:44:34 -07001511
Sunny Goyal2100c782016-08-22 16:00:03 -07001512 if (mPendingRequestArgs != null) {
1513 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1514 }
Sunny Goyaldedda052019-05-14 15:23:48 -07001515 outState.putInt(RUNTIME_STATE_PENDING_REQUEST_CODE, mPendingActivityRequestCode);
1516
Sunny Goyal2100c782016-08-22 16:00:03 -07001517 if (mPendingActivityResult != null) {
1518 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001519 }
1520
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001521 super.onSaveInstanceState(outState);
Sunny Goyalb1d7de22019-09-06 10:36:54 -07001522 mOverlayManager.onActivitySaveInstanceState(this, outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001523 }
1524
1525 @Override
1526 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001527 super.onDestroy();
Sunny Goyale84c5b82019-09-26 17:05:31 -07001528 ACTIVITY_TRACKER.onActivityDestroyed(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001529
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001530 unregisterReceiver(mScreenOffReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001531 mWorkspace.removeFolderListeners();
Sunny Goyalb1d7de22019-09-06 10:36:54 -07001532 PluginManagerWrapper.INSTANCE.get(this).removePluginListener(this);
Winson Chunge7a03942011-08-05 15:05:12 -07001533
Sunny Goyala7a5bf32020-01-05 15:35:29 +05301534 mModel.removeCallbacks(this);
Sunny Goyal623eddd2018-03-02 12:24:41 -08001535 mRotationHelper.destroy();
Sunny Goyal745bad92016-05-02 10:54:12 -07001536
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001537 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001538 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001539 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001540 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001541 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001542
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001543 TextKeyListener.getInstance().release();
Tony2917a8b2017-07-31 23:29:42 -07001544 clearPendingBinds();
Hyunyoung Song31971a32019-01-27 12:31:12 -08001545 LauncherAppState.getIDP(this).removeOnChangeListener(this);
Sunny Goyalb1d7de22019-09-06 10:36:54 -07001546
1547 mOverlayManager.onActivityDestroyed(this);
Winson Chung24ab40c2019-10-30 22:35:09 -07001548 mAppTransitionManager.unregisterRemoteAnimations();
Samuel Fufa41d909f2020-03-20 16:27:09 -07001549 mUserChangedCallbackCloseable.close();
Hyunyoung Song3f821352020-04-09 20:14:58 -07001550 mAllAppsController.onActivityDestroyed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001551 }
1552
Sunny Goyalae502842016-06-17 08:43:56 -07001553 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1554 return mAccessibilityDelegate;
1555 }
1556
Adam Cohena9cf38f2011-05-02 15:36:58 -07001557 public DragController getDragController() {
1558 return mDragController;
1559 }
1560
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001561 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001562 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyaldedda052019-05-14 15:23:48 -07001563 if (requestCode != -1) {
1564 mPendingActivityRequestCode = requestCode;
1565 }
Sunny Goyal210e1742019-10-17 12:05:38 -07001566 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001567 }
1568
1569 @Override
Sunny Goyaldedda052019-05-14 15:23:48 -07001570 public void startIntentSenderForResult(IntentSender intent, int requestCode,
Adam Cohen173f7112015-03-27 15:14:00 -07001571 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Sunny Goyaldedda052019-05-14 15:23:48 -07001572 if (requestCode != -1) {
1573 mPendingActivityRequestCode = requestCode;
1574 }
Sunny Goyal210e1742019-10-17 12:05:38 -07001575 try {
1576 super.startIntentSenderForResult(intent, requestCode,
1577 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1578 } catch (IntentSender.SendIntentException e) {
1579 throw new ActivityNotFoundException();
Adam Cohen173f7112015-03-27 15:14:00 -07001580 }
1581 }
1582
Winson Chung70d72102011-08-12 11:24:35 -07001583 /**
1584 * Indicates that we want global search for this activity by setting the globalSearch
1585 * argument for {@link #startSearch} to true.
1586 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001587 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001588 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001589 Bundle appSearchData, boolean globalSearch) {
1590 if (appSearchData == null) {
1591 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001592 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001593 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001594
Sunny Goyal6f28e712016-09-13 14:19:29 -07001595 if (mLauncherCallbacks == null ||
1596 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1597 // Starting search from the callbacks failed. Start the default global search.
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001598 super.startSearch(initialQuery, selectInitialQuery, appSearchData, true);
Ian Parkinson047036e2014-09-01 15:40:53 +01001599 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001600
1601 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001602 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001603 }
1604
Joe Onorato9c1289c2009-08-17 11:03:03 -04001605 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001606 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001607 }
1608
Adam Cohen517a7f52014-03-01 12:12:59 -08001609 public boolean isWorkspaceLoading() {
1610 return mWorkspaceLoading;
1611 }
1612
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001613 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001614 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001615 }
1616
Sunny Goyal04a324a2017-01-20 21:08:59 -08001617 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001618 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001619 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001620
Sunny Goyal2100c782016-08-22 16:00:03 -07001621 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001622 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001623 if (LOGD) {
1624 Log.d(TAG, "Adding widget from drop");
1625 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001626 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001627 }
1628
Sunny Goyal2100c782016-08-22 16:00:03 -07001629 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001630 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
Samuel Fufaca37b8a2019-08-19 17:04:36 -07001631 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info,
1632 REQUEST_CREATE_APPWIDGET)) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001633 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001634
Adam Cohenad4e15c2013-10-17 16:21:35 -07001635 Runnable onComplete = new Runnable() {
1636 @Override
1637 public void run() {
1638 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001639 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001640 }
1641 };
Samuel Fufaca37b8a2019-08-19 17:04:36 -07001642 completeAddAppWidget(appWidgetId, info, boundWidget,
1643 addFlowHandler.getProviderInfo(this));
Sunny Goyal6e379fc2020-03-19 16:08:14 -07001644 mWorkspace.removeExtraEmptyScreenDelayed(delay, false, onComplete);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001645 }
1646 }
Romain Guycbb89e42009-06-08 15:52:54 -07001647
Sunny Goyalefb7e842018-10-04 15:11:00 -07001648 public void addPendingItem(PendingAddItemInfo info, int container, int screenId,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001649 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001650 info.container = container;
1651 info.screenId = screenId;
1652 if (cell != null) {
1653 info.cellX = cell[0];
1654 info.cellY = cell[1];
1655 }
1656 info.spanX = spanX;
1657 info.spanY = spanY;
1658
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001659 switch (info.itemType) {
1660 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1661 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001662 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001663 break;
1664 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001665 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001666 break;
1667 default:
1668 throw new IllegalStateException("Unknown item type: " + info.itemType);
Samuel Fufaca37b8a2019-08-19 17:04:36 -07001669 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001670 }
1671
Adam Cohenfbba09b2011-07-18 21:43:05 -07001672 /**
1673 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001674 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001675 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001676 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1677 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
vadimtcf275c12020-04-15 12:43:01 -07001678 TestLogging.recordEvent(TestProtocol.SEQUENCE_MAIN, "start: processShortcutFromDrop");
Sunny Goyal782f0c92017-01-19 10:27:54 -08001679 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1680 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1681 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001682 }
1683
Adam Cohenfbba09b2011-07-18 21:43:05 -07001684 /**
1685 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001686 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001687 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001688 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001689 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001690 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001691 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001692 // In the case where we've prebound the widget, we remove it from the DragLayer
1693 if (LOGD) {
1694 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1695 }
1696 getDragLayer().removeView(hostView);
1697
Adam Cohened66b2b2012-01-23 17:28:51 -08001698 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001699 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001700
1701 // Clear the boundWidget so that it doesn't get destroyed.
1702 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001703 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001704 // In this case, we either need to start an activity to get permission to bind
1705 // the widget, or we need to start an activity to configure the widget, or both.
Pinyao Tingc7a6c292019-08-26 14:36:02 -07001706 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1707 appWidgetId = CustomWidgetManager.INSTANCE.get(this).getWidgetIdForCustomProvider(
1708 info.componentName);
Sunny Goyal952e63d2017-08-16 04:59:08 -07001709 } else {
1710 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1711 }
Adam Cohendd70d662012-10-04 16:53:44 -07001712 Bundle options = info.bindOptions;
1713
Sunny Goyalffe83f12014-08-14 17:39:34 -07001714 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1715 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001716 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001717 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001718 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001719 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001720 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001721 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001722 }
1723
Samuel Fufa746d4202020-02-02 18:55:44 -08001724 /**
1725 * Creates and adds new folder to CellLayout
1726 */
Samuel Fufa28c3e452020-02-08 16:07:16 -08001727 public FolderIcon addFolder(CellLayout layout, int container, final int screenId, int cellX,
1728 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001729 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001730
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001731 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001732 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001733
1734 // Create the view
Samuel Fufa746d4202020-02-02 18:55:44 -08001735 FolderIcon newFolder = FolderIcon.inflateFolderAndIcon(R.layout.folder_icon, this, layout,
1736 folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301737 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001738 // Force measure the new folder icon
1739 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1740 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001741 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001742 }
Romain Guycbb89e42009-06-08 15:52:54 -07001743
Winsonc0b52fe2015-09-09 16:38:15 -07001744 /**
Samuel Fufa28c3e452020-02-08 16:07:16 -08001745 * Called when a workspace item is converted into a folder
1746 */
1747 public void folderCreatedFromItem(Folder folder, WorkspaceItemInfo itemInfo){}
1748
1749 /**
1750 * Called when a folder is converted into a workspace item
1751 */
1752 public void folderConvertedToItem(Folder folder, WorkspaceItemInfo itemInfo) {}
1753
1754 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001755 * Unbinds the view for the specified item, and removes the item and all its children.
1756 *
1757 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001758 * @param itemInfo the {@link ItemInfo} for this view.
1759 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001760 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001761 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Sunny Goyal95899162019-03-27 16:03:06 -07001762 if (itemInfo instanceof WorkspaceItemInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001763 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001764 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1765 if (folderIcon instanceof FolderIcon) {
Sunny Goyal95899162019-03-27 16:03:06 -07001766 ((FolderInfo) folderIcon.getTag()).remove((WorkspaceItemInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001767 } else {
1768 mWorkspace.removeWorkspaceItem(v);
1769 }
Winsonc0b52fe2015-09-09 16:38:15 -07001770 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001771 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001772 }
1773 } else if (itemInfo instanceof FolderInfo) {
1774 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001775 if (v instanceof FolderIcon) {
1776 ((FolderIcon) v).removeListeners();
1777 }
Winsonc0b52fe2015-09-09 16:38:15 -07001778 mWorkspace.removeWorkspaceItem(v);
1779 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001780 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001781 }
1782 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1783 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001784 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001785 if (deleteFromDb) {
Tony Wickham6a71a5b2018-08-21 11:40:23 -07001786 getModelWriter().deleteWidgetInfo(widgetInfo, getAppWidgetHost());
Winsonc0b52fe2015-09-09 16:38:15 -07001787 }
1788 } else {
1789 return false;
1790 }
1791 return true;
1792 }
1793
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001794 @Override
1795 public boolean dispatchKeyEvent(KeyEvent event) {
vadimtd633c9c2020-01-22 18:00:37 -08001796 TestLogging.recordEvent(TestProtocol.SEQUENCE_MAIN, "Key event", event);
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001797 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001798 }
1799
Joe Onorato88ec0992009-11-19 13:16:06 -08001800 @Override
vadimt9af34a72020-01-21 11:19:13 -08001801 public boolean dispatchTouchEvent(MotionEvent ev) {
Sunny Goyala306e592020-03-24 01:51:41 -07001802 switch (ev.getAction()) {
1803 case MotionEvent.ACTION_DOWN:
1804 mTouchInProgress = true;
1805 break;
1806 case MotionEvent.ACTION_UP:
1807 mLastTouchUpTime = System.currentTimeMillis();
1808 // Follow through
1809 case MotionEvent.ACTION_CANCEL:
1810 mTouchInProgress = false;
1811 break;
Sunny Goyal9b180102020-03-11 10:02:29 -07001812 }
vadimtd633c9c2020-01-22 18:00:37 -08001813 TestLogging.recordMotionEvent(TestProtocol.SEQUENCE_MAIN, "Touch event", ev);
vadimt9af34a72020-01-21 11:19:13 -08001814 return super.dispatchTouchEvent(ev);
1815 }
1816
Sunny Goyala306e592020-03-24 01:51:41 -07001817 /**
1818 * Returns true if a touch interaction is in progress
1819 */
1820 public boolean isTouchInProgress() {
1821 return mTouchInProgress;
1822 }
1823
vadimt9af34a72020-01-21 11:19:13 -08001824 @Override
Joe Onorato88ec0992009-11-19 13:16:06 -08001825 public void onBackPressed() {
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08001826 if (finishAutoCancelActionMode()) {
1827 return;
1828 }
Adam Cohen9211d422014-10-07 18:14:53 -07001829
Sunny Goyal45478022015-06-08 16:52:41 -07001830 if (mDragController.isDragging()) {
1831 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001832 return;
1833 }
1834
Jon Mirandafeba90f2016-10-06 10:53:29 -07001835 // Note: There should be at most one log per method call. This is enforced implicitly
1836 // by using if-else statements.
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001837 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
Tony Wickham52c1b662018-05-21 13:13:58 -07001838 if (topView != null && topView.onBackPressed()) {
1839 // Handled by the floating view.
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001840 } else {
Sunny Goyale39690b2018-08-02 13:35:07 -07001841 mStateManager.getState().onBackPressed(this);
Michael Jurkaaf442092010-06-10 17:01:57 -07001842 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001843 }
1844
Sunny Goyalfe770c92016-09-27 14:38:58 -07001845 @TargetApi(Build.VERSION_CODES.M)
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001846 @Override
Jon Miranda73c27ec2018-05-16 18:06:23 -07001847 public ActivityOptions getActivityLaunchOptions(View v) {
1848 return mAppTransitionManager.getActivityLaunchOptions(this, v);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001849 }
1850
Tony Wickham3a6746a2018-03-29 09:39:56 -07001851 public LauncherAppTransitionManager getAppTransitionManager() {
1852 return mAppTransitionManager;
1853 }
1854
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001855 @TargetApi(Build.VERSION_CODES.M)
1856 @Override
1857 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
1858 // Due to legacy reasons, direct call shortcuts require Launchers to have the
1859 // corresponding permission. Show the appropriate permission prompt if that
1860 // is the case.
1861 if (intent.getComponent() == null
1862 && Intent.ACTION_CALL.equals(intent.getAction())
1863 && checkSelfPermission(android.Manifest.permission.CALL_PHONE) !=
1864 PackageManager.PERMISSION_GRANTED) {
1865
1866 setWaitingForResult(PendingRequestArgs
1867 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
1868 requestPermissions(new String[]{android.Manifest.permission.CALL_PHONE},
1869 REQUEST_PERMISSION_CALL_PHONE);
1870 return true;
1871 } else {
1872 return false;
1873 }
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001874 }
1875
Hyunyoung Song46d07f72018-05-22 15:41:25 -07001876 @Override
Hyunyoung Songfc007472018-10-25 14:09:50 -07001877 public int getCurrentState() {
Samuel Fufaca37b8a2019-08-19 17:04:36 -07001878 if (mStateManager.getState() == LauncherState.ALL_APPS) {
Hyunyoung Songfc007472018-10-25 14:09:50 -07001879 return StatsLogUtils.LAUNCHER_STATE_ALLAPPS;
Govinda Wassermanc06e1512019-04-09 09:56:53 -04001880 } else if (mStateManager.getState() == OVERVIEW) {
Hyunyoung Songfc007472018-10-25 14:09:50 -07001881 return StatsLogUtils.LAUNCHER_STATE_OVERVIEW;
1882 }
Hyunyoung Song0ae38882018-11-05 14:45:19 -08001883 return StatsLogUtils.LAUNCHER_STATE_HOME;
Hyunyoung Songfc007472018-10-25 14:09:50 -07001884 }
1885
1886 @Override
Sunny Goyal369212a2019-03-26 15:03:57 -07001887 public boolean startActivitySafely(View v, Intent intent, ItemInfo item,
1888 @Nullable String sourceContainer) {
Winson Chung0b70cd42019-06-17 22:34:33 -07001889 if (!hasBeenResumed()) {
1890 // Workaround an issue where the WM launch animation is clobbered when finishing the
1891 // recents animation into launcher. Defer launching the activity until Launcher is
1892 // next resumed.
1893 addOnResumeCallback(() -> startActivitySafely(v, intent, item, sourceContainer));
Winson Chungc9bf6d42019-10-04 15:33:18 -07001894 if (mOnDeferredActivityLaunchCallback != null) {
1895 mOnDeferredActivityLaunchCallback.run();
1896 mOnDeferredActivityLaunchCallback = null;
1897 }
Winson Chung0b70cd42019-06-17 22:34:33 -07001898 return true;
1899 }
1900
Sunny Goyal369212a2019-03-26 15:03:57 -07001901 boolean success = super.startActivitySafely(v, intent, item, sourceContainer);
Hyunyoung Song1241e612018-05-15 21:46:51 -07001902 if (success && v instanceof BubbleTextView) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001903 // This is set to the view that launched the activity that navigated the user away
1904 // from launcher. Since there is no callback for when the activity has finished
1905 // launching, enable the press state and keep this reference to reset the press
1906 // state when we return to launcher.
1907 BubbleTextView btv = (BubbleTextView) v;
1908 btv.setStayPressed(true);
Winson Chung0b70cd42019-06-17 22:34:33 -07001909 addOnResumeCallback(btv);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001910 }
Hyunyoung Song1241e612018-05-15 21:46:51 -07001911 return success;
Michael Jurka86a720e2012-05-09 11:23:23 -07001912 }
1913
Winson Chung3d503fb2011-07-13 17:25:49 -07001914 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001915 // TODO: Remove this method
Sunny Goyal876e4622018-11-02 13:50:40 -07001916 return mHotseat != null && (layout == mHotseat);
Winson Chung3d503fb2011-07-13 17:25:49 -07001917 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001918
Winson Chung3d503fb2011-07-13 17:25:49 -07001919 /**
1920 * Returns the CellLayout of the specified container at the specified screen.
1921 */
Sunny Goyalefb7e842018-10-04 15:11:00 -07001922 public CellLayout getCellLayout(int container, int screenId) {
Sunny Goyal876e4622018-11-02 13:50:40 -07001923 return (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1924 ? mHotseat : mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08001925 }
1926
Michael Jurkae326f182011-11-21 14:05:46 -08001927 @Override
1928 public void onTrimMemory(int level) {
1929 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07001930 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
1931 // The widget preview db can result in holding onto over
1932 // 3MB of memory for caching which isn't necessary.
1933 SQLiteDatabase.releaseMemory();
1934
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08001935 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07001936 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08001937 }
1938 }
1939
Michael Jurkad7c28052012-04-27 15:43:36 -07001940 @Override
1941 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07001942 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07001943 final List<CharSequence> text = event.getText();
1944 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07001945 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001946 // TODO: When can workspace be null?
1947 text.add(mWorkspace == null
1948 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07001949 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07001950 return result;
1951 }
1952
Winson Chung0b70cd42019-06-17 22:34:33 -07001953 public void addOnResumeCallback(OnResumeCallback callback) {
1954 mOnResumeCallbacks.add(callback);
Michael Jurka1e2f4652013-07-08 18:03:46 -07001955 }
1956
Michael Jurka7607c2f2013-04-03 14:33:19 -07001957 /**
Winson Chungc9bf6d42019-10-04 15:33:18 -07001958 * Persistant callback which notifies when an activity launch is deferred because the activity
1959 * was not yet resumed.
1960 */
1961 public void setOnDeferredActivityLaunchCallback(Runnable callback) {
1962 mOnDeferredActivityLaunchCallback = callback;
1963 }
1964
1965 /**
Sunny Goyala7a5bf32020-01-05 15:35:29 +05301966 * Sets the next page to bind synchronously on next bind.
1967 * @param page
1968 */
1969 public void setPageToBindSynchronously(int page) {
1970 mPageToBindSynchronously = page;
1971 }
1972
1973 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001974 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001975 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07001976 @Override
Sunny Goyala7a5bf32020-01-05 15:35:29 +05301977 public int getPageToBindSynchronously() {
1978 if (mPageToBindSynchronously != PagedView.INVALID_PAGE) {
1979 return mPageToBindSynchronously;
1980 } else if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001981 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07001982 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07001983 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07001984 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001985 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001986
Joe Onorato9c1289c2009-08-17 11:03:03 -04001987 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07001988 * Clear any pending bind callbacks. This is called when is loader is planning to
1989 * perform a full rebind from scratch.
1990 */
1991 @Override
1992 public void clearPendingBinds() {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001993 if (mPendingExecutor != null) {
1994 mPendingExecutor.markCompleted();
1995 mPendingExecutor = null;
Sunny Goyal6b6d0a32019-08-05 17:19:32 -07001996
1997 // We might have set this flag previously and forgot to clear it.
1998 mAppsView.getAppsStore()
1999 .disableDeferUpdatesSilently(AllAppsStore.DEFER_UPDATES_NEXT_DRAW);
Sunny Goyal527c7d32015-08-28 15:19:36 -07002000 }
2001 }
2002
2003 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002004 * Refreshes the shortcuts shown on the workspace.
2005 *
2006 * Implementation of the method from LauncherModel.Callbacks.
2007 */
2008 public void startBinding() {
Winson Chung5cc62c72019-10-16 11:32:41 -07002009 Object traceToken = TraceHelper.INSTANCE.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002010 // Floating panels (except the full widget sheet) are associated with individual icons. If
2011 // we are starting a fresh bind, close all such panels as all the icons are about
2012 // to go away.
Pinyao Tinga74b63a2019-07-17 23:26:06 -07002013 AbstractFloatingView.closeOpenViews(this, true, TYPE_ALL & ~TYPE_REBIND_SAFE);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08002014
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002015 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08002016
Winson Chungd64d1762013-08-20 14:37:16 -07002017 // Clear the workspace because it's going to be rebound
Sunny Goyal2db53422019-03-01 16:06:12 -08002018 mDragController.cancelDrag();
2019
Adam Cohendf035382011-04-11 17:22:04 -07002020 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07002021 mWorkspace.removeAllWorkspaceScreens();
Sunny Goyalc11fac32018-02-27 19:20:15 -08002022 mAppWidgetHost.clearViews();
Winson Chungd64d1762013-08-20 14:37:16 -07002023
Winson Chung3d503fb2011-07-13 17:25:49 -07002024 if (mHotseat != null) {
Sunny Goyalc4d32012020-04-03 17:10:11 -07002025 mHotseat.resetLayout(getDeviceProfile().isVerticalBarLayout());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002026 }
Winson Chung5cc62c72019-10-16 11:32:41 -07002027 TraceHelper.INSTANCE.endSection(traceToken);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002028 }
2029
Adam Cohendcd297f2013-06-18 13:13:40 -07002030 @Override
Sunny Goyalefb7e842018-10-04 15:11:00 -07002031 public void bindScreens(IntArray orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002032 // Make sure the first screen is always at the start.
Jon Miranda7143ba62019-03-15 09:00:05 -07002033 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002034 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalefb7e842018-10-04 15:11:00 -07002035 orderedScreenIds.removeValue(Workspace.FIRST_SCREEN_ID);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002036 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Jon Miranda7143ba62019-03-15 09:00:05 -07002037 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002038 // If there are no screens, we need to have an empty screen
2039 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07002040 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002041 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07002042
Winsonc7d2e832016-07-28 12:24:55 -07002043 // After we have added all the screens, if the wallpaper was locked to the default state,
2044 // then notify to indicate that it can be released and a proper wallpaper offset can be
2045 // computed before the next layout
2046 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07002047 }
2048
Sunny Goyalefb7e842018-10-04 15:11:00 -07002049 private void bindAddScreens(IntArray orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002050 int count = orderedScreenIds.size();
2051 for (int i = 0; i < count; i++) {
Sunny Goyalefb7e842018-10-04 15:11:00 -07002052 int screenId = orderedScreenIds.get(i);
Jon Miranda7143ba62019-03-15 09:00:05 -07002053 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002054 // No need to bind the first screen, as its always bound.
2055 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
2056 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002057 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002058 }
2059
Sunny Goyalb23980c2017-08-17 07:45:25 -07002060 @Override
Tony Wickham6a71a5b2018-08-21 11:40:23 -07002061 public void preAddApps() {
2062 // If there's an undo snackbar, force it to complete to ensure empty screens are removed
2063 // before trying to add new items.
2064 mModelWriter.commitDelete();
2065 AbstractFloatingView snackbar = AbstractFloatingView.getOpenView(this, TYPE_SNACKBAR);
2066 if (snackbar != null) {
2067 snackbar.post(() -> snackbar.close(true));
2068 }
2069 }
2070
2071 @Override
Sunny Goyalefb7e842018-10-04 15:11:00 -07002072 public void bindAppsAdded(IntArray newScreens, ArrayList<ItemInfo> addNotAnimated,
Sunny Goyal29947f02017-12-18 13:49:44 -08002073 ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07002074 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08002075 if (newScreens != null) {
2076 bindAddScreens(newScreens);
2077 }
Winson Chungd64d1762013-08-20 14:37:16 -07002078
2079 // We add the items without animation on non-visible pages, and with
2080 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08002081 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002082 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002083 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002084 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002085 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07002086 }
Winson Chungc58497e2013-09-03 17:48:37 -07002087
Winson Chung87412982013-10-03 18:34:14 -07002088 // Remove the extra empty screen
Sunny Goyal6e379fc2020-03-19 16:08:14 -07002089 mWorkspace.removeExtraEmptyScreen(false);
Winson Chungd64d1762013-08-20 14:37:16 -07002090 }
2091
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002092 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002093 * Bind the items start-end from the list.
2094 *
2095 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002096 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07002097 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07002098 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002099 // Get the list of added items and intersect them with the set of items here
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002100 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07002101 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07002102 Workspace workspace = mWorkspace;
Sunny Goyalefb7e842018-10-04 15:11:00 -07002103 int newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07002104 int end = items.size();
2105 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002106 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002107
2108 // Short circuit if we are loading dock items for a configuration which has no dock
2109 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2110 mHotseat == null) {
2111 continue;
2112 }
2113
Sunny Goyal639e9062015-08-19 19:17:06 -07002114 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002115 switch (item.itemType) {
2116 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2117 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08002118 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Sunny Goyal95899162019-03-27 16:03:06 -07002119 WorkspaceItemInfo info = (WorkspaceItemInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07002120 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002121 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002122 }
2123 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Tracy Zhou4d7b2442019-12-09 13:42:57 -08002124 view = FolderIcon.inflateFolderAndIcon(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002125 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002126 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002127 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002128 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07002129 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2130 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07002131 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002132 if (view == null) {
2133 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002134 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08002135 break;
2136 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002137 default:
2138 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002139 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002140
Samuel Fufaca37b8a2019-08-19 17:04:36 -07002141 /*
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002142 * Remove colliding items.
2143 */
2144 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
2145 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
2146 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
2147 View v = cl.getChildAt(item.cellX, item.cellY);
2148 Object tag = v.getTag();
2149 String desc = "Collision while binding workspace item: " + item
2150 + ". Collides with " + tag;
Zak Cohen3eeb41d2020-02-14 14:15:13 -08002151 if (FeatureFlags.IS_STUDIO_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002152 throw (new RuntimeException(desc));
2153 } else {
2154 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002155 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002156 continue;
2157 }
2158 }
2159 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302160 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07002161 if (animateIcons) {
2162 // Animate all the applications up now
2163 view.setAlpha(0f);
2164 view.setScaleX(0f);
2165 view.setScaleY(0f);
2166 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08002167 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07002168 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002169 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07002170
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002171 // Animate to the correct page
2172 if (animateIcons && newItemsScreenId > -1) {
2173 AnimatorSet anim = new AnimatorSet();
2174 anim.playTogether(bounceAnims);
Jon Miranda64eb8ea2018-03-29 11:43:58 -07002175
Sunny Goyalefb7e842018-10-04 15:11:00 -07002176 int currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002177 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
2178 final Runnable startBounceAnimRunnable = anim::start;
2179
2180 if (newItemsScreenId != currentScreenId) {
2181 // We post the animation slightly delayed to prevent slowdowns
2182 // when we are loading right after we return to launcher.
2183 mWorkspace.postDelayed(new Runnable() {
2184 public void run() {
2185 if (mWorkspace != null) {
Andy Wickham65ced1b2020-03-07 02:14:19 +00002186 closeOpenViews(false);
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002187
2188 mWorkspace.snapToPage(newScreenIndex);
2189 mWorkspace.postDelayed(startBounceAnimRunnable,
2190 NEW_APPS_ANIMATION_DELAY);
Winson Chung94d67682013-09-25 16:29:40 -07002191 }
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002192 }
2193 }, NEW_APPS_PAGE_MOVE_DELAY);
2194 } else {
2195 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07002196 }
Winson Chung64359a52013-07-08 17:17:08 -07002197 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002198 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002199 }
2200
Joe Onorato9c1289c2009-08-17 11:03:03 -04002201 /**
2202 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002203 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07002204 public void bindAppWidget(LauncherAppWidgetInfo item) {
2205 View view = inflateAppWidget(item);
2206 if (view != null) {
2207 mWorkspace.addInScreen(view, item);
2208 mWorkspace.requestLayout();
2209 }
2210 }
2211
2212 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Samuel Fufaca37b8a2019-08-19 17:04:36 -07002213 if (item.hasOptionFlag(LauncherAppWidgetInfo.OPTION_SEARCH_WIDGET)) {
2214 item.providerName = QsbContainerView.getSearchComponentName(this);
2215 if (item.providerName == null) {
2216 getModelWriter().deleteItemFromDatabase(item);
2217 return null;
2218 }
2219 }
Sunny Goyal17c72fb2019-10-14 14:06:38 -07002220 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002221 if (mIsSafeModeEnabled) {
Sunny Goyal17c72fb2019-10-14 14:06:38 -07002222 view = new PendingAppWidgetHostView(this, item, mIconCache, true);
Sunny Goyald5462aa2016-11-22 16:52:39 +05302223 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002224 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002225 }
2226
Winson Chung5cc62c72019-10-16 11:32:41 -07002227 Object traceToken = TraceHelper.INSTANCE.beginSection("BIND_WIDGET_id=" + item.appWidgetId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002228
Sunny Goyal17c72fb2019-10-14 14:06:38 -07002229 try {
2230 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08002231
Sunny Goyal17c72fb2019-10-14 14:06:38 -07002232 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2233 // If the provider is not ready, bind as a pending widget.
2234 appWidgetInfo = null;
2235 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2236 // The widget id is not valid. Try to find the widget based on the provider info.
2237 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
2238 } else {
2239 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
Sunny Goyalff572272014-07-23 13:58:07 -07002240 }
Sunny Goyalff572272014-07-23 13:58:07 -07002241
Sunny Goyal17c72fb2019-10-14 14:06:38 -07002242 // If the provider is ready, but the width is not yet restored, try to restore it.
2243 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
2244 && (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
2245 if (appWidgetInfo == null) {
2246 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
2247 + " belongs to component " + item.providerName
2248 + ", as the provider is null");
2249 getModelWriter().deleteItemFromDatabase(item);
2250 return null;
2251 }
Sunny Goyalff572272014-07-23 13:58:07 -07002252
Sunny Goyal17c72fb2019-10-14 14:06:38 -07002253 // If we do not have a valid id, try to bind an id.
2254 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2255 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2256 // Id has not been allocated yet. Allocate a new id.
2257 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
2258 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyal86df1382016-08-10 15:03:22 -07002259
Sunny Goyal17c72fb2019-10-14 14:06:38 -07002260 // Also try to bind the widget. If the bind fails, the user will be shown
2261 // a click to setup UI, which will ask for the bind permission.
2262 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
2263 pendingInfo.spanX = item.spanX;
2264 pendingInfo.spanY = item.spanY;
2265 pendingInfo.minSpanX = item.minSpanX;
2266 pendingInfo.minSpanY = item.minSpanY;
2267 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this,
2268 pendingInfo);
2269
2270 boolean isDirectConfig =
2271 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2272 if (isDirectConfig && item.bindOptions != null) {
2273 Bundle newOptions = item.bindOptions.getExtras();
2274 if (options != null) {
2275 newOptions.putAll(options);
2276 }
2277 options = newOptions;
Sunny Goyal86df1382016-08-10 15:03:22 -07002278 }
Sunny Goyal17c72fb2019-10-14 14:06:38 -07002279 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2280 item.appWidgetId, appWidgetInfo, options);
2281
2282 // We tried to bind once. If we were not able to bind, we would need to
2283 // go through the permission dialog, which means we cannot skip the config
2284 // activity.
2285 item.bindOptions = null;
2286 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2287
2288 // Bind succeeded
2289 if (success) {
2290 // If the widget has a configure activity, it is still needs to set it
2291 // up, otherwise the widget is ready to go.
2292 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
2293 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2294 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
2295 }
2296
2297 getModelWriter().updateItemInDatabase(item);
Sunny Goyal86df1382016-08-10 15:03:22 -07002298 }
Sunny Goyal17c72fb2019-10-14 14:06:38 -07002299 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
2300 && (appWidgetInfo.configure == null)) {
2301 // The widget was marked as UI not ready, but there is no configure activity to
2302 // update the UI.
2303 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002304 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002305 }
Samuel Fufa9151c012020-02-24 17:06:28 -08002306 else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
2307 && appWidgetInfo.configure != null) {
2308 if (mAppWidgetManager.isAppWidgetRestored(item.appWidgetId)) {
2309 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
2310 getModelWriter().updateItemInDatabase(item);
2311 }
2312 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002313 }
2314
Sunny Goyal17c72fb2019-10-14 14:06:38 -07002315 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2316 // Verify that we own the widget
2317 if (appWidgetInfo == null) {
2318 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
2319 getModelWriter().deleteWidgetInfo(item, getAppWidgetHost());
2320 return null;
2321 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002322
Sunny Goyal17c72fb2019-10-14 14:06:38 -07002323 item.minSpanX = appWidgetInfo.minSpanX;
2324 item.minSpanY = appWidgetInfo.minSpanY;
2325 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Samuel Fufa9151c012020-02-24 17:06:28 -08002326 } else if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)
2327 && appWidgetInfo != null) {
2328 mAppWidgetHost.addPendingView(item.appWidgetId,
2329 new PendingAppWidgetHostView(this, item, mIconCache, false));
2330 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal17c72fb2019-10-14 14:06:38 -07002331 } else {
2332 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
2333 }
2334 prepareAppWidget(view, item);
2335 } finally {
Winson Chung5cc62c72019-10-16 11:32:41 -07002336 TraceHelper.INSTANCE.endSection(traceToken);
Sunny Goyal17c72fb2019-10-14 14:06:38 -07002337 }
2338
Sunny Goyalb23980c2017-08-17 07:45:25 -07002339 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002340 }
2341
Sunny Goyalff572272014-07-23 13:58:07 -07002342 /**
2343 * Restores a pending widget.
2344 *
2345 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002346 */
Sunny Goyald478c832016-04-01 12:04:16 -07002347 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002348 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2349 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2350 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002351 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002352 }
2353
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002354 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002355 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002356 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2357 info.pendingItemInfo = null;
2358 }
Sunny Goyalff572272014-07-23 13:58:07 -07002359
Sunny Goyalba47faa2017-10-04 16:50:57 -07002360 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002361 view.reInflate();
Sunny Goyalba47faa2017-10-04 16:50:57 -07002362 }
2363
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002364 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002365 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002366 }
2367
Adam Cohen1462de32012-07-24 22:34:36 -07002368 public void onPageBoundSynchronously(int page) {
Sunny Goyalf4aceab2017-12-06 16:16:31 -08002369 mSynchronouslyBoundPage = page;
Sunny Goyala7a5bf32020-01-05 15:35:29 +05302370 mWorkspace.setCurrentPage(page);
2371 mPageToBindSynchronously = PagedView.INVALID_PAGE;
Adam Cohen1462de32012-07-24 22:34:36 -07002372 }
2373
Sunny Goyal527c7d32015-08-28 15:19:36 -07002374 @Override
2375 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
Sunny Goyal6b6d0a32019-08-05 17:19:32 -07002376 clearPendingBinds();
Sunny Goyal527c7d32015-08-28 15:19:36 -07002377 mPendingExecutor = executor;
Sunny Goyal60180b02018-02-07 12:56:30 -08002378 if (!isInState(ALL_APPS)) {
vadimtd4c90e12019-06-04 13:59:43 -07002379 mAppsView.getAppsStore().enableDeferUpdates(AllAppsStore.DEFER_UPDATES_NEXT_DRAW);
2380 mPendingExecutor.execute(() -> mAppsView.getAppsStore().disableDeferUpdates(
2381 AllAppsStore.DEFER_UPDATES_NEXT_DRAW));
Sunny Goyal60180b02018-02-07 12:56:30 -08002382 }
2383
Sunny Goyal527c7d32015-08-28 15:19:36 -07002384 executor.attachTo(this);
2385 }
2386
2387 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2388 if (mPendingExecutor == executor) {
2389 mPendingExecutor = null;
2390 }
2391 }
2392
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002393 @Override
2394 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
Sunny Goyal6001ea22018-05-10 16:31:00 -07002395 AlphaProperty property = mDragLayer.getAlphaProperty(ALPHA_INDEX_LAUNCHER_LOAD);
2396 if (property.getValue() < 1) {
2397 ObjectAnimator anim = ObjectAnimator.ofFloat(property, MultiValueAlpha.VALUE, 1);
2398 if (executor != null) {
2399 anim.addListener(new AnimatorListenerAdapter() {
2400 @Override
2401 public void onAnimationEnd(Animator animation) {
2402 executor.onLoadAnimationCompleted();
2403 }
2404 });
2405 }
2406 anim.start();
Sunny Goyal29947f02017-12-18 13:49:44 -08002407 } else if (executor != null) {
2408 executor.onLoadAnimationCompleted();
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002409 }
2410 }
2411
Joe Onorato9c1289c2009-08-17 11:03:03 -04002412 /**
2413 * Callback saying that there aren't any more items to bind.
2414 *
2415 * Implementation of the method from LauncherModel.Callbacks.
2416 */
Tonyf80e8932018-12-21 11:58:04 -08002417 public void finishBindingItems(int pageBoundFirst) {
Winson Chung5cc62c72019-10-16 11:32:41 -07002418 Object traceToken = TraceHelper.INSTANCE.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002419 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002420
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002421 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002422
Sunny Goyal2100c782016-08-22 16:00:03 -07002423 if (mPendingActivityResult != null) {
2424 handleActivityResult(mPendingActivityResult.requestCode,
2425 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2426 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002427 }
2428
Sunny Goyala474a9b2017-05-04 16:47:11 -07002429 InstallShortcutReceiver.disableAndFlushInstallQueue(
2430 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002431
Tonyf80e8932018-12-21 11:58:04 -08002432 // When undoing the removal of the last item on a page, return to that page.
Tony Wickham03f27012019-04-25 14:22:54 -07002433 // Since we are just resetting the current page without user interaction,
2434 // override the previous page so we don't log the page switch.
2435 mWorkspace.setCurrentPage(pageBoundFirst, pageBoundFirst /* overridePrevPage */);
Sunny Goyala7a5bf32020-01-05 15:35:29 +05302436 mPageToBindSynchronously = PagedView.INVALID_PAGE;
Tony Wickham6a71a5b2018-08-21 11:40:23 -07002437
Sunny Goyalac6f69f2019-08-16 11:59:55 -07002438 // Cache one page worth of icons
2439 getViewCache().setCacheSize(R.layout.folder_application,
2440 mDeviceProfile.inv.numFolderColumns * mDeviceProfile.inv.numFolderRows);
2441 getViewCache().setCacheSize(R.layout.folder_page, 2);
2442
Winson Chung5cc62c72019-10-16 11:32:41 -07002443 TraceHelper.INSTANCE.endSection(traceToken);
Winson Chungf0c6ae02012-03-21 16:10:31 -07002444 }
2445
Winson Chunga2413752012-04-03 14:22:34 -07002446 private boolean canRunNewAppsAnimation() {
Sunny Goyal9b180102020-03-11 10:02:29 -07002447 if (mDragController.isDragging()) {
2448 return false;
2449 } else {
2450 return (System.currentTimeMillis() - mLastTouchUpTime)
2451 > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
2452 }
Winson Chunga2413752012-04-03 14:22:34 -07002453 }
2454
Winson Chungc9168342013-06-26 14:54:55 -07002455 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002456 ValueAnimator bounceAnim = new PropertyListBuilder().alpha(1).scale(1).build(v)
2457 .setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
Winson Chungc9168342013-06-26 14:54:55 -07002458 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002459 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002460 return bounceAnim;
2461 }
2462
Pinyao Tingc9074272019-11-11 11:51:44 -08002463 private void announceForAccessibility(@StringRes int stringResId) {
2464 getDragLayer().announceForAccessibility(getString(stringResId));
2465 }
2466
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002467 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002468 * Add the icons for all apps.
2469 *
2470 * Implementation of the method from LauncherModel.Callbacks.
2471 */
Sunny Goyal87dcde62019-07-17 20:35:56 -07002472 public void bindAllApplications(AppInfo[] apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002473 mAppsView.getAppsStore().setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002474 }
2475
2476 /**
Zak Cohen658c67a2018-10-19 14:21:05 -07002477 * Copies LauncherModel's map of activities to shortcut counts to Launcher's. This is necessary
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002478 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2479 */
2480 @Override
Zak Cohen658c67a2018-10-19 14:21:05 -07002481 public void bindDeepShortcutMap(HashMap<ComponentKey, Integer> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002482 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002483 }
2484
Sunny Goyal4390ace2014-10-13 11:33:11 -07002485 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002486 public void bindPromiseAppProgressUpdated(PromiseAppInfo app) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002487 mAppsView.getAppsStore().updatePromiseAppProgress(app);
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002488 }
2489
2490 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002491 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002492 mWorkspace.widgetsRestored(widgets);
2493 }
2494
Joe Onorato9c1289c2009-08-17 11:03:03 -04002495 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002496 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002497 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002498 *
2499 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002500 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002501 @Override
Sunny Goyal95899162019-03-27 16:03:06 -07002502 public void bindWorkspaceItemsChanged(ArrayList<WorkspaceItemInfo> updated) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002503 if (!updated.isEmpty()) {
2504 mWorkspace.updateShortcuts(updated);
2505 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002506 }
2507
2508 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002509 * Update the state of a package, typically related to install state.
2510 *
2511 * Implementation of the method from LauncherModel.Callbacks.
2512 */
Sunny Goyale755d462014-07-22 13:48:29 -07002513 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002514 public void bindRestoreItemsChange(HashSet<ItemInfo> updates) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07002515 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07002516 }
2517
2518 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002519 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07002520 * in addition to specific applications to remove, the reason being that
2521 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002522 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07002523 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002524 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07002525 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002526 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002527 mWorkspace.removeItemsByMatcher(matcher);
2528 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002529 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002530
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002531 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002532 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
2533 mPopupDataProvider.setAllWidgets(allWidgets);
Tony Wickham26b17462017-03-20 17:12:24 -07002534 }
2535
Tony Wickham86222d22017-03-29 15:30:43 -07002536 /**
2537 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
2538 * refreshes the widgets and shortcuts associated with the given package/user
2539 */
2540 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07002541 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002542 }
2543
Winson Chung80baf5a2010-08-09 16:03:15 -07002544 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002545 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08002546 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002547 @Override
2548 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
2549 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002550
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002551 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
2552 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07002553 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002554 writer.println(prefix + " Homescreen " + i);
2555
2556 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
2557 for (int j = 0; j < layout.getChildCount(); j++) {
2558 Object tag = layout.getChildAt(j).getTag();
2559 if (tag != null) {
2560 writer.println(prefix + " " + tag.toString());
2561 }
2562 }
2563 }
2564
2565 writer.println(prefix + " Hotseat");
Sunny Goyal876e4622018-11-02 13:50:40 -07002566 ViewGroup layout = mHotseat.getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07002567 for (int j = 0; j < layout.getChildCount(); j++) {
2568 Object tag = layout.getChildAt(j).getTag();
2569 if (tag != null) {
2570 writer.println(prefix + " " + tag.toString());
2571 }
Adam Cohen4caf2982013-08-20 18:54:31 -07002572 }
Sunny Goyala1365452015-10-01 15:46:24 -07002573 }
2574
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002575 writer.println(prefix + "Misc:");
Winson Chungef528762019-09-06 12:05:52 -07002576 dumpMisc(prefix + "\t", writer);
2577 writer.println(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
2578 writer.println(prefix + "\tmPendingRequestArgs=" + mPendingRequestArgs
2579 + " mPendingActivityResult=" + mPendingActivityResult);
2580 writer.println(prefix + "\tmRotationHelper: " + mRotationHelper);
2581 writer.println(prefix + "\tmAppWidgetHost.isListening: " + mAppWidgetHost.isListening());
2582
Tony Wickham646e4482020-04-06 17:25:53 -07002583 // Extra logging for general debugging
Hyunyoung Songa310a802019-04-25 13:57:16 -07002584 mDragLayer.dump(prefix, writer);
2585 mStateManager.dump(prefix, writer);
Tony Wickham646e4482020-04-06 17:25:53 -07002586 mPopupDataProvider.dump(prefix, writer);
Sunny Goyal2bba1902018-02-27 12:20:50 -08002587
2588 try {
2589 FileLog.flushAll(writer);
2590 } catch (Exception e) {
2591 // Ignore
2592 }
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002593
2594 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002595
Adam Cohen9211d422014-10-07 18:14:53 -07002596 if (mLauncherCallbacks != null) {
2597 mLauncherCallbacks.dump(prefix, fd, writer, args);
2598 }
Sunny Goyalb1d7de22019-09-06 10:36:54 -07002599 mOverlayManager.dump(prefix, writer);
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002600 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002601
Sunny Goyal66b24572016-09-21 15:57:55 -07002602 @Override
Sunny Goyal66b24572016-09-21 15:57:55 -07002603 public void onProvideKeyboardShortcuts(
2604 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
2605
2606 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002607 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002608 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
2609 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
Vadim Tryshev74c261c2018-06-12 13:44:43 -07002610 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.widget_button_text),
2611 KeyEvent.KEYCODE_W, KeyEvent.META_CTRL_ON));
Sunny Goyal66b24572016-09-21 15:57:55 -07002612 }
Vadim Tryshev3455f8d2018-05-11 20:02:20 -07002613 final View currentFocus = getCurrentFocus();
2614 if (currentFocus != null) {
2615 if (new CustomActionsPopup(this, currentFocus).canShow()) {
2616 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
2617 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
2618 }
2619 if (currentFocus.getTag() instanceof ItemInfo
Pinyao Ting49a3e692019-07-26 12:28:38 -07002620 && ShortcutUtil.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
Vadim Tryshev3455f8d2018-05-11 20:02:20 -07002621 shortcutInfos.add(new KeyboardShortcutInfo(
2622 getString(R.string.shortcuts_menu_with_notifications_description),
2623 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
2624 }
Sunny Goyal66b24572016-09-21 15:57:55 -07002625 }
2626 if (!shortcutInfos.isEmpty()) {
2627 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
2628 }
2629
2630 super.onProvideKeyboardShortcuts(data, menu, deviceId);
2631 }
2632
2633 @Override
2634 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
2635 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
2636 switch (keyCode) {
2637 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002638 if (isInState(NORMAL)) {
2639 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07002640 return true;
2641 }
2642 break;
2643 case KeyEvent.KEYCODE_S: {
2644 View focusedView = getCurrentFocus();
2645 if (focusedView instanceof BubbleTextView
2646 && focusedView.getTag() instanceof ItemInfo
2647 && mAccessibilityDelegate.performAction(focusedView,
Samuel Fufaca37b8a2019-08-19 17:04:36 -07002648 (ItemInfo) focusedView.getTag(),
2649 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal00ac9202017-11-09 15:24:06 -08002650 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07002651 return true;
2652 }
2653 break;
2654 }
2655 case KeyEvent.KEYCODE_O:
2656 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
2657 return true;
2658 }
2659 break;
Vadim Tryshev74c261c2018-06-12 13:44:43 -07002660 case KeyEvent.KEYCODE_W:
2661 if (isInState(NORMAL)) {
2662 OptionsPopupView.openWidgets(this);
2663 return true;
2664 }
2665 break;
Sunny Goyal66b24572016-09-21 15:57:55 -07002666 }
2667 }
2668 return super.onKeyShortcut(keyCode, event);
2669 }
2670
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002671 @Override
2672 public boolean onKeyUp(int keyCode, KeyEvent event) {
2673 if (keyCode == KeyEvent.KEYCODE_MENU) {
2674 // KEYCODE_MENU is sent by some tests, for example
2675 // LauncherJankTests#testWidgetsContainerFling. Don't just remove its handling.
2676 if (!mDragController.isDragging() && !mWorkspace.isSwitchingState() &&
2677 isInState(NORMAL)) {
2678 // Close any open floating views.
Andy Wickham65ced1b2020-03-07 02:14:19 +00002679 closeOpenViews();
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002680
2681 // Setting the touch point to (-1, -1) will show the options popup in the center of
2682 // the screen.
vadimtbc5d7e62019-12-09 13:53:05 -08002683 if (Utilities.IS_RUNNING_IN_TEST_HARNESS) {
2684 Log.d(TestProtocol.PERMANENT_DIAG_TAG, "Opening options popup on key up");
2685 }
Sunny Goyal2fd7a8b2018-03-30 17:10:13 -07002686 OptionsPopupView.showDefaultOptions(this, -1, -1);
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002687 }
2688 return true;
2689 }
2690 return super.onKeyUp(keyCode, event);
2691 }
2692
Sunny Goyal210e1742019-10-17 12:05:38 -07002693 protected StateHandler[] createStateHandlers() {
Sunny Goyal284a6cf2020-03-24 12:41:56 -07002694 return new StateHandler[] { getAllAppsController(), getWorkspace() };
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002695 }
2696
Sunny Goyal210e1742019-10-17 12:05:38 -07002697 public TouchController[] createTouchControllers() {
2698 return new TouchController[] {getDragController(), new AllAppsSwipeController(this)};
2699 }
2700
Sunny Goyal210e1742019-10-17 12:05:38 -07002701 public void useFadeOutAnimationForLauncherStart(CancellationSignal signal) { }
2702
2703 public void onDragLayerHierarchyChanged() { }
2704
Vinit Nayakf9b585b2019-07-10 14:25:32 -07002705 @Override
2706 public void returnToHomescreen() {
2707 super.returnToHomescreen();
2708 getStateManager().goToState(LauncherState.NORMAL);
2709 }
2710
Andy Wickham65ced1b2020-03-07 02:14:19 +00002711 private void closeOpenViews() {
2712 closeOpenViews(true);
2713 }
2714
2715 protected void closeOpenViews(boolean animate) {
2716 AbstractFloatingView.closeAllOpenViews(this, animate);
2717 }
2718
Sunny Goyal3a1eadd2019-10-23 17:44:27 -07002719 public Stream<SystemShortcut.Factory> getSupportedShortcuts() {
Samuel Fufa5cf3e862020-02-03 20:22:54 -08002720 return Stream.of(APP_INFO, WIDGETS, INSTALL);
Sunny Goyal3a1eadd2019-10-23 17:44:27 -07002721 }
2722
Sunny Goyalf2393f12020-04-01 20:13:12 -07002723
2724 /**
2725 * @see LauncherState#getOverviewScaleAndOffset(Launcher)
2726 */
2727 public float[] getNormalOverviewScaleAndOffset() {
2728 return new float[] {NO_SCALE, NO_OFFSET};
2729 }
2730
Sunny Goyal210e1742019-10-17 12:05:38 -07002731 public static Launcher getLauncher(Context context) {
Winson Chung8687bc22020-02-27 23:34:24 -08002732 return fromContext(context);
Sunny Goyal210e1742019-10-17 12:05:38 -07002733 }
2734
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002735 /**
Sunny Goyalab770a12018-11-14 15:17:26 -08002736 * Just a wrapper around the type cast to allow easier tracking of calls.
2737 */
2738 public static <T extends Launcher> T cast(ActivityContext activityContext) {
2739 return (T) activityContext;
2740 }
2741
Sunny Goyalf2393f12020-04-01 20:13:12 -07002742
Sunny Goyalab770a12018-11-14 15:17:26 -08002743 /**
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002744 * Callback for listening for onResume
2745 */
2746 public interface OnResumeCallback {
2747
2748 void onLauncherResume();
2749 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002750}