blob: 03fdc971dd3453beb8f73a45fb081decc92f156a [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
Jon Mirandaaeb4dd02018-07-11 14:16:23 -070019import static android.content.pm.ActivityInfo.CONFIG_LOCALE;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080020import static android.content.pm.ActivityInfo.CONFIG_ORIENTATION;
21import static android.content.pm.ActivityInfo.CONFIG_SCREEN_SIZE;
vadimt00d42552018-12-11 17:59:36 -080022
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;
Govinda Wassermanc06e1512019-04-09 09:56:53 -040027import static com.android.launcher3.LauncherState.OVERVIEW;
28import static com.android.launcher3.LauncherState.OVERVIEW_PEEK;
Sunny Goyal6001ea22018-05-10 16:31:00 -070029import static com.android.launcher3.dragndrop.DragLayer.ALPHA_INDEX_LAUNCHER_LOAD;
Sunny Goyalaeb16432017-10-16 11:46:41 -070030import static com.android.launcher3.logging.LoggerUtils.newContainerTarget;
Hyunyoung Song46d07f72018-05-22 15:41:25 -070031import static com.android.launcher3.logging.LoggerUtils.newTarget;
Sunny Goyalbe8c3402019-05-22 15:17:52 -070032import static com.android.launcher3.states.RotationHelper.REQUEST_NONE;
vadimt00d42552018-12-11 17:59:36 -080033import static com.android.launcher3.util.RaceConditionTracker.ENTER;
34import static com.android.launcher3.util.RaceConditionTracker.EXIT;
Sunny Goyal326403e2017-10-02 12:45:10 -070035
Gilles Debunnedd6c9922010-10-25 11:23:41 -070036import android.animation.Animator;
Sunny Goyal6001ea22018-05-10 16:31:00 -070037import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070038import android.animation.AnimatorSet;
Sunny Goyal6001ea22018-05-10 16:31:00 -070039import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070040import android.animation.ValueAnimator;
Adam Cohen0b395352014-06-09 22:54:36 +000041import android.annotation.TargetApi;
Tony Wickham005df0b2018-02-13 11:28:12 -080042import android.app.ActivityOptions;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080043import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070044import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080046import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080047import android.content.ComponentCallbacks2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070050import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070051import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070052import android.content.SharedPreferences;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053import android.content.pm.PackageManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080054import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070055import android.database.sqlite.SQLiteDatabase;
Sunny Goyal59d086c2018-05-07 17:31:40 -070056import android.graphics.Point;
Sunny Goyalae6e3182019-04-30 12:04:37 -070057import android.graphics.Rect;
Adam Cohen0f668f32014-09-08 19:54:17 +020058import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059import android.os.Bundle;
Hyunyoung Songbd6fba92018-05-16 15:54:31 -070060import android.os.Handler;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070061import android.os.Parcelable;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080062import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070063import android.os.StrictMode;
Michael Jurkaa33411c2012-06-14 16:18:21 -070064import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070066import android.util.Log;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070067import android.util.SparseArray;
Sunny Goyal59d086c2018-05-07 17:31:40 -070068import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070069import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070070import android.view.KeyboardShortcutGroup;
71import android.view.KeyboardShortcutInfo;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080072import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -070073import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070074import android.view.View;
Adam Cohen96d30a12013-07-16 18:13:21 -070075import android.view.ViewGroup;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080076import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070077import android.view.animation.OvershootInterpolator;
Adam Cohen96d30a12013-07-16 18:13:21 -070078import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070079
Sunny Goyal651077b2014-06-30 14:15:31 -070080import com.android.launcher3.DropTarget.DragObject;
Sunny Goyalae502842016-06-17 08:43:56 -070081import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070082import com.android.launcher3.allapps.AllAppsContainerView;
vadimtd4c90e12019-06-04 13:59:43 -070083import com.android.launcher3.allapps.AllAppsStore;
Hyunyoung Song645764e2016-06-06 14:19:02 -070084import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal7ede6112017-12-05 15:11:21 -080085import com.android.launcher3.allapps.DiscoveryBounce;
Sunny Goyalf0b6db72018-08-13 16:10:14 -070086import com.android.launcher3.anim.PropertyListBuilder;
Sunny Goyalffe83f12014-08-14 17:39:34 -070087import com.android.launcher3.compat.AppWidgetManagerCompat;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -070088import com.android.launcher3.compat.LauncherAppsCompatVO;
Tony Wickhame0c33232016-02-08 11:37:04 -080089import com.android.launcher3.config.FeatureFlags;
vadimt00d42552018-12-11 17:59:36 -080090import com.android.launcher3.dot.DotInfo;
Vadim Tryshevfedca432015-08-19 17:55:02 -070091import com.android.launcher3.dragndrop.DragController;
92import com.android.launcher3.dragndrop.DragLayer;
93import com.android.launcher3.dragndrop.DragView;
Jon Mirandaaeb4dd02018-07-11 14:16:23 -070094import com.android.launcher3.folder.Folder;
Sunny Goyal26119432016-02-18 22:09:23 +000095import com.android.launcher3.folder.FolderIcon;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070096import com.android.launcher3.folder.FolderIconPreviewVerifier;
Sunny Goyalae6e3182019-04-30 12:04:37 -070097import com.android.launcher3.graphics.RotationMode;
Sunny Goyalf840f102018-09-21 14:41:05 -070098import com.android.launcher3.icons.IconCache;
Sunny Goyal66b24572016-09-21 15:57:55 -070099import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700100import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700101import com.android.launcher3.logging.FileLog;
Hyunyoung Songfc007472018-10-25 14:09:50 -0700102import com.android.launcher3.logging.StatsLogUtils;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700103import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700104import com.android.launcher3.logging.UserEventDispatcher.UserEventDelegate;
Sunny Goyal369212a2019-03-26 15:03:57 -0700105import com.android.launcher3.model.AppLaunchTracker;
Sunny Goyala535ae42017-02-27 10:07:13 -0800106import com.android.launcher3.model.ModelWriter;
Sunny Goyala535ae42017-02-27 10:07:13 -0800107import com.android.launcher3.notification.NotificationListener;
Tony Wickham540913e2017-01-23 11:47:51 -0800108import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -0800109import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700110import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyalf8088ee2017-11-10 14:52:00 -0800111import com.android.launcher3.states.InternalStateHandler;
Sunny Goyal623eddd2018-03-02 12:24:41 -0800112import com.android.launcher3.states.RotationHelper;
Sunny Goyalab3963d2019-05-23 00:50:08 -0700113import com.android.launcher3.testing.TestProtocol;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800114import com.android.launcher3.touch.ItemClickHandler;
Sunny Goyala5ace712017-11-15 17:12:51 -0800115import com.android.launcher3.uioverrides.UiFactory;
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700116import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530117import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
118import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
Sunny Goyalaeb16432017-10-16 11:46:41 -0700119import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyal2100c782016-08-22 16:00:03 -0700120import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700121import com.android.launcher3.util.ComponentKey;
Sunny Goyalefb7e842018-10-04 15:11:00 -0700122import com.android.launcher3.util.IntArray;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700123import com.android.launcher3.util.ItemInfoMatcher;
Sunny Goyal6001ea22018-05-10 16:31:00 -0700124import com.android.launcher3.util.MultiValueAlpha;
125import com.android.launcher3.util.MultiValueAlpha.AlphaProperty;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700126import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800127import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700128import com.android.launcher3.util.PendingRequestArgs;
vadimt00d42552018-12-11 17:59:36 -0800129import com.android.launcher3.util.RaceConditionTracker;
Sunny Goyal8392c822017-06-20 10:03:56 -0700130import com.android.launcher3.util.SystemUiController;
Mario Bertschlera6936942017-05-31 14:48:19 -0700131import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700132import com.android.launcher3.util.Thunk;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700133import com.android.launcher3.util.TraceHelper;
Sunny Goyal326403e2017-10-02 12:45:10 -0700134import com.android.launcher3.util.UiThreadHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700135import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyalab770a12018-11-14 15:17:26 -0800136import com.android.launcher3.views.ActivityContext;
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800137import com.android.launcher3.views.OptionsPopupView;
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400138import com.android.launcher3.views.ScrimView;
Sunny Goyal29947f02017-12-18 13:49:44 -0800139import com.android.launcher3.widget.LauncherAppWidgetHostView;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800140import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700141import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal29947f02017-12-18 13:49:44 -0800142import com.android.launcher3.widget.PendingAppWidgetHostView;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800143import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700144import com.android.launcher3.widget.WidgetHostViewLoader;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700145import com.android.launcher3.widget.WidgetListRowEntry;
146import com.android.launcher3.widget.WidgetsFullSheet;
Sunny Goyal952e63d2017-08-16 04:59:08 -0700147import com.android.launcher3.widget.custom.CustomWidgetParser;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700148
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700149import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700150import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700151import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700152import java.util.Collection;
Zak Cohen658c67a2018-10-19 14:21:05 -0700153import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700154import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700155import java.util.List;
Sunny Goyalaae6fbb2019-01-31 16:05:58 -0800156import java.util.function.Predicate;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700157
Sunny Goyalbe8c3402019-05-22 15:17:52 -0700158import androidx.annotation.Nullable;
159
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160/**
161 * Default launcher application.
162 */
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700163public class Launcher extends BaseDraggingActivity implements LauncherExterns,
Hyunyoung Song31971a32019-01-27 12:31:12 -0800164 LauncherModel.Callbacks, LauncherProviderChangeListener, UserEventDelegate,
165 InvariantDeviceProfile.OnIDPChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700166 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700167 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168
Winson Chunga2413752012-04-03 14:22:34 -0700169 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700170
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700172 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700173
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700174 private static final int REQUEST_PICK_APPWIDGET = 9;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175
Michael Jurka8b805b12012-04-18 14:23:14 -0700176 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800177 public static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
178 public static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700179
Jon Mirandac476d6e2017-05-04 16:30:01 -0700180 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700181
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700182 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
183
Mathew Inwood876a8462013-06-14 14:12:41 +0100184 /**
185 * IntentStarter uses request codes starting with this. This must be greater than all activity
186 * request codes used internally.
187 */
188 protected static final int REQUEST_LAST = 100;
189
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 // Type: int
191 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700192 // Type: int
193 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700194 // Type: PendingRequestArgs
195 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
Sunny Goyaldedda052019-05-14 15:23:48 -0700196 // Type: int
197 private static final String RUNTIME_STATE_PENDING_REQUEST_CODE = "launcher.request_code";
Sunny Goyal2100c782016-08-22 16:00:03 -0700198 // Type: ActivityResultInfo
199 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700200 // Type: SparseArray<Parcelable>
201 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
vadimt00d42552018-12-11 17:59:36 -0800202 public static final String ON_CREATE_EVT = "Launcher.onCreate";
vadimtcb863752018-12-19 17:44:57 -0800203 private static final String ON_START_EVT = "Launcher.onStart";
204 private static final String ON_RESUME_EVT = "Launcher.onResume";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700206 private LauncherStateManager mStateManager;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700207
Adam Cohenad4e15c2013-10-17 16:21:35 -0700208 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800209
Winson Chunga2413752012-04-03 14:22:34 -0700210 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700211 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
212 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
213 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700214
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400215 private static final int APPS_VIEW_ALPHA_CHANNEL_INDEX = 1;
216 private static final int SCRIM_VIEW_ALPHA_CHANNEL_INDEX = 0;
217
Jon Miranda54441f52018-01-24 15:38:25 -0800218 private LauncherAppTransitionManager mAppTransitionManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800219 private Configuration mOldConfig;
Jon Miranda54441f52018-01-24 15:38:25 -0800220
Adam Cohen091440a2015-03-18 14:16:05 -0700221 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700222 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700223 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800224 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700225
Sunny Goyalffe83f12014-08-14 17:39:34 -0700226 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700227 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700228
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700229 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700230
Sunny Goyal316490e2015-06-02 09:38:28 -0700231 @Thunk Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700232
Sunny Goyal47328fd2016-05-25 18:56:41 -0700233 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700234
235 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700236 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700237 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700238
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400239 // Scrim view for the all apps and overview state.
240 @Thunk ScrimView mScrimView;
241
Winson Chung8ae41982017-11-10 11:42:13 -0800242 // UI and state for the overview panel
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800243 private View mOverviewPanel;
Winson Chung8ae41982017-11-10 11:42:13 -0800244
Adam Cohen091440a2015-03-18 14:16:05 -0700245 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400246
Winson Chung0b70cd42019-06-17 22:34:33 -0700247 private ArrayList<OnResumeCallback> mOnResumeCallbacks = new ArrayList<>();
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700248
Sunny Goyal527c7d32015-08-28 15:19:36 -0700249 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700250
Joe Onorato9c1289c2009-08-17 11:03:03 -0400251 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800252 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800253 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700254 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400255
Tony Wickham010d2552017-01-20 08:15:28 -0800256 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700257
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800258 private int mSynchronouslyBoundPage = PagedView.INVALID_PAGE;
Adam Cohen1462de32012-07-24 22:34:36 -0700259
Winson Chung46353de2012-02-16 14:05:10 -0800260 // We only want to get the SharedPreferences once since it does an FS stat each time we get
261 // it from the context.
262 private SharedPreferences mSharedPrefs;
263
Sunny Goyal2100c782016-08-22 16:00:03 -0700264 // Activity result which needs to be processed after workspace has loaded.
265 private ActivityResultInfo mPendingActivityResult;
266 /**
267 * Holds extra information required to handle a result from an external call, like
268 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
269 */
270 private PendingRequestArgs mPendingRequestArgs;
Sunny Goyaldedda052019-05-14 15:23:48 -0700271 // Request id for any pending activity result
272 private int mPendingActivityRequestCode = -1;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800273
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700274 public ViewGroupFocusHelper mFocusHandler;
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700275
Sunny Goyal623eddd2018-03-02 12:24:41 -0800276 private RotationHelper mRotationHelper;
Sunny Goyal5743f862019-03-28 15:35:32 -0700277 private Runnable mCancelTouchController;
Sunny Goyal7779d622015-06-11 16:18:39 -0700278
Sunny Goyal2db53422019-03-01 16:06:12 -0800279 final Handler mHandler = new Handler();
Sunny Goyala002c6c2019-02-12 16:20:10 -0800280 private final Runnable mHandleDeferredResume = this::handleDeferredResume;
Sunny Goyale9c6f4c2019-05-30 11:35:25 -0700281 private boolean mDeferredResumePending;
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700282
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400283 private float mCurrentAssistantVisibility = 0f;
284
Sunny Goyalae6e3182019-04-30 12:04:37 -0700285 private DeviceProfile mStableDeviceProfile;
286 private RotationMode mRotationMode = RotationMode.NORMAL;
287
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800288 @Override
289 protected void onCreate(Bundle savedInstanceState) {
vadimt00d42552018-12-11 17:59:36 -0800290 RaceConditionTracker.onEvent(ON_CREATE_EVT, ENTER);
Winson Chunga2413752012-04-03 14:22:34 -0700291 if (DEBUG_STRICT_MODE) {
292 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
293 .detectDiskReads()
294 .detectDiskWrites()
295 .detectNetwork() // or .detectAll() for all detectable problems
296 .penaltyLog()
297 .build());
298 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
299 .detectLeakedSqlLiteObjects()
300 .detectLeakedClosableObjects()
301 .penaltyLog()
302 .penaltyDeath()
303 .build());
304 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700305 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700306
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800307 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700308 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400309
Sunny Goyal87f784c2017-01-11 10:48:34 -0800310 LauncherAppState app = LauncherAppState.getInstance(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800311 mOldConfig = new Configuration(getResources().getConfiguration());
Sunny Goyal0e7724c2018-02-22 13:22:21 -0800312 mModel = app.setLauncher(this);
Sunny Goyal41d51a02019-05-14 09:44:34 -0700313 mRotationHelper = new RotationHelper(this);
Hyunyoung Song31971a32019-01-27 12:31:12 -0800314 InvariantDeviceProfile idp = app.getInvariantDeviceProfile();
315 initDeviceProfile(idp);
316 idp.addOnChangeListener(this);
Sunny Goyalf7258242015-10-19 16:59:07 -0700317 mSharedPrefs = Utilities.getPrefs(this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800318 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700319 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700320
Joe Onorato41a12d22009-10-31 18:30:00 -0400321 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700322 mAllAppsController = new AllAppsTransitionController(this);
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800323 mStateManager = new LauncherStateManager(this);
Tracy Zhou8b23c6f2018-04-30 17:13:27 -0700324 UiFactory.onCreate(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700325
Sunny Goyalffe83f12014-08-14 17:39:34 -0700326 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700327
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700328 mAppWidgetHost = new LauncherAppWidgetHost(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700329 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700330
Sunny Goyal60820d72017-05-09 12:40:11 -0700331 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700332
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800333 setupViews();
Tony Wickham010d2552017-01-20 08:15:28 -0800334 mPopupDataProvider = new PopupDataProvider(this);
335
Sunny Goyal90548432018-04-04 17:17:00 -0700336 mAppTransitionManager = LauncherAppTransitionManager.newInstance(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800337
Sunny Goyald3864fa2017-11-14 15:06:36 -0800338 boolean internalStateHandled = InternalStateHandler.handleCreate(this, getIntent());
339 if (internalStateHandled) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800340 if (savedInstanceState != null) {
341 // InternalStateHandler has already set the appropriate state.
342 // We dont need to do anything.
343 savedInstanceState.remove(RUNTIME_STATE);
344 }
345 }
346 restoreState(savedInstanceState);
Sunny Goyalbbece862019-03-01 13:13:52 -0800347 mStateManager.reapplyState();
Winson Chungb63b44c2017-11-10 17:54:44 -0800348
Sunny Goyal2100c782016-08-22 16:00:03 -0700349 // We only load the page synchronously if the user rotates (or triggers a
350 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700351 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
352 if (savedInstanceState != null) {
353 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
354 }
Sunny Goyald3864fa2017-11-14 15:06:36 -0800355
Sunny Goyalfe770c92016-09-27 14:38:58 -0700356 if (!mModel.startLoader(currentScreen)) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800357 if (!internalStateHandled) {
358 // If we are not binding synchronously, show a fade in animation when
359 // the first page bind completes.
Sunny Goyal6001ea22018-05-10 16:31:00 -0700360 mDragLayer.getAlphaProperty(ALPHA_INDEX_LAUNCHER_LOAD).setValue(0);
Sunny Goyald3864fa2017-11-14 15:06:36 -0800361 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700362 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700363 // Pages bound synchronously.
364 mWorkspace.setCurrentPage(currentScreen);
365
Sunny Goyal2100c782016-08-22 16:00:03 -0700366 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800367 }
368
369 // For handling default keys
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800370 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800371
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800372 setContentView(mLauncherView);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800373 getRootView().dispatchInsets();
Mario Bertschlera6936942017-05-31 14:48:19 -0700374
Jon Miranda7fda2852017-07-19 16:07:01 -0700375 // Listen for broadcasts
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700376 registerReceiver(mScreenOffReceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));
Sunny Goyala616d2b2017-06-12 13:54:01 -0700377
Sunny Goyal8392c822017-06-20 10:03:56 -0700378 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
379 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700380
381 if (mLauncherCallbacks != null) {
382 mLauncherCallbacks.onCreate(savedInstanceState);
383 }
Sunny Goyal623eddd2018-03-02 12:24:41 -0800384 mRotationHelper.initialize();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700385
386 TraceHelper.endSection("Launcher-onCreate");
vadimt00d42552018-12-11 17:59:36 -0800387 RaceConditionTracker.onEvent(ON_CREATE_EVT, EXIT);
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400388 mStateManager.addStateListener(new LauncherStateManager.StateListener() {
389 @Override
390 public void onStateTransitionStart(LauncherState toState) {}
391
392 @Override
393 public void onStateTransitionComplete(LauncherState finalState) {
394 float alpha = 1f - mCurrentAssistantVisibility;
395 if (finalState == NORMAL) {
396 mAppsView.getAlphaProperty(APPS_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
397 } else if (finalState == OVERVIEW || finalState == OVERVIEW_PEEK) {
398 mAppsView.getAlphaProperty(APPS_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
399 mScrimView.getAlphaProperty(SCRIM_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
400 } else {
401 mAppsView.getAlphaProperty(APPS_VIEW_ALPHA_CHANNEL_INDEX).setValue(1f);
402 mScrimView.getAlphaProperty(SCRIM_VIEW_ALPHA_CHANNEL_INDEX).setValue(1f);
403 }
404 }
405 });
Adam Cohen9211d422014-10-07 18:14:53 -0700406 }
407
Sunny Goyal7779d622015-06-11 16:18:39 -0700408 @Override
Winson Chung8eb49e02018-07-16 13:33:31 -0700409 public void onEnterAnimationComplete() {
410 super.onEnterAnimationComplete();
411 UiFactory.onEnterAnimationComplete(this);
arangelovad783052018-08-17 14:46:45 +0100412 mAllAppsController.highlightWorkTabIfNecessary();
Sunny Goyalbe8c3402019-05-22 15:17:52 -0700413 mRotationHelper.setCurrentTransitionRequest(REQUEST_NONE);
Winson Chung8eb49e02018-07-16 13:33:31 -0700414 }
415
416 @Override
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800417 public void onConfigurationChanged(Configuration newConfig) {
418 int diff = newConfig.diff(mOldConfig);
Jon Mirandaaeb4dd02018-07-11 14:16:23 -0700419
420 if ((diff & CONFIG_LOCALE) != 0) {
421 Folder.setLocaleDependentFields(getResources(), true /* force */);
422 }
423
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800424 if ((diff & (CONFIG_ORIENTATION | CONFIG_SCREEN_SIZE)) != 0) {
Sunny Goyal371ea052019-03-06 15:38:32 -0800425 onIdpChanged(mDeviceProfile.inv);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800426 }
427
428 mOldConfig.setTo(newConfig);
Tracy Zhoua706f002018-03-28 13:55:19 -0700429 UiFactory.onLauncherStateOrResumeChanged(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800430 super.onConfigurationChanged(newConfig);
431 }
432
Sunny Goyal3e195d72019-06-19 14:24:38 -0700433 public void reload() {
434 onIdpChanged(mDeviceProfile.inv);
435 }
436
Sunny Goyal41d51a02019-05-14 09:44:34 -0700437 private boolean supportsFakeLandscapeUI() {
438 return FeatureFlags.FAKE_LANDSCAPE_UI.get() && !mRotationHelper.homeScreenCanRotate();
439 }
440
Sunny Goyal605bcf32018-03-02 15:16:12 -0800441 @Override
Sunny Goyal41d51a02019-05-14 09:44:34 -0700442 public void reapplyUi() {
443 if (supportsFakeLandscapeUI()) {
Sunny Goyal7a39b1b2019-05-02 13:39:16 -0700444 mRotationMode = mStableDeviceProfile == null
445 ? RotationMode.NORMAL : UiFactory.getRotationMode(mDeviceProfile);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700446 }
Sunny Goyal59d086c2018-05-07 17:31:40 -0700447 getRootView().dispatchInsets();
448 getStateManager().reapplyState(true /* cancelCurrentAnimation */);
449 }
450
451 @Override
Sunny Goyal605bcf32018-03-02 15:16:12 -0800452 public void rebindModel() {
453 int currentPage = mWorkspace.getNextPage();
454 if (mModel.startLoader(currentPage)) {
455 mWorkspace.setCurrentPage(currentPage);
456 setWorkspaceLoading(true);
457 }
458 }
459
Hyunyoung Song31971a32019-01-27 12:31:12 -0800460 @Override
461 public void onIdpChanged(int changeFlags, InvariantDeviceProfile idp) {
Sunny Goyal371ea052019-03-06 15:38:32 -0800462 onIdpChanged(idp);
463 }
464
465 private void onIdpChanged(InvariantDeviceProfile idp) {
466 mUserEventDispatcher = null;
467
Sunny Goyalc8502c12019-06-10 12:14:47 -0700468 DeviceProfile oldWallpaperProfile = getWallpaperDeviceProfile();
Hyunyoung Song31971a32019-01-27 12:31:12 -0800469 initDeviceProfile(idp);
Sunny Goyal371ea052019-03-06 15:38:32 -0800470 dispatchDeviceProfileChanged();
471 reapplyUi();
472 mDragLayer.recreateControllers();
473
Sunny Goyal76ba0a32019-06-10 11:56:11 -0700474 // Calling onSaveInstanceState ensures that static cache used by listWidgets is
475 // initialized properly.
476 onSaveInstanceState(new Bundle());
Sunny Goyalc8502c12019-06-10 12:14:47 -0700477 if (oldWallpaperProfile != getWallpaperDeviceProfile()) {
478 rebindModel();
479 }
Hyunyoung Song31971a32019-01-27 12:31:12 -0800480 }
481
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400482 public void onAssistantVisibilityChanged(float visibility) {
483 mCurrentAssistantVisibility = visibility;
484 float alpha = 1f - visibility;
485 LauncherState state = mStateManager.getState();
486 if (state == NORMAL) {
487 mAppsView.getAlphaProperty(APPS_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
488 } else if (state == OVERVIEW || state == OVERVIEW_PEEK) {
489 mAppsView.getAlphaProperty(APPS_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
490 mScrimView.getAlphaProperty(SCRIM_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
491 }
492 }
493
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800494 private void initDeviceProfile(InvariantDeviceProfile idp) {
495 // Load configuration-specific DeviceProfile
Sunny Goyal59d086c2018-05-07 17:31:40 -0700496 mDeviceProfile = idp.getDeviceProfile(this);
Sunny Goyal8c48d8b2019-01-25 15:10:18 -0800497 if (isInMultiWindowMode()) {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700498 Display display = getWindowManager().getDefaultDisplay();
499 Point mwSize = new Point();
500 display.getSize(mwSize);
501 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
502 }
Sunny Goyalae6e3182019-04-30 12:04:37 -0700503
Sunny Goyalda14cf82019-05-30 10:31:09 -0700504 if (supportsFakeLandscapeUI() && mDeviceProfile.isVerticalBarLayout()) {
Sunny Goyalae6e3182019-04-30 12:04:37 -0700505 mStableDeviceProfile = mDeviceProfile.inv.portraitProfile;
Sunny Goyal7a39b1b2019-05-02 13:39:16 -0700506 mRotationMode = UiFactory.getRotationMode(mDeviceProfile);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700507 } else {
508 mStableDeviceProfile = null;
509 mRotationMode = RotationMode.NORMAL;
510 }
511
Sunny Goyalda14cf82019-05-30 10:31:09 -0700512 mRotationHelper.updateRotationAnimation();
Sunny Goyal59d086c2018-05-07 17:31:40 -0700513 onDeviceProfileInitiated();
Sunny Goyalae6e3182019-04-30 12:04:37 -0700514 mModelWriter = mModel.getWriter(getWallpaperDeviceProfile().isVerticalBarLayout(), true);
515 }
516
517 public void updateInsets(Rect insets) {
518 mDeviceProfile.updateInsets(insets);
519 if (mStableDeviceProfile != null) {
Sunny Goyal7a39b1b2019-05-02 13:39:16 -0700520 Rect r = mStableDeviceProfile.getInsets();
521 mRotationMode.mapInsets(this, insets, r);
522 mStableDeviceProfile.updateInsets(r);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700523 }
524 }
525
526 @Override
527 public RotationMode getRotationMode() {
528 return mRotationMode;
529 }
530
531 /**
532 * Device profile to be used by UI elements which are shown directly on top of the wallpaper
533 * and whose presentation is tied to the wallpaper (and physical device) and not the activity
534 * configuration.
535 */
536 @Override
537 public DeviceProfile getWallpaperDeviceProfile() {
538 return mStableDeviceProfile == null ? mDeviceProfile : mStableDeviceProfile;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800539 }
540
Sunny Goyal623eddd2018-03-02 12:24:41 -0800541 public RotationHelper getRotationHelper() {
542 return mRotationHelper;
543 }
544
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700545 public LauncherStateManager getStateManager() {
546 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700547 }
548
Mario Bertschler27288382017-05-24 15:35:09 -0700549 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700550 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800551 return mLauncherView.findViewById(id);
552 }
553
554 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700555 public void onAppWidgetHostReset() {
556 if (mAppWidgetHost != null) {
557 mAppWidgetHost.startListening();
558 }
559 }
560
Adam Cohen9211d422014-10-07 18:14:53 -0700561 private LauncherCallbacks mLauncherCallbacks;
562
Sunny Goyal32554d12015-12-03 15:31:25 -0800563 /**
564 * Call this after onCreate to set or clear overlay.
565 */
566 public void setLauncherOverlay(LauncherOverlay overlay) {
567 if (overlay != null) {
568 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
569 }
570 mWorkspace.setLauncherOverlay(overlay);
571 }
572
Adam Cohen9211d422014-10-07 18:14:53 -0700573 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
574 mLauncherCallbacks = callbacks;
575 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700576 }
577
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700578 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700579 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700580 if (mLauncherCallbacks != null) {
581 mLauncherCallbacks.onLauncherProviderChange();
582 }
583 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700584
Hyunyoung Song3f471442015-04-08 19:01:34 -0700585 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700586 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
587 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700588 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700589 }
590
Tony Wickham010d2552017-01-20 08:15:28 -0800591 public PopupDataProvider getPopupDataProvider() {
592 return mPopupDataProvider;
593 }
594
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700595 @Override
Tony Wickhamf34bee82018-12-03 18:11:39 -0800596 public DotInfo getDotInfoForItem(ItemInfo info) {
597 return mPopupDataProvider.getDotInfoForItem(info);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700598 }
599
600 @Override
601 public void invalidateParent(ItemInfo info) {
Jon Mirandae6f3fa42019-03-04 09:14:40 -0800602 if (info.container >= 0) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700603 View folderIcon = getWorkspace().getHomescreenIconByItemId(info.container);
Jon Mirandae6f3fa42019-03-04 09:14:40 -0800604 if (folderIcon instanceof FolderIcon && folderIcon.getTag() instanceof FolderInfo) {
605 FolderIconPreviewVerifier verifier =
606 new FolderIconPreviewVerifier(getDeviceProfile().inv);
607 verifier.setFolderInfo((FolderInfo) folderIcon.getTag());
608 if (verifier.isItemInPreview(info.rank)) {
609 folderIcon.invalidate();
610 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700611 }
612 }
613 }
614
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000615 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700616 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
617 * a configuration step, this allows the proper animations to run after other transitions.
618 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700619 private int completeAdd(
Sunny Goyal2100c782016-08-22 16:00:03 -0700620 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
Sunny Goyalefb7e842018-10-04 15:11:00 -0700621 int screenId = info.screenId;
Sunny Goyal2100c782016-08-22 16:00:03 -0700622 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700623 // When the screen id represents an actual screen (as opposed to a rank) we make sure
624 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700625 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700626 }
Adam Cohendb364c32014-05-20 14:23:40 -0700627
Sunny Goyal2100c782016-08-22 16:00:03 -0700628 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800629 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700630 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700631 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800632 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700633 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700634 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700635 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700636 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700637 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700638 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700639 int widgetId = appWidgetId;
640 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700641 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700642 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700643 // Since the view was just bound, also launch the configure activity if needed
644 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
645 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800646 if (provider != null) {
647 new WidgetAddFlowHandler(provider)
648 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700649 }
650 }
651 break;
652 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800653 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700654
Adam Cohendb364c32014-05-20 14:23:40 -0700655 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800656 }
657
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800658 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700659 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700660 if (isWorkspaceLoading()) {
661 // process the result once the workspace has loaded.
662 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
663 return;
664 }
665 mPendingActivityResult = null;
666
Winson Chunge341d302013-08-16 14:31:00 -0700667 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700668 final PendingRequestArgs requestArgs = mPendingRequestArgs;
669 setWaitingForResult(null);
670 if (requestArgs == null) {
671 return;
672 }
673
674 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700675
Adam Cohenad4e15c2013-10-17 16:21:35 -0700676 Runnable exitSpringLoaded = new Runnable() {
677 @Override
678 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700679 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700680 }
681 };
682
Michael Jurka8b805b12012-04-18 14:23:14 -0700683 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700684 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700685 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700686 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
687 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700688 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700689 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700690 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700691 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700692 addAppWidgetImpl(
693 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800694 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700695 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700696 }
697 return;
698 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200699
Adam Cohened66b2b2012-01-23 17:28:51 -0800700 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700701 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700702
Adam Cohened66b2b2012-01-23 17:28:51 -0800703 // We have special handling for widgets
704 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800705 final int appWidgetId;
706 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
707 : -1;
708 if (widgetId < 0) {
709 appWidgetId = pendingAddWidgetId;
710 } else {
711 appWidgetId = widgetId;
712 }
713
Adam Cohenad4e15c2013-10-17 16:21:35 -0700714 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800715 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700716 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
717 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700718 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700719 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700720 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700721 @Override
722 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700723 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700724 }
725 };
Adam Cohendb364c32014-05-20 14:23:40 -0700726
Sunny Goyal2100c782016-08-22 16:00:03 -0700727 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
728 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
729 } else {
730 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
731 // When the screen id represents an actual screen (as opposed to a rank)
732 // we make sure that the drop page actually exists.
733 requestArgs.screenId =
734 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700735 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700736 final CellLayout dropLayout =
737 mWorkspace.getScreenWithId(requestArgs.screenId);
738
739 dropLayout.setDropPending(true);
740 final Runnable onComplete = new Runnable() {
741 @Override
742 public void run() {
743 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
744 dropLayout.setDropPending(false);
745 }
746 };
747 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
748 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700749 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800750 return;
751 }
752
Sunny Goyald478c832016-04-01 12:04:16 -0700753 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
754 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700755 if (resultCode == RESULT_OK) {
756 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700757 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700758 }
759 // Leave the widget in the pending state if the user canceled the configure.
760 return;
761 }
762
Sunny Goyalaad90582015-07-09 14:22:50 -0700763 if (requestCode == REQUEST_CREATE_SHORTCUT) {
764 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700765 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
766 completeAdd(requestCode, data, -1, requestArgs);
767 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
768 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
769
Sunny Goyalaad90582015-07-09 14:22:50 -0700770 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700771 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
772 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800773 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800774 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800775 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800776 }
777
778 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700779 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800780 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyaldedda052019-05-14 15:23:48 -0700781 mPendingActivityRequestCode = -1;
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800782 handleActivityResult(requestCode, resultCode, data);
783 if (mLauncherCallbacks != null) {
784 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
785 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800786 }
787
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700788 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700789 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600790 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700791 PendingRequestArgs pendingArgs = mPendingRequestArgs;
792 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
793 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
794 setWaitingForResult(null);
795
Sunny Goyal28c6b962015-10-12 11:42:05 -0700796 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700797 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700798 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700799 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700800 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700801 Intent intent = pendingArgs.getPendingIntent();
802
Sunny Goyal28c6b962015-10-12 11:42:05 -0700803 if (grantResults.length > 0
804 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal369212a2019-03-26 15:03:57 -0700805 startActivitySafely(v, intent, null, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700806 } else {
807 // TODO: Show a snack bar with link to settings
808 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700809 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700810 }
811 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600812 if (mLauncherCallbacks != null) {
813 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
814 grantResults);
815 }
816 }
817
Adam Cohendb364c32014-05-20 14:23:40 -0700818 /**
819 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
820 *
821 * @param screenId the screen id to check
822 * @return the new screen, or screenId if it exists
823 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700824 private int ensurePendingDropLayoutExists(int screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800825 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700826 if (dropLayout == null) {
827 // it's possible that the add screen was removed because it was
828 // empty and a re-bind occurred
829 mWorkspace.addExtraEmptyScreen();
830 return mWorkspace.commitExtraEmptyScreen();
831 } else {
832 return screenId;
833 }
834 }
835
Sunny Goyal2100c782016-08-22 16:00:03 -0700836 @Thunk void completeTwoStageWidgetDrop(
837 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
838 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800839 Runnable onCompleteRunnable = null;
840 int animationType = 0;
841
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700842 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800843 if (resultCode == RESULT_OK) {
844 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
845 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800846 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700847 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800848 onCompleteRunnable = new Runnable() {
849 @Override
850 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700851 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700852 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800853 }
854 };
855 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800856 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800857 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800858 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700859 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700860 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700861 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
862 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700863 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700864 // The animated view may be null in the case of a rotation during widget configuration
865 onCompleteRunnable.run();
866 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800867 }
868
869 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700870 protected void onStop() {
871 super.onStop();
Winson Chung0b70cd42019-06-17 22:34:33 -0700872
Adam Cohen9211d422014-10-07 18:14:53 -0700873 if (mLauncherCallbacks != null) {
874 mLauncherCallbacks.onStop();
875 }
Miranda Kephart1a359a22019-02-11 13:07:04 -0500876
Hyunyoung Song1241e612018-05-15 21:46:51 -0700877 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
878 mStateManager.getState().containerType, -1);
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700879
880 mAppWidgetHost.setListenIfResumed(false);
881
Tony Wickham010d2552017-01-20 08:15:28 -0800882 NotificationListener.removeNotificationsChangedListener();
Sunny Goyalaf161fd2018-02-05 16:34:57 -0800883 getStateManager().moveToRestState();
Winson Chungedb0d6b2018-03-21 17:32:55 -0700884
Tracy Zhou62646712018-06-26 14:19:02 -0700885 UiFactory.onLauncherStateOrResumeChanged(this);
886
Winson Chungdea51352018-04-24 13:02:10 -0700887 // Workaround for b/78520668, explicitly trim memory once UI is hidden
Winson Chung473730f2018-04-26 11:54:22 -0700888 onTrimMemory(TRIM_MEMORY_UI_HIDDEN);
Michael Jurkacd496d72013-04-11 11:32:45 -0700889 }
890
891 @Override
892 protected void onStart() {
vadimtcb863752018-12-19 17:44:57 -0800893 RaceConditionTracker.onEvent(ON_START_EVT, ENTER);
Michael Jurkacd496d72013-04-11 11:32:45 -0700894 super.onStart();
Adam Cohen9211d422014-10-07 18:14:53 -0700895 if (mLauncherCallbacks != null) {
896 mLauncherCallbacks.onStart();
897 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800898 mAppWidgetHost.setListenIfResumed(true);
vadimtcb863752018-12-19 17:44:57 -0800899 RaceConditionTracker.onEvent(ON_START_EVT, EXIT);
Michael Jurkacd496d72013-04-11 11:32:45 -0700900 }
901
Sunny Goyala002c6c2019-02-12 16:20:10 -0800902 private void handleDeferredResume() {
Sunny Goyale9c6f4c2019-05-30 11:35:25 -0700903 if (hasBeenResumed() && !mStateManager.getState().disableInteraction) {
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700904 getUserEventDispatcher().logActionCommand(Action.Command.RESUME,
905 mStateManager.getState().containerType, -1);
906 getUserEventDispatcher().startSession();
Sunny Goyala002c6c2019-02-12 16:20:10 -0800907
908 UiFactory.onLauncherStateOrResumeChanged(this);
Sunny Goyal369212a2019-03-26 15:03:57 -0700909 AppLaunchTracker.INSTANCE.get(this).onReturnedToHome();
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700910
Sunny Goyale9c6f4c2019-05-30 11:35:25 -0700911 // Process any items that were added while Launcher was away.
912 InstallShortcutReceiver.disableAndFlushInstallQueue(
913 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
914
915 // Refresh shortcuts if the permission changed.
916 mModel.refreshShortcutsIfRequired();
917
Winson Chung87583112019-06-10 14:20:58 -0700918 // Set the notification listener and fetch updated notifications when we resume
919 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
920
Sunny Goyale9c6f4c2019-05-30 11:35:25 -0700921 DiscoveryBounce.showForHomeIfNeeded(this);
922
923 if (mPendingActivityRequestCode != -1 && isInState(NORMAL)) {
924 UiFactory.resetPendingActivityResults(this, mPendingActivityRequestCode);
925 }
926 mDeferredResumePending = false;
927 } else {
928 mDeferredResumePending = true;
Sunny Goyaldedda052019-05-14 15:23:48 -0700929 }
930 }
931
932 protected void onStateSet(LauncherState state) {
933 getAppWidgetHost().setResumed(state == LauncherState.NORMAL);
Sunny Goyale9c6f4c2019-05-30 11:35:25 -0700934 if (mDeferredResumePending) {
935 handleDeferredResume();
936 }
937 if (mLauncherCallbacks != null) {
938 mLauncherCallbacks.onStateChanged();
939 }
Sunny Goyaldedda052019-05-14 15:23:48 -0700940 }
941
Michael Jurkacd496d72013-04-11 11:32:45 -0700942 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800943 protected void onResume() {
vadimtcb863752018-12-19 17:44:57 -0800944 RaceConditionTracker.onEvent(ON_RESUME_EVT, ENTER);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700945 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800946 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700947 TraceHelper.partitionSection("ON_RESUME", "superCall");
948
Sunny Goyala002c6c2019-02-12 16:20:10 -0800949 mHandler.removeCallbacks(mHandleDeferredResume);
950 Utilities.postAsyncCallback(mHandler, mHandleDeferredResume);
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700951
Winson Chung940b0ac2019-07-09 11:33:50 -0700952 if (!mOnResumeCallbacks.isEmpty()) {
953 final ArrayList<OnResumeCallback> resumeCallbacks = new ArrayList<>(mOnResumeCallbacks);
954 mOnResumeCallbacks.clear();
955 for (int i = resumeCallbacks.size() - 1; i >= 0; i--) {
956 resumeCallbacks.get(i).onLauncherResume();
957 }
958 resumeCallbacks.clear();
Winson Chung0b70cd42019-06-17 22:34:33 -0700959 }
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700960
Adam Cohen9211d422014-10-07 18:14:53 -0700961 if (mLauncherCallbacks != null) {
962 mLauncherCallbacks.onResume();
963 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800964
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700965 TraceHelper.endSection("ON_RESUME");
vadimtcb863752018-12-19 17:44:57 -0800966 RaceConditionTracker.onEvent(ON_RESUME_EVT, EXIT);
Adam Cohen06dff352012-06-01 17:17:08 -0700967 }
968
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800969 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700970 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700971 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700972 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700973
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700974 super.onPause();
Joe Onorato24b6fd82009-11-12 13:47:09 -0800975 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700976 mDragController.resetLastGestureUpTime();
Hyunyoung Song497708c2019-05-01 16:16:53 -0700977 mDropTargetBar.animateToVisibility(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700978 if (mLauncherCallbacks != null) {
979 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700980 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700981 }
982
Winson Chungd7823942018-02-16 03:23:47 +0000983 @Override
Tracy Zhoua706f002018-03-28 13:55:19 -0700984 protected void onUserLeaveHint() {
985 super.onUserLeaveHint();
986 UiFactory.onLauncherStateOrResumeChanged(this);
987 }
988
989 @Override
Winson Chungd7823942018-02-16 03:23:47 +0000990 public void onWindowFocusChanged(boolean hasFocus) {
991 super.onWindowFocusChanged(hasFocus);
992 mStateManager.onWindowFocusChanged();
993 }
994
Adam Cohenc2d6e892014-10-16 09:49:24 -0700995 public interface LauncherOverlay {
996
997 /**
998 * Touch interaction leading to overscroll has begun
999 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001000 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -07001001
1002 /**
1003 * Touch interaction related to overscroll has ended
1004 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001005 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -07001006
1007 /**
1008 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1009 * screen (or in the case of RTL, the rightmost screen).
1010 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001011 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001012
1013 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001014 * Called when the launcher is ready to use the overlay
1015 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001016 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001017 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001018 }
1019
1020 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001021 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001022 }
1023
1024 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1025
Sunny Goyalc86df472016-02-25 09:19:38 -08001026 public void onScrollChanged(float progress) {
1027 if (mWorkspace != null) {
1028 mWorkspace.onOverlayScrollChanged(progress);
1029 }
1030 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001031 }
1032
Sunny Goyalf9403d92017-10-18 10:55:56 -07001033 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -07001034 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -07001035 }
1036
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001037 /**
1038 * Restores the previous state, if it exists.
1039 *
1040 * @param savedState The previous state.
1041 */
1042 private void restoreState(Bundle savedState) {
1043 if (savedState == null) {
1044 return;
1045 }
1046
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001047 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -07001048 LauncherState[] stateValues = LauncherState.values();
1049 LauncherState state = stateValues[stateOrdinal];
Sunny Goyalbc683e92017-12-06 10:25:07 -08001050 if (!state.disableRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001051 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001052 }
1053
Sunny Goyal2100c782016-08-22 16:00:03 -07001054 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
1055 if (requestArgs != null) {
1056 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001057 }
Sunny Goyaldedda052019-05-14 15:23:48 -07001058 mPendingActivityRequestCode = savedState.getInt(RUNTIME_STATE_PENDING_REQUEST_CODE);
Sunny Goyal2100c782016-08-22 16:00:03 -07001059
1060 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001061
1062 SparseArray<Parcelable> widgetsState =
1063 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
1064 if (widgetsState != null) {
1065 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
1066 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001067 }
1068
1069 /**
1070 * Finds all the views we need and configure them properly.
1071 */
1072 private void setupViews() {
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001073 mDragLayer = findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001074 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -07001075 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001076 mWorkspace.initParentViews(mDragLayer);
Sunny Goyal16764582017-11-06 16:00:34 -08001077 mOverviewPanel = findViewById(R.id.overview_panel);
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001078 mHotseat = findViewById(R.id.hotseat);
Craig Mautner360310b2012-10-26 15:13:08 -07001079
Sunny Goyal784f9c32016-03-23 16:56:54 -07001080 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1081 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1082 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001083
Winson Chung4c98d922011-05-31 16:50:48 -07001084 // Setup the drag layer
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001085 mDragLayer.setup(mDragController, mWorkspace);
Sunny Goyal0b1cb752019-06-05 15:18:39 -07001086 mCancelTouchController = UiFactory.enableLiveUIChanges(this);
Winson Chung4c98d922011-05-31 16:50:48 -07001087
Hyunyoung Song645764e2016-06-06 14:19:02 -07001088 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001089 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1090 // default state, otherwise we will update to the wrong offsets in RTL
1091 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001092 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001093 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001094
Tony Wickham34d2c912015-09-11 09:27:58 -07001095 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001096 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001097
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001098 // Setup Apps
1099 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001100
Govinda Wassermanc06e1512019-04-09 09:56:53 -04001101 // Setup Scrim
1102 mScrimView = findViewById(R.id.scrim_view);
1103
Winson Chung3d503fb2011-07-13 17:25:49 -07001104 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001105 mDragController.setMoveTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001106 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001107
Sunny Goyal7185dd62018-03-14 17:51:49 -07001108 mAllAppsController.setupViews(mAppsView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001109 }
1110
1111 /**
1112 * Creates a view representing a shortcut.
1113 *
1114 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001115 */
Sunny Goyal95899162019-03-27 16:03:06 -07001116 View createShortcut(WorkspaceItemInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001117 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001118 }
1119
1120 /**
1121 * Creates a view representing a shortcut inflated from the specified resource.
1122 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001123 * @param parent The group the shortcut belongs to.
1124 * @param info The data structure describing the shortcut.
1125 *
1126 * @return A View inflated from layoutResId.
1127 */
Sunny Goyal95899162019-03-27 16:03:06 -07001128 public View createShortcut(ViewGroup parent, WorkspaceItemInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -08001129 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
1130 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal95899162019-03-27 16:03:06 -07001131 favorite.applyFromWorkspaceItem(info);
Sunny Goyal8a2a63b2018-03-06 22:15:18 -08001132 favorite.setOnClickListener(ItemClickHandler.INSTANCE);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001133 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001134 return favorite;
1135 }
1136
1137 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -07001138 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001139 *
1140 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001141 */
Sunny Goyalefb7e842018-10-04 15:11:00 -07001142 private void completeAddShortcut(Intent data, int container, int screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001143 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001144 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
1145 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001146 return;
1147 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001148
Jon Mirandac476d6e2017-05-04 16:30:01 -07001149 int[] cellXY = mTmpAddItemCellCoordinates;
1150 CellLayout layout = getCellLayout(container, screenId);
1151
Sunny Goyal95899162019-03-27 16:03:06 -07001152 WorkspaceItemInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001153 if (Utilities.ATLEAST_OREO) {
Sunny Goyal95899162019-03-27 16:03:06 -07001154 info = LauncherAppsCompatVO.createWorkspaceItemFromPinItemRequest(
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001155 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001156 }
1157
1158 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001159 // Legacy shortcuts are only supported for primary profile.
1160 info = Process.myUserHandle().equals(args.user)
1161 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001162
Sunny Goyalb57645f2017-03-20 13:57:28 -07001163 if (info == null) {
1164 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1165 return;
1166 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001167 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001168 // The app is trying to add a shortcut without sufficient permissions
1169 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1170 return;
1171 }
1172 }
1173
Jon Mirandac476d6e2017-05-04 16:30:01 -07001174 if (container < 0) {
1175 // Adding a shortcut to the Workspace.
1176 final View view = createShortcut(info);
1177 boolean foundCellSpan = false;
1178 // First we check if we already know the exact location where we want to add this item.
1179 if (cellX >= 0 && cellY >= 0) {
1180 cellXY[0] = cellX;
1181 cellXY[1] = cellY;
1182 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001183
Jon Mirandac476d6e2017-05-04 16:30:01 -07001184 // If appropriate, either create a folder or add to an existing folder
1185 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001186 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001187 return;
1188 }
1189 DragObject dragObject = new DragObject();
1190 dragObject.dragInfo = info;
1191 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1192 true)) {
1193 return;
1194 }
1195 } else {
1196 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1197 }
1198
1199 if (!foundCellSpan) {
1200 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001201 return;
1202 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001203
1204 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1205 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001206 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001207 // Adding a shortcut to a Folder.
Sunny Goyale29897f2017-07-20 10:09:42 -07001208 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001209 if (folderIcon != null) {
1210 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1211 folderInfo.add(info, args.rank, false);
1212 } else {
Sunny Goyale29897f2017-07-20 10:09:42 -07001213 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001214 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001215 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001216 }
1217
Sunny Goyalefb7e842018-10-04 15:11:00 -07001218 public FolderIcon findFolderIcon(final int folderIconId) {
Sunny Goyal83fd25e2018-07-09 16:47:01 -07001219 return (FolderIcon) mWorkspace.getHomescreenIconByItemId(folderIconId);
Sunny Goyale29897f2017-07-20 10:09:42 -07001220 }
1221
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001222 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001223 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001224 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001225 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001226 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001227 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001228 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1229
Adam Cohened66b2b2012-01-23 17:28:51 -08001230 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001231 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001232 }
Romain Guycbb89e42009-06-08 15:52:54 -07001233
Adam Cohen59400422014-03-05 18:07:04 -08001234 LauncherAppWidgetInfo launcherInfo;
1235 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001236 launcherInfo.spanX = itemInfo.spanX;
1237 launcherInfo.spanY = itemInfo.spanY;
1238 launcherInfo.minSpanX = itemInfo.minSpanX;
1239 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001240 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001241
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001242 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001243 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001244
Sunny Goyal2100c782016-08-22 16:00:03 -07001245 if (hostView == null) {
1246 // Perform actual inflation because we're live
1247 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001248 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001249 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301250 prepareAppWidget(hostView, launcherInfo);
1251 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001252 }
Romain Guycbb89e42009-06-08 15:52:54 -07001253
Sunny Goyald5462aa2016-11-22 16:52:39 +05301254 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001255 hostView.setTag(item);
1256 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001257 hostView.setFocusable(true);
1258 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001259 }
1260
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001261 private final BroadcastReceiver mScreenOffReceiver = new BroadcastReceiver() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001262 @Override
1263 public void onReceive(Context context, Intent intent) {
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001264 // Reset AllApps to its initial state only if we are not in the middle of
1265 // processing a multi-step drop
1266 if (mPendingRequestArgs == null) {
1267 mStateManager.goToState(NORMAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001268 }
1269 }
1270 };
1271
Sunny Goyalaae6fbb2019-01-31 16:05:58 -08001272 public void updateNotificationDots(Predicate<PackageUserKey> updatedDots) {
Tony Wickhamf34bee82018-12-03 18:11:39 -08001273 mWorkspace.updateNotificationDots(updatedDots);
1274 mAppsView.getAppsStore().updateNotificationDots(updatedDots);
Tony Wickham010d2552017-01-20 08:15:28 -08001275 }
1276
Adam Cohended9f8d2010-11-03 13:25:16 -07001277 @Override
1278 public void onAttachedToWindow() {
1279 super.onAttachedToWindow();
1280
Sunny Goyalc86df472016-02-25 09:19:38 -08001281 if (mLauncherCallbacks != null) {
1282 mLauncherCallbacks.onAttachedToWindow();
1283 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001284 }
1285
1286 @Override
1287 public void onDetachedFromWindow() {
1288 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001289
1290 if (mLauncherCallbacks != null) {
1291 mLauncherCallbacks.onDetachedFromWindow();
1292 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001293 }
1294
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001295 public AllAppsTransitionController getAllAppsController() {
1296 return mAllAppsController;
1297 }
1298
Sunny Goyal9d69c8d2018-03-19 13:41:31 -07001299 @Override
Winson Chung1a341002018-01-17 10:00:23 -08001300 public LauncherRootView getRootView() {
1301 return (LauncherRootView) mLauncherView;
1302 }
1303
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001304 @Override
Winson Chunga6945242014-01-08 14:04:34 -08001305 public DragLayer getDragLayer() {
1306 return mDragLayer;
1307 }
1308
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001309 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001310 return mAppsView;
1311 }
1312
Winson Chunga6945242014-01-08 14:04:34 -08001313 public Workspace getWorkspace() {
1314 return mWorkspace;
1315 }
1316
1317 public Hotseat getHotseat() {
1318 return mHotseat;
1319 }
1320
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001321 public <T extends View> T getOverviewPanel() {
Winson Chung8ae41982017-11-10 11:42:13 -08001322 return (T) mOverviewPanel;
Winson Chunga6945242014-01-08 14:04:34 -08001323 }
1324
Sunny Goyal47328fd2016-05-25 18:56:41 -07001325 public DropTargetBar getDropTargetBar() {
1326 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001327 }
1328
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001329 public LauncherAppWidgetHost getAppWidgetHost() {
1330 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001331 }
Romain Guycbb89e42009-06-08 15:52:54 -07001332
Winson Chunga9abd0e2010-10-27 17:18:37 -07001333 public LauncherModel getModel() {
1334 return mModel;
1335 }
1336
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001337 public ModelWriter getModelWriter() {
1338 return mModelWriter;
1339 }
1340
Adam Cohen79d90c52016-04-22 13:29:20 -07001341 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001342 return mSharedPrefs;
1343 }
1344
Sunny Goyalf8d56fc2018-01-31 15:18:11 -08001345 public int getOrientation() { return mOldConfig.orientation; }
Jon Miranda6bed3502017-09-19 14:43:17 -07001346
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001347 @Override
1348 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001349 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001350 super.onNewIntent(intent);
1351
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001352 boolean alreadyOnHome = hasWindowFocus() && ((intent.getFlags() &
Winson15f8b172015-08-19 11:02:39 -07001353 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1354 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001355
1356 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001357 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001358 && AbstractFloatingView.getTopOpenView(this) == null;
Winson15f8b172015-08-19 11:02:39 -07001359 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001360 boolean internalStateHandled = InternalStateHandler
Sunny Goyal8b264562018-02-03 23:00:13 -08001361 .handleNewIntent(this, intent, isStarted());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001362
Winson15f8b172015-08-19 11:02:39 -07001363 if (isActionMain) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001364 if (!internalStateHandled) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001365 // In all these cases, only animate if we're already on home
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001366 AbstractFloatingView.closeAllOpenViews(this, isStarted());
Jon Mirandafeba90f2016-10-06 10:53:29 -07001367
Sunny Goyala2467272018-03-16 14:53:05 -07001368 if (!isInState(NORMAL)) {
1369 // Only change state, if not already the same. This prevents cancelling any
1370 // animations running as part of resume
1371 mStateManager.goToState(NORMAL);
1372 }
Sunny Goyald3864fa2017-11-14 15:06:36 -08001373
1374 // Reset the apps view
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001375 if (!alreadyOnHome) {
Sunny Goyal7185dd62018-03-14 17:51:49 -07001376 mAppsView.reset(isStarted() /* animate */);
Sunny Goyald3864fa2017-11-14 15:06:36 -08001377 }
1378
Sunny Goyalaad33592018-08-07 17:20:35 -07001379 if (shouldMoveToDefaultScreen && !mWorkspace.isHandlingTouch()) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001380 mWorkspace.post(mWorkspace::moveToDefaultScreen);
1381 }
1382 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001383
Hyunyoung Song2a70b3d2019-06-11 12:47:07 -07001384 // Handle HOME_INTENT
1385 UserEventDispatcher ued = getUserEventDispatcher();
1386 Target target = newContainerTarget(mStateManager.getState().containerType);
1387 target.pageIndex = mWorkspace.getCurrentPage();
1388 ued.logActionCommand(Action.Command.HOME_INTENT, target,
1389 newContainerTarget(ContainerType.WORKSPACE));
1390
Adam Cohen6fecd412013-10-02 17:41:50 -07001391 final View v = getWindow().peekDecorView();
1392 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001393 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001394 }
1395
Adam Cohen9211d422014-10-07 18:14:53 -07001396 if (mLauncherCallbacks != null) {
Sunny Goyal85462132018-04-24 11:39:37 -07001397 mLauncherCallbacks.onHomeIntent(internalStateHandled);
Adam Cohen9211d422014-10-07 18:14:53 -07001398 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001399 }
Winson15f8b172015-08-19 11:02:39 -07001400
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001401 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001402 }
1403
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001404 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001405 public void onRestoreInstanceState(Bundle state) {
1406 super.onRestoreInstanceState(state);
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001407 mWorkspace.restoreInstanceStateForChild(mSynchronouslyBoundPage);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001408 }
1409
1410 @Override
1411 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001412 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001413 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001414
Adam Cohen21cd0022013-10-09 18:57:02 -07001415 }
Sunny Goyalea609262017-10-25 15:47:38 -07001416 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001417
1418
1419 AbstractFloatingView widgets = AbstractFloatingView
1420 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1421 if (widgets != null) {
1422 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1423 widgets.saveHierarchyState(widgetsState);
1424 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1425 } else {
1426 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1427 }
1428
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001429 // We close any open folders and shortcut containers since they will not be re-opened,
1430 // and we need to make sure this state is reflected.
1431 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001432
Sunny Goyal2100c782016-08-22 16:00:03 -07001433 if (mPendingRequestArgs != null) {
1434 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1435 }
Sunny Goyaldedda052019-05-14 15:23:48 -07001436 outState.putInt(RUNTIME_STATE_PENDING_REQUEST_CODE, mPendingActivityRequestCode);
1437
Sunny Goyal2100c782016-08-22 16:00:03 -07001438 if (mPendingActivityResult != null) {
1439 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001440 }
1441
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001442 super.onSaveInstanceState(outState);
1443
Adam Cohen9211d422014-10-07 18:14:53 -07001444 if (mLauncherCallbacks != null) {
1445 mLauncherCallbacks.onSaveInstanceState(outState);
1446 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001447 }
1448
1449 @Override
1450 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001451 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001452
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001453 unregisterReceiver(mScreenOffReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001454 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001455
Sunny Goyal5743f862019-03-28 15:35:32 -07001456 if (mCancelTouchController != null) {
1457 mCancelTouchController.run();
1458 mCancelTouchController = null;
1459 }
Tony Wickhama0068302018-04-11 16:16:11 -07001460
Winson Chungcd2b0142011-06-08 16:02:26 -07001461 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001462 // It's possible to receive onDestroy after a new Launcher activity has
1463 // been created. In this case, don't interfere with the new Launcher.
1464 if (mModel.isCurrentCallbacks(this)) {
1465 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001466 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001467 }
Sunny Goyal623eddd2018-03-02 12:24:41 -08001468 mRotationHelper.destroy();
Sunny Goyal745bad92016-05-02 10:54:12 -07001469
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001470 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001471 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001473 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001474 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001475
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476 TextKeyListener.getInstance().release();
Tony2917a8b2017-07-31 23:29:42 -07001477 clearPendingBinds();
Hyunyoung Song31971a32019-01-27 12:31:12 -08001478 LauncherAppState.getIDP(this).removeOnChangeListener(this);
Adam Cohen9211d422014-10-07 18:14:53 -07001479 if (mLauncherCallbacks != null) {
1480 mLauncherCallbacks.onDestroy();
1481 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001482 }
1483
Sunny Goyalae502842016-06-17 08:43:56 -07001484 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1485 return mAccessibilityDelegate;
1486 }
1487
Adam Cohena9cf38f2011-05-02 15:36:58 -07001488 public DragController getDragController() {
1489 return mDragController;
1490 }
1491
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001492 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001493 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyaldedda052019-05-14 15:23:48 -07001494 if (requestCode != -1) {
1495 mPendingActivityRequestCode = requestCode;
1496 }
1497 if (requestCode == -1
1498 || !UiFactory.startActivityForResult(this, intent, requestCode, options)) {
1499 super.startActivityForResult(intent, requestCode, options);
1500 }
Adam Cohen173f7112015-03-27 15:14:00 -07001501 }
1502
1503 @Override
Sunny Goyaldedda052019-05-14 15:23:48 -07001504 public void startIntentSenderForResult(IntentSender intent, int requestCode,
Adam Cohen173f7112015-03-27 15:14:00 -07001505 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Sunny Goyaldedda052019-05-14 15:23:48 -07001506 if (requestCode != -1) {
1507 mPendingActivityRequestCode = requestCode;
1508 }
1509 if (requestCode == -1 || !UiFactory.startIntentSenderForResult(this, intent, requestCode,
1510 fillInIntent, flagsMask, flagsValues, extraFlags, options)) {
1511 try {
1512 super.startIntentSenderForResult(intent, requestCode,
1513 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1514 } catch (IntentSender.SendIntentException e) {
1515 throw new ActivityNotFoundException();
1516 }
Adam Cohen173f7112015-03-27 15:14:00 -07001517 }
1518 }
1519
Winson Chung70d72102011-08-12 11:24:35 -07001520 /**
1521 * Indicates that we want global search for this activity by setting the globalSearch
1522 * argument for {@link #startSearch} to true.
1523 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001524 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001525 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001526 Bundle appSearchData, boolean globalSearch) {
1527 if (appSearchData == null) {
1528 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001529 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001530 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001531
Sunny Goyal6f28e712016-09-13 14:19:29 -07001532 if (mLauncherCallbacks == null ||
1533 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1534 // Starting search from the callbacks failed. Start the default global search.
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001535 super.startSearch(initialQuery, selectInitialQuery, appSearchData, true);
Ian Parkinson047036e2014-09-01 15:40:53 +01001536 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001537
1538 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001539 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001540 }
1541
Joe Onorato9c1289c2009-08-17 11:03:03 -04001542 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001543 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001544 }
1545
Adam Cohen517a7f52014-03-01 12:12:59 -08001546 public boolean isWorkspaceLoading() {
1547 return mWorkspaceLoading;
1548 }
1549
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001550 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001551 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001552 }
1553
Sunny Goyal04a324a2017-01-20 21:08:59 -08001554 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001555 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001556 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001557
Sunny Goyal2100c782016-08-22 16:00:03 -07001558 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001559 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001560 if (LOGD) {
1561 Log.d(TAG, "Adding widget from drop");
1562 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001563 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001564 }
1565
Sunny Goyal2100c782016-08-22 16:00:03 -07001566 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001567 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1568 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1569 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001570
Adam Cohenad4e15c2013-10-17 16:21:35 -07001571 Runnable onComplete = new Runnable() {
1572 @Override
1573 public void run() {
1574 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001575 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001576 }
1577 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001578 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001579 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001580 }
1581 }
Romain Guycbb89e42009-06-08 15:52:54 -07001582
Sunny Goyalefb7e842018-10-04 15:11:00 -07001583 public void addPendingItem(PendingAddItemInfo info, int container, int screenId,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001584 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001585 info.container = container;
1586 info.screenId = screenId;
1587 if (cell != null) {
1588 info.cellX = cell[0];
1589 info.cellY = cell[1];
1590 }
1591 info.spanX = spanX;
1592 info.spanY = spanY;
1593
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001594 switch (info.itemType) {
1595 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1596 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001597 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001598 break;
1599 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001600 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001601 break;
1602 default:
1603 throw new IllegalStateException("Unknown item type: " + info.itemType);
1604 }
1605 }
1606
Adam Cohenfbba09b2011-07-18 21:43:05 -07001607 /**
1608 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001609 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001610 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001611 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1612 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001613 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1614 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1615 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001616 }
1617
Adam Cohenfbba09b2011-07-18 21:43:05 -07001618 /**
1619 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001620 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001621 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001622 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001623 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001624 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001625 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001626 // In the case where we've prebound the widget, we remove it from the DragLayer
1627 if (LOGD) {
1628 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1629 }
1630 getDragLayer().removeView(hostView);
1631
Adam Cohened66b2b2012-01-23 17:28:51 -08001632 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001633 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001634
1635 // Clear the boundWidget so that it doesn't get destroyed.
1636 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001637 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001638 // In this case, we either need to start an activity to get permission to bind
1639 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001640 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1641 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1642 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1643 this, info.componentName);
1644 } else {
1645 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1646 }
Adam Cohendd70d662012-10-04 16:53:44 -07001647 Bundle options = info.bindOptions;
1648
Sunny Goyalffe83f12014-08-14 17:39:34 -07001649 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1650 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001651 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001652 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001653 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001654 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001655 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001656 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001657 }
1658
Sunny Goyalefb7e842018-10-04 15:11:00 -07001659 FolderIcon addFolder(CellLayout layout, int container, final int screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001660 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001661 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001662 folderInfo.title = getText(R.string.folder_name);
1663
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001664 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001665 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001666
1667 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001668 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301669 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001670 // Force measure the new folder icon
1671 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1672 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001673 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001674 }
Romain Guycbb89e42009-06-08 15:52:54 -07001675
Winsonc0b52fe2015-09-09 16:38:15 -07001676 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001677 * Unbinds the view for the specified item, and removes the item and all its children.
1678 *
1679 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001680 * @param itemInfo the {@link ItemInfo} for this view.
1681 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001682 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001683 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Sunny Goyal95899162019-03-27 16:03:06 -07001684 if (itemInfo instanceof WorkspaceItemInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001685 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001686 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1687 if (folderIcon instanceof FolderIcon) {
Sunny Goyal95899162019-03-27 16:03:06 -07001688 ((FolderInfo) folderIcon.getTag()).remove((WorkspaceItemInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001689 } else {
1690 mWorkspace.removeWorkspaceItem(v);
1691 }
Winsonc0b52fe2015-09-09 16:38:15 -07001692 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001693 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001694 }
1695 } else if (itemInfo instanceof FolderInfo) {
1696 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001697 if (v instanceof FolderIcon) {
1698 ((FolderIcon) v).removeListeners();
1699 }
Winsonc0b52fe2015-09-09 16:38:15 -07001700 mWorkspace.removeWorkspaceItem(v);
1701 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001702 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001703 }
1704 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1705 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001706 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001707 if (deleteFromDb) {
Tony Wickham6a71a5b2018-08-21 11:40:23 -07001708 getModelWriter().deleteWidgetInfo(widgetInfo, getAppWidgetHost());
Winsonc0b52fe2015-09-09 16:38:15 -07001709 }
1710 } else {
1711 return false;
1712 }
1713 return true;
1714 }
1715
Tony Wickham6a71a5b2018-08-21 11:40:23 -07001716
Winsonc0b52fe2015-09-09 16:38:15 -07001717
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001718 @Override
1719 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001720 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001721 }
1722
Joe Onorato88ec0992009-11-19 13:16:06 -08001723 @Override
1724 public void onBackPressed() {
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08001725 if (finishAutoCancelActionMode()) {
1726 return;
1727 }
Adam Cohen9211d422014-10-07 18:14:53 -07001728 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1729 return;
1730 }
1731
Sunny Goyal45478022015-06-08 16:52:41 -07001732 if (mDragController.isDragging()) {
1733 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001734 return;
1735 }
1736
Jon Mirandafeba90f2016-10-06 10:53:29 -07001737 // Note: There should be at most one log per method call. This is enforced implicitly
1738 // by using if-else statements.
1739 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001740 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
Tony Wickham52c1b662018-05-21 13:13:58 -07001741 if (topView != null && topView.onBackPressed()) {
1742 // Handled by the floating view.
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001743 } else {
Sunny Goyale39690b2018-08-02 13:35:07 -07001744 mStateManager.getState().onBackPressed(this);
Michael Jurkaaf442092010-06-10 17:01:57 -07001745 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001746 }
1747
Sunny Goyalfe770c92016-09-27 14:38:58 -07001748 @TargetApi(Build.VERSION_CODES.M)
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001749 @Override
Jon Miranda73c27ec2018-05-16 18:06:23 -07001750 public ActivityOptions getActivityLaunchOptions(View v) {
1751 return mAppTransitionManager.getActivityLaunchOptions(this, v);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001752 }
1753
Tony Wickham3a6746a2018-03-29 09:39:56 -07001754 public LauncherAppTransitionManager getAppTransitionManager() {
1755 return mAppTransitionManager;
1756 }
1757
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001758 @TargetApi(Build.VERSION_CODES.M)
1759 @Override
1760 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
1761 // Due to legacy reasons, direct call shortcuts require Launchers to have the
1762 // corresponding permission. Show the appropriate permission prompt if that
1763 // is the case.
1764 if (intent.getComponent() == null
1765 && Intent.ACTION_CALL.equals(intent.getAction())
1766 && checkSelfPermission(android.Manifest.permission.CALL_PHONE) !=
1767 PackageManager.PERMISSION_GRANTED) {
1768
1769 setWaitingForResult(PendingRequestArgs
1770 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
1771 requestPermissions(new String[]{android.Manifest.permission.CALL_PHONE},
1772 REQUEST_PERMISSION_CALL_PHONE);
1773 return true;
1774 } else {
1775 return false;
1776 }
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001777 }
1778
Hyunyoung Song46d07f72018-05-22 15:41:25 -07001779 @Override
Hyunyoung Songfc007472018-10-25 14:09:50 -07001780 public int getCurrentState() {
1781 if(mStateManager.getState() == LauncherState.ALL_APPS) {
1782 return StatsLogUtils.LAUNCHER_STATE_ALLAPPS;
Govinda Wassermanc06e1512019-04-09 09:56:53 -04001783 } else if (mStateManager.getState() == OVERVIEW) {
Hyunyoung Songfc007472018-10-25 14:09:50 -07001784 return StatsLogUtils.LAUNCHER_STATE_OVERVIEW;
1785 }
Hyunyoung Song0ae38882018-11-05 14:45:19 -08001786 return StatsLogUtils.LAUNCHER_STATE_HOME;
Hyunyoung Songfc007472018-10-25 14:09:50 -07001787 }
1788
1789 @Override
Hyunyoung Song46d07f72018-05-22 15:41:25 -07001790 public void modifyUserEvent(LauncherLogProto.LauncherEvent event) {
1791 if (event.srcTarget != null && event.srcTarget.length > 0 &&
1792 event.srcTarget[1].containerType == ContainerType.PREDICTION) {
1793 Target[] targets = new Target[3];
1794 targets[0] = event.srcTarget[0];
1795 targets[1] = event.srcTarget[1];
1796 targets[2] = newTarget(Target.Type.CONTAINER);
1797 event.srcTarget = targets;
1798 LauncherState state = mStateManager.getState();
1799 if (state == LauncherState.ALL_APPS) {
1800 event.srcTarget[2].containerType = ContainerType.ALLAPPS;
Govinda Wassermanc06e1512019-04-09 09:56:53 -04001801 } else if (state == OVERVIEW) {
Hyunyoung Song46d07f72018-05-22 15:41:25 -07001802 event.srcTarget[2].containerType = ContainerType.TASKSWITCHER;
1803 }
1804 }
1805 }
1806
Sunny Goyal369212a2019-03-26 15:03:57 -07001807 public boolean startActivitySafely(View v, Intent intent, ItemInfo item,
1808 @Nullable String sourceContainer) {
Winson Chung0b70cd42019-06-17 22:34:33 -07001809 if (!hasBeenResumed()) {
1810 // Workaround an issue where the WM launch animation is clobbered when finishing the
1811 // recents animation into launcher. Defer launching the activity until Launcher is
1812 // next resumed.
1813 addOnResumeCallback(() -> startActivitySafely(v, intent, item, sourceContainer));
1814 UiFactory.clearSwipeSharedState(true /* finishAnimation */);
1815 return true;
1816 }
1817
Sunny Goyal369212a2019-03-26 15:03:57 -07001818 boolean success = super.startActivitySafely(v, intent, item, sourceContainer);
Hyunyoung Song1241e612018-05-15 21:46:51 -07001819 if (success && v instanceof BubbleTextView) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001820 // This is set to the view that launched the activity that navigated the user away
1821 // from launcher. Since there is no callback for when the activity has finished
1822 // launching, enable the press state and keep this reference to reset the press
1823 // state when we return to launcher.
1824 BubbleTextView btv = (BubbleTextView) v;
1825 btv.setStayPressed(true);
Winson Chung0b70cd42019-06-17 22:34:33 -07001826 addOnResumeCallback(btv);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001827 }
Hyunyoung Song1241e612018-05-15 21:46:51 -07001828 return success;
Michael Jurka86a720e2012-05-09 11:23:23 -07001829 }
1830
Winson Chung3d503fb2011-07-13 17:25:49 -07001831 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001832 // TODO: Remove this method
Sunny Goyal876e4622018-11-02 13:50:40 -07001833 return mHotseat != null && (layout == mHotseat);
Winson Chung3d503fb2011-07-13 17:25:49 -07001834 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001835
Winson Chung3d503fb2011-07-13 17:25:49 -07001836 /**
1837 * Returns the CellLayout of the specified container at the specified screen.
1838 */
Sunny Goyalefb7e842018-10-04 15:11:00 -07001839 public CellLayout getCellLayout(int container, int screenId) {
Sunny Goyal876e4622018-11-02 13:50:40 -07001840 return (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1841 ? mHotseat : mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08001842 }
1843
Michael Jurkae326f182011-11-21 14:05:46 -08001844 @Override
1845 public void onTrimMemory(int level) {
1846 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07001847 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
1848 // The widget preview db can result in holding onto over
1849 // 3MB of memory for caching which isn't necessary.
1850 SQLiteDatabase.releaseMemory();
1851
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08001852 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07001853 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08001854 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01001855 if (mLauncherCallbacks != null) {
1856 mLauncherCallbacks.onTrimMemory(level);
1857 }
Winson Chung62a70be2018-02-23 15:10:05 -08001858 UiFactory.onTrimMemory(this, level);
Michael Jurkae326f182011-11-21 14:05:46 -08001859 }
1860
Michael Jurkad7c28052012-04-27 15:43:36 -07001861 @Override
1862 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07001863 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07001864 final List<CharSequence> text = event.getText();
1865 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07001866 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001867 // TODO: When can workspace be null?
1868 text.add(mWorkspace == null
1869 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07001870 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07001871 return result;
1872 }
1873
Winson Chung0b70cd42019-06-17 22:34:33 -07001874 public void addOnResumeCallback(OnResumeCallback callback) {
1875 mOnResumeCallbacks.add(callback);
Michael Jurka1e2f4652013-07-08 18:03:46 -07001876 }
1877
Michael Jurka7607c2f2013-04-03 14:33:19 -07001878 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001879 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001880 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07001881 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001882 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07001883 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001884 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07001885 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07001886 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07001887 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001888 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001889
Joe Onorato9c1289c2009-08-17 11:03:03 -04001890 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07001891 * Clear any pending bind callbacks. This is called when is loader is planning to
1892 * perform a full rebind from scratch.
1893 */
1894 @Override
1895 public void clearPendingBinds() {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001896 if (mPendingExecutor != null) {
1897 mPendingExecutor.markCompleted();
1898 mPendingExecutor = null;
1899 }
1900 }
1901
1902 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001903 * Refreshes the shortcuts shown on the workspace.
1904 *
1905 * Implementation of the method from LauncherModel.Callbacks.
1906 */
1907 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001908 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001909 // Floating panels (except the full widget sheet) are associated with individual icons. If
1910 // we are starting a fresh bind, close all such panels as all the icons are about
1911 // to go away.
1912 AbstractFloatingView.closeOpenViews(this, true,
Sunny Goyal7ede6112017-12-05 15:11:21 -08001913 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_REBIND_SAFE);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08001914
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001915 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08001916
Winson Chungd64d1762013-08-20 14:37:16 -07001917 // Clear the workspace because it's going to be rebound
Sunny Goyal2db53422019-03-01 16:06:12 -08001918 mDragController.cancelDrag();
1919
Adam Cohendf035382011-04-11 17:22:04 -07001920 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07001921 mWorkspace.removeAllWorkspaceScreens();
Sunny Goyalc11fac32018-02-27 19:20:15 -08001922 mAppWidgetHost.clearViews();
Winson Chungd64d1762013-08-20 14:37:16 -07001923
Winson Chung3d503fb2011-07-13 17:25:49 -07001924 if (mHotseat != null) {
Sunny Goyalae6e3182019-04-30 12:04:37 -07001925 mHotseat.resetLayout(getWallpaperDeviceProfile().isVerticalBarLayout());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001926 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001927 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001928 }
1929
Adam Cohendcd297f2013-06-18 13:13:40 -07001930 @Override
Sunny Goyalefb7e842018-10-04 15:11:00 -07001931 public void bindScreens(IntArray orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001932 // Make sure the first screen is always at the start.
Jon Miranda7143ba62019-03-15 09:00:05 -07001933 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001934 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalefb7e842018-10-04 15:11:00 -07001935 orderedScreenIds.removeValue(Workspace.FIRST_SCREEN_ID);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001936 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Jon Miranda7143ba62019-03-15 09:00:05 -07001937 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001938 // If there are no screens, we need to have an empty screen
1939 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07001940 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001941 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07001942
Winsonc7d2e832016-07-28 12:24:55 -07001943 // After we have added all the screens, if the wallpaper was locked to the default state,
1944 // then notify to indicate that it can be released and a proper wallpaper offset can be
1945 // computed before the next layout
1946 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07001947 }
1948
Sunny Goyalefb7e842018-10-04 15:11:00 -07001949 private void bindAddScreens(IntArray orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001950 int count = orderedScreenIds.size();
1951 for (int i = 0; i < count; i++) {
Sunny Goyalefb7e842018-10-04 15:11:00 -07001952 int screenId = orderedScreenIds.get(i);
Jon Miranda7143ba62019-03-15 09:00:05 -07001953 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001954 // No need to bind the first screen, as its always bound.
1955 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
1956 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001957 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001958 }
1959
Sunny Goyalb23980c2017-08-17 07:45:25 -07001960 @Override
Tony Wickham6a71a5b2018-08-21 11:40:23 -07001961 public void preAddApps() {
1962 // If there's an undo snackbar, force it to complete to ensure empty screens are removed
1963 // before trying to add new items.
1964 mModelWriter.commitDelete();
1965 AbstractFloatingView snackbar = AbstractFloatingView.getOpenView(this, TYPE_SNACKBAR);
1966 if (snackbar != null) {
1967 snackbar.post(() -> snackbar.close(true));
1968 }
1969 }
1970
1971 @Override
Sunny Goyalefb7e842018-10-04 15:11:00 -07001972 public void bindAppsAdded(IntArray newScreens, ArrayList<ItemInfo> addNotAnimated,
Sunny Goyal29947f02017-12-18 13:49:44 -08001973 ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07001974 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08001975 if (newScreens != null) {
1976 bindAddScreens(newScreens);
1977 }
Winson Chungd64d1762013-08-20 14:37:16 -07001978
1979 // We add the items without animation on non-visible pages, and with
1980 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08001981 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001982 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001983 }
Adam Cohen76a47a12014-02-05 11:47:43 -08001984 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001985 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07001986 }
Winson Chungc58497e2013-09-03 17:48:37 -07001987
Winson Chung87412982013-10-03 18:34:14 -07001988 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07001989 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001990 }
1991
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001992 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001993 * Bind the items start-end from the list.
1994 *
1995 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001996 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07001997 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07001998 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001999 // Get the list of added items and intersect them with the set of items here
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002000 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07002001 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07002002 Workspace workspace = mWorkspace;
Sunny Goyalefb7e842018-10-04 15:11:00 -07002003 int newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07002004 int end = items.size();
2005 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002006 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002007
2008 // Short circuit if we are loading dock items for a configuration which has no dock
2009 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2010 mHotseat == null) {
2011 continue;
2012 }
2013
Sunny Goyal639e9062015-08-19 19:17:06 -07002014 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002015 switch (item.itemType) {
2016 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2017 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08002018 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Sunny Goyal95899162019-03-27 16:03:06 -07002019 WorkspaceItemInfo info = (WorkspaceItemInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07002020 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002021 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002022 }
2023 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07002024 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002025 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002026 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002027 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002028 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07002029 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2030 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07002031 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002032 if (view == null) {
2033 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002034 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08002035 break;
2036 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002037 default:
2038 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002039 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002040
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002041 /*
2042 * Remove colliding items.
2043 */
2044 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
2045 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
2046 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
2047 View v = cl.getChildAt(item.cellX, item.cellY);
2048 Object tag = v.getTag();
2049 String desc = "Collision while binding workspace item: " + item
2050 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08002051 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002052 throw (new RuntimeException(desc));
2053 } else {
2054 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002055 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002056 continue;
2057 }
2058 }
2059 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302060 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07002061 if (animateIcons) {
2062 // Animate all the applications up now
2063 view.setAlpha(0f);
2064 view.setScaleX(0f);
2065 view.setScaleY(0f);
2066 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08002067 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07002068 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002069 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07002070
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002071 // Animate to the correct page
2072 if (animateIcons && newItemsScreenId > -1) {
2073 AnimatorSet anim = new AnimatorSet();
2074 anim.playTogether(bounceAnims);
Jon Miranda64eb8ea2018-03-29 11:43:58 -07002075
Sunny Goyalefb7e842018-10-04 15:11:00 -07002076 int currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002077 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
2078 final Runnable startBounceAnimRunnable = anim::start;
2079
2080 if (newItemsScreenId != currentScreenId) {
2081 // We post the animation slightly delayed to prevent slowdowns
2082 // when we are loading right after we return to launcher.
2083 mWorkspace.postDelayed(new Runnable() {
2084 public void run() {
2085 if (mWorkspace != null) {
2086 AbstractFloatingView.closeAllOpenViews(Launcher.this, false);
2087
2088 mWorkspace.snapToPage(newScreenIndex);
2089 mWorkspace.postDelayed(startBounceAnimRunnable,
2090 NEW_APPS_ANIMATION_DELAY);
Winson Chung94d67682013-09-25 16:29:40 -07002091 }
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002092 }
2093 }, NEW_APPS_PAGE_MOVE_DELAY);
2094 } else {
2095 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07002096 }
Winson Chung64359a52013-07-08 17:17:08 -07002097 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002098 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002099 }
2100
Joe Onorato9c1289c2009-08-17 11:03:03 -04002101 /**
2102 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002103 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07002104 public void bindAppWidget(LauncherAppWidgetInfo item) {
2105 View view = inflateAppWidget(item);
2106 if (view != null) {
2107 mWorkspace.addInScreen(view, item);
2108 mWorkspace.requestLayout();
2109 }
2110 }
2111
2112 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002113 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302114 PendingAppWidgetHostView view =
2115 new PendingAppWidgetHostView(this, item, mIconCache, true);
2116 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002117 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002118 }
2119
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002120 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002121
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002122 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08002123
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002124 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2125 // If the provider is not ready, bind as a pending widget.
2126 appWidgetInfo = null;
2127 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2128 // The widget id is not valid. Try to find the widget based on the provider info.
2129 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
2130 } else {
2131 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
2132 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002133
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002134 // If the provider is ready, but the width is not yet restored, try to restore it.
2135 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
2136 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07002137 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002138 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
2139 + " belongs to component " + item.providerName
2140 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002141 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002142 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002143 }
Sunny Goyalff572272014-07-23 13:58:07 -07002144
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002145 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002146 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07002147 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2148 // Id has not been allocated yet. Allocate a new id.
2149 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
2150 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07002151
Sunny Goyald478c832016-04-01 12:04:16 -07002152 // Also try to bind the widget. If the bind fails, the user will be shown
2153 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08002154 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07002155 pendingInfo.spanX = item.spanX;
2156 pendingInfo.spanY = item.spanY;
2157 pendingInfo.minSpanX = item.minSpanX;
2158 pendingInfo.minSpanY = item.minSpanY;
2159 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07002160
2161 boolean isDirectConfig =
2162 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2163 if (isDirectConfig && item.bindOptions != null) {
2164 Bundle newOptions = item.bindOptions.getExtras();
2165 if (options != null) {
2166 newOptions.putAll(options);
2167 }
2168 options = newOptions;
2169 }
Sunny Goyald478c832016-04-01 12:04:16 -07002170 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2171 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002172
Sunny Goyal86df1382016-08-10 15:03:22 -07002173 // We tried to bind once. If we were not able to bind, we would need to
2174 // go through the permission dialog, which means we cannot skip the config
2175 // activity.
2176 item.bindOptions = null;
2177 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2178
Sunny Goyald478c832016-04-01 12:04:16 -07002179 // Bind succeeded
2180 if (success) {
2181 // If the widget has a configure activity, it is still needs to set it up,
2182 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002183 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002184 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2185 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002186 }
Sunny Goyald478c832016-04-01 12:04:16 -07002187
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002188 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002189 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002190 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002191 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002192 // The widget was marked as UI not ready, but there is no configure activity to
2193 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002194 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002195 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002196 }
Sunny Goyalff572272014-07-23 13:58:07 -07002197 }
2198
Sunny Goyald5462aa2016-11-22 16:52:39 +05302199 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002200 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002201 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002202 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002203 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Tony Wickham6a71a5b2018-08-21 11:40:23 -07002204 getModelWriter().deleteWidgetInfo(item, getAppWidgetHost());
Sunny Goyalb23980c2017-08-17 07:45:25 -07002205 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002206 }
2207
Sunny Goyal233ee962015-08-03 13:05:01 -07002208 item.minSpanX = appWidgetInfo.minSpanX;
2209 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302210 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002211 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302212 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002213 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302214 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002215
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002216 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002217 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002218 }
2219
Sunny Goyalff572272014-07-23 13:58:07 -07002220 /**
2221 * Restores a pending widget.
2222 *
2223 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002224 */
Sunny Goyald478c832016-04-01 12:04:16 -07002225 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002226 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2227 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2228 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002229 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002230 }
2231
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002232 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002233 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002234 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2235 info.pendingItemInfo = null;
2236 }
Sunny Goyalff572272014-07-23 13:58:07 -07002237
Sunny Goyalba47faa2017-10-04 16:50:57 -07002238 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002239 view.reInflate();
Sunny Goyalba47faa2017-10-04 16:50:57 -07002240 }
2241
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002242 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002243 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002244 }
2245
Adam Cohen1462de32012-07-24 22:34:36 -07002246 public void onPageBoundSynchronously(int page) {
Sunny Goyalf4aceab2017-12-06 16:16:31 -08002247 mSynchronouslyBoundPage = page;
Adam Cohen1462de32012-07-24 22:34:36 -07002248 }
2249
Sunny Goyal527c7d32015-08-28 15:19:36 -07002250 @Override
2251 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2252 if (mPendingExecutor != null) {
2253 mPendingExecutor.markCompleted();
2254 }
2255 mPendingExecutor = executor;
Sunny Goyal60180b02018-02-07 12:56:30 -08002256 if (!isInState(ALL_APPS)) {
vadimtd4c90e12019-06-04 13:59:43 -07002257 mAppsView.getAppsStore().enableDeferUpdates(AllAppsStore.DEFER_UPDATES_NEXT_DRAW);
2258 mPendingExecutor.execute(() -> mAppsView.getAppsStore().disableDeferUpdates(
2259 AllAppsStore.DEFER_UPDATES_NEXT_DRAW));
Sunny Goyal60180b02018-02-07 12:56:30 -08002260 }
2261
Sunny Goyal527c7d32015-08-28 15:19:36 -07002262 executor.attachTo(this);
2263 }
2264
2265 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2266 if (mPendingExecutor == executor) {
2267 mPendingExecutor = null;
2268 }
2269 }
2270
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002271 @Override
2272 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
Sunny Goyal6001ea22018-05-10 16:31:00 -07002273 AlphaProperty property = mDragLayer.getAlphaProperty(ALPHA_INDEX_LAUNCHER_LOAD);
2274 if (property.getValue() < 1) {
2275 ObjectAnimator anim = ObjectAnimator.ofFloat(property, MultiValueAlpha.VALUE, 1);
2276 if (executor != null) {
2277 anim.addListener(new AnimatorListenerAdapter() {
2278 @Override
2279 public void onAnimationEnd(Animator animation) {
2280 executor.onLoadAnimationCompleted();
2281 }
2282 });
2283 }
2284 anim.start();
Sunny Goyal29947f02017-12-18 13:49:44 -08002285 } else if (executor != null) {
2286 executor.onLoadAnimationCompleted();
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002287 }
2288 }
2289
Joe Onorato9c1289c2009-08-17 11:03:03 -04002290 /**
2291 * Callback saying that there aren't any more items to bind.
2292 *
2293 * Implementation of the method from LauncherModel.Callbacks.
2294 */
Tonyf80e8932018-12-21 11:58:04 -08002295 public void finishBindingItems(int pageBoundFirst) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002296 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002297 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002298
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002299 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002300
Sunny Goyal2100c782016-08-22 16:00:03 -07002301 if (mPendingActivityResult != null) {
2302 handleActivityResult(mPendingActivityResult.requestCode,
2303 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2304 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002305 }
2306
Sunny Goyala474a9b2017-05-04 16:47:11 -07002307 InstallShortcutReceiver.disableAndFlushInstallQueue(
2308 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002309
Tonyf80e8932018-12-21 11:58:04 -08002310 // When undoing the removal of the last item on a page, return to that page.
Tony Wickham03f27012019-04-25 14:22:54 -07002311 // Since we are just resetting the current page without user interaction,
2312 // override the previous page so we don't log the page switch.
2313 mWorkspace.setCurrentPage(pageBoundFirst, pageBoundFirst /* overridePrevPage */);
Tony Wickham6a71a5b2018-08-21 11:40:23 -07002314
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002315 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002316 }
2317
Winson Chunga2413752012-04-03 14:22:34 -07002318 private boolean canRunNewAppsAnimation() {
2319 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002320 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002321 }
2322
Winson Chungc9168342013-06-26 14:54:55 -07002323 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002324 ValueAnimator bounceAnim = new PropertyListBuilder().alpha(1).scale(1).build(v)
2325 .setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
Winson Chungc9168342013-06-26 14:54:55 -07002326 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002327 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002328 return bounceAnim;
2329 }
2330
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002331 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002332 * Add the icons for all apps.
2333 *
2334 * Implementation of the method from LauncherModel.Callbacks.
2335 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002336 public void bindAllApplications(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002337 mAppsView.getAppsStore().setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002338 }
2339
2340 /**
Zak Cohen658c67a2018-10-19 14:21:05 -07002341 * Copies LauncherModel's map of activities to shortcut counts to Launcher's. This is necessary
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002342 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2343 */
2344 @Override
Zak Cohen658c67a2018-10-19 14:21:05 -07002345 public void bindDeepShortcutMap(HashMap<ComponentKey, Integer> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002346 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002347 }
2348
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002349 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002350 * A package was updated.
2351 *
2352 * Implementation of the method from LauncherModel.Callbacks.
2353 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002354 @Override
2355 public void bindAppsAddedOrUpdated(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002356 mAppsView.getAppsStore().addOrUpdateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002357 }
2358
Sunny Goyal4390ace2014-10-13 11:33:11 -07002359 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002360 public void bindPromiseAppProgressUpdated(PromiseAppInfo app) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002361 mAppsView.getAppsStore().updatePromiseAppProgress(app);
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002362 }
2363
2364 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002365 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002366 mWorkspace.widgetsRestored(widgets);
2367 }
2368
Joe Onorato9c1289c2009-08-17 11:03:03 -04002369 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002370 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002371 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002372 *
2373 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002374 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002375 @Override
Sunny Goyal95899162019-03-27 16:03:06 -07002376 public void bindWorkspaceItemsChanged(ArrayList<WorkspaceItemInfo> updated) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002377 if (!updated.isEmpty()) {
2378 mWorkspace.updateShortcuts(updated);
2379 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002380 }
2381
2382 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002383 * Update the state of a package, typically related to install state.
2384 *
2385 * Implementation of the method from LauncherModel.Callbacks.
2386 */
Sunny Goyale755d462014-07-22 13:48:29 -07002387 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002388 public void bindRestoreItemsChange(HashSet<ItemInfo> updates) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07002389 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07002390 }
2391
2392 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002393 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07002394 * in addition to specific applications to remove, the reason being that
2395 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002396 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07002397 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002398 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07002399 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002400 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002401 mWorkspace.removeItemsByMatcher(matcher);
2402 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002403 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002404
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002405 @Override
2406 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002407 mAppsView.getAppsStore().removeApps(appInfos);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002408 }
Joe Onoratobe386092009-11-17 17:32:16 -08002409
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002410 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002411 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
2412 mPopupDataProvider.setAllWidgets(allWidgets);
Tony Wickham26b17462017-03-20 17:12:24 -07002413 }
2414
Tony Wickham86222d22017-03-29 15:30:43 -07002415 /**
2416 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
2417 * refreshes the widgets and shortcuts associated with the given package/user
2418 */
2419 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07002420 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002421 }
2422
Winson Chung80baf5a2010-08-09 16:03:15 -07002423 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002424 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08002425 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002426 @Override
2427 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
2428 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002429
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002430 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
2431 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07002432 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002433 writer.println(prefix + " Homescreen " + i);
2434
2435 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
2436 for (int j = 0; j < layout.getChildCount(); j++) {
2437 Object tag = layout.getChildAt(j).getTag();
2438 if (tag != null) {
2439 writer.println(prefix + " " + tag.toString());
2440 }
2441 }
2442 }
2443
2444 writer.println(prefix + " Hotseat");
Sunny Goyal876e4622018-11-02 13:50:40 -07002445 ViewGroup layout = mHotseat.getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07002446 for (int j = 0; j < layout.getChildCount(); j++) {
2447 Object tag = layout.getChildAt(j).getTag();
2448 if (tag != null) {
2449 writer.println(prefix + " " + tag.toString());
2450 }
Adam Cohen4caf2982013-08-20 18:54:31 -07002451 }
Sunny Goyala1365452015-10-01 15:46:24 -07002452 }
2453
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002454 writer.println(prefix + "Misc:");
2455 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
2456 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
2457 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
Sunny Goyale43d00d2018-05-14 14:23:18 -07002458 writer.println(" mRotationHelper: " + mRotationHelper);
Sunny Goyalcb2c5052018-10-15 12:30:29 -07002459 // Extra logging for b/116853349
Hyunyoung Songa310a802019-04-25 13:57:16 -07002460 mDragLayer.dump(prefix, writer);
2461 mStateManager.dump(prefix, writer);
Sunny Goyale43d00d2018-05-14 14:23:18 -07002462 dumpMisc(writer);
Sunny Goyal2bba1902018-02-27 12:20:50 -08002463
2464 try {
2465 FileLog.flushAll(writer);
2466 } catch (Exception e) {
2467 // Ignore
2468 }
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002469
2470 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002471
Adam Cohen9211d422014-10-07 18:14:53 -07002472 if (mLauncherCallbacks != null) {
2473 mLauncherCallbacks.dump(prefix, fd, writer, args);
2474 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002475 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002476
Sunny Goyal66b24572016-09-21 15:57:55 -07002477 @Override
Sunny Goyal66b24572016-09-21 15:57:55 -07002478 public void onProvideKeyboardShortcuts(
2479 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
2480
2481 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002482 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002483 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
2484 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
Vadim Tryshev74c261c2018-06-12 13:44:43 -07002485 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.widget_button_text),
2486 KeyEvent.KEYCODE_W, KeyEvent.META_CTRL_ON));
Sunny Goyal66b24572016-09-21 15:57:55 -07002487 }
Vadim Tryshev3455f8d2018-05-11 20:02:20 -07002488 final View currentFocus = getCurrentFocus();
2489 if (currentFocus != null) {
2490 if (new CustomActionsPopup(this, currentFocus).canShow()) {
2491 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
2492 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
2493 }
2494 if (currentFocus.getTag() instanceof ItemInfo
2495 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
2496 shortcutInfos.add(new KeyboardShortcutInfo(
2497 getString(R.string.shortcuts_menu_with_notifications_description),
2498 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
2499 }
Sunny Goyal66b24572016-09-21 15:57:55 -07002500 }
2501 if (!shortcutInfos.isEmpty()) {
2502 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
2503 }
2504
2505 super.onProvideKeyboardShortcuts(data, menu, deviceId);
2506 }
2507
2508 @Override
2509 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
2510 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
2511 switch (keyCode) {
2512 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002513 if (isInState(NORMAL)) {
2514 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07002515 return true;
2516 }
2517 break;
2518 case KeyEvent.KEYCODE_S: {
2519 View focusedView = getCurrentFocus();
2520 if (focusedView instanceof BubbleTextView
2521 && focusedView.getTag() instanceof ItemInfo
2522 && mAccessibilityDelegate.performAction(focusedView,
2523 (ItemInfo) focusedView.getTag(),
2524 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal00ac9202017-11-09 15:24:06 -08002525 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07002526 return true;
2527 }
2528 break;
2529 }
2530 case KeyEvent.KEYCODE_O:
2531 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
2532 return true;
2533 }
2534 break;
Vadim Tryshev74c261c2018-06-12 13:44:43 -07002535 case KeyEvent.KEYCODE_W:
2536 if (isInState(NORMAL)) {
2537 OptionsPopupView.openWidgets(this);
2538 return true;
2539 }
2540 break;
Sunny Goyal66b24572016-09-21 15:57:55 -07002541 }
2542 }
2543 return super.onKeyShortcut(keyCode, event);
2544 }
2545
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002546 @Override
2547 public boolean onKeyUp(int keyCode, KeyEvent event) {
2548 if (keyCode == KeyEvent.KEYCODE_MENU) {
2549 // KEYCODE_MENU is sent by some tests, for example
2550 // LauncherJankTests#testWidgetsContainerFling. Don't just remove its handling.
2551 if (!mDragController.isDragging() && !mWorkspace.isSwitchingState() &&
2552 isInState(NORMAL)) {
2553 // Close any open floating views.
2554 AbstractFloatingView.closeAllOpenViews(this);
2555
2556 // Setting the touch point to (-1, -1) will show the options popup in the center of
2557 // the screen.
Sunny Goyal2fd7a8b2018-03-30 17:10:13 -07002558 OptionsPopupView.showDefaultOptions(this, -1, -1);
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002559 }
2560 return true;
2561 }
2562 return super.onKeyUp(keyCode, event);
2563 }
2564
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002565 public static Launcher getLauncher(Context context) {
Sunny Goyal87b5eb62018-07-03 15:53:39 -07002566 return (Launcher) fromContext(context);
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002567 }
2568
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002569 /**
Sunny Goyalab770a12018-11-14 15:17:26 -08002570 * Just a wrapper around the type cast to allow easier tracking of calls.
2571 */
2572 public static <T extends Launcher> T cast(ActivityContext activityContext) {
2573 return (T) activityContext;
2574 }
2575
2576 /**
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002577 * Callback for listening for onResume
2578 */
2579 public interface OnResumeCallback {
2580
2581 void onLauncherResume();
2582 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002583}