blob: 92e9509e0b07cd3ad5bda3e1aec6604715b1ea50 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080019import static android.content.pm.ActivityInfo.CONFIG_ORIENTATION;
20import static android.content.pm.ActivityInfo.CONFIG_SCREEN_SIZE;
vadimt00d42552018-12-11 17:59:36 -080021
Pinyao Tinga74b63a2019-07-17 23:26:06 -070022import static com.android.launcher3.AbstractFloatingView.TYPE_ALL;
23import static com.android.launcher3.AbstractFloatingView.TYPE_REBIND_SAFE;
Tony Wickham6a71a5b2018-08-21 11:40:23 -070024import static com.android.launcher3.AbstractFloatingView.TYPE_SNACKBAR;
Sunny Goyal4c7f2152017-10-17 17:17:16 -070025import static com.android.launcher3.LauncherAnimUtils.SPRING_LOADED_EXIT_DELAY;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -070026import static com.android.launcher3.LauncherState.ALL_APPS;
27import static com.android.launcher3.LauncherState.NORMAL;
Govinda Wassermanc06e1512019-04-09 09:56:53 -040028import static com.android.launcher3.LauncherState.OVERVIEW;
29import static com.android.launcher3.LauncherState.OVERVIEW_PEEK;
Sunny Goyal6001ea22018-05-10 16:31:00 -070030import static com.android.launcher3.dragndrop.DragLayer.ALPHA_INDEX_LAUNCHER_LOAD;
Sunny Goyalaeb16432017-10-16 11:46:41 -070031import static com.android.launcher3.logging.LoggerUtils.newContainerTarget;
Hyunyoung Song46d07f72018-05-22 15:41:25 -070032import static com.android.launcher3.logging.LoggerUtils.newTarget;
Sunny Goyalbe8c3402019-05-22 15:17:52 -070033import static com.android.launcher3.states.RotationHelper.REQUEST_NONE;
vadimt00d42552018-12-11 17:59:36 -080034import static com.android.launcher3.util.RaceConditionTracker.ENTER;
35import static com.android.launcher3.util.RaceConditionTracker.EXIT;
Sunny Goyal326403e2017-10-02 12:45:10 -070036
Gilles Debunnedd6c9922010-10-25 11:23:41 -070037import android.animation.Animator;
Sunny Goyal6001ea22018-05-10 16:31:00 -070038import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070039import android.animation.AnimatorSet;
Sunny Goyal6001ea22018-05-10 16:31:00 -070040import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070041import android.animation.ValueAnimator;
Adam Cohen0b395352014-06-09 22:54:36 +000042import android.annotation.TargetApi;
Tony Wickham005df0b2018-02-13 11:28:12 -080043import android.app.ActivityOptions;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080044import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070045import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080047import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080048import android.content.ComponentCallbacks2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070051import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070052import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070053import android.content.SharedPreferences;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054import android.content.pm.PackageManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080055import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070056import android.database.sqlite.SQLiteDatabase;
Sunny Goyal59d086c2018-05-07 17:31:40 -070057import android.graphics.Point;
Sunny Goyalae6e3182019-04-30 12:04:37 -070058import android.graphics.Rect;
Adam Cohen0f668f32014-09-08 19:54:17 +020059import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.os.Bundle;
Hyunyoung Songbd6fba92018-05-16 15:54:31 -070061import android.os.Handler;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070062import android.os.Parcelable;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080063import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070064import android.os.StrictMode;
Michael Jurkaa33411c2012-06-14 16:18:21 -070065import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070067import android.util.Log;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070068import android.util.SparseArray;
Sunny Goyal59d086c2018-05-07 17:31:40 -070069import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070070import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070071import android.view.KeyboardShortcutGroup;
72import android.view.KeyboardShortcutInfo;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080073import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -070074import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070075import android.view.View;
Adam Cohen96d30a12013-07-16 18:13:21 -070076import android.view.ViewGroup;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080077import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070078import android.view.animation.OvershootInterpolator;
Adam Cohen96d30a12013-07-16 18:13:21 -070079import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070080
Sunny Goyal11a50742019-08-07 09:24:26 -070081import androidx.annotation.Nullable;
82
Sunny Goyal651077b2014-06-30 14:15:31 -070083import com.android.launcher3.DropTarget.DragObject;
Sunny Goyalae502842016-06-17 08:43:56 -070084import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070085import com.android.launcher3.allapps.AllAppsContainerView;
vadimtd4c90e12019-06-04 13:59:43 -070086import com.android.launcher3.allapps.AllAppsStore;
Hyunyoung Song645764e2016-06-06 14:19:02 -070087import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal7ede6112017-12-05 15:11:21 -080088import com.android.launcher3.allapps.DiscoveryBounce;
Sunny Goyalf0b6db72018-08-13 16:10:14 -070089import com.android.launcher3.anim.PropertyListBuilder;
Sunny Goyalffe83f12014-08-14 17:39:34 -070090import com.android.launcher3.compat.AppWidgetManagerCompat;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -070091import com.android.launcher3.compat.LauncherAppsCompatVO;
Tony Wickhame0c33232016-02-08 11:37:04 -080092import com.android.launcher3.config.FeatureFlags;
vadimt00d42552018-12-11 17:59:36 -080093import com.android.launcher3.dot.DotInfo;
Vadim Tryshevfedca432015-08-19 17:55:02 -070094import com.android.launcher3.dragndrop.DragController;
95import com.android.launcher3.dragndrop.DragLayer;
96import com.android.launcher3.dragndrop.DragView;
Sunny Goyal11a50742019-08-07 09:24:26 -070097import com.android.launcher3.folder.FolderGridOrganizer;
Sunny Goyal26119432016-02-18 22:09:23 +000098import com.android.launcher3.folder.FolderIcon;
Sunny Goyalae6e3182019-04-30 12:04:37 -070099import com.android.launcher3.graphics.RotationMode;
Sunny Goyalf840f102018-09-21 14:41:05 -0700100import com.android.launcher3.icons.IconCache;
Sunny Goyal66b24572016-09-21 15:57:55 -0700101import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700102import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700103import com.android.launcher3.logging.FileLog;
Hyunyoung Songfc007472018-10-25 14:09:50 -0700104import com.android.launcher3.logging.StatsLogUtils;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700105import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700106import com.android.launcher3.logging.UserEventDispatcher.UserEventDelegate;
Sunny Goyal369212a2019-03-26 15:03:57 -0700107import com.android.launcher3.model.AppLaunchTracker;
Sunny Goyal87dcde62019-07-17 20:35:56 -0700108import com.android.launcher3.model.BgDataModel.Callbacks;
Sunny Goyala535ae42017-02-27 10:07:13 -0800109import com.android.launcher3.model.ModelWriter;
Sunny Goyala535ae42017-02-27 10:07:13 -0800110import com.android.launcher3.notification.NotificationListener;
Tony Wickham540913e2017-01-23 11:47:51 -0800111import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -0800112import com.android.launcher3.popup.PopupDataProvider;
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700113import com.android.launcher3.qsb.QsbContainerView;
Sunny Goyalf8088ee2017-11-10 14:52:00 -0800114import com.android.launcher3.states.InternalStateHandler;
Sunny Goyal623eddd2018-03-02 12:24:41 -0800115import com.android.launcher3.states.RotationHelper;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800116import com.android.launcher3.touch.ItemClickHandler;
Sunny Goyala5ace712017-11-15 17:12:51 -0800117import com.android.launcher3.uioverrides.UiFactory;
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700118import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530119import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
120import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
Sunny Goyalaeb16432017-10-16 11:46:41 -0700121import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyal2100c782016-08-22 16:00:03 -0700122import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700123import com.android.launcher3.util.ComponentKey;
Sunny Goyalefb7e842018-10-04 15:11:00 -0700124import com.android.launcher3.util.IntArray;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700125import com.android.launcher3.util.ItemInfoMatcher;
Sunny Goyal6001ea22018-05-10 16:31:00 -0700126import com.android.launcher3.util.MultiValueAlpha;
127import com.android.launcher3.util.MultiValueAlpha.AlphaProperty;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700128import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800129import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700130import com.android.launcher3.util.PendingRequestArgs;
vadimt00d42552018-12-11 17:59:36 -0800131import com.android.launcher3.util.RaceConditionTracker;
Pinyao Ting49a3e692019-07-26 12:28:38 -0700132import com.android.launcher3.util.ShortcutUtil;
Sunny Goyal8392c822017-06-20 10:03:56 -0700133import com.android.launcher3.util.SystemUiController;
Mario Bertschlera6936942017-05-31 14:48:19 -0700134import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700135import com.android.launcher3.util.Thunk;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700136import com.android.launcher3.util.TraceHelper;
Sunny Goyal326403e2017-10-02 12:45:10 -0700137import com.android.launcher3.util.UiThreadHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700138import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyalab770a12018-11-14 15:17:26 -0800139import com.android.launcher3.views.ActivityContext;
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800140import com.android.launcher3.views.OptionsPopupView;
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400141import com.android.launcher3.views.ScrimView;
Sunny Goyal29947f02017-12-18 13:49:44 -0800142import com.android.launcher3.widget.LauncherAppWidgetHostView;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800143import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700144import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal29947f02017-12-18 13:49:44 -0800145import com.android.launcher3.widget.PendingAppWidgetHostView;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800146import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700147import com.android.launcher3.widget.WidgetHostViewLoader;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700148import com.android.launcher3.widget.WidgetListRowEntry;
149import com.android.launcher3.widget.WidgetsFullSheet;
Sunny Goyal952e63d2017-08-16 04:59:08 -0700150import com.android.launcher3.widget.custom.CustomWidgetParser;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700151
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700152import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700153import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700154import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700155import java.util.Collection;
Zak Cohen658c67a2018-10-19 14:21:05 -0700156import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700157import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700158import java.util.List;
Sunny Goyalaae6fbb2019-01-31 16:05:58 -0800159import java.util.function.Predicate;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700160
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161/**
162 * Default launcher application.
163 */
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700164public class Launcher extends BaseDraggingActivity implements LauncherExterns,
Sunny Goyal87dcde62019-07-17 20:35:56 -0700165 Callbacks, LauncherProviderChangeListener, UserEventDelegate,
Hyunyoung Song31971a32019-01-27 12:31:12 -0800166 InvariantDeviceProfile.OnIDPChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700167 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700168 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169
Winson Chunga2413752012-04-03 14:22:34 -0700170 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700171
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700173 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700174
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700175 private static final int REQUEST_PICK_APPWIDGET = 9;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176
Michael Jurka8b805b12012-04-18 14:23:14 -0700177 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800178 public static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
179 public static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700180
Jon Mirandac476d6e2017-05-04 16:30:01 -0700181 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700182
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700183 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
184
Mathew Inwood876a8462013-06-14 14:12:41 +0100185 /**
186 * IntentStarter uses request codes starting with this. This must be greater than all activity
187 * request codes used internally.
188 */
189 protected static final int REQUEST_LAST = 100;
190
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191 // Type: int
192 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700193 // Type: int
194 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700195 // Type: PendingRequestArgs
196 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
Sunny Goyaldedda052019-05-14 15:23:48 -0700197 // Type: int
198 private static final String RUNTIME_STATE_PENDING_REQUEST_CODE = "launcher.request_code";
Sunny Goyal2100c782016-08-22 16:00:03 -0700199 // Type: ActivityResultInfo
200 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700201 // Type: SparseArray<Parcelable>
202 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
vadimt00d42552018-12-11 17:59:36 -0800203 public static final String ON_CREATE_EVT = "Launcher.onCreate";
vadimtcb863752018-12-19 17:44:57 -0800204 private static final String ON_START_EVT = "Launcher.onStart";
205 private static final String ON_RESUME_EVT = "Launcher.onResume";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700207 private LauncherStateManager mStateManager;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700208
Adam Cohenad4e15c2013-10-17 16:21:35 -0700209 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210
Winson Chunga2413752012-04-03 14:22:34 -0700211 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700212 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
213 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700214 @Thunk
215 static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700216
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400217 private static final int APPS_VIEW_ALPHA_CHANNEL_INDEX = 1;
218 private static final int SCRIM_VIEW_ALPHA_CHANNEL_INDEX = 0;
219
Jon Miranda54441f52018-01-24 15:38:25 -0800220 private LauncherAppTransitionManager mAppTransitionManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800221 private Configuration mOldConfig;
Jon Miranda54441f52018-01-24 15:38:25 -0800222
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700223 @Thunk
224 Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700225 private View mLauncherView;
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700226 @Thunk
227 DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800228 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700229
Sunny Goyalffe83f12014-08-14 17:39:34 -0700230 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700231 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700232
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700233 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700234
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700235 @Thunk
236 Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700237
Sunny Goyal47328fd2016-05-25 18:56:41 -0700238 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700239
240 // Main container view for the all apps screen.
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700241 @Thunk
242 AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700243 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700244
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400245 // Scrim view for the all apps and overview state.
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700246 @Thunk
247 ScrimView mScrimView;
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400248
Winson Chung8ae41982017-11-10 11:42:13 -0800249 // UI and state for the overview panel
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800250 private View mOverviewPanel;
Winson Chung8ae41982017-11-10 11:42:13 -0800251
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700252 @Thunk
253 boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400254
Winson Chung0b70cd42019-06-17 22:34:33 -0700255 private ArrayList<OnResumeCallback> mOnResumeCallbacks = new ArrayList<>();
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700256
Sunny Goyal527c7d32015-08-28 15:19:36 -0700257 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700258
Joe Onorato9c1289c2009-08-17 11:03:03 -0400259 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800260 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800261 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700262 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400263
Tony Wickham010d2552017-01-20 08:15:28 -0800264 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700265
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800266 private int mSynchronouslyBoundPage = PagedView.INVALID_PAGE;
Adam Cohen1462de32012-07-24 22:34:36 -0700267
Winson Chung46353de2012-02-16 14:05:10 -0800268 // We only want to get the SharedPreferences once since it does an FS stat each time we get
269 // it from the context.
270 private SharedPreferences mSharedPrefs;
271
Sunny Goyal2100c782016-08-22 16:00:03 -0700272 // Activity result which needs to be processed after workspace has loaded.
273 private ActivityResultInfo mPendingActivityResult;
274 /**
275 * Holds extra information required to handle a result from an external call, like
276 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
277 */
278 private PendingRequestArgs mPendingRequestArgs;
Sunny Goyaldedda052019-05-14 15:23:48 -0700279 // Request id for any pending activity result
280 private int mPendingActivityRequestCode = -1;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800281
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700282 public ViewGroupFocusHelper mFocusHandler;
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700283
Sunny Goyal623eddd2018-03-02 12:24:41 -0800284 private RotationHelper mRotationHelper;
Sunny Goyal5743f862019-03-28 15:35:32 -0700285 private Runnable mCancelTouchController;
Sunny Goyal7779d622015-06-11 16:18:39 -0700286
Sunny Goyal2db53422019-03-01 16:06:12 -0800287 final Handler mHandler = new Handler();
Sunny Goyala002c6c2019-02-12 16:20:10 -0800288 private final Runnable mHandleDeferredResume = this::handleDeferredResume;
Sunny Goyale9c6f4c2019-05-30 11:35:25 -0700289 private boolean mDeferredResumePending;
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700290
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400291 private float mCurrentAssistantVisibility = 0f;
292
Sunny Goyalae6e3182019-04-30 12:04:37 -0700293 private DeviceProfile mStableDeviceProfile;
294 private RotationMode mRotationMode = RotationMode.NORMAL;
295
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800296 @Override
297 protected void onCreate(Bundle savedInstanceState) {
vadimt00d42552018-12-11 17:59:36 -0800298 RaceConditionTracker.onEvent(ON_CREATE_EVT, ENTER);
Winson Chunga2413752012-04-03 14:22:34 -0700299 if (DEBUG_STRICT_MODE) {
300 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
301 .detectDiskReads()
302 .detectDiskWrites()
303 .detectNetwork() // or .detectAll() for all detectable problems
304 .penaltyLog()
305 .build());
306 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
307 .detectLeakedSqlLiteObjects()
308 .detectLeakedClosableObjects()
309 .penaltyLog()
310 .penaltyDeath()
311 .build());
312 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700313 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700314
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800315 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700316 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400317
Sunny Goyal87f784c2017-01-11 10:48:34 -0800318 LauncherAppState app = LauncherAppState.getInstance(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800319 mOldConfig = new Configuration(getResources().getConfiguration());
Sunny Goyal0e7724c2018-02-22 13:22:21 -0800320 mModel = app.setLauncher(this);
Sunny Goyal41d51a02019-05-14 09:44:34 -0700321 mRotationHelper = new RotationHelper(this);
Hyunyoung Song31971a32019-01-27 12:31:12 -0800322 InvariantDeviceProfile idp = app.getInvariantDeviceProfile();
323 initDeviceProfile(idp);
324 idp.addOnChangeListener(this);
Sunny Goyalf7258242015-10-19 16:59:07 -0700325 mSharedPrefs = Utilities.getPrefs(this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800326 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700327 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700328
Joe Onorato41a12d22009-10-31 18:30:00 -0400329 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700330 mAllAppsController = new AllAppsTransitionController(this);
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800331 mStateManager = new LauncherStateManager(this);
Tracy Zhou8b23c6f2018-04-30 17:13:27 -0700332 UiFactory.onCreate(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700333
Sunny Goyalffe83f12014-08-14 17:39:34 -0700334 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700335
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700336 mAppWidgetHost = new LauncherAppWidgetHost(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700337 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700338
Sunny Goyal60820d72017-05-09 12:40:11 -0700339 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700340
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800341 setupViews();
Tony Wickham010d2552017-01-20 08:15:28 -0800342 mPopupDataProvider = new PopupDataProvider(this);
343
Sunny Goyal90548432018-04-04 17:17:00 -0700344 mAppTransitionManager = LauncherAppTransitionManager.newInstance(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800345
Sunny Goyald3864fa2017-11-14 15:06:36 -0800346 boolean internalStateHandled = InternalStateHandler.handleCreate(this, getIntent());
347 if (internalStateHandled) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800348 if (savedInstanceState != null) {
349 // InternalStateHandler has already set the appropriate state.
350 // We dont need to do anything.
351 savedInstanceState.remove(RUNTIME_STATE);
352 }
353 }
354 restoreState(savedInstanceState);
Sunny Goyalbbece862019-03-01 13:13:52 -0800355 mStateManager.reapplyState();
Winson Chungb63b44c2017-11-10 17:54:44 -0800356
Sunny Goyal2100c782016-08-22 16:00:03 -0700357 // We only load the page synchronously if the user rotates (or triggers a
358 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700359 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
360 if (savedInstanceState != null) {
361 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
362 }
Sunny Goyald3864fa2017-11-14 15:06:36 -0800363
Sunny Goyalfe770c92016-09-27 14:38:58 -0700364 if (!mModel.startLoader(currentScreen)) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800365 if (!internalStateHandled) {
366 // If we are not binding synchronously, show a fade in animation when
367 // the first page bind completes.
Sunny Goyal6001ea22018-05-10 16:31:00 -0700368 mDragLayer.getAlphaProperty(ALPHA_INDEX_LAUNCHER_LOAD).setValue(0);
Sunny Goyald3864fa2017-11-14 15:06:36 -0800369 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700370 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700371 // Pages bound synchronously.
372 mWorkspace.setCurrentPage(currentScreen);
373
Sunny Goyal2100c782016-08-22 16:00:03 -0700374 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800375 }
376
377 // For handling default keys
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800378 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800379
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800380 setContentView(mLauncherView);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800381 getRootView().dispatchInsets();
Mario Bertschlera6936942017-05-31 14:48:19 -0700382
Jon Miranda7fda2852017-07-19 16:07:01 -0700383 // Listen for broadcasts
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700384 registerReceiver(mScreenOffReceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));
Sunny Goyala616d2b2017-06-12 13:54:01 -0700385
Sunny Goyal8392c822017-06-20 10:03:56 -0700386 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
387 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700388
389 if (mLauncherCallbacks != null) {
390 mLauncherCallbacks.onCreate(savedInstanceState);
391 }
Sunny Goyal623eddd2018-03-02 12:24:41 -0800392 mRotationHelper.initialize();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700393
394 TraceHelper.endSection("Launcher-onCreate");
vadimt00d42552018-12-11 17:59:36 -0800395 RaceConditionTracker.onEvent(ON_CREATE_EVT, EXIT);
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400396 mStateManager.addStateListener(new LauncherStateManager.StateListener() {
397 @Override
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700398 public void onStateTransitionStart(LauncherState toState) {
399 }
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400400
401 @Override
402 public void onStateTransitionComplete(LauncherState finalState) {
403 float alpha = 1f - mCurrentAssistantVisibility;
404 if (finalState == NORMAL) {
405 mAppsView.getAlphaProperty(APPS_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
406 } else if (finalState == OVERVIEW || finalState == OVERVIEW_PEEK) {
407 mAppsView.getAlphaProperty(APPS_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
408 mScrimView.getAlphaProperty(SCRIM_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
409 } else {
410 mAppsView.getAlphaProperty(APPS_VIEW_ALPHA_CHANNEL_INDEX).setValue(1f);
411 mScrimView.getAlphaProperty(SCRIM_VIEW_ALPHA_CHANNEL_INDEX).setValue(1f);
412 }
413 }
414 });
Adam Cohen9211d422014-10-07 18:14:53 -0700415 }
416
Sunny Goyal7779d622015-06-11 16:18:39 -0700417 @Override
Winson Chung8eb49e02018-07-16 13:33:31 -0700418 public void onEnterAnimationComplete() {
419 super.onEnterAnimationComplete();
420 UiFactory.onEnterAnimationComplete(this);
arangelovad783052018-08-17 14:46:45 +0100421 mAllAppsController.highlightWorkTabIfNecessary();
Sunny Goyalbe8c3402019-05-22 15:17:52 -0700422 mRotationHelper.setCurrentTransitionRequest(REQUEST_NONE);
Winson Chung8eb49e02018-07-16 13:33:31 -0700423 }
424
425 @Override
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800426 public void onConfigurationChanged(Configuration newConfig) {
427 int diff = newConfig.diff(mOldConfig);
Jon Mirandaaeb4dd02018-07-11 14:16:23 -0700428
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800429 if ((diff & (CONFIG_ORIENTATION | CONFIG_SCREEN_SIZE)) != 0) {
Sunny Goyal371ea052019-03-06 15:38:32 -0800430 onIdpChanged(mDeviceProfile.inv);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800431 }
432
433 mOldConfig.setTo(newConfig);
Tracy Zhoua706f002018-03-28 13:55:19 -0700434 UiFactory.onLauncherStateOrResumeChanged(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800435 super.onConfigurationChanged(newConfig);
436 }
437
Sunny Goyal3e195d72019-06-19 14:24:38 -0700438 public void reload() {
439 onIdpChanged(mDeviceProfile.inv);
440 }
441
Sunny Goyal41d51a02019-05-14 09:44:34 -0700442 private boolean supportsFakeLandscapeUI() {
443 return FeatureFlags.FAKE_LANDSCAPE_UI.get() && !mRotationHelper.homeScreenCanRotate();
444 }
445
Sunny Goyal605bcf32018-03-02 15:16:12 -0800446 @Override
Sunny Goyal41d51a02019-05-14 09:44:34 -0700447 public void reapplyUi() {
448 if (supportsFakeLandscapeUI()) {
Sunny Goyal7a39b1b2019-05-02 13:39:16 -0700449 mRotationMode = mStableDeviceProfile == null
450 ? RotationMode.NORMAL : UiFactory.getRotationMode(mDeviceProfile);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700451 }
Sunny Goyal59d086c2018-05-07 17:31:40 -0700452 getRootView().dispatchInsets();
453 getStateManager().reapplyState(true /* cancelCurrentAnimation */);
454 }
455
456 @Override
Sunny Goyal605bcf32018-03-02 15:16:12 -0800457 public void rebindModel() {
458 int currentPage = mWorkspace.getNextPage();
459 if (mModel.startLoader(currentPage)) {
460 mWorkspace.setCurrentPage(currentPage);
461 setWorkspaceLoading(true);
462 }
463 }
464
Hyunyoung Song31971a32019-01-27 12:31:12 -0800465 @Override
466 public void onIdpChanged(int changeFlags, InvariantDeviceProfile idp) {
Sunny Goyal371ea052019-03-06 15:38:32 -0800467 onIdpChanged(idp);
468 }
469
470 private void onIdpChanged(InvariantDeviceProfile idp) {
471 mUserEventDispatcher = null;
472
Sunny Goyalc8502c12019-06-10 12:14:47 -0700473 DeviceProfile oldWallpaperProfile = getWallpaperDeviceProfile();
Hyunyoung Song31971a32019-01-27 12:31:12 -0800474 initDeviceProfile(idp);
Sunny Goyal371ea052019-03-06 15:38:32 -0800475 dispatchDeviceProfileChanged();
476 reapplyUi();
477 mDragLayer.recreateControllers();
478
Sunny Goyal76ba0a32019-06-10 11:56:11 -0700479 // Calling onSaveInstanceState ensures that static cache used by listWidgets is
480 // initialized properly.
481 onSaveInstanceState(new Bundle());
Sunny Goyalc8502c12019-06-10 12:14:47 -0700482 if (oldWallpaperProfile != getWallpaperDeviceProfile()) {
483 rebindModel();
484 }
Hyunyoung Song31971a32019-01-27 12:31:12 -0800485 }
486
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400487 public void onAssistantVisibilityChanged(float visibility) {
488 mCurrentAssistantVisibility = visibility;
489 float alpha = 1f - visibility;
490 LauncherState state = mStateManager.getState();
491 if (state == NORMAL) {
492 mAppsView.getAlphaProperty(APPS_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
493 } else if (state == OVERVIEW || state == OVERVIEW_PEEK) {
494 mAppsView.getAlphaProperty(APPS_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
495 mScrimView.getAlphaProperty(SCRIM_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
496 }
497 }
498
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800499 private void initDeviceProfile(InvariantDeviceProfile idp) {
500 // Load configuration-specific DeviceProfile
Sunny Goyal59d086c2018-05-07 17:31:40 -0700501 mDeviceProfile = idp.getDeviceProfile(this);
Sunny Goyal8c48d8b2019-01-25 15:10:18 -0800502 if (isInMultiWindowMode()) {
Winson Chung13c1c2c2019-09-06 11:46:19 -0700503 // Note: Calls to getSize() can't rely on our cached DefaultDisplay since it can return
504 // the app window size
Sunny Goyal59d086c2018-05-07 17:31:40 -0700505 Display display = getWindowManager().getDefaultDisplay();
506 Point mwSize = new Point();
507 display.getSize(mwSize);
508 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
509 }
Sunny Goyalae6e3182019-04-30 12:04:37 -0700510
Sunny Goyalda14cf82019-05-30 10:31:09 -0700511 if (supportsFakeLandscapeUI() && mDeviceProfile.isVerticalBarLayout()) {
Sunny Goyalae6e3182019-04-30 12:04:37 -0700512 mStableDeviceProfile = mDeviceProfile.inv.portraitProfile;
Sunny Goyal7a39b1b2019-05-02 13:39:16 -0700513 mRotationMode = UiFactory.getRotationMode(mDeviceProfile);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700514 } else {
515 mStableDeviceProfile = null;
516 mRotationMode = RotationMode.NORMAL;
517 }
518
Sunny Goyalda14cf82019-05-30 10:31:09 -0700519 mRotationHelper.updateRotationAnimation();
Sunny Goyal59d086c2018-05-07 17:31:40 -0700520 onDeviceProfileInitiated();
Sunny Goyalae6e3182019-04-30 12:04:37 -0700521 mModelWriter = mModel.getWriter(getWallpaperDeviceProfile().isVerticalBarLayout(), true);
522 }
523
524 public void updateInsets(Rect insets) {
525 mDeviceProfile.updateInsets(insets);
526 if (mStableDeviceProfile != null) {
Sunny Goyal7a39b1b2019-05-02 13:39:16 -0700527 Rect r = mStableDeviceProfile.getInsets();
528 mRotationMode.mapInsets(this, insets, r);
529 mStableDeviceProfile.updateInsets(r);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700530 }
531 }
532
533 @Override
534 public RotationMode getRotationMode() {
535 return mRotationMode;
536 }
537
538 /**
539 * Device profile to be used by UI elements which are shown directly on top of the wallpaper
540 * and whose presentation is tied to the wallpaper (and physical device) and not the activity
541 * configuration.
542 */
543 @Override
544 public DeviceProfile getWallpaperDeviceProfile() {
545 return mStableDeviceProfile == null ? mDeviceProfile : mStableDeviceProfile;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800546 }
547
Sunny Goyal623eddd2018-03-02 12:24:41 -0800548 public RotationHelper getRotationHelper() {
549 return mRotationHelper;
550 }
551
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700552 public LauncherStateManager getStateManager() {
553 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700554 }
555
Mario Bertschler27288382017-05-24 15:35:09 -0700556 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700557 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800558 return mLauncherView.findViewById(id);
559 }
560
561 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700562 public void onAppWidgetHostReset() {
563 if (mAppWidgetHost != null) {
564 mAppWidgetHost.startListening();
565 }
566 }
567
Adam Cohen9211d422014-10-07 18:14:53 -0700568 private LauncherCallbacks mLauncherCallbacks;
569
Sunny Goyal32554d12015-12-03 15:31:25 -0800570 /**
571 * Call this after onCreate to set or clear overlay.
572 */
573 public void setLauncherOverlay(LauncherOverlay overlay) {
574 if (overlay != null) {
575 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
576 }
577 mWorkspace.setLauncherOverlay(overlay);
578 }
579
Adam Cohen9211d422014-10-07 18:14:53 -0700580 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
581 mLauncherCallbacks = callbacks;
582 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700583 }
584
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700585 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700586 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700587 if (mLauncherCallbacks != null) {
588 mLauncherCallbacks.onLauncherProviderChange();
589 }
590 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700591
Hyunyoung Song3f471442015-04-08 19:01:34 -0700592 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700593 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
594 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700595 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700596 }
597
Tony Wickham010d2552017-01-20 08:15:28 -0800598 public PopupDataProvider getPopupDataProvider() {
599 return mPopupDataProvider;
600 }
601
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700602 @Override
Tony Wickhamf34bee82018-12-03 18:11:39 -0800603 public DotInfo getDotInfoForItem(ItemInfo info) {
604 return mPopupDataProvider.getDotInfoForItem(info);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700605 }
606
607 @Override
608 public void invalidateParent(ItemInfo info) {
Jon Mirandae6f3fa42019-03-04 09:14:40 -0800609 if (info.container >= 0) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700610 View folderIcon = getWorkspace().getHomescreenIconByItemId(info.container);
Jon Mirandae6f3fa42019-03-04 09:14:40 -0800611 if (folderIcon instanceof FolderIcon && folderIcon.getTag() instanceof FolderInfo) {
Sunny Goyal11a50742019-08-07 09:24:26 -0700612 if (new FolderGridOrganizer(getDeviceProfile().inv)
613 .setFolderInfo((FolderInfo) folderIcon.getTag())
614 .isItemInPreview(info.rank)) {
Jon Mirandae6f3fa42019-03-04 09:14:40 -0800615 folderIcon.invalidate();
616 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700617 }
618 }
619 }
620
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000621 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700622 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
623 * a configuration step, this allows the proper animations to run after other transitions.
624 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700625 private int completeAdd(
Sunny Goyal2100c782016-08-22 16:00:03 -0700626 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
Sunny Goyalefb7e842018-10-04 15:11:00 -0700627 int screenId = info.screenId;
Sunny Goyal2100c782016-08-22 16:00:03 -0700628 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700629 // When the screen id represents an actual screen (as opposed to a rank) we make sure
630 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700631 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700632 }
Adam Cohendb364c32014-05-20 14:23:40 -0700633
Sunny Goyal2100c782016-08-22 16:00:03 -0700634 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800635 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700636 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700637 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800638 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700639 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700640 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700641 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700642 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700643 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700644 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700645 int widgetId = appWidgetId;
646 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700647 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700648 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700649 // Since the view was just bound, also launch the configure activity if needed
650 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
651 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800652 if (provider != null) {
653 new WidgetAddFlowHandler(provider)
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700654 .startConfigActivity(this, widgetInfo,
655 REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700656 }
657 }
658 break;
659 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800660 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700661
Adam Cohendb364c32014-05-20 14:23:40 -0700662 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800663 }
664
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800665 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700666 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700667 if (isWorkspaceLoading()) {
668 // process the result once the workspace has loaded.
669 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
670 return;
671 }
672 mPendingActivityResult = null;
673
Winson Chunge341d302013-08-16 14:31:00 -0700674 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700675 final PendingRequestArgs requestArgs = mPendingRequestArgs;
676 setWaitingForResult(null);
677 if (requestArgs == null) {
678 return;
679 }
680
681 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700682
Adam Cohenad4e15c2013-10-17 16:21:35 -0700683 Runnable exitSpringLoaded = new Runnable() {
684 @Override
685 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700686 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700687 }
688 };
689
Michael Jurka8b805b12012-04-18 14:23:14 -0700690 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700691 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700692 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700693 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
694 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700695 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700696 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700697 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700698 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700699 addAppWidgetImpl(
700 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800701 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700702 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700703 }
704 return;
705 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200706
Adam Cohened66b2b2012-01-23 17:28:51 -0800707 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700708 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700709
Adam Cohened66b2b2012-01-23 17:28:51 -0800710 // We have special handling for widgets
711 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800712 final int appWidgetId;
713 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
714 : -1;
715 if (widgetId < 0) {
716 appWidgetId = pendingAddWidgetId;
717 } else {
718 appWidgetId = widgetId;
719 }
720
Adam Cohenad4e15c2013-10-17 16:21:35 -0700721 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800722 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700723 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
724 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700725 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700726 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700727 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700728 @Override
729 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700730 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700731 }
732 };
Adam Cohendb364c32014-05-20 14:23:40 -0700733
Sunny Goyal2100c782016-08-22 16:00:03 -0700734 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
735 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
736 } else {
737 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
738 // When the screen id represents an actual screen (as opposed to a rank)
739 // we make sure that the drop page actually exists.
740 requestArgs.screenId =
741 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700742 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700743 final CellLayout dropLayout =
744 mWorkspace.getScreenWithId(requestArgs.screenId);
745
746 dropLayout.setDropPending(true);
747 final Runnable onComplete = new Runnable() {
748 @Override
749 public void run() {
750 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
751 dropLayout.setDropPending(false);
752 }
753 };
754 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
755 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700756 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800757 return;
758 }
759
Sunny Goyald478c832016-04-01 12:04:16 -0700760 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
761 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700762 if (resultCode == RESULT_OK) {
763 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700764 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700765 }
766 // Leave the widget in the pending state if the user canceled the configure.
767 return;
768 }
769
Sunny Goyalaad90582015-07-09 14:22:50 -0700770 if (requestCode == REQUEST_CREATE_SHORTCUT) {
771 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700772 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
773 completeAdd(requestCode, data, -1, requestArgs);
774 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
775 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
776
Sunny Goyalaad90582015-07-09 14:22:50 -0700777 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700778 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
779 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800780 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800781 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800782 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800783 }
784
785 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700786 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800787 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyaldedda052019-05-14 15:23:48 -0700788 mPendingActivityRequestCode = -1;
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800789 handleActivityResult(requestCode, resultCode, data);
790 if (mLauncherCallbacks != null) {
791 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
792 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800793 }
794
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700795 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700796 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600797 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700798 PendingRequestArgs pendingArgs = mPendingRequestArgs;
799 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
800 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
801 setWaitingForResult(null);
802
Sunny Goyal28c6b962015-10-12 11:42:05 -0700803 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700804 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700805 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700806 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700807 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700808 Intent intent = pendingArgs.getPendingIntent();
809
Sunny Goyal28c6b962015-10-12 11:42:05 -0700810 if (grantResults.length > 0
811 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal369212a2019-03-26 15:03:57 -0700812 startActivitySafely(v, intent, null, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700813 } else {
814 // TODO: Show a snack bar with link to settings
815 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700816 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700817 }
818 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600819 if (mLauncherCallbacks != null) {
820 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
821 grantResults);
822 }
823 }
824
Adam Cohendb364c32014-05-20 14:23:40 -0700825 /**
826 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
827 *
828 * @param screenId the screen id to check
829 * @return the new screen, or screenId if it exists
830 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700831 private int ensurePendingDropLayoutExists(int screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800832 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700833 if (dropLayout == null) {
834 // it's possible that the add screen was removed because it was
835 // empty and a re-bind occurred
836 mWorkspace.addExtraEmptyScreen();
837 return mWorkspace.commitExtraEmptyScreen();
838 } else {
839 return screenId;
840 }
841 }
842
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700843 @Thunk
844 void completeTwoStageWidgetDrop(
Sunny Goyal2100c782016-08-22 16:00:03 -0700845 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
846 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800847 Runnable onCompleteRunnable = null;
848 int animationType = 0;
849
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700850 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800851 if (resultCode == RESULT_OK) {
852 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
853 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800854 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700855 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800856 onCompleteRunnable = new Runnable() {
857 @Override
858 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700859 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700860 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800861 }
862 };
863 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800864 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800865 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800866 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700867 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700868 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700869 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
870 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700871 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700872 // The animated view may be null in the case of a rotation during widget configuration
873 onCompleteRunnable.run();
874 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800875 }
876
877 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700878 protected void onStop() {
879 super.onStop();
Winson Chung0b70cd42019-06-17 22:34:33 -0700880
Adam Cohen9211d422014-10-07 18:14:53 -0700881 if (mLauncherCallbacks != null) {
882 mLauncherCallbacks.onStop();
883 }
Becky Qiubfc2da12019-08-01 11:09:38 -0700884 logStopAndResume(Action.Command.STOP);
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700885
886 mAppWidgetHost.setListenIfResumed(false);
887
Tony Wickham010d2552017-01-20 08:15:28 -0800888 NotificationListener.removeNotificationsChangedListener();
Sunny Goyalaf161fd2018-02-05 16:34:57 -0800889 getStateManager().moveToRestState();
Winson Chungedb0d6b2018-03-21 17:32:55 -0700890
Tracy Zhou62646712018-06-26 14:19:02 -0700891 UiFactory.onLauncherStateOrResumeChanged(this);
892
Winson Chungdea51352018-04-24 13:02:10 -0700893 // Workaround for b/78520668, explicitly trim memory once UI is hidden
Winson Chung473730f2018-04-26 11:54:22 -0700894 onTrimMemory(TRIM_MEMORY_UI_HIDDEN);
Michael Jurkacd496d72013-04-11 11:32:45 -0700895 }
896
897 @Override
898 protected void onStart() {
vadimtcb863752018-12-19 17:44:57 -0800899 RaceConditionTracker.onEvent(ON_START_EVT, ENTER);
Michael Jurkacd496d72013-04-11 11:32:45 -0700900 super.onStart();
Adam Cohen9211d422014-10-07 18:14:53 -0700901 if (mLauncherCallbacks != null) {
902 mLauncherCallbacks.onStart();
903 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800904 mAppWidgetHost.setListenIfResumed(true);
vadimtcb863752018-12-19 17:44:57 -0800905 RaceConditionTracker.onEvent(ON_START_EVT, EXIT);
Michael Jurkacd496d72013-04-11 11:32:45 -0700906 }
907
Sunny Goyala002c6c2019-02-12 16:20:10 -0800908 private void handleDeferredResume() {
Sunny Goyale9c6f4c2019-05-30 11:35:25 -0700909 if (hasBeenResumed() && !mStateManager.getState().disableInteraction) {
Becky Qiubfc2da12019-08-01 11:09:38 -0700910 logStopAndResume(Action.Command.RESUME);
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700911 getUserEventDispatcher().startSession();
Sunny Goyala002c6c2019-02-12 16:20:10 -0800912
913 UiFactory.onLauncherStateOrResumeChanged(this);
Sunny Goyal369212a2019-03-26 15:03:57 -0700914 AppLaunchTracker.INSTANCE.get(this).onReturnedToHome();
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700915
Sunny Goyale9c6f4c2019-05-30 11:35:25 -0700916 // Process any items that were added while Launcher was away.
917 InstallShortcutReceiver.disableAndFlushInstallQueue(
918 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
919
920 // Refresh shortcuts if the permission changed.
921 mModel.refreshShortcutsIfRequired();
922
Winson Chung87583112019-06-10 14:20:58 -0700923 // Set the notification listener and fetch updated notifications when we resume
924 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
925
Sunny Goyale9c6f4c2019-05-30 11:35:25 -0700926 DiscoveryBounce.showForHomeIfNeeded(this);
927
928 if (mPendingActivityRequestCode != -1 && isInState(NORMAL)) {
929 UiFactory.resetPendingActivityResults(this, mPendingActivityRequestCode);
930 }
931 mDeferredResumePending = false;
932 } else {
933 mDeferredResumePending = true;
Sunny Goyaldedda052019-05-14 15:23:48 -0700934 }
935 }
936
Becky Qiubfc2da12019-08-01 11:09:38 -0700937 private void logStopAndResume(int command) {
938 int containerType = mStateManager.getState().containerType;
939 if (containerType == ContainerType.WORKSPACE && mWorkspace != null) {
940 getUserEventDispatcher().logActionCommand(command,
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700941 containerType, -1, mWorkspace.isOverlayShown() ? -1 : 0);
Becky Qiubfc2da12019-08-01 11:09:38 -0700942 } else {
943 getUserEventDispatcher().logActionCommand(command, containerType, -1);
944 }
945
946 }
947
Sunny Goyaldedda052019-05-14 15:23:48 -0700948 protected void onStateSet(LauncherState state) {
949 getAppWidgetHost().setResumed(state == LauncherState.NORMAL);
Sunny Goyale9c6f4c2019-05-30 11:35:25 -0700950 if (mDeferredResumePending) {
951 handleDeferredResume();
952 }
953 if (mLauncherCallbacks != null) {
954 mLauncherCallbacks.onStateChanged();
955 }
Sunny Goyaldedda052019-05-14 15:23:48 -0700956 }
957
Michael Jurkacd496d72013-04-11 11:32:45 -0700958 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800959 protected void onResume() {
vadimtcb863752018-12-19 17:44:57 -0800960 RaceConditionTracker.onEvent(ON_RESUME_EVT, ENTER);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700961 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800962 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700963 TraceHelper.partitionSection("ON_RESUME", "superCall");
964
Sunny Goyala002c6c2019-02-12 16:20:10 -0800965 mHandler.removeCallbacks(mHandleDeferredResume);
966 Utilities.postAsyncCallback(mHandler, mHandleDeferredResume);
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700967
Winson Chung940b0ac2019-07-09 11:33:50 -0700968 if (!mOnResumeCallbacks.isEmpty()) {
969 final ArrayList<OnResumeCallback> resumeCallbacks = new ArrayList<>(mOnResumeCallbacks);
970 mOnResumeCallbacks.clear();
971 for (int i = resumeCallbacks.size() - 1; i >= 0; i--) {
972 resumeCallbacks.get(i).onLauncherResume();
973 }
974 resumeCallbacks.clear();
Winson Chung0b70cd42019-06-17 22:34:33 -0700975 }
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700976
Adam Cohen9211d422014-10-07 18:14:53 -0700977 if (mLauncherCallbacks != null) {
978 mLauncherCallbacks.onResume();
979 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800980
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700981 TraceHelper.endSection("ON_RESUME");
vadimtcb863752018-12-19 17:44:57 -0800982 RaceConditionTracker.onEvent(ON_RESUME_EVT, EXIT);
Adam Cohen06dff352012-06-01 17:17:08 -0700983 }
984
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800985 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700986 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700987 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700988 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700989
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700990 super.onPause();
Joe Onorato24b6fd82009-11-12 13:47:09 -0800991 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700992 mDragController.resetLastGestureUpTime();
Hyunyoung Song497708c2019-05-01 16:16:53 -0700993 mDropTargetBar.animateToVisibility(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700994 if (mLauncherCallbacks != null) {
995 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700996 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700997 }
998
Winson Chungd7823942018-02-16 03:23:47 +0000999 @Override
Tracy Zhoua706f002018-03-28 13:55:19 -07001000 protected void onUserLeaveHint() {
1001 super.onUserLeaveHint();
1002 UiFactory.onLauncherStateOrResumeChanged(this);
1003 }
1004
1005 @Override
Winson Chungd7823942018-02-16 03:23:47 +00001006 public void onWindowFocusChanged(boolean hasFocus) {
1007 super.onWindowFocusChanged(hasFocus);
1008 mStateManager.onWindowFocusChanged();
1009 }
1010
Adam Cohenc2d6e892014-10-16 09:49:24 -07001011 public interface LauncherOverlay {
1012
1013 /**
1014 * Touch interaction leading to overscroll has begun
1015 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001016 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -07001017
1018 /**
1019 * Touch interaction related to overscroll has ended
1020 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001021 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -07001022
1023 /**
1024 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1025 * screen (or in the case of RTL, the rightmost screen).
1026 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001027 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001028
1029 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001030 * Called when the launcher is ready to use the overlay
1031 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001032 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001033 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001034 }
1035
1036 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001037 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001038 }
1039
1040 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1041
Sunny Goyalc86df472016-02-25 09:19:38 -08001042 public void onScrollChanged(float progress) {
1043 if (mWorkspace != null) {
1044 mWorkspace.onOverlayScrollChanged(progress);
1045 }
1046 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001047 }
1048
Sunny Goyalf9403d92017-10-18 10:55:56 -07001049 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -07001050 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -07001051 }
1052
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001053 /**
1054 * Restores the previous state, if it exists.
1055 *
1056 * @param savedState The previous state.
1057 */
1058 private void restoreState(Bundle savedState) {
1059 if (savedState == null) {
1060 return;
1061 }
1062
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001063 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -07001064 LauncherState[] stateValues = LauncherState.values();
1065 LauncherState state = stateValues[stateOrdinal];
Sunny Goyalbc683e92017-12-06 10:25:07 -08001066 if (!state.disableRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001067 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001068 }
1069
Samuel Fufaca37b8a2019-08-19 17:04:36 -07001070 PendingRequestArgs requestArgs = savedState.getParcelable(
1071 RUNTIME_STATE_PENDING_REQUEST_ARGS);
Sunny Goyal2100c782016-08-22 16:00:03 -07001072 if (requestArgs != null) {
1073 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001074 }
Sunny Goyaldedda052019-05-14 15:23:48 -07001075 mPendingActivityRequestCode = savedState.getInt(RUNTIME_STATE_PENDING_REQUEST_CODE);
Sunny Goyal2100c782016-08-22 16:00:03 -07001076
1077 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001078
1079 SparseArray<Parcelable> widgetsState =
1080 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
1081 if (widgetsState != null) {
1082 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
1083 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001084 }
1085
1086 /**
1087 * Finds all the views we need and configure them properly.
1088 */
1089 private void setupViews() {
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001090 mDragLayer = findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001091 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -07001092 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001093 mWorkspace.initParentViews(mDragLayer);
Sunny Goyal16764582017-11-06 16:00:34 -08001094 mOverviewPanel = findViewById(R.id.overview_panel);
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001095 mHotseat = findViewById(R.id.hotseat);
Craig Mautner360310b2012-10-26 15:13:08 -07001096
Sunny Goyal784f9c32016-03-23 16:56:54 -07001097 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1098 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1099 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001100
Winson Chung4c98d922011-05-31 16:50:48 -07001101 // Setup the drag layer
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001102 mDragLayer.setup(mDragController, mWorkspace);
Sunny Goyal0b1cb752019-06-05 15:18:39 -07001103 mCancelTouchController = UiFactory.enableLiveUIChanges(this);
Winson Chung4c98d922011-05-31 16:50:48 -07001104
Hyunyoung Song645764e2016-06-06 14:19:02 -07001105 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001106 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1107 // default state, otherwise we will update to the wrong offsets in RTL
1108 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001109 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001110 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001111
Tony Wickham34d2c912015-09-11 09:27:58 -07001112 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001113 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001114
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001115 // Setup Apps
1116 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001117
Govinda Wassermanc06e1512019-04-09 09:56:53 -04001118 // Setup Scrim
1119 mScrimView = findViewById(R.id.scrim_view);
1120
Winson Chung3d503fb2011-07-13 17:25:49 -07001121 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001122 mDragController.setMoveTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001123 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001124
Sunny Goyal7185dd62018-03-14 17:51:49 -07001125 mAllAppsController.setupViews(mAppsView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001126 }
1127
1128 /**
1129 * Creates a view representing a shortcut.
1130 *
1131 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001132 */
Sunny Goyal95899162019-03-27 16:03:06 -07001133 View createShortcut(WorkspaceItemInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001134 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001135 }
1136
1137 /**
1138 * Creates a view representing a shortcut inflated from the specified resource.
1139 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001140 * @param parent The group the shortcut belongs to.
Samuel Fufaca37b8a2019-08-19 17:04:36 -07001141 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001142 * @return A View inflated from layoutResId.
1143 */
Sunny Goyal95899162019-03-27 16:03:06 -07001144 public View createShortcut(ViewGroup parent, WorkspaceItemInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -08001145 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
1146 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal95899162019-03-27 16:03:06 -07001147 favorite.applyFromWorkspaceItem(info);
Sunny Goyal8a2a63b2018-03-06 22:15:18 -08001148 favorite.setOnClickListener(ItemClickHandler.INSTANCE);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001149 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001150 return favorite;
1151 }
1152
1153 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -07001154 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001155 *
1156 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001157 */
Sunny Goyalefb7e842018-10-04 15:11:00 -07001158 private void completeAddShortcut(Intent data, int container, int screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001159 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001160 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
1161 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001162 return;
1163 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001164
Jon Mirandac476d6e2017-05-04 16:30:01 -07001165 int[] cellXY = mTmpAddItemCellCoordinates;
1166 CellLayout layout = getCellLayout(container, screenId);
1167
Sunny Goyal95899162019-03-27 16:03:06 -07001168 WorkspaceItemInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001169 if (Utilities.ATLEAST_OREO) {
Sunny Goyal95899162019-03-27 16:03:06 -07001170 info = LauncherAppsCompatVO.createWorkspaceItemFromPinItemRequest(
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001171 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001172 }
1173
1174 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001175 // Legacy shortcuts are only supported for primary profile.
1176 info = Process.myUserHandle().equals(args.user)
1177 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001178
Sunny Goyalb57645f2017-03-20 13:57:28 -07001179 if (info == null) {
1180 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1181 return;
1182 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001183 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001184 // The app is trying to add a shortcut without sufficient permissions
1185 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1186 return;
1187 }
1188 }
1189
Jon Mirandac476d6e2017-05-04 16:30:01 -07001190 if (container < 0) {
1191 // Adding a shortcut to the Workspace.
1192 final View view = createShortcut(info);
1193 boolean foundCellSpan = false;
1194 // First we check if we already know the exact location where we want to add this item.
1195 if (cellX >= 0 && cellY >= 0) {
1196 cellXY[0] = cellX;
1197 cellXY[1] = cellY;
1198 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001199
Jon Mirandac476d6e2017-05-04 16:30:01 -07001200 // If appropriate, either create a folder or add to an existing folder
1201 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001202 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001203 return;
1204 }
1205 DragObject dragObject = new DragObject();
1206 dragObject.dragInfo = info;
1207 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1208 true)) {
1209 return;
1210 }
1211 } else {
1212 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1213 }
1214
1215 if (!foundCellSpan) {
1216 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001217 return;
1218 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001219
1220 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1221 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001222 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001223 // Adding a shortcut to a Folder.
Sunny Goyale29897f2017-07-20 10:09:42 -07001224 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001225 if (folderIcon != null) {
1226 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1227 folderInfo.add(info, args.rank, false);
1228 } else {
Sunny Goyale29897f2017-07-20 10:09:42 -07001229 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001230 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001231 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001232 }
1233
Sunny Goyalefb7e842018-10-04 15:11:00 -07001234 public FolderIcon findFolderIcon(final int folderIconId) {
Sunny Goyal83fd25e2018-07-09 16:47:01 -07001235 return (FolderIcon) mWorkspace.getHomescreenIconByItemId(folderIconId);
Sunny Goyale29897f2017-07-20 10:09:42 -07001236 }
1237
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001238 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001239 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001240 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001241 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001242 */
Samuel Fufaca37b8a2019-08-19 17:04:36 -07001243 @Thunk
1244 void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001245 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1246
Adam Cohened66b2b2012-01-23 17:28:51 -08001247 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001248 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001249 }
Romain Guycbb89e42009-06-08 15:52:54 -07001250
Adam Cohen59400422014-03-05 18:07:04 -08001251 LauncherAppWidgetInfo launcherInfo;
1252 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001253 launcherInfo.spanX = itemInfo.spanX;
1254 launcherInfo.spanY = itemInfo.spanY;
1255 launcherInfo.minSpanX = itemInfo.minSpanX;
1256 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001257 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001258
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001259 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001260 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001261
Sunny Goyal2100c782016-08-22 16:00:03 -07001262 if (hostView == null) {
1263 // Perform actual inflation because we're live
1264 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001265 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001266 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301267 prepareAppWidget(hostView, launcherInfo);
1268 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001269 }
Romain Guycbb89e42009-06-08 15:52:54 -07001270
Sunny Goyald5462aa2016-11-22 16:52:39 +05301271 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001272 hostView.setTag(item);
1273 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001274 hostView.setFocusable(true);
1275 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001276 }
1277
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001278 private final BroadcastReceiver mScreenOffReceiver = new BroadcastReceiver() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001279 @Override
1280 public void onReceive(Context context, Intent intent) {
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001281 // Reset AllApps to its initial state only if we are not in the middle of
1282 // processing a multi-step drop
1283 if (mPendingRequestArgs == null) {
1284 mStateManager.goToState(NORMAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001285 }
1286 }
1287 };
1288
Sunny Goyalaae6fbb2019-01-31 16:05:58 -08001289 public void updateNotificationDots(Predicate<PackageUserKey> updatedDots) {
Tony Wickhamf34bee82018-12-03 18:11:39 -08001290 mWorkspace.updateNotificationDots(updatedDots);
1291 mAppsView.getAppsStore().updateNotificationDots(updatedDots);
Tony Wickham010d2552017-01-20 08:15:28 -08001292 }
1293
Adam Cohended9f8d2010-11-03 13:25:16 -07001294 @Override
1295 public void onAttachedToWindow() {
1296 super.onAttachedToWindow();
1297
Sunny Goyalc86df472016-02-25 09:19:38 -08001298 if (mLauncherCallbacks != null) {
1299 mLauncherCallbacks.onAttachedToWindow();
1300 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001301 }
1302
1303 @Override
1304 public void onDetachedFromWindow() {
1305 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001306
1307 if (mLauncherCallbacks != null) {
1308 mLauncherCallbacks.onDetachedFromWindow();
1309 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001310 }
1311
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001312 public AllAppsTransitionController getAllAppsController() {
1313 return mAllAppsController;
1314 }
1315
Sunny Goyal9d69c8d2018-03-19 13:41:31 -07001316 @Override
Winson Chung1a341002018-01-17 10:00:23 -08001317 public LauncherRootView getRootView() {
1318 return (LauncherRootView) mLauncherView;
1319 }
1320
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001321 @Override
Winson Chunga6945242014-01-08 14:04:34 -08001322 public DragLayer getDragLayer() {
1323 return mDragLayer;
1324 }
1325
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001326 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001327 return mAppsView;
1328 }
1329
Winson Chunga6945242014-01-08 14:04:34 -08001330 public Workspace getWorkspace() {
1331 return mWorkspace;
1332 }
1333
1334 public Hotseat getHotseat() {
1335 return mHotseat;
1336 }
1337
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001338 public <T extends View> T getOverviewPanel() {
Winson Chung8ae41982017-11-10 11:42:13 -08001339 return (T) mOverviewPanel;
Winson Chunga6945242014-01-08 14:04:34 -08001340 }
1341
Sunny Goyal47328fd2016-05-25 18:56:41 -07001342 public DropTargetBar getDropTargetBar() {
1343 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001344 }
1345
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001346 public LauncherAppWidgetHost getAppWidgetHost() {
1347 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001348 }
Romain Guycbb89e42009-06-08 15:52:54 -07001349
Winson Chunga9abd0e2010-10-27 17:18:37 -07001350 public LauncherModel getModel() {
1351 return mModel;
1352 }
1353
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001354 public ModelWriter getModelWriter() {
1355 return mModelWriter;
1356 }
1357
Adam Cohen79d90c52016-04-22 13:29:20 -07001358 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001359 return mSharedPrefs;
1360 }
1361
Samuel Fufaca37b8a2019-08-19 17:04:36 -07001362 public int getOrientation() {
1363 return mOldConfig.orientation;
1364 }
Jon Miranda6bed3502017-09-19 14:43:17 -07001365
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001366 @Override
1367 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001368 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001369 super.onNewIntent(intent);
1370
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001371 boolean alreadyOnHome = hasWindowFocus() && ((intent.getFlags() &
Winson15f8b172015-08-19 11:02:39 -07001372 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1373 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001374
1375 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001376 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001377 && AbstractFloatingView.getTopOpenView(this) == null;
Winson15f8b172015-08-19 11:02:39 -07001378 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001379 boolean internalStateHandled = InternalStateHandler
Sunny Goyal8b264562018-02-03 23:00:13 -08001380 .handleNewIntent(this, intent, isStarted());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001381
Winson15f8b172015-08-19 11:02:39 -07001382 if (isActionMain) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001383 if (!internalStateHandled) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001384 // In all these cases, only animate if we're already on home
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001385 AbstractFloatingView.closeAllOpenViews(this, isStarted());
Jon Mirandafeba90f2016-10-06 10:53:29 -07001386
Sunny Goyala2467272018-03-16 14:53:05 -07001387 if (!isInState(NORMAL)) {
1388 // Only change state, if not already the same. This prevents cancelling any
1389 // animations running as part of resume
1390 mStateManager.goToState(NORMAL);
1391 }
Sunny Goyald3864fa2017-11-14 15:06:36 -08001392
1393 // Reset the apps view
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001394 if (!alreadyOnHome) {
Sunny Goyal7185dd62018-03-14 17:51:49 -07001395 mAppsView.reset(isStarted() /* animate */);
Sunny Goyald3864fa2017-11-14 15:06:36 -08001396 }
1397
Sunny Goyalaad33592018-08-07 17:20:35 -07001398 if (shouldMoveToDefaultScreen && !mWorkspace.isHandlingTouch()) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001399 mWorkspace.post(mWorkspace::moveToDefaultScreen);
1400 }
1401 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001402
Hyunyoung Song2a70b3d2019-06-11 12:47:07 -07001403 // Handle HOME_INTENT
1404 UserEventDispatcher ued = getUserEventDispatcher();
1405 Target target = newContainerTarget(mStateManager.getState().containerType);
1406 target.pageIndex = mWorkspace.getCurrentPage();
1407 ued.logActionCommand(Action.Command.HOME_INTENT, target,
1408 newContainerTarget(ContainerType.WORKSPACE));
1409
Adam Cohen6fecd412013-10-02 17:41:50 -07001410 final View v = getWindow().peekDecorView();
1411 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001412 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001413 }
1414
Adam Cohen9211d422014-10-07 18:14:53 -07001415 if (mLauncherCallbacks != null) {
Sunny Goyal85462132018-04-24 11:39:37 -07001416 mLauncherCallbacks.onHomeIntent(internalStateHandled);
Adam Cohen9211d422014-10-07 18:14:53 -07001417 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001418 }
Winson15f8b172015-08-19 11:02:39 -07001419
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001420 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001421 }
1422
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001423 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001424 public void onRestoreInstanceState(Bundle state) {
1425 super.onRestoreInstanceState(state);
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001426 mWorkspace.restoreInstanceStateForChild(mSynchronouslyBoundPage);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001427 }
1428
1429 @Override
1430 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001431 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001432 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001433
Adam Cohen21cd0022013-10-09 18:57:02 -07001434 }
Sunny Goyalea609262017-10-25 15:47:38 -07001435 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001436
1437
1438 AbstractFloatingView widgets = AbstractFloatingView
1439 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1440 if (widgets != null) {
1441 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1442 widgets.saveHierarchyState(widgetsState);
1443 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1444 } else {
1445 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1446 }
1447
Pinyao Tinga74b63a2019-07-17 23:26:06 -07001448 // We close any open folders and shortcut containers that are not safe for rebind,
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001449 // and we need to make sure this state is reflected.
Pinyao Tinga74b63a2019-07-17 23:26:06 -07001450 AbstractFloatingView.closeOpenViews(this, false, TYPE_ALL & ~TYPE_REBIND_SAFE);
1451 finishAutoCancelActionMode();
Tony Wickham49c8d292016-07-01 11:44:34 -07001452
Sunny Goyal2100c782016-08-22 16:00:03 -07001453 if (mPendingRequestArgs != null) {
1454 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1455 }
Sunny Goyaldedda052019-05-14 15:23:48 -07001456 outState.putInt(RUNTIME_STATE_PENDING_REQUEST_CODE, mPendingActivityRequestCode);
1457
Sunny Goyal2100c782016-08-22 16:00:03 -07001458 if (mPendingActivityResult != null) {
1459 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001460 }
1461
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001462 super.onSaveInstanceState(outState);
1463
Adam Cohen9211d422014-10-07 18:14:53 -07001464 if (mLauncherCallbacks != null) {
1465 mLauncherCallbacks.onSaveInstanceState(outState);
1466 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001467 }
1468
1469 @Override
1470 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001471 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001472
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001473 unregisterReceiver(mScreenOffReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001474 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001475
Sunny Goyal5743f862019-03-28 15:35:32 -07001476 if (mCancelTouchController != null) {
1477 mCancelTouchController.run();
1478 mCancelTouchController = null;
1479 }
Tony Wickhama0068302018-04-11 16:16:11 -07001480
Winson Chungcd2b0142011-06-08 16:02:26 -07001481 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001482 // It's possible to receive onDestroy after a new Launcher activity has
1483 // been created. In this case, don't interfere with the new Launcher.
1484 if (mModel.isCurrentCallbacks(this)) {
1485 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001486 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001487 }
Sunny Goyal623eddd2018-03-02 12:24:41 -08001488 mRotationHelper.destroy();
Sunny Goyal745bad92016-05-02 10:54:12 -07001489
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001490 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001491 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001492 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001493 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001494 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001495
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001496 TextKeyListener.getInstance().release();
Tony2917a8b2017-07-31 23:29:42 -07001497 clearPendingBinds();
Hyunyoung Song31971a32019-01-27 12:31:12 -08001498 LauncherAppState.getIDP(this).removeOnChangeListener(this);
Adam Cohen9211d422014-10-07 18:14:53 -07001499 if (mLauncherCallbacks != null) {
1500 mLauncherCallbacks.onDestroy();
1501 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001502 }
1503
Sunny Goyalae502842016-06-17 08:43:56 -07001504 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1505 return mAccessibilityDelegate;
1506 }
1507
Adam Cohena9cf38f2011-05-02 15:36:58 -07001508 public DragController getDragController() {
1509 return mDragController;
1510 }
1511
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001512 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001513 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyaldedda052019-05-14 15:23:48 -07001514 if (requestCode != -1) {
1515 mPendingActivityRequestCode = requestCode;
1516 }
1517 if (requestCode == -1
1518 || !UiFactory.startActivityForResult(this, intent, requestCode, options)) {
1519 super.startActivityForResult(intent, requestCode, options);
1520 }
Adam Cohen173f7112015-03-27 15:14:00 -07001521 }
1522
1523 @Override
Sunny Goyaldedda052019-05-14 15:23:48 -07001524 public void startIntentSenderForResult(IntentSender intent, int requestCode,
Adam Cohen173f7112015-03-27 15:14:00 -07001525 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Sunny Goyaldedda052019-05-14 15:23:48 -07001526 if (requestCode != -1) {
1527 mPendingActivityRequestCode = requestCode;
1528 }
1529 if (requestCode == -1 || !UiFactory.startIntentSenderForResult(this, intent, requestCode,
1530 fillInIntent, flagsMask, flagsValues, extraFlags, options)) {
1531 try {
1532 super.startIntentSenderForResult(intent, requestCode,
1533 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1534 } catch (IntentSender.SendIntentException e) {
1535 throw new ActivityNotFoundException();
1536 }
Adam Cohen173f7112015-03-27 15:14:00 -07001537 }
1538 }
1539
Winson Chung70d72102011-08-12 11:24:35 -07001540 /**
1541 * Indicates that we want global search for this activity by setting the globalSearch
1542 * argument for {@link #startSearch} to true.
1543 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001544 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001545 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001546 Bundle appSearchData, boolean globalSearch) {
1547 if (appSearchData == null) {
1548 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001549 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001550 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001551
Sunny Goyal6f28e712016-09-13 14:19:29 -07001552 if (mLauncherCallbacks == null ||
1553 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1554 // Starting search from the callbacks failed. Start the default global search.
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001555 super.startSearch(initialQuery, selectInitialQuery, appSearchData, true);
Ian Parkinson047036e2014-09-01 15:40:53 +01001556 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001557
1558 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001559 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001560 }
1561
Joe Onorato9c1289c2009-08-17 11:03:03 -04001562 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001563 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001564 }
1565
Adam Cohen517a7f52014-03-01 12:12:59 -08001566 public boolean isWorkspaceLoading() {
1567 return mWorkspaceLoading;
1568 }
1569
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001570 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001571 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001572 }
1573
Sunny Goyal04a324a2017-01-20 21:08:59 -08001574 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001575 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001576 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001577
Sunny Goyal2100c782016-08-22 16:00:03 -07001578 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001579 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001580 if (LOGD) {
1581 Log.d(TAG, "Adding widget from drop");
1582 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001583 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001584 }
1585
Sunny Goyal2100c782016-08-22 16:00:03 -07001586 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001587 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
Samuel Fufaca37b8a2019-08-19 17:04:36 -07001588 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info,
1589 REQUEST_CREATE_APPWIDGET)) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001590 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001591
Adam Cohenad4e15c2013-10-17 16:21:35 -07001592 Runnable onComplete = new Runnable() {
1593 @Override
1594 public void run() {
1595 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001596 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001597 }
1598 };
Samuel Fufaca37b8a2019-08-19 17:04:36 -07001599 completeAddAppWidget(appWidgetId, info, boundWidget,
1600 addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001601 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001602 }
1603 }
Romain Guycbb89e42009-06-08 15:52:54 -07001604
Sunny Goyalefb7e842018-10-04 15:11:00 -07001605 public void addPendingItem(PendingAddItemInfo info, int container, int screenId,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001606 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001607 info.container = container;
1608 info.screenId = screenId;
1609 if (cell != null) {
1610 info.cellX = cell[0];
1611 info.cellY = cell[1];
1612 }
1613 info.spanX = spanX;
1614 info.spanY = spanY;
1615
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001616 switch (info.itemType) {
1617 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1618 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001619 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001620 break;
1621 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001622 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001623 break;
1624 default:
1625 throw new IllegalStateException("Unknown item type: " + info.itemType);
Samuel Fufaca37b8a2019-08-19 17:04:36 -07001626 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001627 }
1628
Adam Cohenfbba09b2011-07-18 21:43:05 -07001629 /**
1630 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001631 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001632 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001633 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1634 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001635 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1636 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1637 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001638 }
1639
Adam Cohenfbba09b2011-07-18 21:43:05 -07001640 /**
1641 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001642 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001643 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001644 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001645 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001646 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001647 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001648 // In the case where we've prebound the widget, we remove it from the DragLayer
1649 if (LOGD) {
1650 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1651 }
1652 getDragLayer().removeView(hostView);
1653
Adam Cohened66b2b2012-01-23 17:28:51 -08001654 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001655 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001656
1657 // Clear the boundWidget so that it doesn't get destroyed.
1658 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001659 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001660 // In this case, we either need to start an activity to get permission to bind
1661 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001662 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1663 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1664 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1665 this, info.componentName);
1666 } else {
1667 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1668 }
Adam Cohendd70d662012-10-04 16:53:44 -07001669 Bundle options = info.bindOptions;
1670
Sunny Goyalffe83f12014-08-14 17:39:34 -07001671 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1672 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001673 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001674 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001675 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001676 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001677 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001678 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001679 }
1680
Sunny Goyalefb7e842018-10-04 15:11:00 -07001681 FolderIcon addFolder(CellLayout layout, int container, final int screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001682 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001683 final FolderInfo folderInfo = new FolderInfo();
Samuel Fufa84245e82019-08-14 13:44:12 -07001684 folderInfo.title = "";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001685
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001686 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001687 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001688
1689 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001690 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301691 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001692 // Force measure the new folder icon
1693 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1694 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001695 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001696 }
Romain Guycbb89e42009-06-08 15:52:54 -07001697
Winsonc0b52fe2015-09-09 16:38:15 -07001698 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001699 * Unbinds the view for the specified item, and removes the item and all its children.
1700 *
1701 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001702 * @param itemInfo the {@link ItemInfo} for this view.
1703 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001704 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001705 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Sunny Goyal95899162019-03-27 16:03:06 -07001706 if (itemInfo instanceof WorkspaceItemInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001707 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001708 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1709 if (folderIcon instanceof FolderIcon) {
Sunny Goyal95899162019-03-27 16:03:06 -07001710 ((FolderInfo) folderIcon.getTag()).remove((WorkspaceItemInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001711 } else {
1712 mWorkspace.removeWorkspaceItem(v);
1713 }
Winsonc0b52fe2015-09-09 16:38:15 -07001714 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001715 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001716 }
1717 } else if (itemInfo instanceof FolderInfo) {
1718 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001719 if (v instanceof FolderIcon) {
1720 ((FolderIcon) v).removeListeners();
1721 }
Winsonc0b52fe2015-09-09 16:38:15 -07001722 mWorkspace.removeWorkspaceItem(v);
1723 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001724 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001725 }
1726 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1727 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001728 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001729 if (deleteFromDb) {
Tony Wickham6a71a5b2018-08-21 11:40:23 -07001730 getModelWriter().deleteWidgetInfo(widgetInfo, getAppWidgetHost());
Winsonc0b52fe2015-09-09 16:38:15 -07001731 }
1732 } else {
1733 return false;
1734 }
1735 return true;
1736 }
1737
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001738 @Override
1739 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001740 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001741 }
1742
Joe Onorato88ec0992009-11-19 13:16:06 -08001743 @Override
1744 public void onBackPressed() {
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08001745 if (finishAutoCancelActionMode()) {
1746 return;
1747 }
Adam Cohen9211d422014-10-07 18:14:53 -07001748 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1749 return;
1750 }
1751
Sunny Goyal45478022015-06-08 16:52:41 -07001752 if (mDragController.isDragging()) {
1753 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001754 return;
1755 }
1756
Jon Mirandafeba90f2016-10-06 10:53:29 -07001757 // Note: There should be at most one log per method call. This is enforced implicitly
1758 // by using if-else statements.
1759 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001760 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
Tony Wickham52c1b662018-05-21 13:13:58 -07001761 if (topView != null && topView.onBackPressed()) {
1762 // Handled by the floating view.
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001763 } else {
Sunny Goyale39690b2018-08-02 13:35:07 -07001764 mStateManager.getState().onBackPressed(this);
Michael Jurkaaf442092010-06-10 17:01:57 -07001765 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001766 }
1767
Sunny Goyalfe770c92016-09-27 14:38:58 -07001768 @TargetApi(Build.VERSION_CODES.M)
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001769 @Override
Jon Miranda73c27ec2018-05-16 18:06:23 -07001770 public ActivityOptions getActivityLaunchOptions(View v) {
1771 return mAppTransitionManager.getActivityLaunchOptions(this, v);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001772 }
1773
Tony Wickham3a6746a2018-03-29 09:39:56 -07001774 public LauncherAppTransitionManager getAppTransitionManager() {
1775 return mAppTransitionManager;
1776 }
1777
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001778 @TargetApi(Build.VERSION_CODES.M)
1779 @Override
1780 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
1781 // Due to legacy reasons, direct call shortcuts require Launchers to have the
1782 // corresponding permission. Show the appropriate permission prompt if that
1783 // is the case.
1784 if (intent.getComponent() == null
1785 && Intent.ACTION_CALL.equals(intent.getAction())
1786 && checkSelfPermission(android.Manifest.permission.CALL_PHONE) !=
1787 PackageManager.PERMISSION_GRANTED) {
1788
1789 setWaitingForResult(PendingRequestArgs
1790 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
1791 requestPermissions(new String[]{android.Manifest.permission.CALL_PHONE},
1792 REQUEST_PERMISSION_CALL_PHONE);
1793 return true;
1794 } else {
1795 return false;
1796 }
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001797 }
1798
Hyunyoung Song46d07f72018-05-22 15:41:25 -07001799 @Override
Hyunyoung Songfc007472018-10-25 14:09:50 -07001800 public int getCurrentState() {
Samuel Fufaca37b8a2019-08-19 17:04:36 -07001801 if (mStateManager.getState() == LauncherState.ALL_APPS) {
Hyunyoung Songfc007472018-10-25 14:09:50 -07001802 return StatsLogUtils.LAUNCHER_STATE_ALLAPPS;
Govinda Wassermanc06e1512019-04-09 09:56:53 -04001803 } else if (mStateManager.getState() == OVERVIEW) {
Hyunyoung Songfc007472018-10-25 14:09:50 -07001804 return StatsLogUtils.LAUNCHER_STATE_OVERVIEW;
1805 }
Hyunyoung Song0ae38882018-11-05 14:45:19 -08001806 return StatsLogUtils.LAUNCHER_STATE_HOME;
Hyunyoung Songfc007472018-10-25 14:09:50 -07001807 }
1808
1809 @Override
Hyunyoung Song46d07f72018-05-22 15:41:25 -07001810 public void modifyUserEvent(LauncherLogProto.LauncherEvent event) {
1811 if (event.srcTarget != null && event.srcTarget.length > 0 &&
1812 event.srcTarget[1].containerType == ContainerType.PREDICTION) {
1813 Target[] targets = new Target[3];
1814 targets[0] = event.srcTarget[0];
1815 targets[1] = event.srcTarget[1];
1816 targets[2] = newTarget(Target.Type.CONTAINER);
1817 event.srcTarget = targets;
1818 LauncherState state = mStateManager.getState();
1819 if (state == LauncherState.ALL_APPS) {
1820 event.srcTarget[2].containerType = ContainerType.ALLAPPS;
Govinda Wassermanc06e1512019-04-09 09:56:53 -04001821 } else if (state == OVERVIEW) {
Hyunyoung Song46d07f72018-05-22 15:41:25 -07001822 event.srcTarget[2].containerType = ContainerType.TASKSWITCHER;
1823 }
1824 }
1825 }
1826
Sunny Goyal369212a2019-03-26 15:03:57 -07001827 public boolean startActivitySafely(View v, Intent intent, ItemInfo item,
1828 @Nullable String sourceContainer) {
Winson Chung0b70cd42019-06-17 22:34:33 -07001829 if (!hasBeenResumed()) {
1830 // Workaround an issue where the WM launch animation is clobbered when finishing the
1831 // recents animation into launcher. Defer launching the activity until Launcher is
1832 // next resumed.
1833 addOnResumeCallback(() -> startActivitySafely(v, intent, item, sourceContainer));
1834 UiFactory.clearSwipeSharedState(true /* finishAnimation */);
1835 return true;
1836 }
1837
Sunny Goyal369212a2019-03-26 15:03:57 -07001838 boolean success = super.startActivitySafely(v, intent, item, sourceContainer);
Hyunyoung Song1241e612018-05-15 21:46:51 -07001839 if (success && v instanceof BubbleTextView) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001840 // This is set to the view that launched the activity that navigated the user away
1841 // from launcher. Since there is no callback for when the activity has finished
1842 // launching, enable the press state and keep this reference to reset the press
1843 // state when we return to launcher.
1844 BubbleTextView btv = (BubbleTextView) v;
1845 btv.setStayPressed(true);
Winson Chung0b70cd42019-06-17 22:34:33 -07001846 addOnResumeCallback(btv);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001847 }
Hyunyoung Song1241e612018-05-15 21:46:51 -07001848 return success;
Michael Jurka86a720e2012-05-09 11:23:23 -07001849 }
1850
Winson Chung3d503fb2011-07-13 17:25:49 -07001851 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001852 // TODO: Remove this method
Sunny Goyal876e4622018-11-02 13:50:40 -07001853 return mHotseat != null && (layout == mHotseat);
Winson Chung3d503fb2011-07-13 17:25:49 -07001854 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001855
Winson Chung3d503fb2011-07-13 17:25:49 -07001856 /**
1857 * Returns the CellLayout of the specified container at the specified screen.
1858 */
Sunny Goyalefb7e842018-10-04 15:11:00 -07001859 public CellLayout getCellLayout(int container, int screenId) {
Sunny Goyal876e4622018-11-02 13:50:40 -07001860 return (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1861 ? mHotseat : mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08001862 }
1863
Michael Jurkae326f182011-11-21 14:05:46 -08001864 @Override
1865 public void onTrimMemory(int level) {
1866 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07001867 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
1868 // The widget preview db can result in holding onto over
1869 // 3MB of memory for caching which isn't necessary.
1870 SQLiteDatabase.releaseMemory();
1871
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08001872 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07001873 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08001874 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01001875 if (mLauncherCallbacks != null) {
1876 mLauncherCallbacks.onTrimMemory(level);
1877 }
Winson Chung62a70be2018-02-23 15:10:05 -08001878 UiFactory.onTrimMemory(this, level);
Michael Jurkae326f182011-11-21 14:05:46 -08001879 }
1880
Michael Jurkad7c28052012-04-27 15:43:36 -07001881 @Override
1882 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07001883 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07001884 final List<CharSequence> text = event.getText();
1885 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07001886 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001887 // TODO: When can workspace be null?
1888 text.add(mWorkspace == null
1889 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07001890 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07001891 return result;
1892 }
1893
Winson Chung0b70cd42019-06-17 22:34:33 -07001894 public void addOnResumeCallback(OnResumeCallback callback) {
1895 mOnResumeCallbacks.add(callback);
Michael Jurka1e2f4652013-07-08 18:03:46 -07001896 }
1897
Michael Jurka7607c2f2013-04-03 14:33:19 -07001898 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001899 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001900 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07001901 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001902 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07001903 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001904 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07001905 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07001906 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07001907 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001908 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001909
Joe Onorato9c1289c2009-08-17 11:03:03 -04001910 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07001911 * Clear any pending bind callbacks. This is called when is loader is planning to
1912 * perform a full rebind from scratch.
1913 */
1914 @Override
1915 public void clearPendingBinds() {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001916 if (mPendingExecutor != null) {
1917 mPendingExecutor.markCompleted();
1918 mPendingExecutor = null;
Sunny Goyal6b6d0a32019-08-05 17:19:32 -07001919
1920 // We might have set this flag previously and forgot to clear it.
1921 mAppsView.getAppsStore()
1922 .disableDeferUpdatesSilently(AllAppsStore.DEFER_UPDATES_NEXT_DRAW);
Sunny Goyal527c7d32015-08-28 15:19:36 -07001923 }
1924 }
1925
1926 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001927 * Refreshes the shortcuts shown on the workspace.
1928 *
1929 * Implementation of the method from LauncherModel.Callbacks.
1930 */
1931 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001932 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001933 // Floating panels (except the full widget sheet) are associated with individual icons. If
1934 // we are starting a fresh bind, close all such panels as all the icons are about
1935 // to go away.
Pinyao Tinga74b63a2019-07-17 23:26:06 -07001936 AbstractFloatingView.closeOpenViews(this, true, TYPE_ALL & ~TYPE_REBIND_SAFE);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08001937
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001938 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08001939
Winson Chungd64d1762013-08-20 14:37:16 -07001940 // Clear the workspace because it's going to be rebound
Sunny Goyal2db53422019-03-01 16:06:12 -08001941 mDragController.cancelDrag();
1942
Adam Cohendf035382011-04-11 17:22:04 -07001943 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07001944 mWorkspace.removeAllWorkspaceScreens();
Sunny Goyalc11fac32018-02-27 19:20:15 -08001945 mAppWidgetHost.clearViews();
Winson Chungd64d1762013-08-20 14:37:16 -07001946
Winson Chung3d503fb2011-07-13 17:25:49 -07001947 if (mHotseat != null) {
Sunny Goyalae6e3182019-04-30 12:04:37 -07001948 mHotseat.resetLayout(getWallpaperDeviceProfile().isVerticalBarLayout());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001949 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001950 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001951 }
1952
Adam Cohendcd297f2013-06-18 13:13:40 -07001953 @Override
Sunny Goyalefb7e842018-10-04 15:11:00 -07001954 public void bindScreens(IntArray orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001955 // Make sure the first screen is always at the start.
Jon Miranda7143ba62019-03-15 09:00:05 -07001956 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001957 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalefb7e842018-10-04 15:11:00 -07001958 orderedScreenIds.removeValue(Workspace.FIRST_SCREEN_ID);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001959 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Jon Miranda7143ba62019-03-15 09:00:05 -07001960 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001961 // If there are no screens, we need to have an empty screen
1962 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07001963 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001964 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07001965
Winsonc7d2e832016-07-28 12:24:55 -07001966 // After we have added all the screens, if the wallpaper was locked to the default state,
1967 // then notify to indicate that it can be released and a proper wallpaper offset can be
1968 // computed before the next layout
1969 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07001970 }
1971
Sunny Goyalefb7e842018-10-04 15:11:00 -07001972 private void bindAddScreens(IntArray orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001973 int count = orderedScreenIds.size();
1974 for (int i = 0; i < count; i++) {
Sunny Goyalefb7e842018-10-04 15:11:00 -07001975 int screenId = orderedScreenIds.get(i);
Jon Miranda7143ba62019-03-15 09:00:05 -07001976 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001977 // No need to bind the first screen, as its always bound.
1978 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
1979 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001980 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001981 }
1982
Sunny Goyalb23980c2017-08-17 07:45:25 -07001983 @Override
Tony Wickham6a71a5b2018-08-21 11:40:23 -07001984 public void preAddApps() {
1985 // If there's an undo snackbar, force it to complete to ensure empty screens are removed
1986 // before trying to add new items.
1987 mModelWriter.commitDelete();
1988 AbstractFloatingView snackbar = AbstractFloatingView.getOpenView(this, TYPE_SNACKBAR);
1989 if (snackbar != null) {
1990 snackbar.post(() -> snackbar.close(true));
1991 }
1992 }
1993
1994 @Override
Sunny Goyalefb7e842018-10-04 15:11:00 -07001995 public void bindAppsAdded(IntArray newScreens, ArrayList<ItemInfo> addNotAnimated,
Sunny Goyal29947f02017-12-18 13:49:44 -08001996 ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07001997 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08001998 if (newScreens != null) {
1999 bindAddScreens(newScreens);
2000 }
Winson Chungd64d1762013-08-20 14:37:16 -07002001
2002 // We add the items without animation on non-visible pages, and with
2003 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08002004 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002005 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002006 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002007 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002008 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07002009 }
Winson Chungc58497e2013-09-03 17:48:37 -07002010
Winson Chung87412982013-10-03 18:34:14 -07002011 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07002012 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002013 }
2014
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002015 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002016 * Bind the items start-end from the list.
2017 *
2018 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002019 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07002020 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07002021 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002022 // Get the list of added items and intersect them with the set of items here
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002023 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07002024 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07002025 Workspace workspace = mWorkspace;
Sunny Goyalefb7e842018-10-04 15:11:00 -07002026 int newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07002027 int end = items.size();
2028 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002029 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002030
2031 // Short circuit if we are loading dock items for a configuration which has no dock
2032 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2033 mHotseat == null) {
2034 continue;
2035 }
2036
Sunny Goyal639e9062015-08-19 19:17:06 -07002037 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002038 switch (item.itemType) {
2039 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2040 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08002041 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Sunny Goyal95899162019-03-27 16:03:06 -07002042 WorkspaceItemInfo info = (WorkspaceItemInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07002043 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002044 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002045 }
2046 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07002047 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002048 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002049 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002050 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002051 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07002052 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2053 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07002054 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002055 if (view == null) {
2056 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002057 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08002058 break;
2059 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002060 default:
2061 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002062 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002063
Samuel Fufaca37b8a2019-08-19 17:04:36 -07002064 /*
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002065 * Remove colliding items.
2066 */
2067 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
2068 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
2069 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
2070 View v = cl.getChildAt(item.cellX, item.cellY);
2071 Object tag = v.getTag();
2072 String desc = "Collision while binding workspace item: " + item
2073 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08002074 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002075 throw (new RuntimeException(desc));
2076 } else {
2077 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002078 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002079 continue;
2080 }
2081 }
2082 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302083 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07002084 if (animateIcons) {
2085 // Animate all the applications up now
2086 view.setAlpha(0f);
2087 view.setScaleX(0f);
2088 view.setScaleY(0f);
2089 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08002090 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07002091 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002092 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07002093
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002094 // Animate to the correct page
2095 if (animateIcons && newItemsScreenId > -1) {
2096 AnimatorSet anim = new AnimatorSet();
2097 anim.playTogether(bounceAnims);
Jon Miranda64eb8ea2018-03-29 11:43:58 -07002098
Sunny Goyalefb7e842018-10-04 15:11:00 -07002099 int currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002100 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
2101 final Runnable startBounceAnimRunnable = anim::start;
2102
2103 if (newItemsScreenId != currentScreenId) {
2104 // We post the animation slightly delayed to prevent slowdowns
2105 // when we are loading right after we return to launcher.
2106 mWorkspace.postDelayed(new Runnable() {
2107 public void run() {
2108 if (mWorkspace != null) {
2109 AbstractFloatingView.closeAllOpenViews(Launcher.this, false);
2110
2111 mWorkspace.snapToPage(newScreenIndex);
2112 mWorkspace.postDelayed(startBounceAnimRunnable,
2113 NEW_APPS_ANIMATION_DELAY);
Winson Chung94d67682013-09-25 16:29:40 -07002114 }
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002115 }
2116 }, NEW_APPS_PAGE_MOVE_DELAY);
2117 } else {
2118 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07002119 }
Winson Chung64359a52013-07-08 17:17:08 -07002120 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002121 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002122 }
2123
Joe Onorato9c1289c2009-08-17 11:03:03 -04002124 /**
2125 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002126 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07002127 public void bindAppWidget(LauncherAppWidgetInfo item) {
2128 View view = inflateAppWidget(item);
2129 if (view != null) {
2130 mWorkspace.addInScreen(view, item);
2131 mWorkspace.requestLayout();
2132 }
2133 }
2134
2135 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Samuel Fufaca37b8a2019-08-19 17:04:36 -07002136 if (item.hasOptionFlag(LauncherAppWidgetInfo.OPTION_SEARCH_WIDGET)) {
2137 item.providerName = QsbContainerView.getSearchComponentName(this);
2138 if (item.providerName == null) {
2139 getModelWriter().deleteItemFromDatabase(item);
2140 return null;
2141 }
2142 }
2143
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002144 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302145 PendingAppWidgetHostView view =
2146 new PendingAppWidgetHostView(this, item, mIconCache, true);
2147 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002148 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002149 }
2150
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002151 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002152
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002153 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08002154
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002155 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2156 // If the provider is not ready, bind as a pending widget.
2157 appWidgetInfo = null;
2158 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2159 // The widget id is not valid. Try to find the widget based on the provider info.
2160 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
2161 } else {
2162 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
2163 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002164
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002165 // If the provider is ready, but the width is not yet restored, try to restore it.
2166 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
2167 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07002168 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002169 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
2170 + " belongs to component " + item.providerName
2171 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002172 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002173 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002174 }
Sunny Goyalff572272014-07-23 13:58:07 -07002175
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002176 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002177 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07002178 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2179 // Id has not been allocated yet. Allocate a new id.
2180 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
2181 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07002182
Sunny Goyald478c832016-04-01 12:04:16 -07002183 // Also try to bind the widget. If the bind fails, the user will be shown
2184 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08002185 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07002186 pendingInfo.spanX = item.spanX;
2187 pendingInfo.spanY = item.spanY;
2188 pendingInfo.minSpanX = item.minSpanX;
2189 pendingInfo.minSpanY = item.minSpanY;
Samuel Fufaca37b8a2019-08-19 17:04:36 -07002190 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this,
2191 pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07002192
2193 boolean isDirectConfig =
2194 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2195 if (isDirectConfig && item.bindOptions != null) {
2196 Bundle newOptions = item.bindOptions.getExtras();
2197 if (options != null) {
2198 newOptions.putAll(options);
2199 }
2200 options = newOptions;
2201 }
Sunny Goyald478c832016-04-01 12:04:16 -07002202 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2203 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002204
Sunny Goyal86df1382016-08-10 15:03:22 -07002205 // We tried to bind once. If we were not able to bind, we would need to
2206 // go through the permission dialog, which means we cannot skip the config
2207 // activity.
2208 item.bindOptions = null;
2209 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2210
Sunny Goyald478c832016-04-01 12:04:16 -07002211 // Bind succeeded
2212 if (success) {
2213 // If the widget has a configure activity, it is still needs to set it up,
2214 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002215 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002216 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2217 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002218 }
Sunny Goyald478c832016-04-01 12:04:16 -07002219
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002220 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002221 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002222 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002223 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002224 // The widget was marked as UI not ready, but there is no configure activity to
2225 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002226 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002227 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002228 }
Sunny Goyalff572272014-07-23 13:58:07 -07002229 }
2230
Sunny Goyald5462aa2016-11-22 16:52:39 +05302231 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002232 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002233 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002234 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002235 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Tony Wickham6a71a5b2018-08-21 11:40:23 -07002236 getModelWriter().deleteWidgetInfo(item, getAppWidgetHost());
Sunny Goyalb23980c2017-08-17 07:45:25 -07002237 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002238 }
2239
Sunny Goyal233ee962015-08-03 13:05:01 -07002240 item.minSpanX = appWidgetInfo.minSpanX;
2241 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302242 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002243 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302244 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002245 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302246 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002247
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002248 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002249 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002250 }
2251
Sunny Goyalff572272014-07-23 13:58:07 -07002252 /**
2253 * Restores a pending widget.
2254 *
2255 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002256 */
Sunny Goyald478c832016-04-01 12:04:16 -07002257 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002258 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2259 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2260 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002261 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002262 }
2263
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002264 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002265 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002266 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2267 info.pendingItemInfo = null;
2268 }
Sunny Goyalff572272014-07-23 13:58:07 -07002269
Sunny Goyalba47faa2017-10-04 16:50:57 -07002270 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002271 view.reInflate();
Sunny Goyalba47faa2017-10-04 16:50:57 -07002272 }
2273
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002274 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002275 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002276 }
2277
Adam Cohen1462de32012-07-24 22:34:36 -07002278 public void onPageBoundSynchronously(int page) {
Sunny Goyalf4aceab2017-12-06 16:16:31 -08002279 mSynchronouslyBoundPage = page;
Adam Cohen1462de32012-07-24 22:34:36 -07002280 }
2281
Sunny Goyal527c7d32015-08-28 15:19:36 -07002282 @Override
2283 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
Sunny Goyal6b6d0a32019-08-05 17:19:32 -07002284 clearPendingBinds();
Sunny Goyal527c7d32015-08-28 15:19:36 -07002285 mPendingExecutor = executor;
Sunny Goyal60180b02018-02-07 12:56:30 -08002286 if (!isInState(ALL_APPS)) {
vadimtd4c90e12019-06-04 13:59:43 -07002287 mAppsView.getAppsStore().enableDeferUpdates(AllAppsStore.DEFER_UPDATES_NEXT_DRAW);
2288 mPendingExecutor.execute(() -> mAppsView.getAppsStore().disableDeferUpdates(
2289 AllAppsStore.DEFER_UPDATES_NEXT_DRAW));
Sunny Goyal60180b02018-02-07 12:56:30 -08002290 }
2291
Sunny Goyal527c7d32015-08-28 15:19:36 -07002292 executor.attachTo(this);
2293 }
2294
2295 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2296 if (mPendingExecutor == executor) {
2297 mPendingExecutor = null;
2298 }
2299 }
2300
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002301 @Override
2302 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
Sunny Goyal6001ea22018-05-10 16:31:00 -07002303 AlphaProperty property = mDragLayer.getAlphaProperty(ALPHA_INDEX_LAUNCHER_LOAD);
2304 if (property.getValue() < 1) {
2305 ObjectAnimator anim = ObjectAnimator.ofFloat(property, MultiValueAlpha.VALUE, 1);
2306 if (executor != null) {
2307 anim.addListener(new AnimatorListenerAdapter() {
2308 @Override
2309 public void onAnimationEnd(Animator animation) {
2310 executor.onLoadAnimationCompleted();
2311 }
2312 });
2313 }
2314 anim.start();
Sunny Goyal29947f02017-12-18 13:49:44 -08002315 } else if (executor != null) {
2316 executor.onLoadAnimationCompleted();
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002317 }
2318 }
2319
Joe Onorato9c1289c2009-08-17 11:03:03 -04002320 /**
2321 * Callback saying that there aren't any more items to bind.
2322 *
2323 * Implementation of the method from LauncherModel.Callbacks.
2324 */
Tonyf80e8932018-12-21 11:58:04 -08002325 public void finishBindingItems(int pageBoundFirst) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002326 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002327 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002328
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002329 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002330
Sunny Goyal2100c782016-08-22 16:00:03 -07002331 if (mPendingActivityResult != null) {
2332 handleActivityResult(mPendingActivityResult.requestCode,
2333 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2334 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002335 }
2336
Sunny Goyala474a9b2017-05-04 16:47:11 -07002337 InstallShortcutReceiver.disableAndFlushInstallQueue(
2338 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002339
Tonyf80e8932018-12-21 11:58:04 -08002340 // When undoing the removal of the last item on a page, return to that page.
Tony Wickham03f27012019-04-25 14:22:54 -07002341 // Since we are just resetting the current page without user interaction,
2342 // override the previous page so we don't log the page switch.
2343 mWorkspace.setCurrentPage(pageBoundFirst, pageBoundFirst /* overridePrevPage */);
Tony Wickham6a71a5b2018-08-21 11:40:23 -07002344
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002345 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002346 }
2347
Winson Chunga2413752012-04-03 14:22:34 -07002348 private boolean canRunNewAppsAnimation() {
2349 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002350 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002351 }
2352
Winson Chungc9168342013-06-26 14:54:55 -07002353 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002354 ValueAnimator bounceAnim = new PropertyListBuilder().alpha(1).scale(1).build(v)
2355 .setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
Winson Chungc9168342013-06-26 14:54:55 -07002356 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002357 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002358 return bounceAnim;
2359 }
2360
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002361 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002362 * Add the icons for all apps.
2363 *
2364 * Implementation of the method from LauncherModel.Callbacks.
2365 */
Sunny Goyal87dcde62019-07-17 20:35:56 -07002366 public void bindAllApplications(AppInfo[] apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002367 mAppsView.getAppsStore().setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002368 }
2369
2370 /**
Zak Cohen658c67a2018-10-19 14:21:05 -07002371 * Copies LauncherModel's map of activities to shortcut counts to Launcher's. This is necessary
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002372 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2373 */
2374 @Override
Zak Cohen658c67a2018-10-19 14:21:05 -07002375 public void bindDeepShortcutMap(HashMap<ComponentKey, Integer> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002376 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002377 }
2378
Sunny Goyal4390ace2014-10-13 11:33:11 -07002379 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002380 public void bindPromiseAppProgressUpdated(PromiseAppInfo app) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002381 mAppsView.getAppsStore().updatePromiseAppProgress(app);
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002382 }
2383
2384 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002385 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002386 mWorkspace.widgetsRestored(widgets);
2387 }
2388
Joe Onorato9c1289c2009-08-17 11:03:03 -04002389 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002390 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002391 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002392 *
2393 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002394 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002395 @Override
Sunny Goyal95899162019-03-27 16:03:06 -07002396 public void bindWorkspaceItemsChanged(ArrayList<WorkspaceItemInfo> updated) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002397 if (!updated.isEmpty()) {
2398 mWorkspace.updateShortcuts(updated);
2399 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002400 }
2401
2402 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002403 * Update the state of a package, typically related to install state.
2404 *
2405 * Implementation of the method from LauncherModel.Callbacks.
2406 */
Sunny Goyale755d462014-07-22 13:48:29 -07002407 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002408 public void bindRestoreItemsChange(HashSet<ItemInfo> updates) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07002409 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07002410 }
2411
2412 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002413 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07002414 * in addition to specific applications to remove, the reason being that
2415 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002416 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07002417 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002418 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07002419 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002420 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002421 mWorkspace.removeItemsByMatcher(matcher);
2422 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002423 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002424
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002425 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002426 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
2427 mPopupDataProvider.setAllWidgets(allWidgets);
Tony Wickham26b17462017-03-20 17:12:24 -07002428 }
2429
Tony Wickham86222d22017-03-29 15:30:43 -07002430 /**
2431 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
2432 * refreshes the widgets and shortcuts associated with the given package/user
2433 */
2434 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07002435 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002436 }
2437
Winson Chung80baf5a2010-08-09 16:03:15 -07002438 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002439 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08002440 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002441 @Override
2442 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
2443 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002444
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002445 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
2446 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07002447 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002448 writer.println(prefix + " Homescreen " + i);
2449
2450 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
2451 for (int j = 0; j < layout.getChildCount(); j++) {
2452 Object tag = layout.getChildAt(j).getTag();
2453 if (tag != null) {
2454 writer.println(prefix + " " + tag.toString());
2455 }
2456 }
2457 }
2458
2459 writer.println(prefix + " Hotseat");
Sunny Goyal876e4622018-11-02 13:50:40 -07002460 ViewGroup layout = mHotseat.getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07002461 for (int j = 0; j < layout.getChildCount(); j++) {
2462 Object tag = layout.getChildAt(j).getTag();
2463 if (tag != null) {
2464 writer.println(prefix + " " + tag.toString());
2465 }
Adam Cohen4caf2982013-08-20 18:54:31 -07002466 }
Sunny Goyala1365452015-10-01 15:46:24 -07002467 }
2468
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002469 writer.println(prefix + "Misc:");
2470 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
2471 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
2472 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
Sunny Goyale43d00d2018-05-14 14:23:18 -07002473 writer.println(" mRotationHelper: " + mRotationHelper);
Sunny Goyalcb2c5052018-10-15 12:30:29 -07002474 // Extra logging for b/116853349
Hyunyoung Songa310a802019-04-25 13:57:16 -07002475 mDragLayer.dump(prefix, writer);
2476 mStateManager.dump(prefix, writer);
Sunny Goyale43d00d2018-05-14 14:23:18 -07002477 dumpMisc(writer);
Sunny Goyal2bba1902018-02-27 12:20:50 -08002478
2479 try {
2480 FileLog.flushAll(writer);
2481 } catch (Exception e) {
2482 // Ignore
2483 }
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002484
2485 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002486
Adam Cohen9211d422014-10-07 18:14:53 -07002487 if (mLauncherCallbacks != null) {
2488 mLauncherCallbacks.dump(prefix, fd, writer, args);
2489 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002490 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002491
Sunny Goyal66b24572016-09-21 15:57:55 -07002492 @Override
Sunny Goyal66b24572016-09-21 15:57:55 -07002493 public void onProvideKeyboardShortcuts(
2494 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
2495
2496 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002497 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002498 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
2499 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
Vadim Tryshev74c261c2018-06-12 13:44:43 -07002500 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.widget_button_text),
2501 KeyEvent.KEYCODE_W, KeyEvent.META_CTRL_ON));
Sunny Goyal66b24572016-09-21 15:57:55 -07002502 }
Vadim Tryshev3455f8d2018-05-11 20:02:20 -07002503 final View currentFocus = getCurrentFocus();
2504 if (currentFocus != null) {
2505 if (new CustomActionsPopup(this, currentFocus).canShow()) {
2506 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
2507 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
2508 }
2509 if (currentFocus.getTag() instanceof ItemInfo
Pinyao Ting49a3e692019-07-26 12:28:38 -07002510 && ShortcutUtil.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
Vadim Tryshev3455f8d2018-05-11 20:02:20 -07002511 shortcutInfos.add(new KeyboardShortcutInfo(
2512 getString(R.string.shortcuts_menu_with_notifications_description),
2513 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
2514 }
Sunny Goyal66b24572016-09-21 15:57:55 -07002515 }
2516 if (!shortcutInfos.isEmpty()) {
2517 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
2518 }
2519
2520 super.onProvideKeyboardShortcuts(data, menu, deviceId);
2521 }
2522
2523 @Override
2524 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
2525 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
2526 switch (keyCode) {
2527 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002528 if (isInState(NORMAL)) {
2529 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07002530 return true;
2531 }
2532 break;
2533 case KeyEvent.KEYCODE_S: {
2534 View focusedView = getCurrentFocus();
2535 if (focusedView instanceof BubbleTextView
2536 && focusedView.getTag() instanceof ItemInfo
2537 && mAccessibilityDelegate.performAction(focusedView,
Samuel Fufaca37b8a2019-08-19 17:04:36 -07002538 (ItemInfo) focusedView.getTag(),
2539 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal00ac9202017-11-09 15:24:06 -08002540 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07002541 return true;
2542 }
2543 break;
2544 }
2545 case KeyEvent.KEYCODE_O:
2546 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
2547 return true;
2548 }
2549 break;
Vadim Tryshev74c261c2018-06-12 13:44:43 -07002550 case KeyEvent.KEYCODE_W:
2551 if (isInState(NORMAL)) {
2552 OptionsPopupView.openWidgets(this);
2553 return true;
2554 }
2555 break;
Sunny Goyal66b24572016-09-21 15:57:55 -07002556 }
2557 }
2558 return super.onKeyShortcut(keyCode, event);
2559 }
2560
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002561 @Override
2562 public boolean onKeyUp(int keyCode, KeyEvent event) {
2563 if (keyCode == KeyEvent.KEYCODE_MENU) {
2564 // KEYCODE_MENU is sent by some tests, for example
2565 // LauncherJankTests#testWidgetsContainerFling. Don't just remove its handling.
2566 if (!mDragController.isDragging() && !mWorkspace.isSwitchingState() &&
2567 isInState(NORMAL)) {
2568 // Close any open floating views.
2569 AbstractFloatingView.closeAllOpenViews(this);
2570
2571 // Setting the touch point to (-1, -1) will show the options popup in the center of
2572 // the screen.
Sunny Goyal2fd7a8b2018-03-30 17:10:13 -07002573 OptionsPopupView.showDefaultOptions(this, -1, -1);
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002574 }
2575 return true;
2576 }
2577 return super.onKeyUp(keyCode, event);
2578 }
2579
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002580 public static Launcher getLauncher(Context context) {
Sunny Goyal87b5eb62018-07-03 15:53:39 -07002581 return (Launcher) fromContext(context);
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002582 }
2583
Vinit Nayakf9b585b2019-07-10 14:25:32 -07002584 @Override
2585 public void returnToHomescreen() {
2586 super.returnToHomescreen();
2587 getStateManager().goToState(LauncherState.NORMAL);
2588 }
2589
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002590 /**
Sunny Goyalab770a12018-11-14 15:17:26 -08002591 * Just a wrapper around the type cast to allow easier tracking of calls.
2592 */
2593 public static <T extends Launcher> T cast(ActivityContext activityContext) {
2594 return (T) activityContext;
2595 }
2596
2597 /**
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002598 * Callback for listening for onResume
2599 */
2600 public interface OnResumeCallback {
2601
2602 void onLauncherResume();
2603 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002604}