blob: ed0b90fcce81e8238542a013001ed831d8599aae [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Jon Mirandaaeb4dd02018-07-11 14:16:23 -070019import static android.content.pm.ActivityInfo.CONFIG_LOCALE;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080020import static android.content.pm.ActivityInfo.CONFIG_ORIENTATION;
21import static android.content.pm.ActivityInfo.CONFIG_SCREEN_SIZE;
vadimt00d42552018-12-11 17:59:36 -080022
Tony Wickham6a71a5b2018-08-21 11:40:23 -070023import static com.android.launcher3.AbstractFloatingView.TYPE_SNACKBAR;
Sunny Goyal4c7f2152017-10-17 17:17:16 -070024import static com.android.launcher3.LauncherAnimUtils.SPRING_LOADED_EXIT_DELAY;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -070025import static com.android.launcher3.LauncherState.ALL_APPS;
26import static com.android.launcher3.LauncherState.NORMAL;
Govinda Wassermanc06e1512019-04-09 09:56:53 -040027import static com.android.launcher3.LauncherState.OVERVIEW;
28import static com.android.launcher3.LauncherState.OVERVIEW_PEEK;
Sunny Goyal6001ea22018-05-10 16:31:00 -070029import static com.android.launcher3.dragndrop.DragLayer.ALPHA_INDEX_LAUNCHER_LOAD;
Sunny Goyalaeb16432017-10-16 11:46:41 -070030import static com.android.launcher3.logging.LoggerUtils.newContainerTarget;
Hyunyoung Song46d07f72018-05-22 15:41:25 -070031import static com.android.launcher3.logging.LoggerUtils.newTarget;
Sunny Goyalbe8c3402019-05-22 15:17:52 -070032import static com.android.launcher3.states.RotationHelper.REQUEST_NONE;
vadimt00d42552018-12-11 17:59:36 -080033import static com.android.launcher3.util.RaceConditionTracker.ENTER;
34import static com.android.launcher3.util.RaceConditionTracker.EXIT;
Sunny Goyal326403e2017-10-02 12:45:10 -070035
Gilles Debunnedd6c9922010-10-25 11:23:41 -070036import android.animation.Animator;
Sunny Goyal6001ea22018-05-10 16:31:00 -070037import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070038import android.animation.AnimatorSet;
Sunny Goyal6001ea22018-05-10 16:31:00 -070039import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070040import android.animation.ValueAnimator;
Adam Cohen0b395352014-06-09 22:54:36 +000041import android.annotation.TargetApi;
Tony Wickham005df0b2018-02-13 11:28:12 -080042import android.app.ActivityOptions;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080043import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070044import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080046import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080047import android.content.ComponentCallbacks2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070050import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070051import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070052import android.content.SharedPreferences;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053import android.content.pm.PackageManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080054import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070055import android.database.sqlite.SQLiteDatabase;
Sunny Goyal59d086c2018-05-07 17:31:40 -070056import android.graphics.Point;
Sunny Goyalae6e3182019-04-30 12:04:37 -070057import android.graphics.Rect;
Adam Cohen0f668f32014-09-08 19:54:17 +020058import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059import android.os.Bundle;
Hyunyoung Songbd6fba92018-05-16 15:54:31 -070060import android.os.Handler;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070061import android.os.Parcelable;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080062import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070063import android.os.StrictMode;
Michael Jurkaa33411c2012-06-14 16:18:21 -070064import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070066import android.util.Log;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070067import android.util.SparseArray;
Sunny Goyal59d086c2018-05-07 17:31:40 -070068import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070069import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070070import android.view.KeyboardShortcutGroup;
71import android.view.KeyboardShortcutInfo;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080072import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -070073import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070074import android.view.View;
Adam Cohen96d30a12013-07-16 18:13:21 -070075import android.view.ViewGroup;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080076import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070077import android.view.animation.OvershootInterpolator;
Adam Cohen96d30a12013-07-16 18:13:21 -070078import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070079
Sunny Goyal651077b2014-06-30 14:15:31 -070080import com.android.launcher3.DropTarget.DragObject;
Sunny Goyalae502842016-06-17 08:43:56 -070081import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070082import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070083import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal7ede6112017-12-05 15:11:21 -080084import com.android.launcher3.allapps.DiscoveryBounce;
Sunny Goyalf0b6db72018-08-13 16:10:14 -070085import com.android.launcher3.anim.PropertyListBuilder;
Sunny Goyalffe83f12014-08-14 17:39:34 -070086import com.android.launcher3.compat.AppWidgetManagerCompat;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -070087import com.android.launcher3.compat.LauncherAppsCompatVO;
Tony Wickhame0c33232016-02-08 11:37:04 -080088import com.android.launcher3.config.FeatureFlags;
vadimt00d42552018-12-11 17:59:36 -080089import com.android.launcher3.dot.DotInfo;
Vadim Tryshevfedca432015-08-19 17:55:02 -070090import com.android.launcher3.dragndrop.DragController;
91import com.android.launcher3.dragndrop.DragLayer;
92import com.android.launcher3.dragndrop.DragView;
Jon Mirandaaeb4dd02018-07-11 14:16:23 -070093import com.android.launcher3.folder.Folder;
Sunny Goyal26119432016-02-18 22:09:23 +000094import com.android.launcher3.folder.FolderIcon;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070095import com.android.launcher3.folder.FolderIconPreviewVerifier;
Sunny Goyalae6e3182019-04-30 12:04:37 -070096import com.android.launcher3.graphics.RotationMode;
Sunny Goyalf840f102018-09-21 14:41:05 -070097import com.android.launcher3.icons.IconCache;
Sunny Goyal66b24572016-09-21 15:57:55 -070098import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -070099import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700100import com.android.launcher3.logging.FileLog;
Hyunyoung Songfc007472018-10-25 14:09:50 -0700101import com.android.launcher3.logging.StatsLogUtils;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700102import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700103import com.android.launcher3.logging.UserEventDispatcher.UserEventDelegate;
Sunny Goyal369212a2019-03-26 15:03:57 -0700104import com.android.launcher3.model.AppLaunchTracker;
Sunny Goyala535ae42017-02-27 10:07:13 -0800105import com.android.launcher3.model.ModelWriter;
Sunny Goyala535ae42017-02-27 10:07:13 -0800106import com.android.launcher3.notification.NotificationListener;
Tony Wickham540913e2017-01-23 11:47:51 -0800107import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -0800108import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700109import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyalf8088ee2017-11-10 14:52:00 -0800110import com.android.launcher3.states.InternalStateHandler;
Sunny Goyal623eddd2018-03-02 12:24:41 -0800111import com.android.launcher3.states.RotationHelper;
Sunny Goyalab3963d2019-05-23 00:50:08 -0700112import com.android.launcher3.testing.TestProtocol;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800113import com.android.launcher3.touch.ItemClickHandler;
Sunny Goyala5ace712017-11-15 17:12:51 -0800114import com.android.launcher3.uioverrides.UiFactory;
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700115import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530116import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
117import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
Sunny Goyalaeb16432017-10-16 11:46:41 -0700118import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyal2100c782016-08-22 16:00:03 -0700119import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700120import com.android.launcher3.util.ComponentKey;
Sunny Goyalefb7e842018-10-04 15:11:00 -0700121import com.android.launcher3.util.IntArray;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700122import com.android.launcher3.util.ItemInfoMatcher;
Sunny Goyal6001ea22018-05-10 16:31:00 -0700123import com.android.launcher3.util.MultiValueAlpha;
124import com.android.launcher3.util.MultiValueAlpha.AlphaProperty;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700125import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800126import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700127import com.android.launcher3.util.PendingRequestArgs;
vadimt00d42552018-12-11 17:59:36 -0800128import com.android.launcher3.util.RaceConditionTracker;
Sunny Goyal8392c822017-06-20 10:03:56 -0700129import com.android.launcher3.util.SystemUiController;
Mario Bertschlera6936942017-05-31 14:48:19 -0700130import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700131import com.android.launcher3.util.Thunk;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700132import com.android.launcher3.util.TraceHelper;
Sunny Goyal326403e2017-10-02 12:45:10 -0700133import com.android.launcher3.util.UiThreadHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700134import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyalab770a12018-11-14 15:17:26 -0800135import com.android.launcher3.views.ActivityContext;
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800136import com.android.launcher3.views.OptionsPopupView;
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400137import com.android.launcher3.views.ScrimView;
Sunny Goyal29947f02017-12-18 13:49:44 -0800138import com.android.launcher3.widget.LauncherAppWidgetHostView;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800139import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700140import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal29947f02017-12-18 13:49:44 -0800141import com.android.launcher3.widget.PendingAppWidgetHostView;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800142import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700143import com.android.launcher3.widget.WidgetHostViewLoader;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700144import com.android.launcher3.widget.WidgetListRowEntry;
145import com.android.launcher3.widget.WidgetsFullSheet;
Sunny Goyal952e63d2017-08-16 04:59:08 -0700146import com.android.launcher3.widget.custom.CustomWidgetParser;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700147
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700148import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700149import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700150import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700151import java.util.Collection;
Zak Cohen658c67a2018-10-19 14:21:05 -0700152import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700153import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700154import java.util.List;
Sunny Goyalaae6fbb2019-01-31 16:05:58 -0800155import java.util.function.Predicate;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700156
Sunny Goyalbe8c3402019-05-22 15:17:52 -0700157import androidx.annotation.Nullable;
158
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159/**
160 * Default launcher application.
161 */
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700162public class Launcher extends BaseDraggingActivity implements LauncherExterns,
Hyunyoung Song31971a32019-01-27 12:31:12 -0800163 LauncherModel.Callbacks, LauncherProviderChangeListener, UserEventDelegate,
164 InvariantDeviceProfile.OnIDPChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700165 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700166 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167
Winson Chunga2413752012-04-03 14:22:34 -0700168 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700169
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700171 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700172
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700173 private static final int REQUEST_PICK_APPWIDGET = 9;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174
Michael Jurka8b805b12012-04-18 14:23:14 -0700175 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800176 public static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
177 public static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700178
Jon Mirandac476d6e2017-05-04 16:30:01 -0700179 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700180
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700181 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
182
Mathew Inwood876a8462013-06-14 14:12:41 +0100183 /**
184 * IntentStarter uses request codes starting with this. This must be greater than all activity
185 * request codes used internally.
186 */
187 protected static final int REQUEST_LAST = 100;
188
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189 // Type: int
190 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700191 // Type: int
192 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700193 // Type: PendingRequestArgs
194 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
Sunny Goyaldedda052019-05-14 15:23:48 -0700195 // Type: int
196 private static final String RUNTIME_STATE_PENDING_REQUEST_CODE = "launcher.request_code";
Sunny Goyal2100c782016-08-22 16:00:03 -0700197 // Type: ActivityResultInfo
198 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700199 // Type: SparseArray<Parcelable>
200 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
vadimt00d42552018-12-11 17:59:36 -0800201 public static final String ON_CREATE_EVT = "Launcher.onCreate";
vadimtcb863752018-12-19 17:44:57 -0800202 private static final String ON_START_EVT = "Launcher.onStart";
203 private static final String ON_RESUME_EVT = "Launcher.onResume";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800204
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700205 private LauncherStateManager mStateManager;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700206
Adam Cohenad4e15c2013-10-17 16:21:35 -0700207 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208
Winson Chunga2413752012-04-03 14:22:34 -0700209 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700210 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
211 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
212 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700213
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400214 private static final int APPS_VIEW_ALPHA_CHANNEL_INDEX = 1;
215 private static final int SCRIM_VIEW_ALPHA_CHANNEL_INDEX = 0;
216
Jon Miranda54441f52018-01-24 15:38:25 -0800217 private LauncherAppTransitionManager mAppTransitionManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800218 private Configuration mOldConfig;
Jon Miranda54441f52018-01-24 15:38:25 -0800219
Adam Cohen091440a2015-03-18 14:16:05 -0700220 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700221 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700222 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800223 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700224
Sunny Goyalffe83f12014-08-14 17:39:34 -0700225 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700226 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700227
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700228 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700229
Sunny Goyal316490e2015-06-02 09:38:28 -0700230 @Thunk Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700231
Sunny Goyal47328fd2016-05-25 18:56:41 -0700232 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700233
234 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700235 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700236 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700237
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400238 // Scrim view for the all apps and overview state.
239 @Thunk ScrimView mScrimView;
240
Winson Chung8ae41982017-11-10 11:42:13 -0800241 // UI and state for the overview panel
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800242 private View mOverviewPanel;
Winson Chung8ae41982017-11-10 11:42:13 -0800243
Adam Cohen091440a2015-03-18 14:16:05 -0700244 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400245
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700246 private OnResumeCallback mOnResumeCallback;
247
Sunny Goyal527c7d32015-08-28 15:19:36 -0700248 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700249
Joe Onorato9c1289c2009-08-17 11:03:03 -0400250 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800251 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800252 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700253 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400254
Tony Wickham010d2552017-01-20 08:15:28 -0800255 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700256
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800257 private int mSynchronouslyBoundPage = PagedView.INVALID_PAGE;
Adam Cohen1462de32012-07-24 22:34:36 -0700258
Winson Chung46353de2012-02-16 14:05:10 -0800259 // We only want to get the SharedPreferences once since it does an FS stat each time we get
260 // it from the context.
261 private SharedPreferences mSharedPrefs;
262
Sunny Goyal2100c782016-08-22 16:00:03 -0700263 // Activity result which needs to be processed after workspace has loaded.
264 private ActivityResultInfo mPendingActivityResult;
265 /**
266 * Holds extra information required to handle a result from an external call, like
267 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
268 */
269 private PendingRequestArgs mPendingRequestArgs;
Sunny Goyaldedda052019-05-14 15:23:48 -0700270 // Request id for any pending activity result
271 private int mPendingActivityRequestCode = -1;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800272
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700273 public ViewGroupFocusHelper mFocusHandler;
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700274
Sunny Goyal623eddd2018-03-02 12:24:41 -0800275 private RotationHelper mRotationHelper;
Sunny Goyal5743f862019-03-28 15:35:32 -0700276 private Runnable mCancelTouchController;
Sunny Goyal7779d622015-06-11 16:18:39 -0700277
Sunny Goyal2db53422019-03-01 16:06:12 -0800278 final Handler mHandler = new Handler();
Sunny Goyala002c6c2019-02-12 16:20:10 -0800279 private final Runnable mHandleDeferredResume = this::handleDeferredResume;
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700280
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400281 private float mCurrentAssistantVisibility = 0f;
282
Sunny Goyalae6e3182019-04-30 12:04:37 -0700283 private DeviceProfile mStableDeviceProfile;
284 private RotationMode mRotationMode = RotationMode.NORMAL;
285
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800286 @Override
287 protected void onCreate(Bundle savedInstanceState) {
vadimt00d42552018-12-11 17:59:36 -0800288 RaceConditionTracker.onEvent(ON_CREATE_EVT, ENTER);
Winson Chunga2413752012-04-03 14:22:34 -0700289 if (DEBUG_STRICT_MODE) {
290 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
291 .detectDiskReads()
292 .detectDiskWrites()
293 .detectNetwork() // or .detectAll() for all detectable problems
294 .penaltyLog()
295 .build());
296 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
297 .detectLeakedSqlLiteObjects()
298 .detectLeakedClosableObjects()
299 .penaltyLog()
300 .penaltyDeath()
301 .build());
302 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700303 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700304
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800305 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700306 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400307
Sunny Goyal87f784c2017-01-11 10:48:34 -0800308 LauncherAppState app = LauncherAppState.getInstance(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800309 mOldConfig = new Configuration(getResources().getConfiguration());
Sunny Goyal0e7724c2018-02-22 13:22:21 -0800310 mModel = app.setLauncher(this);
Sunny Goyal41d51a02019-05-14 09:44:34 -0700311 mRotationHelper = new RotationHelper(this);
Hyunyoung Song31971a32019-01-27 12:31:12 -0800312 InvariantDeviceProfile idp = app.getInvariantDeviceProfile();
313 initDeviceProfile(idp);
314 idp.addOnChangeListener(this);
Sunny Goyalf7258242015-10-19 16:59:07 -0700315 mSharedPrefs = Utilities.getPrefs(this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800316 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700317 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700318
Joe Onorato41a12d22009-10-31 18:30:00 -0400319 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700320 mAllAppsController = new AllAppsTransitionController(this);
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800321 mStateManager = new LauncherStateManager(this);
Tracy Zhou8b23c6f2018-04-30 17:13:27 -0700322 UiFactory.onCreate(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700323
Sunny Goyalffe83f12014-08-14 17:39:34 -0700324 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700325
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700326 mAppWidgetHost = new LauncherAppWidgetHost(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700327 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700328
Sunny Goyal60820d72017-05-09 12:40:11 -0700329 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700330
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800331 setupViews();
Tony Wickham010d2552017-01-20 08:15:28 -0800332 mPopupDataProvider = new PopupDataProvider(this);
333
Sunny Goyal90548432018-04-04 17:17:00 -0700334 mAppTransitionManager = LauncherAppTransitionManager.newInstance(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800335
Sunny Goyald3864fa2017-11-14 15:06:36 -0800336 boolean internalStateHandled = InternalStateHandler.handleCreate(this, getIntent());
337 if (internalStateHandled) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800338 if (savedInstanceState != null) {
339 // InternalStateHandler has already set the appropriate state.
340 // We dont need to do anything.
341 savedInstanceState.remove(RUNTIME_STATE);
342 }
343 }
344 restoreState(savedInstanceState);
Sunny Goyalbbece862019-03-01 13:13:52 -0800345 mStateManager.reapplyState();
Winson Chungb63b44c2017-11-10 17:54:44 -0800346
Sunny Goyal2100c782016-08-22 16:00:03 -0700347 // We only load the page synchronously if the user rotates (or triggers a
348 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700349 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
350 if (savedInstanceState != null) {
351 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
352 }
Sunny Goyald3864fa2017-11-14 15:06:36 -0800353
Sunny Goyalfe770c92016-09-27 14:38:58 -0700354 if (!mModel.startLoader(currentScreen)) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800355 if (!internalStateHandled) {
356 // If we are not binding synchronously, show a fade in animation when
357 // the first page bind completes.
Sunny Goyal6001ea22018-05-10 16:31:00 -0700358 mDragLayer.getAlphaProperty(ALPHA_INDEX_LAUNCHER_LOAD).setValue(0);
Sunny Goyald3864fa2017-11-14 15:06:36 -0800359 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700360 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700361 // Pages bound synchronously.
362 mWorkspace.setCurrentPage(currentScreen);
363
Sunny Goyal2100c782016-08-22 16:00:03 -0700364 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800365 }
366
367 // For handling default keys
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800368 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800369
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800370 setContentView(mLauncherView);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800371 getRootView().dispatchInsets();
Mario Bertschlera6936942017-05-31 14:48:19 -0700372
Jon Miranda7fda2852017-07-19 16:07:01 -0700373 // Listen for broadcasts
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700374 registerReceiver(mScreenOffReceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));
Sunny Goyala616d2b2017-06-12 13:54:01 -0700375
Sunny Goyal8392c822017-06-20 10:03:56 -0700376 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
377 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700378
379 if (mLauncherCallbacks != null) {
380 mLauncherCallbacks.onCreate(savedInstanceState);
381 }
Sunny Goyal623eddd2018-03-02 12:24:41 -0800382 mRotationHelper.initialize();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700383
384 TraceHelper.endSection("Launcher-onCreate");
vadimt00d42552018-12-11 17:59:36 -0800385 RaceConditionTracker.onEvent(ON_CREATE_EVT, EXIT);
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400386 mStateManager.addStateListener(new LauncherStateManager.StateListener() {
387 @Override
388 public void onStateTransitionStart(LauncherState toState) {}
389
390 @Override
391 public void onStateTransitionComplete(LauncherState finalState) {
392 float alpha = 1f - mCurrentAssistantVisibility;
393 if (finalState == NORMAL) {
394 mAppsView.getAlphaProperty(APPS_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
395 } else if (finalState == OVERVIEW || finalState == OVERVIEW_PEEK) {
396 mAppsView.getAlphaProperty(APPS_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
397 mScrimView.getAlphaProperty(SCRIM_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
398 } else {
399 mAppsView.getAlphaProperty(APPS_VIEW_ALPHA_CHANNEL_INDEX).setValue(1f);
400 mScrimView.getAlphaProperty(SCRIM_VIEW_ALPHA_CHANNEL_INDEX).setValue(1f);
401 }
402 }
403 });
Adam Cohen9211d422014-10-07 18:14:53 -0700404 }
405
Sunny Goyal7779d622015-06-11 16:18:39 -0700406 @Override
Winson Chung8eb49e02018-07-16 13:33:31 -0700407 public void onEnterAnimationComplete() {
408 super.onEnterAnimationComplete();
409 UiFactory.onEnterAnimationComplete(this);
arangelovad783052018-08-17 14:46:45 +0100410 mAllAppsController.highlightWorkTabIfNecessary();
Sunny Goyalbe8c3402019-05-22 15:17:52 -0700411 mRotationHelper.setCurrentTransitionRequest(REQUEST_NONE);
Winson Chung8eb49e02018-07-16 13:33:31 -0700412 }
413
414 @Override
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800415 public void onConfigurationChanged(Configuration newConfig) {
416 int diff = newConfig.diff(mOldConfig);
Jon Mirandaaeb4dd02018-07-11 14:16:23 -0700417
418 if ((diff & CONFIG_LOCALE) != 0) {
419 Folder.setLocaleDependentFields(getResources(), true /* force */);
420 }
421
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800422 if ((diff & (CONFIG_ORIENTATION | CONFIG_SCREEN_SIZE)) != 0) {
Sunny Goyal371ea052019-03-06 15:38:32 -0800423 onIdpChanged(mDeviceProfile.inv);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800424 }
425
426 mOldConfig.setTo(newConfig);
Tracy Zhoua706f002018-03-28 13:55:19 -0700427 UiFactory.onLauncherStateOrResumeChanged(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800428 super.onConfigurationChanged(newConfig);
429 }
430
Sunny Goyal41d51a02019-05-14 09:44:34 -0700431 private boolean supportsFakeLandscapeUI() {
432 return FeatureFlags.FAKE_LANDSCAPE_UI.get() && !mRotationHelper.homeScreenCanRotate();
433 }
434
Sunny Goyal605bcf32018-03-02 15:16:12 -0800435 @Override
Sunny Goyal41d51a02019-05-14 09:44:34 -0700436 public void reapplyUi() {
437 if (supportsFakeLandscapeUI()) {
Sunny Goyal7a39b1b2019-05-02 13:39:16 -0700438 mRotationMode = mStableDeviceProfile == null
439 ? RotationMode.NORMAL : UiFactory.getRotationMode(mDeviceProfile);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700440 }
Sunny Goyal59d086c2018-05-07 17:31:40 -0700441 getRootView().dispatchInsets();
442 getStateManager().reapplyState(true /* cancelCurrentAnimation */);
443 }
444
445 @Override
Sunny Goyal605bcf32018-03-02 15:16:12 -0800446 public void rebindModel() {
447 int currentPage = mWorkspace.getNextPage();
448 if (mModel.startLoader(currentPage)) {
449 mWorkspace.setCurrentPage(currentPage);
450 setWorkspaceLoading(true);
451 }
452 }
453
Hyunyoung Song31971a32019-01-27 12:31:12 -0800454 @Override
455 public void onIdpChanged(int changeFlags, InvariantDeviceProfile idp) {
Sunny Goyal371ea052019-03-06 15:38:32 -0800456 onIdpChanged(idp);
457 }
458
459 private void onIdpChanged(InvariantDeviceProfile idp) {
460 mUserEventDispatcher = null;
461
Hyunyoung Song31971a32019-01-27 12:31:12 -0800462 initDeviceProfile(idp);
Sunny Goyal371ea052019-03-06 15:38:32 -0800463 dispatchDeviceProfileChanged();
464 reapplyUi();
465 mDragLayer.recreateControllers();
466
467 // TODO: We can probably avoid rebind when only screen size changed.
468 rebindModel();
Hyunyoung Song31971a32019-01-27 12:31:12 -0800469 }
470
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400471 public void onAssistantVisibilityChanged(float visibility) {
472 mCurrentAssistantVisibility = visibility;
473 float alpha = 1f - visibility;
474 LauncherState state = mStateManager.getState();
475 if (state == NORMAL) {
476 mAppsView.getAlphaProperty(APPS_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
477 } else if (state == OVERVIEW || state == OVERVIEW_PEEK) {
478 mAppsView.getAlphaProperty(APPS_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
479 mScrimView.getAlphaProperty(SCRIM_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
480 }
481 }
482
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800483 private void initDeviceProfile(InvariantDeviceProfile idp) {
484 // Load configuration-specific DeviceProfile
Sunny Goyal59d086c2018-05-07 17:31:40 -0700485 mDeviceProfile = idp.getDeviceProfile(this);
Sunny Goyal8c48d8b2019-01-25 15:10:18 -0800486 if (isInMultiWindowMode()) {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700487 Display display = getWindowManager().getDefaultDisplay();
488 Point mwSize = new Point();
489 display.getSize(mwSize);
490 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
491 }
Sunny Goyalae6e3182019-04-30 12:04:37 -0700492
Sunny Goyalda14cf82019-05-30 10:31:09 -0700493 if (supportsFakeLandscapeUI() && mDeviceProfile.isVerticalBarLayout()) {
Sunny Goyalae6e3182019-04-30 12:04:37 -0700494 mStableDeviceProfile = mDeviceProfile.inv.portraitProfile;
Sunny Goyal7a39b1b2019-05-02 13:39:16 -0700495 mRotationMode = UiFactory.getRotationMode(mDeviceProfile);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700496 } else {
497 mStableDeviceProfile = null;
498 mRotationMode = RotationMode.NORMAL;
499 }
500
Sunny Goyalda14cf82019-05-30 10:31:09 -0700501 mRotationHelper.updateRotationAnimation();
Sunny Goyal59d086c2018-05-07 17:31:40 -0700502 onDeviceProfileInitiated();
Sunny Goyalae6e3182019-04-30 12:04:37 -0700503 mModelWriter = mModel.getWriter(getWallpaperDeviceProfile().isVerticalBarLayout(), true);
504 }
505
506 public void updateInsets(Rect insets) {
507 mDeviceProfile.updateInsets(insets);
508 if (mStableDeviceProfile != null) {
Sunny Goyal7a39b1b2019-05-02 13:39:16 -0700509 Rect r = mStableDeviceProfile.getInsets();
510 mRotationMode.mapInsets(this, insets, r);
511 mStableDeviceProfile.updateInsets(r);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700512 }
513 }
514
515 @Override
516 public RotationMode getRotationMode() {
517 return mRotationMode;
518 }
519
520 /**
521 * Device profile to be used by UI elements which are shown directly on top of the wallpaper
522 * and whose presentation is tied to the wallpaper (and physical device) and not the activity
523 * configuration.
524 */
525 @Override
526 public DeviceProfile getWallpaperDeviceProfile() {
527 return mStableDeviceProfile == null ? mDeviceProfile : mStableDeviceProfile;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800528 }
529
Sunny Goyal623eddd2018-03-02 12:24:41 -0800530 public RotationHelper getRotationHelper() {
531 return mRotationHelper;
532 }
533
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700534 public LauncherStateManager getStateManager() {
535 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700536 }
537
Mario Bertschler27288382017-05-24 15:35:09 -0700538 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700539 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800540 return mLauncherView.findViewById(id);
541 }
542
543 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700544 public void onAppWidgetHostReset() {
545 if (mAppWidgetHost != null) {
546 mAppWidgetHost.startListening();
547 }
548 }
549
Adam Cohen9211d422014-10-07 18:14:53 -0700550 private LauncherCallbacks mLauncherCallbacks;
551
Sunny Goyal32554d12015-12-03 15:31:25 -0800552 /**
553 * Call this after onCreate to set or clear overlay.
554 */
555 public void setLauncherOverlay(LauncherOverlay overlay) {
556 if (overlay != null) {
557 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
558 }
559 mWorkspace.setLauncherOverlay(overlay);
560 }
561
Adam Cohen9211d422014-10-07 18:14:53 -0700562 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
563 mLauncherCallbacks = callbacks;
564 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700565 }
566
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700567 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700568 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700569 if (mLauncherCallbacks != null) {
570 mLauncherCallbacks.onLauncherProviderChange();
571 }
572 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700573
Hyunyoung Song3f471442015-04-08 19:01:34 -0700574 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700575 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
576 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700577 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700578 }
579
Tony Wickham010d2552017-01-20 08:15:28 -0800580 public PopupDataProvider getPopupDataProvider() {
581 return mPopupDataProvider;
582 }
583
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700584 @Override
Tony Wickhamf34bee82018-12-03 18:11:39 -0800585 public DotInfo getDotInfoForItem(ItemInfo info) {
586 return mPopupDataProvider.getDotInfoForItem(info);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700587 }
588
589 @Override
590 public void invalidateParent(ItemInfo info) {
Jon Mirandae6f3fa42019-03-04 09:14:40 -0800591 if (info.container >= 0) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700592 View folderIcon = getWorkspace().getHomescreenIconByItemId(info.container);
Jon Mirandae6f3fa42019-03-04 09:14:40 -0800593 if (folderIcon instanceof FolderIcon && folderIcon.getTag() instanceof FolderInfo) {
594 FolderIconPreviewVerifier verifier =
595 new FolderIconPreviewVerifier(getDeviceProfile().inv);
596 verifier.setFolderInfo((FolderInfo) folderIcon.getTag());
597 if (verifier.isItemInPreview(info.rank)) {
598 folderIcon.invalidate();
599 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700600 }
601 }
602 }
603
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000604 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700605 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
606 * a configuration step, this allows the proper animations to run after other transitions.
607 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700608 private int completeAdd(
Sunny Goyal2100c782016-08-22 16:00:03 -0700609 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
Sunny Goyalefb7e842018-10-04 15:11:00 -0700610 int screenId = info.screenId;
Sunny Goyal2100c782016-08-22 16:00:03 -0700611 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700612 // When the screen id represents an actual screen (as opposed to a rank) we make sure
613 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700614 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700615 }
Adam Cohendb364c32014-05-20 14:23:40 -0700616
Sunny Goyal2100c782016-08-22 16:00:03 -0700617 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800618 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700619 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700620 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800621 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700622 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700623 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700624 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700625 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700626 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700627 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700628 int widgetId = appWidgetId;
629 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700630 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700631 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700632 // Since the view was just bound, also launch the configure activity if needed
633 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
634 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800635 if (provider != null) {
636 new WidgetAddFlowHandler(provider)
637 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700638 }
639 }
640 break;
641 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800642 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700643
Adam Cohendb364c32014-05-20 14:23:40 -0700644 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800645 }
646
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800647 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700648 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700649 if (isWorkspaceLoading()) {
650 // process the result once the workspace has loaded.
651 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
652 return;
653 }
654 mPendingActivityResult = null;
655
Winson Chunge341d302013-08-16 14:31:00 -0700656 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700657 final PendingRequestArgs requestArgs = mPendingRequestArgs;
658 setWaitingForResult(null);
659 if (requestArgs == null) {
660 return;
661 }
662
663 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700664
Adam Cohenad4e15c2013-10-17 16:21:35 -0700665 Runnable exitSpringLoaded = new Runnable() {
666 @Override
667 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700668 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700669 }
670 };
671
Michael Jurka8b805b12012-04-18 14:23:14 -0700672 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700673 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700674 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700675 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
676 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700677 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700678 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700679 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700680 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700681 addAppWidgetImpl(
682 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800683 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700684 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700685 }
686 return;
687 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200688
Adam Cohened66b2b2012-01-23 17:28:51 -0800689 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700690 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700691
Adam Cohened66b2b2012-01-23 17:28:51 -0800692 // We have special handling for widgets
693 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800694 final int appWidgetId;
695 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
696 : -1;
697 if (widgetId < 0) {
698 appWidgetId = pendingAddWidgetId;
699 } else {
700 appWidgetId = widgetId;
701 }
702
Adam Cohenad4e15c2013-10-17 16:21:35 -0700703 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800704 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700705 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
706 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700707 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700708 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700709 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700710 @Override
711 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700712 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700713 }
714 };
Adam Cohendb364c32014-05-20 14:23:40 -0700715
Sunny Goyal2100c782016-08-22 16:00:03 -0700716 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
717 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
718 } else {
719 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
720 // When the screen id represents an actual screen (as opposed to a rank)
721 // we make sure that the drop page actually exists.
722 requestArgs.screenId =
723 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700724 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700725 final CellLayout dropLayout =
726 mWorkspace.getScreenWithId(requestArgs.screenId);
727
728 dropLayout.setDropPending(true);
729 final Runnable onComplete = new Runnable() {
730 @Override
731 public void run() {
732 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
733 dropLayout.setDropPending(false);
734 }
735 };
736 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
737 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700738 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800739 return;
740 }
741
Sunny Goyald478c832016-04-01 12:04:16 -0700742 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
743 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700744 if (resultCode == RESULT_OK) {
745 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700746 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700747 }
748 // Leave the widget in the pending state if the user canceled the configure.
749 return;
750 }
751
Sunny Goyalaad90582015-07-09 14:22:50 -0700752 if (requestCode == REQUEST_CREATE_SHORTCUT) {
753 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700754 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
755 completeAdd(requestCode, data, -1, requestArgs);
756 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
757 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
758
Sunny Goyalaad90582015-07-09 14:22:50 -0700759 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700760 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
761 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800762 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800763 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800764 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800765 }
766
767 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700768 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800769 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyaldedda052019-05-14 15:23:48 -0700770 mPendingActivityRequestCode = -1;
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800771 handleActivityResult(requestCode, resultCode, data);
772 if (mLauncherCallbacks != null) {
773 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
774 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800775 }
776
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700777 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700778 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600779 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700780 PendingRequestArgs pendingArgs = mPendingRequestArgs;
781 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
782 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
783 setWaitingForResult(null);
784
Sunny Goyal28c6b962015-10-12 11:42:05 -0700785 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700786 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700787 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700788 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700789 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700790 Intent intent = pendingArgs.getPendingIntent();
791
Sunny Goyal28c6b962015-10-12 11:42:05 -0700792 if (grantResults.length > 0
793 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal369212a2019-03-26 15:03:57 -0700794 startActivitySafely(v, intent, null, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700795 } else {
796 // TODO: Show a snack bar with link to settings
797 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700798 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700799 }
800 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600801 if (mLauncherCallbacks != null) {
802 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
803 grantResults);
804 }
805 }
806
Adam Cohendb364c32014-05-20 14:23:40 -0700807 /**
808 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
809 *
810 * @param screenId the screen id to check
811 * @return the new screen, or screenId if it exists
812 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700813 private int ensurePendingDropLayoutExists(int screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800814 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700815 if (dropLayout == null) {
816 // it's possible that the add screen was removed because it was
817 // empty and a re-bind occurred
818 mWorkspace.addExtraEmptyScreen();
819 return mWorkspace.commitExtraEmptyScreen();
820 } else {
821 return screenId;
822 }
823 }
824
Sunny Goyal2100c782016-08-22 16:00:03 -0700825 @Thunk void completeTwoStageWidgetDrop(
826 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
827 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800828 Runnable onCompleteRunnable = null;
829 int animationType = 0;
830
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700831 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800832 if (resultCode == RESULT_OK) {
833 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
834 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800835 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700836 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800837 onCompleteRunnable = new Runnable() {
838 @Override
839 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700840 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700841 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800842 }
843 };
844 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800845 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800846 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800847 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700848 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700849 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700850 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
851 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700852 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700853 // The animated view may be null in the case of a rotation during widget configuration
854 onCompleteRunnable.run();
855 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800856 }
857
858 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700859 protected void onStop() {
860 super.onStop();
Adam Cohen9211d422014-10-07 18:14:53 -0700861 if (mLauncherCallbacks != null) {
862 mLauncherCallbacks.onStop();
863 }
Miranda Kephart1a359a22019-02-11 13:07:04 -0500864
Hyunyoung Song1241e612018-05-15 21:46:51 -0700865 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
866 mStateManager.getState().containerType, -1);
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700867
868 mAppWidgetHost.setListenIfResumed(false);
869
Tony Wickham010d2552017-01-20 08:15:28 -0800870 NotificationListener.removeNotificationsChangedListener();
Sunny Goyalaf161fd2018-02-05 16:34:57 -0800871 getStateManager().moveToRestState();
Winson Chungedb0d6b2018-03-21 17:32:55 -0700872
Tracy Zhou62646712018-06-26 14:19:02 -0700873 UiFactory.onLauncherStateOrResumeChanged(this);
874
Winson Chungdea51352018-04-24 13:02:10 -0700875 // Workaround for b/78520668, explicitly trim memory once UI is hidden
Winson Chung473730f2018-04-26 11:54:22 -0700876 onTrimMemory(TRIM_MEMORY_UI_HIDDEN);
Michael Jurkacd496d72013-04-11 11:32:45 -0700877 }
878
879 @Override
880 protected void onStart() {
vadimtcb863752018-12-19 17:44:57 -0800881 RaceConditionTracker.onEvent(ON_START_EVT, ENTER);
Michael Jurkacd496d72013-04-11 11:32:45 -0700882 super.onStart();
Adam Cohen9211d422014-10-07 18:14:53 -0700883 if (mLauncherCallbacks != null) {
884 mLauncherCallbacks.onStart();
885 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800886 mAppWidgetHost.setListenIfResumed(true);
Sunny Goyalcac002d2018-01-04 09:51:45 -0800887 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
vadimtcb863752018-12-19 17:44:57 -0800888 RaceConditionTracker.onEvent(ON_START_EVT, EXIT);
Michael Jurkacd496d72013-04-11 11:32:45 -0700889 }
890
Sunny Goyala002c6c2019-02-12 16:20:10 -0800891 private void handleDeferredResume() {
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700892 if (hasBeenResumed()) {
893 getUserEventDispatcher().logActionCommand(Action.Command.RESUME,
894 mStateManager.getState().containerType, -1);
895 getUserEventDispatcher().startSession();
Sunny Goyala002c6c2019-02-12 16:20:10 -0800896
897 UiFactory.onLauncherStateOrResumeChanged(this);
Sunny Goyal369212a2019-03-26 15:03:57 -0700898 AppLaunchTracker.INSTANCE.get(this).onReturnedToHome();
Sunny Goyaldedda052019-05-14 15:23:48 -0700899 resetPendingActivityResultIfNeeded();
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700900 }
901 }
902
Sunny Goyaldedda052019-05-14 15:23:48 -0700903 private void resetPendingActivityResultIfNeeded() {
904 if (hasBeenResumed() && mPendingActivityRequestCode != -1 && isInState(NORMAL)) {
905 UiFactory.resetPendingActivityResults(this, mPendingActivityRequestCode);
906 }
907 }
908
909 protected void onStateSet(LauncherState state) {
910 getAppWidgetHost().setResumed(state == LauncherState.NORMAL);
911 resetPendingActivityResultIfNeeded();
912 }
913
Michael Jurkacd496d72013-04-11 11:32:45 -0700914 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800915 protected void onResume() {
vadimtcb863752018-12-19 17:44:57 -0800916 RaceConditionTracker.onEvent(ON_RESUME_EVT, ENTER);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700917 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800918 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700919 TraceHelper.partitionSection("ON_RESUME", "superCall");
920
Sunny Goyala002c6c2019-02-12 16:20:10 -0800921 mHandler.removeCallbacks(mHandleDeferredResume);
922 Utilities.postAsyncCallback(mHandler, mHandleDeferredResume);
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700923
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700924 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700925 // Process any items that were added while Launcher was away.
926 InstallShortcutReceiver.disableAndFlushInstallQueue(
927 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700928
Sunny Goyala474a9b2017-05-04 16:47:11 -0700929 // Refresh shortcuts if the permission changed.
930 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700931
Tracy Zhoue5575f92018-04-19 11:12:53 -0700932 DiscoveryBounce.showForHomeIfNeeded(this);
Adam Cohen9211d422014-10-07 18:14:53 -0700933 if (mLauncherCallbacks != null) {
934 mLauncherCallbacks.onResume();
935 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800936
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700937 TraceHelper.endSection("ON_RESUME");
vadimtcb863752018-12-19 17:44:57 -0800938 RaceConditionTracker.onEvent(ON_RESUME_EVT, EXIT);
Adam Cohen06dff352012-06-01 17:17:08 -0700939 }
940
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800941 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700942 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700943 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700944 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700945
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700946 super.onPause();
Joe Onorato24b6fd82009-11-12 13:47:09 -0800947 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700948 mDragController.resetLastGestureUpTime();
Hyunyoung Song497708c2019-05-01 16:16:53 -0700949 mDropTargetBar.animateToVisibility(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700950 if (mLauncherCallbacks != null) {
951 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700952 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700953 }
954
Winson Chungd7823942018-02-16 03:23:47 +0000955 @Override
Tracy Zhoua706f002018-03-28 13:55:19 -0700956 protected void onUserLeaveHint() {
957 super.onUserLeaveHint();
958 UiFactory.onLauncherStateOrResumeChanged(this);
959 }
960
961 @Override
Winson Chungd7823942018-02-16 03:23:47 +0000962 public void onWindowFocusChanged(boolean hasFocus) {
963 super.onWindowFocusChanged(hasFocus);
964 mStateManager.onWindowFocusChanged();
965 }
966
Adam Cohenc2d6e892014-10-16 09:49:24 -0700967 public interface LauncherOverlay {
968
969 /**
970 * Touch interaction leading to overscroll has begun
971 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700972 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700973
974 /**
975 * Touch interaction related to overscroll has ended
976 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700977 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700978
979 /**
980 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
981 * screen (or in the case of RTL, the rightmost screen).
982 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700983 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700984
985 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800986 * Called when the launcher is ready to use the overlay
987 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700988 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700989 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700990 }
991
992 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700993 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700994 }
995
996 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
997
Sunny Goyalc86df472016-02-25 09:19:38 -0800998 public void onScrollChanged(float progress) {
999 if (mWorkspace != null) {
1000 mWorkspace.onOverlayScrollChanged(progress);
1001 }
1002 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001003 }
1004
Sunny Goyalf9403d92017-10-18 10:55:56 -07001005 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -07001006 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -07001007 }
1008
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001009 /**
1010 * Restores the previous state, if it exists.
1011 *
1012 * @param savedState The previous state.
1013 */
1014 private void restoreState(Bundle savedState) {
1015 if (savedState == null) {
1016 return;
1017 }
1018
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001019 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -07001020 LauncherState[] stateValues = LauncherState.values();
1021 LauncherState state = stateValues[stateOrdinal];
Sunny Goyalbc683e92017-12-06 10:25:07 -08001022 if (!state.disableRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001023 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001024 }
1025
Sunny Goyal2100c782016-08-22 16:00:03 -07001026 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
1027 if (requestArgs != null) {
1028 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001029 }
Sunny Goyaldedda052019-05-14 15:23:48 -07001030 mPendingActivityRequestCode = savedState.getInt(RUNTIME_STATE_PENDING_REQUEST_CODE);
Sunny Goyal2100c782016-08-22 16:00:03 -07001031
1032 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001033
1034 SparseArray<Parcelable> widgetsState =
1035 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
1036 if (widgetsState != null) {
1037 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
1038 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001039 }
1040
1041 /**
1042 * Finds all the views we need and configure them properly.
1043 */
1044 private void setupViews() {
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001045 mDragLayer = findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001046 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -07001047 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001048 mWorkspace.initParentViews(mDragLayer);
Sunny Goyal16764582017-11-06 16:00:34 -08001049 mOverviewPanel = findViewById(R.id.overview_panel);
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001050 mHotseat = findViewById(R.id.hotseat);
Craig Mautner360310b2012-10-26 15:13:08 -07001051
Sunny Goyal784f9c32016-03-23 16:56:54 -07001052 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1053 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1054 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001055
Winson Chung4c98d922011-05-31 16:50:48 -07001056 // Setup the drag layer
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001057 mDragLayer.setup(mDragController, mWorkspace);
Sunny Goyal5743f862019-03-28 15:35:32 -07001058 mCancelTouchController = UiFactory.enableLiveTouchControllerChanges(mDragLayer);
Winson Chung4c98d922011-05-31 16:50:48 -07001059
Hyunyoung Song645764e2016-06-06 14:19:02 -07001060 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001061 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1062 // default state, otherwise we will update to the wrong offsets in RTL
1063 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001064 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001065 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001066
Tony Wickham34d2c912015-09-11 09:27:58 -07001067 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001068 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001069
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001070 // Setup Apps
1071 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001072
Govinda Wassermanc06e1512019-04-09 09:56:53 -04001073 // Setup Scrim
1074 mScrimView = findViewById(R.id.scrim_view);
1075
Winson Chung3d503fb2011-07-13 17:25:49 -07001076 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001077 mDragController.setMoveTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001078 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001079
Sunny Goyal7185dd62018-03-14 17:51:49 -07001080 mAllAppsController.setupViews(mAppsView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001081 }
1082
1083 /**
1084 * Creates a view representing a shortcut.
1085 *
1086 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001087 */
Sunny Goyal95899162019-03-27 16:03:06 -07001088 View createShortcut(WorkspaceItemInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001089 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090 }
1091
1092 /**
1093 * Creates a view representing a shortcut inflated from the specified resource.
1094 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001095 * @param parent The group the shortcut belongs to.
1096 * @param info The data structure describing the shortcut.
1097 *
1098 * @return A View inflated from layoutResId.
1099 */
Sunny Goyal95899162019-03-27 16:03:06 -07001100 public View createShortcut(ViewGroup parent, WorkspaceItemInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -08001101 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
1102 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal95899162019-03-27 16:03:06 -07001103 favorite.applyFromWorkspaceItem(info);
Sunny Goyal8a2a63b2018-03-06 22:15:18 -08001104 favorite.setOnClickListener(ItemClickHandler.INSTANCE);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001105 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001106 return favorite;
1107 }
1108
1109 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -07001110 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001111 *
1112 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001113 */
Sunny Goyalefb7e842018-10-04 15:11:00 -07001114 private void completeAddShortcut(Intent data, int container, int screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001115 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001116 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
1117 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001118 return;
1119 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001120
Jon Mirandac476d6e2017-05-04 16:30:01 -07001121 int[] cellXY = mTmpAddItemCellCoordinates;
1122 CellLayout layout = getCellLayout(container, screenId);
1123
Sunny Goyal95899162019-03-27 16:03:06 -07001124 WorkspaceItemInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001125 if (Utilities.ATLEAST_OREO) {
Sunny Goyal95899162019-03-27 16:03:06 -07001126 info = LauncherAppsCompatVO.createWorkspaceItemFromPinItemRequest(
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001127 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001128 }
1129
1130 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001131 // Legacy shortcuts are only supported for primary profile.
1132 info = Process.myUserHandle().equals(args.user)
1133 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001134
Sunny Goyalb57645f2017-03-20 13:57:28 -07001135 if (info == null) {
1136 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1137 return;
1138 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001139 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001140 // The app is trying to add a shortcut without sufficient permissions
1141 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1142 return;
1143 }
1144 }
1145
Jon Mirandac476d6e2017-05-04 16:30:01 -07001146 if (container < 0) {
1147 // Adding a shortcut to the Workspace.
1148 final View view = createShortcut(info);
1149 boolean foundCellSpan = false;
1150 // First we check if we already know the exact location where we want to add this item.
1151 if (cellX >= 0 && cellY >= 0) {
1152 cellXY[0] = cellX;
1153 cellXY[1] = cellY;
1154 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001155
Jon Mirandac476d6e2017-05-04 16:30:01 -07001156 // If appropriate, either create a folder or add to an existing folder
1157 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001158 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001159 return;
1160 }
1161 DragObject dragObject = new DragObject();
1162 dragObject.dragInfo = info;
1163 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1164 true)) {
1165 return;
1166 }
1167 } else {
1168 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1169 }
1170
1171 if (!foundCellSpan) {
1172 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001173 return;
1174 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001175
1176 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1177 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001178 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001179 // Adding a shortcut to a Folder.
Sunny Goyale29897f2017-07-20 10:09:42 -07001180 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001181 if (folderIcon != null) {
1182 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1183 folderInfo.add(info, args.rank, false);
1184 } else {
Sunny Goyale29897f2017-07-20 10:09:42 -07001185 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001186 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001187 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001188 }
1189
Sunny Goyalefb7e842018-10-04 15:11:00 -07001190 public FolderIcon findFolderIcon(final int folderIconId) {
Sunny Goyal83fd25e2018-07-09 16:47:01 -07001191 return (FolderIcon) mWorkspace.getHomescreenIconByItemId(folderIconId);
Sunny Goyale29897f2017-07-20 10:09:42 -07001192 }
1193
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001194 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001195 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001196 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001197 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001198 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001199 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001200 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1201
Adam Cohened66b2b2012-01-23 17:28:51 -08001202 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001203 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001204 }
Romain Guycbb89e42009-06-08 15:52:54 -07001205
Adam Cohen59400422014-03-05 18:07:04 -08001206 LauncherAppWidgetInfo launcherInfo;
1207 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001208 launcherInfo.spanX = itemInfo.spanX;
1209 launcherInfo.spanY = itemInfo.spanY;
1210 launcherInfo.minSpanX = itemInfo.minSpanX;
1211 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001212 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001213
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001214 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001215 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001216
Sunny Goyal2100c782016-08-22 16:00:03 -07001217 if (hostView == null) {
1218 // Perform actual inflation because we're live
1219 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001220 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001221 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301222 prepareAppWidget(hostView, launcherInfo);
1223 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001224 }
Romain Guycbb89e42009-06-08 15:52:54 -07001225
Sunny Goyald5462aa2016-11-22 16:52:39 +05301226 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001227 hostView.setTag(item);
1228 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001229 hostView.setFocusable(true);
1230 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001231 }
1232
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001233 private final BroadcastReceiver mScreenOffReceiver = new BroadcastReceiver() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001234 @Override
1235 public void onReceive(Context context, Intent intent) {
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001236 // Reset AllApps to its initial state only if we are not in the middle of
1237 // processing a multi-step drop
1238 if (mPendingRequestArgs == null) {
1239 mStateManager.goToState(NORMAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001240 }
1241 }
1242 };
1243
Sunny Goyalaae6fbb2019-01-31 16:05:58 -08001244 public void updateNotificationDots(Predicate<PackageUserKey> updatedDots) {
Tony Wickhamf34bee82018-12-03 18:11:39 -08001245 mWorkspace.updateNotificationDots(updatedDots);
1246 mAppsView.getAppsStore().updateNotificationDots(updatedDots);
Tony Wickham010d2552017-01-20 08:15:28 -08001247 }
1248
Adam Cohended9f8d2010-11-03 13:25:16 -07001249 @Override
1250 public void onAttachedToWindow() {
1251 super.onAttachedToWindow();
1252
Sunny Goyalc86df472016-02-25 09:19:38 -08001253 if (mLauncherCallbacks != null) {
1254 mLauncherCallbacks.onAttachedToWindow();
1255 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001256 }
1257
1258 @Override
1259 public void onDetachedFromWindow() {
1260 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001261
1262 if (mLauncherCallbacks != null) {
1263 mLauncherCallbacks.onDetachedFromWindow();
1264 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001265 }
1266
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001267 public AllAppsTransitionController getAllAppsController() {
1268 return mAllAppsController;
1269 }
1270
Sunny Goyal9d69c8d2018-03-19 13:41:31 -07001271 @Override
Winson Chung1a341002018-01-17 10:00:23 -08001272 public LauncherRootView getRootView() {
1273 return (LauncherRootView) mLauncherView;
1274 }
1275
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001276 @Override
Winson Chunga6945242014-01-08 14:04:34 -08001277 public DragLayer getDragLayer() {
1278 return mDragLayer;
1279 }
1280
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001281 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001282 return mAppsView;
1283 }
1284
Winson Chunga6945242014-01-08 14:04:34 -08001285 public Workspace getWorkspace() {
1286 return mWorkspace;
1287 }
1288
1289 public Hotseat getHotseat() {
1290 return mHotseat;
1291 }
1292
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001293 public <T extends View> T getOverviewPanel() {
Winson Chung8ae41982017-11-10 11:42:13 -08001294 return (T) mOverviewPanel;
Winson Chunga6945242014-01-08 14:04:34 -08001295 }
1296
Sunny Goyal47328fd2016-05-25 18:56:41 -07001297 public DropTargetBar getDropTargetBar() {
1298 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001299 }
1300
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001301 public LauncherAppWidgetHost getAppWidgetHost() {
1302 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001303 }
Romain Guycbb89e42009-06-08 15:52:54 -07001304
Winson Chunga9abd0e2010-10-27 17:18:37 -07001305 public LauncherModel getModel() {
1306 return mModel;
1307 }
1308
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001309 public ModelWriter getModelWriter() {
1310 return mModelWriter;
1311 }
1312
Adam Cohen79d90c52016-04-22 13:29:20 -07001313 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001314 return mSharedPrefs;
1315 }
1316
Sunny Goyalf8d56fc2018-01-31 15:18:11 -08001317 public int getOrientation() { return mOldConfig.orientation; }
Jon Miranda6bed3502017-09-19 14:43:17 -07001318
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001319 @Override
1320 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001321 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001322 super.onNewIntent(intent);
1323
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001324 boolean alreadyOnHome = hasWindowFocus() && ((intent.getFlags() &
Winson15f8b172015-08-19 11:02:39 -07001325 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1326 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001327
1328 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001329 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001330 && AbstractFloatingView.getTopOpenView(this) == null;
Winson15f8b172015-08-19 11:02:39 -07001331 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001332 boolean internalStateHandled = InternalStateHandler
Sunny Goyal8b264562018-02-03 23:00:13 -08001333 .handleNewIntent(this, intent, isStarted());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001334
Winson15f8b172015-08-19 11:02:39 -07001335 if (isActionMain) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001336 if (!internalStateHandled) {
1337 // Note: There should be at most one log per method call. This is enforced
1338 // implicitly by using if-else statements.
1339 UserEventDispatcher ued = getUserEventDispatcher();
1340 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
1341 if (topOpenView != null) {
1342 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
1343 } else if (alreadyOnHome) {
1344 Target target = newContainerTarget(mStateManager.getState().containerType);
1345 target.pageIndex = mWorkspace.getCurrentPage();
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001346 ued.logActionCommand(Action.Command.HOME_INTENT, target,
1347 newContainerTarget(ContainerType.WORKSPACE));
Sunny Goyald3864fa2017-11-14 15:06:36 -08001348 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001349
Sunny Goyald3864fa2017-11-14 15:06:36 -08001350 // In all these cases, only animate if we're already on home
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001351 AbstractFloatingView.closeAllOpenViews(this, isStarted());
Jon Mirandafeba90f2016-10-06 10:53:29 -07001352
Sunny Goyala2467272018-03-16 14:53:05 -07001353 if (!isInState(NORMAL)) {
1354 // Only change state, if not already the same. This prevents cancelling any
1355 // animations running as part of resume
1356 mStateManager.goToState(NORMAL);
1357 }
Sunny Goyald3864fa2017-11-14 15:06:36 -08001358
1359 // Reset the apps view
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001360 if (!alreadyOnHome) {
Sunny Goyal7185dd62018-03-14 17:51:49 -07001361 mAppsView.reset(isStarted() /* animate */);
Sunny Goyald3864fa2017-11-14 15:06:36 -08001362 }
1363
Sunny Goyalaad33592018-08-07 17:20:35 -07001364 if (shouldMoveToDefaultScreen && !mWorkspace.isHandlingTouch()) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001365 mWorkspace.post(mWorkspace::moveToDefaultScreen);
1366 }
1367 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001368
1369 final View v = getWindow().peekDecorView();
1370 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001371 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001372 }
1373
Adam Cohen9211d422014-10-07 18:14:53 -07001374 if (mLauncherCallbacks != null) {
Sunny Goyal85462132018-04-24 11:39:37 -07001375 mLauncherCallbacks.onHomeIntent(internalStateHandled);
Adam Cohen9211d422014-10-07 18:14:53 -07001376 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001377 }
Winson15f8b172015-08-19 11:02:39 -07001378
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001379 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001380 }
1381
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001382 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001383 public void onRestoreInstanceState(Bundle state) {
1384 super.onRestoreInstanceState(state);
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001385 mWorkspace.restoreInstanceStateForChild(mSynchronouslyBoundPage);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001386 }
1387
1388 @Override
1389 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001390 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001391 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001392
Adam Cohen21cd0022013-10-09 18:57:02 -07001393 }
Sunny Goyalea609262017-10-25 15:47:38 -07001394 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001395
1396
1397 AbstractFloatingView widgets = AbstractFloatingView
1398 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1399 if (widgets != null) {
1400 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1401 widgets.saveHierarchyState(widgetsState);
1402 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1403 } else {
1404 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1405 }
1406
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001407 // We close any open folders and shortcut containers since they will not be re-opened,
1408 // and we need to make sure this state is reflected.
1409 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001410
Sunny Goyal2100c782016-08-22 16:00:03 -07001411 if (mPendingRequestArgs != null) {
1412 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1413 }
Sunny Goyaldedda052019-05-14 15:23:48 -07001414 outState.putInt(RUNTIME_STATE_PENDING_REQUEST_CODE, mPendingActivityRequestCode);
1415
Sunny Goyal2100c782016-08-22 16:00:03 -07001416 if (mPendingActivityResult != null) {
1417 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001418 }
1419
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001420 super.onSaveInstanceState(outState);
1421
Adam Cohen9211d422014-10-07 18:14:53 -07001422 if (mLauncherCallbacks != null) {
1423 mLauncherCallbacks.onSaveInstanceState(outState);
1424 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001425 }
1426
1427 @Override
1428 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001429 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001430
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001431 unregisterReceiver(mScreenOffReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001432 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001433
Sunny Goyal5743f862019-03-28 15:35:32 -07001434 if (mCancelTouchController != null) {
1435 mCancelTouchController.run();
1436 mCancelTouchController = null;
1437 }
Tony Wickhama0068302018-04-11 16:16:11 -07001438
Winson Chungcd2b0142011-06-08 16:02:26 -07001439 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001440 // It's possible to receive onDestroy after a new Launcher activity has
1441 // been created. In this case, don't interfere with the new Launcher.
1442 if (mModel.isCurrentCallbacks(this)) {
1443 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001444 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001445 }
Sunny Goyal623eddd2018-03-02 12:24:41 -08001446 mRotationHelper.destroy();
Sunny Goyal745bad92016-05-02 10:54:12 -07001447
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001448 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001449 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001450 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001451 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001452 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001453
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001454 TextKeyListener.getInstance().release();
Tony2917a8b2017-07-31 23:29:42 -07001455 clearPendingBinds();
Hyunyoung Song31971a32019-01-27 12:31:12 -08001456 LauncherAppState.getIDP(this).removeOnChangeListener(this);
Adam Cohen9211d422014-10-07 18:14:53 -07001457 if (mLauncherCallbacks != null) {
1458 mLauncherCallbacks.onDestroy();
1459 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001460 }
1461
Sunny Goyalae502842016-06-17 08:43:56 -07001462 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1463 return mAccessibilityDelegate;
1464 }
1465
Adam Cohena9cf38f2011-05-02 15:36:58 -07001466 public DragController getDragController() {
1467 return mDragController;
1468 }
1469
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001470 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001471 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyaldedda052019-05-14 15:23:48 -07001472 if (requestCode != -1) {
1473 mPendingActivityRequestCode = requestCode;
1474 }
1475 if (requestCode == -1
1476 || !UiFactory.startActivityForResult(this, intent, requestCode, options)) {
1477 super.startActivityForResult(intent, requestCode, options);
1478 }
Adam Cohen173f7112015-03-27 15:14:00 -07001479 }
1480
1481 @Override
Sunny Goyaldedda052019-05-14 15:23:48 -07001482 public void startIntentSenderForResult(IntentSender intent, int requestCode,
Adam Cohen173f7112015-03-27 15:14:00 -07001483 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Sunny Goyaldedda052019-05-14 15:23:48 -07001484 if (requestCode != -1) {
1485 mPendingActivityRequestCode = requestCode;
1486 }
1487 if (requestCode == -1 || !UiFactory.startIntentSenderForResult(this, intent, requestCode,
1488 fillInIntent, flagsMask, flagsValues, extraFlags, options)) {
1489 try {
1490 super.startIntentSenderForResult(intent, requestCode,
1491 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1492 } catch (IntentSender.SendIntentException e) {
1493 throw new ActivityNotFoundException();
1494 }
Adam Cohen173f7112015-03-27 15:14:00 -07001495 }
1496 }
1497
Winson Chung70d72102011-08-12 11:24:35 -07001498 /**
1499 * Indicates that we want global search for this activity by setting the globalSearch
1500 * argument for {@link #startSearch} to true.
1501 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001502 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001503 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001504 Bundle appSearchData, boolean globalSearch) {
1505 if (appSearchData == null) {
1506 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001507 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001508 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001509
Sunny Goyal6f28e712016-09-13 14:19:29 -07001510 if (mLauncherCallbacks == null ||
1511 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1512 // Starting search from the callbacks failed. Start the default global search.
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001513 super.startSearch(initialQuery, selectInitialQuery, appSearchData, true);
Ian Parkinson047036e2014-09-01 15:40:53 +01001514 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001515
1516 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001517 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001518 }
1519
Joe Onorato9c1289c2009-08-17 11:03:03 -04001520 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001521 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001522 }
1523
Adam Cohen517a7f52014-03-01 12:12:59 -08001524 public boolean isWorkspaceLoading() {
1525 return mWorkspaceLoading;
1526 }
1527
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001528 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001529 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001530 }
1531
Sunny Goyal04a324a2017-01-20 21:08:59 -08001532 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001533 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001534 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001535
Sunny Goyal2100c782016-08-22 16:00:03 -07001536 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001537 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001538 if (LOGD) {
1539 Log.d(TAG, "Adding widget from drop");
1540 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001541 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001542 }
1543
Sunny Goyal2100c782016-08-22 16:00:03 -07001544 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001545 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1546 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1547 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001548
Adam Cohenad4e15c2013-10-17 16:21:35 -07001549 Runnable onComplete = new Runnable() {
1550 @Override
1551 public void run() {
1552 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001553 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001554 }
1555 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001556 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001557 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001558 }
1559 }
Romain Guycbb89e42009-06-08 15:52:54 -07001560
Sunny Goyalefb7e842018-10-04 15:11:00 -07001561 public void addPendingItem(PendingAddItemInfo info, int container, int screenId,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001562 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001563 info.container = container;
1564 info.screenId = screenId;
1565 if (cell != null) {
1566 info.cellX = cell[0];
1567 info.cellY = cell[1];
1568 }
1569 info.spanX = spanX;
1570 info.spanY = spanY;
1571
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001572 switch (info.itemType) {
1573 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1574 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001575 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001576 break;
1577 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001578 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001579 break;
1580 default:
1581 throw new IllegalStateException("Unknown item type: " + info.itemType);
1582 }
1583 }
1584
Adam Cohenfbba09b2011-07-18 21:43:05 -07001585 /**
1586 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001587 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001588 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001589 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1590 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001591 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1592 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1593 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001594 }
1595
Adam Cohenfbba09b2011-07-18 21:43:05 -07001596 /**
1597 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001598 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001599 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001600 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001601 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001602 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001603 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001604 // In the case where we've prebound the widget, we remove it from the DragLayer
1605 if (LOGD) {
1606 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1607 }
1608 getDragLayer().removeView(hostView);
1609
Adam Cohened66b2b2012-01-23 17:28:51 -08001610 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001611 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001612
1613 // Clear the boundWidget so that it doesn't get destroyed.
1614 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001615 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001616 // In this case, we either need to start an activity to get permission to bind
1617 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001618 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1619 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1620 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1621 this, info.componentName);
1622 } else {
1623 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1624 }
Adam Cohendd70d662012-10-04 16:53:44 -07001625 Bundle options = info.bindOptions;
1626
Sunny Goyalffe83f12014-08-14 17:39:34 -07001627 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1628 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001629 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001630 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001631 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001632 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001633 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001634 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001635 }
1636
Sunny Goyalefb7e842018-10-04 15:11:00 -07001637 FolderIcon addFolder(CellLayout layout, int container, final int screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001638 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001639 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001640 folderInfo.title = getText(R.string.folder_name);
1641
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001642 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001643 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001644
1645 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001646 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301647 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001648 // Force measure the new folder icon
1649 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1650 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001651 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001652 }
Romain Guycbb89e42009-06-08 15:52:54 -07001653
Winsonc0b52fe2015-09-09 16:38:15 -07001654 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001655 * Unbinds the view for the specified item, and removes the item and all its children.
1656 *
1657 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001658 * @param itemInfo the {@link ItemInfo} for this view.
1659 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001660 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001661 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Sunny Goyal95899162019-03-27 16:03:06 -07001662 if (itemInfo instanceof WorkspaceItemInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001663 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001664 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1665 if (folderIcon instanceof FolderIcon) {
Sunny Goyal95899162019-03-27 16:03:06 -07001666 ((FolderInfo) folderIcon.getTag()).remove((WorkspaceItemInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001667 } else {
1668 mWorkspace.removeWorkspaceItem(v);
1669 }
Winsonc0b52fe2015-09-09 16:38:15 -07001670 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001671 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001672 }
1673 } else if (itemInfo instanceof FolderInfo) {
1674 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001675 if (v instanceof FolderIcon) {
1676 ((FolderIcon) v).removeListeners();
1677 }
Winsonc0b52fe2015-09-09 16:38:15 -07001678 mWorkspace.removeWorkspaceItem(v);
1679 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001680 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001681 }
1682 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1683 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001684 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001685 if (deleteFromDb) {
Tony Wickham6a71a5b2018-08-21 11:40:23 -07001686 getModelWriter().deleteWidgetInfo(widgetInfo, getAppWidgetHost());
Winsonc0b52fe2015-09-09 16:38:15 -07001687 }
1688 } else {
1689 return false;
1690 }
1691 return true;
1692 }
1693
Tony Wickham6a71a5b2018-08-21 11:40:23 -07001694
Winsonc0b52fe2015-09-09 16:38:15 -07001695
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001696 @Override
1697 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001698 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001699 }
1700
Joe Onorato88ec0992009-11-19 13:16:06 -08001701 @Override
1702 public void onBackPressed() {
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08001703 if (finishAutoCancelActionMode()) {
1704 return;
1705 }
Adam Cohen9211d422014-10-07 18:14:53 -07001706 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1707 return;
1708 }
1709
Sunny Goyal45478022015-06-08 16:52:41 -07001710 if (mDragController.isDragging()) {
1711 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001712 return;
1713 }
1714
Jon Mirandafeba90f2016-10-06 10:53:29 -07001715 // Note: There should be at most one log per method call. This is enforced implicitly
1716 // by using if-else statements.
1717 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001718 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
Tony Wickham52c1b662018-05-21 13:13:58 -07001719 if (topView != null && topView.onBackPressed()) {
1720 // Handled by the floating view.
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001721 } else {
Sunny Goyale39690b2018-08-02 13:35:07 -07001722 mStateManager.getState().onBackPressed(this);
Michael Jurkaaf442092010-06-10 17:01:57 -07001723 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001724 }
1725
Sunny Goyalfe770c92016-09-27 14:38:58 -07001726 @TargetApi(Build.VERSION_CODES.M)
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001727 @Override
Jon Miranda73c27ec2018-05-16 18:06:23 -07001728 public ActivityOptions getActivityLaunchOptions(View v) {
1729 return mAppTransitionManager.getActivityLaunchOptions(this, v);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001730 }
1731
Tony Wickham3a6746a2018-03-29 09:39:56 -07001732 public LauncherAppTransitionManager getAppTransitionManager() {
1733 return mAppTransitionManager;
1734 }
1735
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001736 @TargetApi(Build.VERSION_CODES.M)
1737 @Override
1738 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
1739 // Due to legacy reasons, direct call shortcuts require Launchers to have the
1740 // corresponding permission. Show the appropriate permission prompt if that
1741 // is the case.
1742 if (intent.getComponent() == null
1743 && Intent.ACTION_CALL.equals(intent.getAction())
1744 && checkSelfPermission(android.Manifest.permission.CALL_PHONE) !=
1745 PackageManager.PERMISSION_GRANTED) {
1746
1747 setWaitingForResult(PendingRequestArgs
1748 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
1749 requestPermissions(new String[]{android.Manifest.permission.CALL_PHONE},
1750 REQUEST_PERMISSION_CALL_PHONE);
1751 return true;
1752 } else {
1753 return false;
1754 }
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001755 }
1756
Hyunyoung Song46d07f72018-05-22 15:41:25 -07001757 @Override
Hyunyoung Songfc007472018-10-25 14:09:50 -07001758 public int getCurrentState() {
1759 if(mStateManager.getState() == LauncherState.ALL_APPS) {
1760 return StatsLogUtils.LAUNCHER_STATE_ALLAPPS;
Govinda Wassermanc06e1512019-04-09 09:56:53 -04001761 } else if (mStateManager.getState() == OVERVIEW) {
Hyunyoung Songfc007472018-10-25 14:09:50 -07001762 return StatsLogUtils.LAUNCHER_STATE_OVERVIEW;
1763 }
Hyunyoung Song0ae38882018-11-05 14:45:19 -08001764 return StatsLogUtils.LAUNCHER_STATE_HOME;
Hyunyoung Songfc007472018-10-25 14:09:50 -07001765 }
1766
1767 @Override
Hyunyoung Song46d07f72018-05-22 15:41:25 -07001768 public void modifyUserEvent(LauncherLogProto.LauncherEvent event) {
1769 if (event.srcTarget != null && event.srcTarget.length > 0 &&
1770 event.srcTarget[1].containerType == ContainerType.PREDICTION) {
1771 Target[] targets = new Target[3];
1772 targets[0] = event.srcTarget[0];
1773 targets[1] = event.srcTarget[1];
1774 targets[2] = newTarget(Target.Type.CONTAINER);
1775 event.srcTarget = targets;
1776 LauncherState state = mStateManager.getState();
1777 if (state == LauncherState.ALL_APPS) {
1778 event.srcTarget[2].containerType = ContainerType.ALLAPPS;
Govinda Wassermanc06e1512019-04-09 09:56:53 -04001779 } else if (state == OVERVIEW) {
Hyunyoung Song46d07f72018-05-22 15:41:25 -07001780 event.srcTarget[2].containerType = ContainerType.TASKSWITCHER;
1781 }
1782 }
1783 }
1784
Sunny Goyal369212a2019-03-26 15:03:57 -07001785 public boolean startActivitySafely(View v, Intent intent, ItemInfo item,
1786 @Nullable String sourceContainer) {
Sunny Goyalab3963d2019-05-23 00:50:08 -07001787 if (TestProtocol.sDebugTracing) {
1788 android.util.Log.d(TestProtocol.NO_START_TAG,
vadimt3ab80bb2019-05-22 12:58:35 -07001789 "startActivitySafely outer");
1790 }
Sunny Goyal369212a2019-03-26 15:03:57 -07001791 boolean success = super.startActivitySafely(v, intent, item, sourceContainer);
Hyunyoung Song1241e612018-05-15 21:46:51 -07001792 if (success && v instanceof BubbleTextView) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001793 // This is set to the view that launched the activity that navigated the user away
1794 // from launcher. Since there is no callback for when the activity has finished
1795 // launching, enable the press state and keep this reference to reset the press
1796 // state when we return to launcher.
1797 BubbleTextView btv = (BubbleTextView) v;
1798 btv.setStayPressed(true);
1799 setOnResumeCallback(btv);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001800 }
Hyunyoung Song1241e612018-05-15 21:46:51 -07001801 return success;
Michael Jurka86a720e2012-05-09 11:23:23 -07001802 }
1803
Winson Chung3d503fb2011-07-13 17:25:49 -07001804 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001805 // TODO: Remove this method
Sunny Goyal876e4622018-11-02 13:50:40 -07001806 return mHotseat != null && (layout == mHotseat);
Winson Chung3d503fb2011-07-13 17:25:49 -07001807 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001808
Winson Chung3d503fb2011-07-13 17:25:49 -07001809 /**
1810 * Returns the CellLayout of the specified container at the specified screen.
1811 */
Sunny Goyalefb7e842018-10-04 15:11:00 -07001812 public CellLayout getCellLayout(int container, int screenId) {
Sunny Goyal876e4622018-11-02 13:50:40 -07001813 return (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1814 ? mHotseat : mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08001815 }
1816
Michael Jurkae326f182011-11-21 14:05:46 -08001817 @Override
1818 public void onTrimMemory(int level) {
1819 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07001820 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
1821 // The widget preview db can result in holding onto over
1822 // 3MB of memory for caching which isn't necessary.
1823 SQLiteDatabase.releaseMemory();
1824
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08001825 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07001826 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08001827 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01001828 if (mLauncherCallbacks != null) {
1829 mLauncherCallbacks.onTrimMemory(level);
1830 }
Winson Chung62a70be2018-02-23 15:10:05 -08001831 UiFactory.onTrimMemory(this, level);
Michael Jurkae326f182011-11-21 14:05:46 -08001832 }
1833
Michael Jurkad7c28052012-04-27 15:43:36 -07001834 @Override
1835 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07001836 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07001837 final List<CharSequence> text = event.getText();
1838 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07001839 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001840 // TODO: When can workspace be null?
1841 text.add(mWorkspace == null
1842 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07001843 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07001844 return result;
1845 }
1846
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001847 public void setOnResumeCallback(OnResumeCallback callback) {
1848 if (mOnResumeCallback != null) {
1849 mOnResumeCallback.onLauncherResume();
1850 }
1851 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07001852 }
1853
Michael Jurka7607c2f2013-04-03 14:33:19 -07001854 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001855 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001856 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07001857 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001858 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07001859 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001860 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07001861 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07001862 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07001863 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001864 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001865
Joe Onorato9c1289c2009-08-17 11:03:03 -04001866 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07001867 * Clear any pending bind callbacks. This is called when is loader is planning to
1868 * perform a full rebind from scratch.
1869 */
1870 @Override
1871 public void clearPendingBinds() {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001872 if (mPendingExecutor != null) {
1873 mPendingExecutor.markCompleted();
1874 mPendingExecutor = null;
1875 }
1876 }
1877
1878 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001879 * Refreshes the shortcuts shown on the workspace.
1880 *
1881 * Implementation of the method from LauncherModel.Callbacks.
1882 */
1883 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001884 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001885 // Floating panels (except the full widget sheet) are associated with individual icons. If
1886 // we are starting a fresh bind, close all such panels as all the icons are about
1887 // to go away.
1888 AbstractFloatingView.closeOpenViews(this, true,
Sunny Goyal7ede6112017-12-05 15:11:21 -08001889 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_REBIND_SAFE);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08001890
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001891 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08001892
Winson Chungd64d1762013-08-20 14:37:16 -07001893 // Clear the workspace because it's going to be rebound
Sunny Goyal2db53422019-03-01 16:06:12 -08001894 mDragController.cancelDrag();
1895
Adam Cohendf035382011-04-11 17:22:04 -07001896 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07001897 mWorkspace.removeAllWorkspaceScreens();
Sunny Goyalc11fac32018-02-27 19:20:15 -08001898 mAppWidgetHost.clearViews();
Winson Chungd64d1762013-08-20 14:37:16 -07001899
Winson Chung3d503fb2011-07-13 17:25:49 -07001900 if (mHotseat != null) {
Sunny Goyalae6e3182019-04-30 12:04:37 -07001901 mHotseat.resetLayout(getWallpaperDeviceProfile().isVerticalBarLayout());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001902 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001903 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001904 }
1905
Adam Cohendcd297f2013-06-18 13:13:40 -07001906 @Override
Sunny Goyalefb7e842018-10-04 15:11:00 -07001907 public void bindScreens(IntArray orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001908 // Make sure the first screen is always at the start.
Jon Miranda7143ba62019-03-15 09:00:05 -07001909 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001910 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalefb7e842018-10-04 15:11:00 -07001911 orderedScreenIds.removeValue(Workspace.FIRST_SCREEN_ID);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001912 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Jon Miranda7143ba62019-03-15 09:00:05 -07001913 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001914 // If there are no screens, we need to have an empty screen
1915 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07001916 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001917 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07001918
Winsonc7d2e832016-07-28 12:24:55 -07001919 // After we have added all the screens, if the wallpaper was locked to the default state,
1920 // then notify to indicate that it can be released and a proper wallpaper offset can be
1921 // computed before the next layout
1922 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07001923 }
1924
Sunny Goyalefb7e842018-10-04 15:11:00 -07001925 private void bindAddScreens(IntArray orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001926 int count = orderedScreenIds.size();
1927 for (int i = 0; i < count; i++) {
Sunny Goyalefb7e842018-10-04 15:11:00 -07001928 int screenId = orderedScreenIds.get(i);
Jon Miranda7143ba62019-03-15 09:00:05 -07001929 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001930 // No need to bind the first screen, as its always bound.
1931 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
1932 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001933 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001934 }
1935
Sunny Goyalb23980c2017-08-17 07:45:25 -07001936 @Override
Tony Wickham6a71a5b2018-08-21 11:40:23 -07001937 public void preAddApps() {
1938 // If there's an undo snackbar, force it to complete to ensure empty screens are removed
1939 // before trying to add new items.
1940 mModelWriter.commitDelete();
1941 AbstractFloatingView snackbar = AbstractFloatingView.getOpenView(this, TYPE_SNACKBAR);
1942 if (snackbar != null) {
1943 snackbar.post(() -> snackbar.close(true));
1944 }
1945 }
1946
1947 @Override
Sunny Goyalefb7e842018-10-04 15:11:00 -07001948 public void bindAppsAdded(IntArray newScreens, ArrayList<ItemInfo> addNotAnimated,
Sunny Goyal29947f02017-12-18 13:49:44 -08001949 ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07001950 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08001951 if (newScreens != null) {
1952 bindAddScreens(newScreens);
1953 }
Winson Chungd64d1762013-08-20 14:37:16 -07001954
1955 // We add the items without animation on non-visible pages, and with
1956 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08001957 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001958 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001959 }
Adam Cohen76a47a12014-02-05 11:47:43 -08001960 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001961 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07001962 }
Winson Chungc58497e2013-09-03 17:48:37 -07001963
Winson Chung87412982013-10-03 18:34:14 -07001964 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07001965 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001966 }
1967
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001968 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001969 * Bind the items start-end from the list.
1970 *
1971 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001972 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07001973 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07001974 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001975 // Get the list of added items and intersect them with the set of items here
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001976 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07001977 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001978 Workspace workspace = mWorkspace;
Sunny Goyalefb7e842018-10-04 15:11:00 -07001979 int newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07001980 int end = items.size();
1981 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001982 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07001983
1984 // Short circuit if we are loading dock items for a configuration which has no dock
1985 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
1986 mHotseat == null) {
1987 continue;
1988 }
1989
Sunny Goyal639e9062015-08-19 19:17:06 -07001990 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001991 switch (item.itemType) {
1992 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1993 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08001994 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Sunny Goyal95899162019-03-27 16:03:06 -07001995 WorkspaceItemInfo info = (WorkspaceItemInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07001996 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001997 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001998 }
1999 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07002000 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002001 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002002 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002003 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002004 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07002005 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2006 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07002007 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002008 if (view == null) {
2009 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002010 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08002011 break;
2012 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002013 default:
2014 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002015 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002016
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002017 /*
2018 * Remove colliding items.
2019 */
2020 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
2021 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
2022 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
2023 View v = cl.getChildAt(item.cellX, item.cellY);
2024 Object tag = v.getTag();
2025 String desc = "Collision while binding workspace item: " + item
2026 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08002027 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002028 throw (new RuntimeException(desc));
2029 } else {
2030 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002031 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002032 continue;
2033 }
2034 }
2035 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302036 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07002037 if (animateIcons) {
2038 // Animate all the applications up now
2039 view.setAlpha(0f);
2040 view.setScaleX(0f);
2041 view.setScaleY(0f);
2042 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08002043 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07002044 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002045 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07002046
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002047 // Animate to the correct page
2048 if (animateIcons && newItemsScreenId > -1) {
2049 AnimatorSet anim = new AnimatorSet();
2050 anim.playTogether(bounceAnims);
Jon Miranda64eb8ea2018-03-29 11:43:58 -07002051
Sunny Goyalefb7e842018-10-04 15:11:00 -07002052 int currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002053 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
2054 final Runnable startBounceAnimRunnable = anim::start;
2055
2056 if (newItemsScreenId != currentScreenId) {
2057 // We post the animation slightly delayed to prevent slowdowns
2058 // when we are loading right after we return to launcher.
2059 mWorkspace.postDelayed(new Runnable() {
2060 public void run() {
2061 if (mWorkspace != null) {
2062 AbstractFloatingView.closeAllOpenViews(Launcher.this, false);
2063
2064 mWorkspace.snapToPage(newScreenIndex);
2065 mWorkspace.postDelayed(startBounceAnimRunnable,
2066 NEW_APPS_ANIMATION_DELAY);
Winson Chung94d67682013-09-25 16:29:40 -07002067 }
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002068 }
2069 }, NEW_APPS_PAGE_MOVE_DELAY);
2070 } else {
2071 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07002072 }
Winson Chung64359a52013-07-08 17:17:08 -07002073 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002074 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002075 }
2076
Joe Onorato9c1289c2009-08-17 11:03:03 -04002077 /**
2078 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002079 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07002080 public void bindAppWidget(LauncherAppWidgetInfo item) {
2081 View view = inflateAppWidget(item);
2082 if (view != null) {
2083 mWorkspace.addInScreen(view, item);
2084 mWorkspace.requestLayout();
2085 }
2086 }
2087
2088 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002089 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302090 PendingAppWidgetHostView view =
2091 new PendingAppWidgetHostView(this, item, mIconCache, true);
2092 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002093 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002094 }
2095
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002096 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002097
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002098 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08002099
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002100 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2101 // If the provider is not ready, bind as a pending widget.
2102 appWidgetInfo = null;
2103 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2104 // The widget id is not valid. Try to find the widget based on the provider info.
2105 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
2106 } else {
2107 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
2108 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002109
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002110 // If the provider is ready, but the width is not yet restored, try to restore it.
2111 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
2112 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07002113 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002114 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
2115 + " belongs to component " + item.providerName
2116 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002117 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002118 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002119 }
Sunny Goyalff572272014-07-23 13:58:07 -07002120
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002121 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002122 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07002123 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2124 // Id has not been allocated yet. Allocate a new id.
2125 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
2126 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07002127
Sunny Goyald478c832016-04-01 12:04:16 -07002128 // Also try to bind the widget. If the bind fails, the user will be shown
2129 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08002130 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07002131 pendingInfo.spanX = item.spanX;
2132 pendingInfo.spanY = item.spanY;
2133 pendingInfo.minSpanX = item.minSpanX;
2134 pendingInfo.minSpanY = item.minSpanY;
2135 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07002136
2137 boolean isDirectConfig =
2138 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2139 if (isDirectConfig && item.bindOptions != null) {
2140 Bundle newOptions = item.bindOptions.getExtras();
2141 if (options != null) {
2142 newOptions.putAll(options);
2143 }
2144 options = newOptions;
2145 }
Sunny Goyald478c832016-04-01 12:04:16 -07002146 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2147 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002148
Sunny Goyal86df1382016-08-10 15:03:22 -07002149 // We tried to bind once. If we were not able to bind, we would need to
2150 // go through the permission dialog, which means we cannot skip the config
2151 // activity.
2152 item.bindOptions = null;
2153 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2154
Sunny Goyald478c832016-04-01 12:04:16 -07002155 // Bind succeeded
2156 if (success) {
2157 // If the widget has a configure activity, it is still needs to set it up,
2158 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002159 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002160 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2161 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002162 }
Sunny Goyald478c832016-04-01 12:04:16 -07002163
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002164 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002165 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002166 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002167 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002168 // The widget was marked as UI not ready, but there is no configure activity to
2169 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002170 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002171 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002172 }
Sunny Goyalff572272014-07-23 13:58:07 -07002173 }
2174
Sunny Goyald5462aa2016-11-22 16:52:39 +05302175 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002176 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002177 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002178 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002179 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Tony Wickham6a71a5b2018-08-21 11:40:23 -07002180 getModelWriter().deleteWidgetInfo(item, getAppWidgetHost());
Sunny Goyalb23980c2017-08-17 07:45:25 -07002181 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002182 }
2183
Sunny Goyal233ee962015-08-03 13:05:01 -07002184 item.minSpanX = appWidgetInfo.minSpanX;
2185 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302186 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002187 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302188 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002189 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302190 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002191
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002192 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002193 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002194 }
2195
Sunny Goyalff572272014-07-23 13:58:07 -07002196 /**
2197 * Restores a pending widget.
2198 *
2199 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002200 */
Sunny Goyald478c832016-04-01 12:04:16 -07002201 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002202 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2203 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2204 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002205 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002206 }
2207
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002208 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002209 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002210 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2211 info.pendingItemInfo = null;
2212 }
Sunny Goyalff572272014-07-23 13:58:07 -07002213
Sunny Goyalba47faa2017-10-04 16:50:57 -07002214 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002215 view.reInflate();
Sunny Goyalba47faa2017-10-04 16:50:57 -07002216 }
2217
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002218 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002219 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002220 }
2221
Adam Cohen1462de32012-07-24 22:34:36 -07002222 public void onPageBoundSynchronously(int page) {
Sunny Goyalf4aceab2017-12-06 16:16:31 -08002223 mSynchronouslyBoundPage = page;
Adam Cohen1462de32012-07-24 22:34:36 -07002224 }
2225
Sunny Goyal527c7d32015-08-28 15:19:36 -07002226 @Override
2227 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2228 if (mPendingExecutor != null) {
2229 mPendingExecutor.markCompleted();
2230 }
2231 mPendingExecutor = executor;
Sunny Goyal60180b02018-02-07 12:56:30 -08002232 if (!isInState(ALL_APPS)) {
2233 mAppsView.getAppsStore().setDeferUpdates(true);
2234 mPendingExecutor.execute(() -> mAppsView.getAppsStore().setDeferUpdates(false));
2235 }
2236
Sunny Goyal527c7d32015-08-28 15:19:36 -07002237 executor.attachTo(this);
2238 }
2239
2240 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2241 if (mPendingExecutor == executor) {
2242 mPendingExecutor = null;
2243 }
2244 }
2245
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002246 @Override
2247 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
Sunny Goyal6001ea22018-05-10 16:31:00 -07002248 AlphaProperty property = mDragLayer.getAlphaProperty(ALPHA_INDEX_LAUNCHER_LOAD);
2249 if (property.getValue() < 1) {
2250 ObjectAnimator anim = ObjectAnimator.ofFloat(property, MultiValueAlpha.VALUE, 1);
2251 if (executor != null) {
2252 anim.addListener(new AnimatorListenerAdapter() {
2253 @Override
2254 public void onAnimationEnd(Animator animation) {
2255 executor.onLoadAnimationCompleted();
2256 }
2257 });
2258 }
2259 anim.start();
Sunny Goyal29947f02017-12-18 13:49:44 -08002260 } else if (executor != null) {
2261 executor.onLoadAnimationCompleted();
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002262 }
2263 }
2264
Joe Onorato9c1289c2009-08-17 11:03:03 -04002265 /**
2266 * Callback saying that there aren't any more items to bind.
2267 *
2268 * Implementation of the method from LauncherModel.Callbacks.
2269 */
Tonyf80e8932018-12-21 11:58:04 -08002270 public void finishBindingItems(int pageBoundFirst) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002271 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002272 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002273
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002274 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002275
Sunny Goyal2100c782016-08-22 16:00:03 -07002276 if (mPendingActivityResult != null) {
2277 handleActivityResult(mPendingActivityResult.requestCode,
2278 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2279 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002280 }
2281
Sunny Goyala474a9b2017-05-04 16:47:11 -07002282 InstallShortcutReceiver.disableAndFlushInstallQueue(
2283 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002284
Tonyf80e8932018-12-21 11:58:04 -08002285 // When undoing the removal of the last item on a page, return to that page.
Tony Wickham03f27012019-04-25 14:22:54 -07002286 // Since we are just resetting the current page without user interaction,
2287 // override the previous page so we don't log the page switch.
2288 mWorkspace.setCurrentPage(pageBoundFirst, pageBoundFirst /* overridePrevPage */);
Tony Wickham6a71a5b2018-08-21 11:40:23 -07002289
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002290 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002291 }
2292
Winson Chunga2413752012-04-03 14:22:34 -07002293 private boolean canRunNewAppsAnimation() {
2294 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002295 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002296 }
2297
Winson Chungc9168342013-06-26 14:54:55 -07002298 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002299 ValueAnimator bounceAnim = new PropertyListBuilder().alpha(1).scale(1).build(v)
2300 .setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
Winson Chungc9168342013-06-26 14:54:55 -07002301 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002302 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002303 return bounceAnim;
2304 }
2305
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002306 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002307 * Add the icons for all apps.
2308 *
2309 * Implementation of the method from LauncherModel.Callbacks.
2310 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002311 public void bindAllApplications(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002312 mAppsView.getAppsStore().setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002313 }
2314
2315 /**
Zak Cohen658c67a2018-10-19 14:21:05 -07002316 * Copies LauncherModel's map of activities to shortcut counts to Launcher's. This is necessary
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002317 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2318 */
2319 @Override
Zak Cohen658c67a2018-10-19 14:21:05 -07002320 public void bindDeepShortcutMap(HashMap<ComponentKey, Integer> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002321 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002322 }
2323
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002324 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002325 * A package was updated.
2326 *
2327 * Implementation of the method from LauncherModel.Callbacks.
2328 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002329 @Override
2330 public void bindAppsAddedOrUpdated(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002331 mAppsView.getAppsStore().addOrUpdateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002332 }
2333
Sunny Goyal4390ace2014-10-13 11:33:11 -07002334 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002335 public void bindPromiseAppProgressUpdated(PromiseAppInfo app) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002336 mAppsView.getAppsStore().updatePromiseAppProgress(app);
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002337 }
2338
2339 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002340 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002341 mWorkspace.widgetsRestored(widgets);
2342 }
2343
Joe Onorato9c1289c2009-08-17 11:03:03 -04002344 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002345 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002346 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002347 *
2348 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002349 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002350 @Override
Sunny Goyal95899162019-03-27 16:03:06 -07002351 public void bindWorkspaceItemsChanged(ArrayList<WorkspaceItemInfo> updated) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002352 if (!updated.isEmpty()) {
2353 mWorkspace.updateShortcuts(updated);
2354 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002355 }
2356
2357 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002358 * Update the state of a package, typically related to install state.
2359 *
2360 * Implementation of the method from LauncherModel.Callbacks.
2361 */
Sunny Goyale755d462014-07-22 13:48:29 -07002362 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002363 public void bindRestoreItemsChange(HashSet<ItemInfo> updates) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07002364 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07002365 }
2366
2367 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002368 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07002369 * in addition to specific applications to remove, the reason being that
2370 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002371 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07002372 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002373 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07002374 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002375 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002376 mWorkspace.removeItemsByMatcher(matcher);
2377 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002378 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002379
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002380 @Override
2381 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002382 mAppsView.getAppsStore().removeApps(appInfos);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002383 }
Joe Onoratobe386092009-11-17 17:32:16 -08002384
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002385 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002386 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
2387 mPopupDataProvider.setAllWidgets(allWidgets);
Tony Wickham26b17462017-03-20 17:12:24 -07002388 }
2389
Tony Wickham86222d22017-03-29 15:30:43 -07002390 /**
2391 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
2392 * refreshes the widgets and shortcuts associated with the given package/user
2393 */
2394 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07002395 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002396 }
2397
Winson Chung80baf5a2010-08-09 16:03:15 -07002398 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002399 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08002400 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002401 @Override
2402 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
2403 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002404
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002405 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
2406 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07002407 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002408 writer.println(prefix + " Homescreen " + i);
2409
2410 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
2411 for (int j = 0; j < layout.getChildCount(); j++) {
2412 Object tag = layout.getChildAt(j).getTag();
2413 if (tag != null) {
2414 writer.println(prefix + " " + tag.toString());
2415 }
2416 }
2417 }
2418
2419 writer.println(prefix + " Hotseat");
Sunny Goyal876e4622018-11-02 13:50:40 -07002420 ViewGroup layout = mHotseat.getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07002421 for (int j = 0; j < layout.getChildCount(); j++) {
2422 Object tag = layout.getChildAt(j).getTag();
2423 if (tag != null) {
2424 writer.println(prefix + " " + tag.toString());
2425 }
Adam Cohen4caf2982013-08-20 18:54:31 -07002426 }
Sunny Goyala1365452015-10-01 15:46:24 -07002427 }
2428
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002429 writer.println(prefix + "Misc:");
2430 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
2431 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
2432 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
Sunny Goyale43d00d2018-05-14 14:23:18 -07002433 writer.println(" mRotationHelper: " + mRotationHelper);
Sunny Goyalcb2c5052018-10-15 12:30:29 -07002434 // Extra logging for b/116853349
Hyunyoung Songa310a802019-04-25 13:57:16 -07002435 mDragLayer.dump(prefix, writer);
2436 mStateManager.dump(prefix, writer);
Sunny Goyale43d00d2018-05-14 14:23:18 -07002437 dumpMisc(writer);
Sunny Goyal2bba1902018-02-27 12:20:50 -08002438
2439 try {
2440 FileLog.flushAll(writer);
2441 } catch (Exception e) {
2442 // Ignore
2443 }
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002444
2445 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002446
Adam Cohen9211d422014-10-07 18:14:53 -07002447 if (mLauncherCallbacks != null) {
2448 mLauncherCallbacks.dump(prefix, fd, writer, args);
2449 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002450 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002451
Sunny Goyal66b24572016-09-21 15:57:55 -07002452 @Override
Sunny Goyal66b24572016-09-21 15:57:55 -07002453 public void onProvideKeyboardShortcuts(
2454 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
2455
2456 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002457 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002458 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
2459 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
Vadim Tryshev74c261c2018-06-12 13:44:43 -07002460 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.widget_button_text),
2461 KeyEvent.KEYCODE_W, KeyEvent.META_CTRL_ON));
Sunny Goyal66b24572016-09-21 15:57:55 -07002462 }
Vadim Tryshev3455f8d2018-05-11 20:02:20 -07002463 final View currentFocus = getCurrentFocus();
2464 if (currentFocus != null) {
2465 if (new CustomActionsPopup(this, currentFocus).canShow()) {
2466 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
2467 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
2468 }
2469 if (currentFocus.getTag() instanceof ItemInfo
2470 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
2471 shortcutInfos.add(new KeyboardShortcutInfo(
2472 getString(R.string.shortcuts_menu_with_notifications_description),
2473 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
2474 }
Sunny Goyal66b24572016-09-21 15:57:55 -07002475 }
2476 if (!shortcutInfos.isEmpty()) {
2477 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
2478 }
2479
2480 super.onProvideKeyboardShortcuts(data, menu, deviceId);
2481 }
2482
2483 @Override
2484 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
2485 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
2486 switch (keyCode) {
2487 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002488 if (isInState(NORMAL)) {
2489 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07002490 return true;
2491 }
2492 break;
2493 case KeyEvent.KEYCODE_S: {
2494 View focusedView = getCurrentFocus();
2495 if (focusedView instanceof BubbleTextView
2496 && focusedView.getTag() instanceof ItemInfo
2497 && mAccessibilityDelegate.performAction(focusedView,
2498 (ItemInfo) focusedView.getTag(),
2499 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal00ac9202017-11-09 15:24:06 -08002500 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07002501 return true;
2502 }
2503 break;
2504 }
2505 case KeyEvent.KEYCODE_O:
2506 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
2507 return true;
2508 }
2509 break;
Vadim Tryshev74c261c2018-06-12 13:44:43 -07002510 case KeyEvent.KEYCODE_W:
2511 if (isInState(NORMAL)) {
2512 OptionsPopupView.openWidgets(this);
2513 return true;
2514 }
2515 break;
Sunny Goyal66b24572016-09-21 15:57:55 -07002516 }
2517 }
2518 return super.onKeyShortcut(keyCode, event);
2519 }
2520
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002521 @Override
2522 public boolean onKeyUp(int keyCode, KeyEvent event) {
2523 if (keyCode == KeyEvent.KEYCODE_MENU) {
2524 // KEYCODE_MENU is sent by some tests, for example
2525 // LauncherJankTests#testWidgetsContainerFling. Don't just remove its handling.
2526 if (!mDragController.isDragging() && !mWorkspace.isSwitchingState() &&
2527 isInState(NORMAL)) {
2528 // Close any open floating views.
2529 AbstractFloatingView.closeAllOpenViews(this);
2530
2531 // Setting the touch point to (-1, -1) will show the options popup in the center of
2532 // the screen.
Sunny Goyal2fd7a8b2018-03-30 17:10:13 -07002533 OptionsPopupView.showDefaultOptions(this, -1, -1);
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002534 }
2535 return true;
2536 }
2537 return super.onKeyUp(keyCode, event);
2538 }
2539
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002540 public static Launcher getLauncher(Context context) {
Sunny Goyal87b5eb62018-07-03 15:53:39 -07002541 return (Launcher) fromContext(context);
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002542 }
2543
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002544 /**
Sunny Goyalab770a12018-11-14 15:17:26 -08002545 * Just a wrapper around the type cast to allow easier tracking of calls.
2546 */
2547 public static <T extends Launcher> T cast(ActivityContext activityContext) {
2548 return (T) activityContext;
2549 }
2550
2551 /**
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002552 * Callback for listening for onResume
2553 */
2554 public interface OnResumeCallback {
2555
2556 void onLauncherResume();
2557 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002558}