blob: 67086a578d2a4520bb46d40e23fa36d41282e9c4 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Jon Mirandaaeb4dd02018-07-11 14:16:23 -070019import static android.content.pm.ActivityInfo.CONFIG_LOCALE;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080020import static android.content.pm.ActivityInfo.CONFIG_ORIENTATION;
21import static android.content.pm.ActivityInfo.CONFIG_SCREEN_SIZE;
vadimt00d42552018-12-11 17:59:36 -080022
Tony Wickham6a71a5b2018-08-21 11:40:23 -070023import static com.android.launcher3.AbstractFloatingView.TYPE_SNACKBAR;
Sunny Goyal4c7f2152017-10-17 17:17:16 -070024import static com.android.launcher3.LauncherAnimUtils.SPRING_LOADED_EXIT_DELAY;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -070025import static com.android.launcher3.LauncherState.ALL_APPS;
26import static com.android.launcher3.LauncherState.NORMAL;
Govinda Wassermanc06e1512019-04-09 09:56:53 -040027import static com.android.launcher3.LauncherState.OVERVIEW;
28import static com.android.launcher3.LauncherState.OVERVIEW_PEEK;
Sunny Goyal6001ea22018-05-10 16:31:00 -070029import static com.android.launcher3.dragndrop.DragLayer.ALPHA_INDEX_LAUNCHER_LOAD;
Sunny Goyalaeb16432017-10-16 11:46:41 -070030import static com.android.launcher3.logging.LoggerUtils.newContainerTarget;
Hyunyoung Song46d07f72018-05-22 15:41:25 -070031import static com.android.launcher3.logging.LoggerUtils.newTarget;
Sunny Goyalbe8c3402019-05-22 15:17:52 -070032import static com.android.launcher3.states.RotationHelper.REQUEST_NONE;
vadimt00d42552018-12-11 17:59:36 -080033import static com.android.launcher3.util.RaceConditionTracker.ENTER;
34import static com.android.launcher3.util.RaceConditionTracker.EXIT;
Sunny Goyal326403e2017-10-02 12:45:10 -070035
Gilles Debunnedd6c9922010-10-25 11:23:41 -070036import android.animation.Animator;
Sunny Goyal6001ea22018-05-10 16:31:00 -070037import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070038import android.animation.AnimatorSet;
Sunny Goyal6001ea22018-05-10 16:31:00 -070039import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070040import android.animation.ValueAnimator;
Adam Cohen0b395352014-06-09 22:54:36 +000041import android.annotation.TargetApi;
Tony Wickham005df0b2018-02-13 11:28:12 -080042import android.app.ActivityOptions;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080043import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070044import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080046import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080047import android.content.ComponentCallbacks2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070050import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070051import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070052import android.content.SharedPreferences;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053import android.content.pm.PackageManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080054import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070055import android.database.sqlite.SQLiteDatabase;
Sunny Goyal59d086c2018-05-07 17:31:40 -070056import android.graphics.Point;
Sunny Goyalae6e3182019-04-30 12:04:37 -070057import android.graphics.Rect;
Adam Cohen0f668f32014-09-08 19:54:17 +020058import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059import android.os.Bundle;
Hyunyoung Songbd6fba92018-05-16 15:54:31 -070060import android.os.Handler;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070061import android.os.Parcelable;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080062import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070063import android.os.StrictMode;
Michael Jurkaa33411c2012-06-14 16:18:21 -070064import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070066import android.util.Log;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070067import android.util.SparseArray;
Sunny Goyal59d086c2018-05-07 17:31:40 -070068import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070069import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070070import android.view.KeyboardShortcutGroup;
71import android.view.KeyboardShortcutInfo;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080072import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -070073import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070074import android.view.View;
Adam Cohen96d30a12013-07-16 18:13:21 -070075import android.view.ViewGroup;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080076import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070077import android.view.animation.OvershootInterpolator;
Adam Cohen96d30a12013-07-16 18:13:21 -070078import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070079
Sunny Goyal651077b2014-06-30 14:15:31 -070080import com.android.launcher3.DropTarget.DragObject;
Sunny Goyalae502842016-06-17 08:43:56 -070081import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070082import com.android.launcher3.allapps.AllAppsContainerView;
vadimtd4c90e12019-06-04 13:59:43 -070083import com.android.launcher3.allapps.AllAppsStore;
Hyunyoung Song645764e2016-06-06 14:19:02 -070084import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal7ede6112017-12-05 15:11:21 -080085import com.android.launcher3.allapps.DiscoveryBounce;
Sunny Goyalf0b6db72018-08-13 16:10:14 -070086import com.android.launcher3.anim.PropertyListBuilder;
Sunny Goyalffe83f12014-08-14 17:39:34 -070087import com.android.launcher3.compat.AppWidgetManagerCompat;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -070088import com.android.launcher3.compat.LauncherAppsCompatVO;
Tony Wickhame0c33232016-02-08 11:37:04 -080089import com.android.launcher3.config.FeatureFlags;
vadimt00d42552018-12-11 17:59:36 -080090import com.android.launcher3.dot.DotInfo;
Vadim Tryshevfedca432015-08-19 17:55:02 -070091import com.android.launcher3.dragndrop.DragController;
92import com.android.launcher3.dragndrop.DragLayer;
93import com.android.launcher3.dragndrop.DragView;
Jon Mirandaaeb4dd02018-07-11 14:16:23 -070094import com.android.launcher3.folder.Folder;
Sunny Goyal26119432016-02-18 22:09:23 +000095import com.android.launcher3.folder.FolderIcon;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070096import com.android.launcher3.folder.FolderIconPreviewVerifier;
Sunny Goyalae6e3182019-04-30 12:04:37 -070097import com.android.launcher3.graphics.RotationMode;
Sunny Goyalf840f102018-09-21 14:41:05 -070098import com.android.launcher3.icons.IconCache;
Sunny Goyal66b24572016-09-21 15:57:55 -070099import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700100import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700101import com.android.launcher3.logging.FileLog;
Hyunyoung Songfc007472018-10-25 14:09:50 -0700102import com.android.launcher3.logging.StatsLogUtils;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700103import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700104import com.android.launcher3.logging.UserEventDispatcher.UserEventDelegate;
Sunny Goyal369212a2019-03-26 15:03:57 -0700105import com.android.launcher3.model.AppLaunchTracker;
Sunny Goyala535ae42017-02-27 10:07:13 -0800106import com.android.launcher3.model.ModelWriter;
Sunny Goyala535ae42017-02-27 10:07:13 -0800107import com.android.launcher3.notification.NotificationListener;
Tony Wickham540913e2017-01-23 11:47:51 -0800108import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -0800109import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700110import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyalf8088ee2017-11-10 14:52:00 -0800111import com.android.launcher3.states.InternalStateHandler;
Sunny Goyal623eddd2018-03-02 12:24:41 -0800112import com.android.launcher3.states.RotationHelper;
Sunny Goyalab3963d2019-05-23 00:50:08 -0700113import com.android.launcher3.testing.TestProtocol;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800114import com.android.launcher3.touch.ItemClickHandler;
Sunny Goyala5ace712017-11-15 17:12:51 -0800115import com.android.launcher3.uioverrides.UiFactory;
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700116import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530117import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
118import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
Sunny Goyalaeb16432017-10-16 11:46:41 -0700119import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyal2100c782016-08-22 16:00:03 -0700120import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700121import com.android.launcher3.util.ComponentKey;
Sunny Goyalefb7e842018-10-04 15:11:00 -0700122import com.android.launcher3.util.IntArray;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700123import com.android.launcher3.util.ItemInfoMatcher;
Sunny Goyal6001ea22018-05-10 16:31:00 -0700124import com.android.launcher3.util.MultiValueAlpha;
125import com.android.launcher3.util.MultiValueAlpha.AlphaProperty;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700126import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800127import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700128import com.android.launcher3.util.PendingRequestArgs;
vadimt00d42552018-12-11 17:59:36 -0800129import com.android.launcher3.util.RaceConditionTracker;
Sunny Goyal8392c822017-06-20 10:03:56 -0700130import com.android.launcher3.util.SystemUiController;
Mario Bertschlera6936942017-05-31 14:48:19 -0700131import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700132import com.android.launcher3.util.Thunk;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700133import com.android.launcher3.util.TraceHelper;
Sunny Goyal326403e2017-10-02 12:45:10 -0700134import com.android.launcher3.util.UiThreadHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700135import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyalab770a12018-11-14 15:17:26 -0800136import com.android.launcher3.views.ActivityContext;
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800137import com.android.launcher3.views.OptionsPopupView;
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400138import com.android.launcher3.views.ScrimView;
Sunny Goyal29947f02017-12-18 13:49:44 -0800139import com.android.launcher3.widget.LauncherAppWidgetHostView;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800140import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700141import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal29947f02017-12-18 13:49:44 -0800142import com.android.launcher3.widget.PendingAppWidgetHostView;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800143import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700144import com.android.launcher3.widget.WidgetHostViewLoader;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700145import com.android.launcher3.widget.WidgetListRowEntry;
146import com.android.launcher3.widget.WidgetsFullSheet;
Sunny Goyal952e63d2017-08-16 04:59:08 -0700147import com.android.launcher3.widget.custom.CustomWidgetParser;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700148
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700149import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700150import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700151import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700152import java.util.Collection;
Zak Cohen658c67a2018-10-19 14:21:05 -0700153import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700154import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700155import java.util.List;
Sunny Goyalaae6fbb2019-01-31 16:05:58 -0800156import java.util.function.Predicate;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700157
Sunny Goyalbe8c3402019-05-22 15:17:52 -0700158import androidx.annotation.Nullable;
159
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160/**
161 * Default launcher application.
162 */
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700163public class Launcher extends BaseDraggingActivity implements LauncherExterns,
Hyunyoung Song31971a32019-01-27 12:31:12 -0800164 LauncherModel.Callbacks, LauncherProviderChangeListener, UserEventDelegate,
165 InvariantDeviceProfile.OnIDPChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700166 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700167 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168
Winson Chunga2413752012-04-03 14:22:34 -0700169 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700170
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700172 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700173
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700174 private static final int REQUEST_PICK_APPWIDGET = 9;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175
Michael Jurka8b805b12012-04-18 14:23:14 -0700176 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800177 public static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
178 public static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700179
Jon Mirandac476d6e2017-05-04 16:30:01 -0700180 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700181
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700182 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
183
Mathew Inwood876a8462013-06-14 14:12:41 +0100184 /**
185 * IntentStarter uses request codes starting with this. This must be greater than all activity
186 * request codes used internally.
187 */
188 protected static final int REQUEST_LAST = 100;
189
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 // Type: int
191 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700192 // Type: int
193 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700194 // Type: PendingRequestArgs
195 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
Sunny Goyaldedda052019-05-14 15:23:48 -0700196 // Type: int
197 private static final String RUNTIME_STATE_PENDING_REQUEST_CODE = "launcher.request_code";
Sunny Goyal2100c782016-08-22 16:00:03 -0700198 // Type: ActivityResultInfo
199 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700200 // Type: SparseArray<Parcelable>
201 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
vadimt00d42552018-12-11 17:59:36 -0800202 public static final String ON_CREATE_EVT = "Launcher.onCreate";
vadimtcb863752018-12-19 17:44:57 -0800203 private static final String ON_START_EVT = "Launcher.onStart";
204 private static final String ON_RESUME_EVT = "Launcher.onResume";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700206 private LauncherStateManager mStateManager;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700207
Adam Cohenad4e15c2013-10-17 16:21:35 -0700208 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800209
Winson Chunga2413752012-04-03 14:22:34 -0700210 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700211 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
212 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
213 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700214
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400215 private static final int APPS_VIEW_ALPHA_CHANNEL_INDEX = 1;
216 private static final int SCRIM_VIEW_ALPHA_CHANNEL_INDEX = 0;
217
Jon Miranda54441f52018-01-24 15:38:25 -0800218 private LauncherAppTransitionManager mAppTransitionManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800219 private Configuration mOldConfig;
Jon Miranda54441f52018-01-24 15:38:25 -0800220
Adam Cohen091440a2015-03-18 14:16:05 -0700221 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700222 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700223 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800224 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700225
Sunny Goyalffe83f12014-08-14 17:39:34 -0700226 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700227 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700228
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700229 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700230
Sunny Goyal316490e2015-06-02 09:38:28 -0700231 @Thunk Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700232
Sunny Goyal47328fd2016-05-25 18:56:41 -0700233 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700234
235 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700236 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700237 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700238
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400239 // Scrim view for the all apps and overview state.
240 @Thunk ScrimView mScrimView;
241
Winson Chung8ae41982017-11-10 11:42:13 -0800242 // UI and state for the overview panel
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800243 private View mOverviewPanel;
Winson Chung8ae41982017-11-10 11:42:13 -0800244
Adam Cohen091440a2015-03-18 14:16:05 -0700245 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400246
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700247 private OnResumeCallback mOnResumeCallback;
248
Sunny Goyal527c7d32015-08-28 15:19:36 -0700249 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700250
Joe Onorato9c1289c2009-08-17 11:03:03 -0400251 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800252 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800253 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700254 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400255
Tony Wickham010d2552017-01-20 08:15:28 -0800256 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700257
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800258 private int mSynchronouslyBoundPage = PagedView.INVALID_PAGE;
Adam Cohen1462de32012-07-24 22:34:36 -0700259
Winson Chung46353de2012-02-16 14:05:10 -0800260 // We only want to get the SharedPreferences once since it does an FS stat each time we get
261 // it from the context.
262 private SharedPreferences mSharedPrefs;
263
Sunny Goyal2100c782016-08-22 16:00:03 -0700264 // Activity result which needs to be processed after workspace has loaded.
265 private ActivityResultInfo mPendingActivityResult;
266 /**
267 * Holds extra information required to handle a result from an external call, like
268 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
269 */
270 private PendingRequestArgs mPendingRequestArgs;
Sunny Goyaldedda052019-05-14 15:23:48 -0700271 // Request id for any pending activity result
272 private int mPendingActivityRequestCode = -1;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800273
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700274 public ViewGroupFocusHelper mFocusHandler;
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700275
Sunny Goyal623eddd2018-03-02 12:24:41 -0800276 private RotationHelper mRotationHelper;
Sunny Goyal5743f862019-03-28 15:35:32 -0700277 private Runnable mCancelTouchController;
Sunny Goyal7779d622015-06-11 16:18:39 -0700278
Sunny Goyal2db53422019-03-01 16:06:12 -0800279 final Handler mHandler = new Handler();
Sunny Goyala002c6c2019-02-12 16:20:10 -0800280 private final Runnable mHandleDeferredResume = this::handleDeferredResume;
Sunny Goyale9c6f4c2019-05-30 11:35:25 -0700281 private boolean mDeferredResumePending;
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700282
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400283 private float mCurrentAssistantVisibility = 0f;
284
Sunny Goyalae6e3182019-04-30 12:04:37 -0700285 private DeviceProfile mStableDeviceProfile;
286 private RotationMode mRotationMode = RotationMode.NORMAL;
287
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800288 @Override
289 protected void onCreate(Bundle savedInstanceState) {
vadimt00d42552018-12-11 17:59:36 -0800290 RaceConditionTracker.onEvent(ON_CREATE_EVT, ENTER);
Winson Chunga2413752012-04-03 14:22:34 -0700291 if (DEBUG_STRICT_MODE) {
292 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
293 .detectDiskReads()
294 .detectDiskWrites()
295 .detectNetwork() // or .detectAll() for all detectable problems
296 .penaltyLog()
297 .build());
298 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
299 .detectLeakedSqlLiteObjects()
300 .detectLeakedClosableObjects()
301 .penaltyLog()
302 .penaltyDeath()
303 .build());
304 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700305 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700306
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800307 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700308 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400309
Sunny Goyal87f784c2017-01-11 10:48:34 -0800310 LauncherAppState app = LauncherAppState.getInstance(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800311 mOldConfig = new Configuration(getResources().getConfiguration());
Sunny Goyal0e7724c2018-02-22 13:22:21 -0800312 mModel = app.setLauncher(this);
Sunny Goyal41d51a02019-05-14 09:44:34 -0700313 mRotationHelper = new RotationHelper(this);
Hyunyoung Song31971a32019-01-27 12:31:12 -0800314 InvariantDeviceProfile idp = app.getInvariantDeviceProfile();
315 initDeviceProfile(idp);
316 idp.addOnChangeListener(this);
Sunny Goyalf7258242015-10-19 16:59:07 -0700317 mSharedPrefs = Utilities.getPrefs(this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800318 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700319 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700320
Joe Onorato41a12d22009-10-31 18:30:00 -0400321 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700322 mAllAppsController = new AllAppsTransitionController(this);
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800323 mStateManager = new LauncherStateManager(this);
Tracy Zhou8b23c6f2018-04-30 17:13:27 -0700324 UiFactory.onCreate(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700325
Sunny Goyalffe83f12014-08-14 17:39:34 -0700326 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700327
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700328 mAppWidgetHost = new LauncherAppWidgetHost(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700329 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700330
Sunny Goyal60820d72017-05-09 12:40:11 -0700331 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700332
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800333 setupViews();
Tony Wickham010d2552017-01-20 08:15:28 -0800334 mPopupDataProvider = new PopupDataProvider(this);
335
Sunny Goyal90548432018-04-04 17:17:00 -0700336 mAppTransitionManager = LauncherAppTransitionManager.newInstance(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800337
Sunny Goyald3864fa2017-11-14 15:06:36 -0800338 boolean internalStateHandled = InternalStateHandler.handleCreate(this, getIntent());
339 if (internalStateHandled) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800340 if (savedInstanceState != null) {
341 // InternalStateHandler has already set the appropriate state.
342 // We dont need to do anything.
343 savedInstanceState.remove(RUNTIME_STATE);
344 }
345 }
346 restoreState(savedInstanceState);
Sunny Goyalbbece862019-03-01 13:13:52 -0800347 mStateManager.reapplyState();
Winson Chungb63b44c2017-11-10 17:54:44 -0800348
Sunny Goyal2100c782016-08-22 16:00:03 -0700349 // We only load the page synchronously if the user rotates (or triggers a
350 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700351 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
352 if (savedInstanceState != null) {
353 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
354 }
Sunny Goyald3864fa2017-11-14 15:06:36 -0800355
Sunny Goyalfe770c92016-09-27 14:38:58 -0700356 if (!mModel.startLoader(currentScreen)) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800357 if (!internalStateHandled) {
358 // If we are not binding synchronously, show a fade in animation when
359 // the first page bind completes.
Sunny Goyal6001ea22018-05-10 16:31:00 -0700360 mDragLayer.getAlphaProperty(ALPHA_INDEX_LAUNCHER_LOAD).setValue(0);
Sunny Goyald3864fa2017-11-14 15:06:36 -0800361 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700362 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700363 // Pages bound synchronously.
364 mWorkspace.setCurrentPage(currentScreen);
365
Sunny Goyal2100c782016-08-22 16:00:03 -0700366 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800367 }
368
369 // For handling default keys
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800370 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800371
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800372 setContentView(mLauncherView);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800373 getRootView().dispatchInsets();
Mario Bertschlera6936942017-05-31 14:48:19 -0700374
Jon Miranda7fda2852017-07-19 16:07:01 -0700375 // Listen for broadcasts
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700376 registerReceiver(mScreenOffReceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));
Sunny Goyala616d2b2017-06-12 13:54:01 -0700377
Sunny Goyal8392c822017-06-20 10:03:56 -0700378 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
379 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700380
381 if (mLauncherCallbacks != null) {
382 mLauncherCallbacks.onCreate(savedInstanceState);
383 }
Sunny Goyal623eddd2018-03-02 12:24:41 -0800384 mRotationHelper.initialize();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700385
386 TraceHelper.endSection("Launcher-onCreate");
vadimt00d42552018-12-11 17:59:36 -0800387 RaceConditionTracker.onEvent(ON_CREATE_EVT, EXIT);
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400388 mStateManager.addStateListener(new LauncherStateManager.StateListener() {
389 @Override
390 public void onStateTransitionStart(LauncherState toState) {}
391
392 @Override
393 public void onStateTransitionComplete(LauncherState finalState) {
394 float alpha = 1f - mCurrentAssistantVisibility;
395 if (finalState == NORMAL) {
396 mAppsView.getAlphaProperty(APPS_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
397 } else if (finalState == OVERVIEW || finalState == OVERVIEW_PEEK) {
398 mAppsView.getAlphaProperty(APPS_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
399 mScrimView.getAlphaProperty(SCRIM_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
400 } else {
401 mAppsView.getAlphaProperty(APPS_VIEW_ALPHA_CHANNEL_INDEX).setValue(1f);
402 mScrimView.getAlphaProperty(SCRIM_VIEW_ALPHA_CHANNEL_INDEX).setValue(1f);
403 }
404 }
405 });
Adam Cohen9211d422014-10-07 18:14:53 -0700406 }
407
Sunny Goyal7779d622015-06-11 16:18:39 -0700408 @Override
Winson Chung8eb49e02018-07-16 13:33:31 -0700409 public void onEnterAnimationComplete() {
410 super.onEnterAnimationComplete();
411 UiFactory.onEnterAnimationComplete(this);
arangelovad783052018-08-17 14:46:45 +0100412 mAllAppsController.highlightWorkTabIfNecessary();
Sunny Goyalbe8c3402019-05-22 15:17:52 -0700413 mRotationHelper.setCurrentTransitionRequest(REQUEST_NONE);
Winson Chung8eb49e02018-07-16 13:33:31 -0700414 }
415
416 @Override
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800417 public void onConfigurationChanged(Configuration newConfig) {
418 int diff = newConfig.diff(mOldConfig);
Jon Mirandaaeb4dd02018-07-11 14:16:23 -0700419
420 if ((diff & CONFIG_LOCALE) != 0) {
421 Folder.setLocaleDependentFields(getResources(), true /* force */);
422 }
423
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800424 if ((diff & (CONFIG_ORIENTATION | CONFIG_SCREEN_SIZE)) != 0) {
Sunny Goyal371ea052019-03-06 15:38:32 -0800425 onIdpChanged(mDeviceProfile.inv);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800426 }
427
428 mOldConfig.setTo(newConfig);
Tracy Zhoua706f002018-03-28 13:55:19 -0700429 UiFactory.onLauncherStateOrResumeChanged(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800430 super.onConfigurationChanged(newConfig);
431 }
432
Sunny Goyal41d51a02019-05-14 09:44:34 -0700433 private boolean supportsFakeLandscapeUI() {
434 return FeatureFlags.FAKE_LANDSCAPE_UI.get() && !mRotationHelper.homeScreenCanRotate();
435 }
436
Sunny Goyal605bcf32018-03-02 15:16:12 -0800437 @Override
Sunny Goyal41d51a02019-05-14 09:44:34 -0700438 public void reapplyUi() {
439 if (supportsFakeLandscapeUI()) {
Sunny Goyal7a39b1b2019-05-02 13:39:16 -0700440 mRotationMode = mStableDeviceProfile == null
441 ? RotationMode.NORMAL : UiFactory.getRotationMode(mDeviceProfile);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700442 }
Sunny Goyal59d086c2018-05-07 17:31:40 -0700443 getRootView().dispatchInsets();
444 getStateManager().reapplyState(true /* cancelCurrentAnimation */);
445 }
446
447 @Override
Sunny Goyal605bcf32018-03-02 15:16:12 -0800448 public void rebindModel() {
449 int currentPage = mWorkspace.getNextPage();
450 if (mModel.startLoader(currentPage)) {
451 mWorkspace.setCurrentPage(currentPage);
452 setWorkspaceLoading(true);
453 }
454 }
455
Hyunyoung Song31971a32019-01-27 12:31:12 -0800456 @Override
457 public void onIdpChanged(int changeFlags, InvariantDeviceProfile idp) {
Sunny Goyal371ea052019-03-06 15:38:32 -0800458 onIdpChanged(idp);
459 }
460
461 private void onIdpChanged(InvariantDeviceProfile idp) {
462 mUserEventDispatcher = null;
463
Hyunyoung Song31971a32019-01-27 12:31:12 -0800464 initDeviceProfile(idp);
Sunny Goyal371ea052019-03-06 15:38:32 -0800465 dispatchDeviceProfileChanged();
466 reapplyUi();
467 mDragLayer.recreateControllers();
468
469 // TODO: We can probably avoid rebind when only screen size changed.
470 rebindModel();
Hyunyoung Song31971a32019-01-27 12:31:12 -0800471 }
472
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400473 public void onAssistantVisibilityChanged(float visibility) {
474 mCurrentAssistantVisibility = visibility;
475 float alpha = 1f - visibility;
476 LauncherState state = mStateManager.getState();
477 if (state == NORMAL) {
478 mAppsView.getAlphaProperty(APPS_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
479 } else if (state == OVERVIEW || state == OVERVIEW_PEEK) {
480 mAppsView.getAlphaProperty(APPS_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
481 mScrimView.getAlphaProperty(SCRIM_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
482 }
483 }
484
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800485 private void initDeviceProfile(InvariantDeviceProfile idp) {
486 // Load configuration-specific DeviceProfile
Sunny Goyal59d086c2018-05-07 17:31:40 -0700487 mDeviceProfile = idp.getDeviceProfile(this);
Sunny Goyal8c48d8b2019-01-25 15:10:18 -0800488 if (isInMultiWindowMode()) {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700489 Display display = getWindowManager().getDefaultDisplay();
490 Point mwSize = new Point();
491 display.getSize(mwSize);
492 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
493 }
Sunny Goyalae6e3182019-04-30 12:04:37 -0700494
Sunny Goyalda14cf82019-05-30 10:31:09 -0700495 if (supportsFakeLandscapeUI() && mDeviceProfile.isVerticalBarLayout()) {
Sunny Goyalae6e3182019-04-30 12:04:37 -0700496 mStableDeviceProfile = mDeviceProfile.inv.portraitProfile;
Sunny Goyal7a39b1b2019-05-02 13:39:16 -0700497 mRotationMode = UiFactory.getRotationMode(mDeviceProfile);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700498 } else {
499 mStableDeviceProfile = null;
500 mRotationMode = RotationMode.NORMAL;
501 }
502
Sunny Goyalda14cf82019-05-30 10:31:09 -0700503 mRotationHelper.updateRotationAnimation();
Sunny Goyal59d086c2018-05-07 17:31:40 -0700504 onDeviceProfileInitiated();
Sunny Goyalae6e3182019-04-30 12:04:37 -0700505 mModelWriter = mModel.getWriter(getWallpaperDeviceProfile().isVerticalBarLayout(), true);
506 }
507
508 public void updateInsets(Rect insets) {
509 mDeviceProfile.updateInsets(insets);
510 if (mStableDeviceProfile != null) {
Sunny Goyal7a39b1b2019-05-02 13:39:16 -0700511 Rect r = mStableDeviceProfile.getInsets();
512 mRotationMode.mapInsets(this, insets, r);
513 mStableDeviceProfile.updateInsets(r);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700514 }
515 }
516
517 @Override
518 public RotationMode getRotationMode() {
519 return mRotationMode;
520 }
521
522 /**
523 * Device profile to be used by UI elements which are shown directly on top of the wallpaper
524 * and whose presentation is tied to the wallpaper (and physical device) and not the activity
525 * configuration.
526 */
527 @Override
528 public DeviceProfile getWallpaperDeviceProfile() {
529 return mStableDeviceProfile == null ? mDeviceProfile : mStableDeviceProfile;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800530 }
531
Sunny Goyal623eddd2018-03-02 12:24:41 -0800532 public RotationHelper getRotationHelper() {
533 return mRotationHelper;
534 }
535
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700536 public LauncherStateManager getStateManager() {
537 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700538 }
539
Mario Bertschler27288382017-05-24 15:35:09 -0700540 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700541 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800542 return mLauncherView.findViewById(id);
543 }
544
545 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700546 public void onAppWidgetHostReset() {
547 if (mAppWidgetHost != null) {
548 mAppWidgetHost.startListening();
549 }
550 }
551
Adam Cohen9211d422014-10-07 18:14:53 -0700552 private LauncherCallbacks mLauncherCallbacks;
553
Sunny Goyal32554d12015-12-03 15:31:25 -0800554 /**
555 * Call this after onCreate to set or clear overlay.
556 */
557 public void setLauncherOverlay(LauncherOverlay overlay) {
558 if (overlay != null) {
559 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
560 }
561 mWorkspace.setLauncherOverlay(overlay);
562 }
563
Adam Cohen9211d422014-10-07 18:14:53 -0700564 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
565 mLauncherCallbacks = callbacks;
566 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700567 }
568
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700569 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700570 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700571 if (mLauncherCallbacks != null) {
572 mLauncherCallbacks.onLauncherProviderChange();
573 }
574 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700575
Hyunyoung Song3f471442015-04-08 19:01:34 -0700576 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700577 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
578 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700579 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700580 }
581
Tony Wickham010d2552017-01-20 08:15:28 -0800582 public PopupDataProvider getPopupDataProvider() {
583 return mPopupDataProvider;
584 }
585
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700586 @Override
Tony Wickhamf34bee82018-12-03 18:11:39 -0800587 public DotInfo getDotInfoForItem(ItemInfo info) {
588 return mPopupDataProvider.getDotInfoForItem(info);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700589 }
590
591 @Override
592 public void invalidateParent(ItemInfo info) {
Jon Mirandae6f3fa42019-03-04 09:14:40 -0800593 if (info.container >= 0) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700594 View folderIcon = getWorkspace().getHomescreenIconByItemId(info.container);
Jon Mirandae6f3fa42019-03-04 09:14:40 -0800595 if (folderIcon instanceof FolderIcon && folderIcon.getTag() instanceof FolderInfo) {
596 FolderIconPreviewVerifier verifier =
597 new FolderIconPreviewVerifier(getDeviceProfile().inv);
598 verifier.setFolderInfo((FolderInfo) folderIcon.getTag());
599 if (verifier.isItemInPreview(info.rank)) {
600 folderIcon.invalidate();
601 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700602 }
603 }
604 }
605
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000606 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700607 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
608 * a configuration step, this allows the proper animations to run after other transitions.
609 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700610 private int completeAdd(
Sunny Goyal2100c782016-08-22 16:00:03 -0700611 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
Sunny Goyalefb7e842018-10-04 15:11:00 -0700612 int screenId = info.screenId;
Sunny Goyal2100c782016-08-22 16:00:03 -0700613 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700614 // When the screen id represents an actual screen (as opposed to a rank) we make sure
615 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700616 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700617 }
Adam Cohendb364c32014-05-20 14:23:40 -0700618
Sunny Goyal2100c782016-08-22 16:00:03 -0700619 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800620 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700621 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700622 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800623 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700624 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700625 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700626 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700627 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700628 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700629 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700630 int widgetId = appWidgetId;
631 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700632 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700633 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700634 // Since the view was just bound, also launch the configure activity if needed
635 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
636 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800637 if (provider != null) {
638 new WidgetAddFlowHandler(provider)
639 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700640 }
641 }
642 break;
643 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800644 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700645
Adam Cohendb364c32014-05-20 14:23:40 -0700646 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800647 }
648
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800649 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700650 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700651 if (isWorkspaceLoading()) {
652 // process the result once the workspace has loaded.
653 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
654 return;
655 }
656 mPendingActivityResult = null;
657
Winson Chunge341d302013-08-16 14:31:00 -0700658 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700659 final PendingRequestArgs requestArgs = mPendingRequestArgs;
660 setWaitingForResult(null);
661 if (requestArgs == null) {
662 return;
663 }
664
665 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700666
Adam Cohenad4e15c2013-10-17 16:21:35 -0700667 Runnable exitSpringLoaded = new Runnable() {
668 @Override
669 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700670 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700671 }
672 };
673
Michael Jurka8b805b12012-04-18 14:23:14 -0700674 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700675 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700676 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700677 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
678 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700679 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700680 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700681 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700682 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700683 addAppWidgetImpl(
684 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800685 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700686 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700687 }
688 return;
689 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200690
Adam Cohened66b2b2012-01-23 17:28:51 -0800691 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700692 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700693
Adam Cohened66b2b2012-01-23 17:28:51 -0800694 // We have special handling for widgets
695 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800696 final int appWidgetId;
697 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
698 : -1;
699 if (widgetId < 0) {
700 appWidgetId = pendingAddWidgetId;
701 } else {
702 appWidgetId = widgetId;
703 }
704
Adam Cohenad4e15c2013-10-17 16:21:35 -0700705 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800706 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700707 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
708 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700709 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700710 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700711 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700712 @Override
713 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700714 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700715 }
716 };
Adam Cohendb364c32014-05-20 14:23:40 -0700717
Sunny Goyal2100c782016-08-22 16:00:03 -0700718 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
719 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
720 } else {
721 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
722 // When the screen id represents an actual screen (as opposed to a rank)
723 // we make sure that the drop page actually exists.
724 requestArgs.screenId =
725 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700726 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700727 final CellLayout dropLayout =
728 mWorkspace.getScreenWithId(requestArgs.screenId);
729
730 dropLayout.setDropPending(true);
731 final Runnable onComplete = new Runnable() {
732 @Override
733 public void run() {
734 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
735 dropLayout.setDropPending(false);
736 }
737 };
738 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
739 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700740 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800741 return;
742 }
743
Sunny Goyald478c832016-04-01 12:04:16 -0700744 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
745 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700746 if (resultCode == RESULT_OK) {
747 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700748 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700749 }
750 // Leave the widget in the pending state if the user canceled the configure.
751 return;
752 }
753
Sunny Goyalaad90582015-07-09 14:22:50 -0700754 if (requestCode == REQUEST_CREATE_SHORTCUT) {
755 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700756 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
757 completeAdd(requestCode, data, -1, requestArgs);
758 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
759 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
760
Sunny Goyalaad90582015-07-09 14:22:50 -0700761 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700762 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
763 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800764 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800765 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800766 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800767 }
768
769 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700770 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800771 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyaldedda052019-05-14 15:23:48 -0700772 mPendingActivityRequestCode = -1;
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800773 handleActivityResult(requestCode, resultCode, data);
774 if (mLauncherCallbacks != null) {
775 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
776 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800777 }
778
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700779 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700780 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600781 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700782 PendingRequestArgs pendingArgs = mPendingRequestArgs;
783 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
784 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
785 setWaitingForResult(null);
786
Sunny Goyal28c6b962015-10-12 11:42:05 -0700787 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700788 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700789 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700790 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700791 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700792 Intent intent = pendingArgs.getPendingIntent();
793
Sunny Goyal28c6b962015-10-12 11:42:05 -0700794 if (grantResults.length > 0
795 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal369212a2019-03-26 15:03:57 -0700796 startActivitySafely(v, intent, null, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700797 } else {
798 // TODO: Show a snack bar with link to settings
799 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700800 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700801 }
802 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600803 if (mLauncherCallbacks != null) {
804 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
805 grantResults);
806 }
807 }
808
Adam Cohendb364c32014-05-20 14:23:40 -0700809 /**
810 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
811 *
812 * @param screenId the screen id to check
813 * @return the new screen, or screenId if it exists
814 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700815 private int ensurePendingDropLayoutExists(int screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800816 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700817 if (dropLayout == null) {
818 // it's possible that the add screen was removed because it was
819 // empty and a re-bind occurred
820 mWorkspace.addExtraEmptyScreen();
821 return mWorkspace.commitExtraEmptyScreen();
822 } else {
823 return screenId;
824 }
825 }
826
Sunny Goyal2100c782016-08-22 16:00:03 -0700827 @Thunk void completeTwoStageWidgetDrop(
828 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
829 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800830 Runnable onCompleteRunnable = null;
831 int animationType = 0;
832
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700833 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800834 if (resultCode == RESULT_OK) {
835 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
836 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800837 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700838 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800839 onCompleteRunnable = new Runnable() {
840 @Override
841 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700842 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700843 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800844 }
845 };
846 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800847 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800848 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800849 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700850 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700851 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700852 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
853 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700854 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700855 // The animated view may be null in the case of a rotation during widget configuration
856 onCompleteRunnable.run();
857 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800858 }
859
860 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700861 protected void onStop() {
862 super.onStop();
Adam Cohen9211d422014-10-07 18:14:53 -0700863 if (mLauncherCallbacks != null) {
864 mLauncherCallbacks.onStop();
865 }
Miranda Kephart1a359a22019-02-11 13:07:04 -0500866
Hyunyoung Song1241e612018-05-15 21:46:51 -0700867 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
868 mStateManager.getState().containerType, -1);
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700869
870 mAppWidgetHost.setListenIfResumed(false);
871
Tony Wickham010d2552017-01-20 08:15:28 -0800872 NotificationListener.removeNotificationsChangedListener();
Sunny Goyalaf161fd2018-02-05 16:34:57 -0800873 getStateManager().moveToRestState();
Winson Chungedb0d6b2018-03-21 17:32:55 -0700874
Tracy Zhou62646712018-06-26 14:19:02 -0700875 UiFactory.onLauncherStateOrResumeChanged(this);
876
Winson Chungdea51352018-04-24 13:02:10 -0700877 // Workaround for b/78520668, explicitly trim memory once UI is hidden
Winson Chung473730f2018-04-26 11:54:22 -0700878 onTrimMemory(TRIM_MEMORY_UI_HIDDEN);
Michael Jurkacd496d72013-04-11 11:32:45 -0700879 }
880
881 @Override
882 protected void onStart() {
vadimtcb863752018-12-19 17:44:57 -0800883 RaceConditionTracker.onEvent(ON_START_EVT, ENTER);
Michael Jurkacd496d72013-04-11 11:32:45 -0700884 super.onStart();
Adam Cohen9211d422014-10-07 18:14:53 -0700885 if (mLauncherCallbacks != null) {
886 mLauncherCallbacks.onStart();
887 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800888 mAppWidgetHost.setListenIfResumed(true);
Sunny Goyalcac002d2018-01-04 09:51:45 -0800889 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
vadimtcb863752018-12-19 17:44:57 -0800890 RaceConditionTracker.onEvent(ON_START_EVT, EXIT);
Michael Jurkacd496d72013-04-11 11:32:45 -0700891 }
892
Sunny Goyala002c6c2019-02-12 16:20:10 -0800893 private void handleDeferredResume() {
Sunny Goyale9c6f4c2019-05-30 11:35:25 -0700894 if (hasBeenResumed() && !mStateManager.getState().disableInteraction) {
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700895 getUserEventDispatcher().logActionCommand(Action.Command.RESUME,
896 mStateManager.getState().containerType, -1);
897 getUserEventDispatcher().startSession();
Sunny Goyala002c6c2019-02-12 16:20:10 -0800898
899 UiFactory.onLauncherStateOrResumeChanged(this);
Sunny Goyal369212a2019-03-26 15:03:57 -0700900 AppLaunchTracker.INSTANCE.get(this).onReturnedToHome();
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700901
Sunny Goyale9c6f4c2019-05-30 11:35:25 -0700902 // Process any items that were added while Launcher was away.
903 InstallShortcutReceiver.disableAndFlushInstallQueue(
904 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
905
906 // Refresh shortcuts if the permission changed.
907 mModel.refreshShortcutsIfRequired();
908
909 DiscoveryBounce.showForHomeIfNeeded(this);
910
911 if (mPendingActivityRequestCode != -1 && isInState(NORMAL)) {
912 UiFactory.resetPendingActivityResults(this, mPendingActivityRequestCode);
913 }
914 mDeferredResumePending = false;
915 } else {
916 mDeferredResumePending = true;
Sunny Goyaldedda052019-05-14 15:23:48 -0700917 }
918 }
919
920 protected void onStateSet(LauncherState state) {
921 getAppWidgetHost().setResumed(state == LauncherState.NORMAL);
Sunny Goyale9c6f4c2019-05-30 11:35:25 -0700922 if (mDeferredResumePending) {
923 handleDeferredResume();
924 }
925 if (mLauncherCallbacks != null) {
926 mLauncherCallbacks.onStateChanged();
927 }
Sunny Goyaldedda052019-05-14 15:23:48 -0700928 }
929
Michael Jurkacd496d72013-04-11 11:32:45 -0700930 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800931 protected void onResume() {
vadimtcb863752018-12-19 17:44:57 -0800932 RaceConditionTracker.onEvent(ON_RESUME_EVT, ENTER);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700933 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800934 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700935 TraceHelper.partitionSection("ON_RESUME", "superCall");
936
Sunny Goyala002c6c2019-02-12 16:20:10 -0800937 mHandler.removeCallbacks(mHandleDeferredResume);
938 Utilities.postAsyncCallback(mHandler, mHandleDeferredResume);
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700939
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700940 setOnResumeCallback(null);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700941
Adam Cohen9211d422014-10-07 18:14:53 -0700942 if (mLauncherCallbacks != null) {
943 mLauncherCallbacks.onResume();
944 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800945
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700946 TraceHelper.endSection("ON_RESUME");
vadimtcb863752018-12-19 17:44:57 -0800947 RaceConditionTracker.onEvent(ON_RESUME_EVT, EXIT);
Adam Cohen06dff352012-06-01 17:17:08 -0700948 }
949
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800950 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700951 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700952 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700953 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700954
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700955 super.onPause();
Joe Onorato24b6fd82009-11-12 13:47:09 -0800956 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700957 mDragController.resetLastGestureUpTime();
Hyunyoung Song497708c2019-05-01 16:16:53 -0700958 mDropTargetBar.animateToVisibility(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700959 if (mLauncherCallbacks != null) {
960 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700961 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700962 }
963
Winson Chungd7823942018-02-16 03:23:47 +0000964 @Override
Tracy Zhoua706f002018-03-28 13:55:19 -0700965 protected void onUserLeaveHint() {
966 super.onUserLeaveHint();
967 UiFactory.onLauncherStateOrResumeChanged(this);
968 }
969
970 @Override
Winson Chungd7823942018-02-16 03:23:47 +0000971 public void onWindowFocusChanged(boolean hasFocus) {
972 super.onWindowFocusChanged(hasFocus);
973 mStateManager.onWindowFocusChanged();
974 }
975
Adam Cohenc2d6e892014-10-16 09:49:24 -0700976 public interface LauncherOverlay {
977
978 /**
979 * Touch interaction leading to overscroll has begun
980 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700981 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700982
983 /**
984 * Touch interaction related to overscroll has ended
985 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700986 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700987
988 /**
989 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
990 * screen (or in the case of RTL, the rightmost screen).
991 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700992 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700993
994 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800995 * Called when the launcher is ready to use the overlay
996 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700997 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700998 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700999 }
1000
1001 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001002 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001003 }
1004
1005 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1006
Sunny Goyalc86df472016-02-25 09:19:38 -08001007 public void onScrollChanged(float progress) {
1008 if (mWorkspace != null) {
1009 mWorkspace.onOverlayScrollChanged(progress);
1010 }
1011 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001012 }
1013
Sunny Goyalf9403d92017-10-18 10:55:56 -07001014 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -07001015 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -07001016 }
1017
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001018 /**
1019 * Restores the previous state, if it exists.
1020 *
1021 * @param savedState The previous state.
1022 */
1023 private void restoreState(Bundle savedState) {
1024 if (savedState == null) {
1025 return;
1026 }
1027
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001028 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -07001029 LauncherState[] stateValues = LauncherState.values();
1030 LauncherState state = stateValues[stateOrdinal];
Sunny Goyalbc683e92017-12-06 10:25:07 -08001031 if (!state.disableRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001032 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001033 }
1034
Sunny Goyal2100c782016-08-22 16:00:03 -07001035 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
1036 if (requestArgs != null) {
1037 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001038 }
Sunny Goyaldedda052019-05-14 15:23:48 -07001039 mPendingActivityRequestCode = savedState.getInt(RUNTIME_STATE_PENDING_REQUEST_CODE);
Sunny Goyal2100c782016-08-22 16:00:03 -07001040
1041 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001042
1043 SparseArray<Parcelable> widgetsState =
1044 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
1045 if (widgetsState != null) {
1046 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
1047 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001048 }
1049
1050 /**
1051 * Finds all the views we need and configure them properly.
1052 */
1053 private void setupViews() {
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001054 mDragLayer = findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001055 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -07001056 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001057 mWorkspace.initParentViews(mDragLayer);
Sunny Goyal16764582017-11-06 16:00:34 -08001058 mOverviewPanel = findViewById(R.id.overview_panel);
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001059 mHotseat = findViewById(R.id.hotseat);
Craig Mautner360310b2012-10-26 15:13:08 -07001060
Sunny Goyal784f9c32016-03-23 16:56:54 -07001061 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1062 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1063 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001064
Winson Chung4c98d922011-05-31 16:50:48 -07001065 // Setup the drag layer
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001066 mDragLayer.setup(mDragController, mWorkspace);
Sunny Goyal5743f862019-03-28 15:35:32 -07001067 mCancelTouchController = UiFactory.enableLiveTouchControllerChanges(mDragLayer);
Winson Chung4c98d922011-05-31 16:50:48 -07001068
Hyunyoung Song645764e2016-06-06 14:19:02 -07001069 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001070 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1071 // default state, otherwise we will update to the wrong offsets in RTL
1072 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001073 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001074 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001075
Tony Wickham34d2c912015-09-11 09:27:58 -07001076 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001077 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001078
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001079 // Setup Apps
1080 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001081
Govinda Wassermanc06e1512019-04-09 09:56:53 -04001082 // Setup Scrim
1083 mScrimView = findViewById(R.id.scrim_view);
1084
Winson Chung3d503fb2011-07-13 17:25:49 -07001085 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001086 mDragController.setMoveTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001087 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001088
Sunny Goyal7185dd62018-03-14 17:51:49 -07001089 mAllAppsController.setupViews(mAppsView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090 }
1091
1092 /**
1093 * Creates a view representing a shortcut.
1094 *
1095 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001096 */
Sunny Goyal95899162019-03-27 16:03:06 -07001097 View createShortcut(WorkspaceItemInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001098 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001099 }
1100
1101 /**
1102 * Creates a view representing a shortcut inflated from the specified resource.
1103 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001104 * @param parent The group the shortcut belongs to.
1105 * @param info The data structure describing the shortcut.
1106 *
1107 * @return A View inflated from layoutResId.
1108 */
Sunny Goyal95899162019-03-27 16:03:06 -07001109 public View createShortcut(ViewGroup parent, WorkspaceItemInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -08001110 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
1111 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal95899162019-03-27 16:03:06 -07001112 favorite.applyFromWorkspaceItem(info);
Sunny Goyal8a2a63b2018-03-06 22:15:18 -08001113 favorite.setOnClickListener(ItemClickHandler.INSTANCE);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001114 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001115 return favorite;
1116 }
1117
1118 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -07001119 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001120 *
1121 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001122 */
Sunny Goyalefb7e842018-10-04 15:11:00 -07001123 private void completeAddShortcut(Intent data, int container, int screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001124 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001125 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
1126 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001127 return;
1128 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001129
Jon Mirandac476d6e2017-05-04 16:30:01 -07001130 int[] cellXY = mTmpAddItemCellCoordinates;
1131 CellLayout layout = getCellLayout(container, screenId);
1132
Sunny Goyal95899162019-03-27 16:03:06 -07001133 WorkspaceItemInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001134 if (Utilities.ATLEAST_OREO) {
Sunny Goyal95899162019-03-27 16:03:06 -07001135 info = LauncherAppsCompatVO.createWorkspaceItemFromPinItemRequest(
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001136 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001137 }
1138
1139 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001140 // Legacy shortcuts are only supported for primary profile.
1141 info = Process.myUserHandle().equals(args.user)
1142 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001143
Sunny Goyalb57645f2017-03-20 13:57:28 -07001144 if (info == null) {
1145 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1146 return;
1147 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001148 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001149 // The app is trying to add a shortcut without sufficient permissions
1150 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1151 return;
1152 }
1153 }
1154
Jon Mirandac476d6e2017-05-04 16:30:01 -07001155 if (container < 0) {
1156 // Adding a shortcut to the Workspace.
1157 final View view = createShortcut(info);
1158 boolean foundCellSpan = false;
1159 // First we check if we already know the exact location where we want to add this item.
1160 if (cellX >= 0 && cellY >= 0) {
1161 cellXY[0] = cellX;
1162 cellXY[1] = cellY;
1163 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001164
Jon Mirandac476d6e2017-05-04 16:30:01 -07001165 // If appropriate, either create a folder or add to an existing folder
1166 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001167 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001168 return;
1169 }
1170 DragObject dragObject = new DragObject();
1171 dragObject.dragInfo = info;
1172 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1173 true)) {
1174 return;
1175 }
1176 } else {
1177 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1178 }
1179
1180 if (!foundCellSpan) {
1181 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001182 return;
1183 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001184
1185 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1186 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001187 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001188 // Adding a shortcut to a Folder.
Sunny Goyale29897f52017-07-20 10:09:42 -07001189 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001190 if (folderIcon != null) {
1191 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1192 folderInfo.add(info, args.rank, false);
1193 } else {
Sunny Goyale29897f52017-07-20 10:09:42 -07001194 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001195 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001196 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001197 }
1198
Sunny Goyalefb7e842018-10-04 15:11:00 -07001199 public FolderIcon findFolderIcon(final int folderIconId) {
Sunny Goyal83fd25e2018-07-09 16:47:01 -07001200 return (FolderIcon) mWorkspace.getHomescreenIconByItemId(folderIconId);
Sunny Goyale29897f52017-07-20 10:09:42 -07001201 }
1202
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001203 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001204 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001205 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001206 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001207 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001208 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001209 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1210
Adam Cohened66b2b2012-01-23 17:28:51 -08001211 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001212 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001213 }
Romain Guycbb89e42009-06-08 15:52:54 -07001214
Adam Cohen59400422014-03-05 18:07:04 -08001215 LauncherAppWidgetInfo launcherInfo;
1216 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001217 launcherInfo.spanX = itemInfo.spanX;
1218 launcherInfo.spanY = itemInfo.spanY;
1219 launcherInfo.minSpanX = itemInfo.minSpanX;
1220 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001221 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001222
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001223 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001224 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001225
Sunny Goyal2100c782016-08-22 16:00:03 -07001226 if (hostView == null) {
1227 // Perform actual inflation because we're live
1228 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001229 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001230 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301231 prepareAppWidget(hostView, launcherInfo);
1232 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001233 }
Romain Guycbb89e42009-06-08 15:52:54 -07001234
Sunny Goyald5462aa2016-11-22 16:52:39 +05301235 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001236 hostView.setTag(item);
1237 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001238 hostView.setFocusable(true);
1239 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001240 }
1241
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001242 private final BroadcastReceiver mScreenOffReceiver = new BroadcastReceiver() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001243 @Override
1244 public void onReceive(Context context, Intent intent) {
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001245 // Reset AllApps to its initial state only if we are not in the middle of
1246 // processing a multi-step drop
1247 if (mPendingRequestArgs == null) {
1248 mStateManager.goToState(NORMAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001249 }
1250 }
1251 };
1252
Sunny Goyalaae6fbb2019-01-31 16:05:58 -08001253 public void updateNotificationDots(Predicate<PackageUserKey> updatedDots) {
Tony Wickhamf34bee82018-12-03 18:11:39 -08001254 mWorkspace.updateNotificationDots(updatedDots);
1255 mAppsView.getAppsStore().updateNotificationDots(updatedDots);
Tony Wickham010d2552017-01-20 08:15:28 -08001256 }
1257
Adam Cohended9f8d2010-11-03 13:25:16 -07001258 @Override
1259 public void onAttachedToWindow() {
1260 super.onAttachedToWindow();
1261
Sunny Goyalc86df472016-02-25 09:19:38 -08001262 if (mLauncherCallbacks != null) {
1263 mLauncherCallbacks.onAttachedToWindow();
1264 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001265 }
1266
1267 @Override
1268 public void onDetachedFromWindow() {
1269 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001270
1271 if (mLauncherCallbacks != null) {
1272 mLauncherCallbacks.onDetachedFromWindow();
1273 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001274 }
1275
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001276 public AllAppsTransitionController getAllAppsController() {
1277 return mAllAppsController;
1278 }
1279
Sunny Goyal9d69c8d2018-03-19 13:41:31 -07001280 @Override
Winson Chung1a341002018-01-17 10:00:23 -08001281 public LauncherRootView getRootView() {
1282 return (LauncherRootView) mLauncherView;
1283 }
1284
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001285 @Override
Winson Chunga6945242014-01-08 14:04:34 -08001286 public DragLayer getDragLayer() {
1287 return mDragLayer;
1288 }
1289
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001290 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001291 return mAppsView;
1292 }
1293
Winson Chunga6945242014-01-08 14:04:34 -08001294 public Workspace getWorkspace() {
1295 return mWorkspace;
1296 }
1297
1298 public Hotseat getHotseat() {
1299 return mHotseat;
1300 }
1301
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001302 public <T extends View> T getOverviewPanel() {
Winson Chung8ae41982017-11-10 11:42:13 -08001303 return (T) mOverviewPanel;
Winson Chunga6945242014-01-08 14:04:34 -08001304 }
1305
Sunny Goyal47328fd2016-05-25 18:56:41 -07001306 public DropTargetBar getDropTargetBar() {
1307 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001308 }
1309
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001310 public LauncherAppWidgetHost getAppWidgetHost() {
1311 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001312 }
Romain Guycbb89e42009-06-08 15:52:54 -07001313
Winson Chunga9abd0e2010-10-27 17:18:37 -07001314 public LauncherModel getModel() {
1315 return mModel;
1316 }
1317
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001318 public ModelWriter getModelWriter() {
1319 return mModelWriter;
1320 }
1321
Adam Cohen79d90c52016-04-22 13:29:20 -07001322 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001323 return mSharedPrefs;
1324 }
1325
Sunny Goyalf8d56fc2018-01-31 15:18:11 -08001326 public int getOrientation() { return mOldConfig.orientation; }
Jon Miranda6bed3502017-09-19 14:43:17 -07001327
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001328 @Override
1329 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001330 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001331 super.onNewIntent(intent);
1332
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001333 boolean alreadyOnHome = hasWindowFocus() && ((intent.getFlags() &
Winson15f8b172015-08-19 11:02:39 -07001334 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1335 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001336
1337 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001338 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001339 && AbstractFloatingView.getTopOpenView(this) == null;
Winson15f8b172015-08-19 11:02:39 -07001340 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001341 boolean internalStateHandled = InternalStateHandler
Sunny Goyal8b264562018-02-03 23:00:13 -08001342 .handleNewIntent(this, intent, isStarted());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001343
Winson15f8b172015-08-19 11:02:39 -07001344 if (isActionMain) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001345 if (!internalStateHandled) {
1346 // Note: There should be at most one log per method call. This is enforced
1347 // implicitly by using if-else statements.
1348 UserEventDispatcher ued = getUserEventDispatcher();
1349 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
1350 if (topOpenView != null) {
1351 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
1352 } else if (alreadyOnHome) {
1353 Target target = newContainerTarget(mStateManager.getState().containerType);
1354 target.pageIndex = mWorkspace.getCurrentPage();
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001355 ued.logActionCommand(Action.Command.HOME_INTENT, target,
1356 newContainerTarget(ContainerType.WORKSPACE));
Sunny Goyald3864fa2017-11-14 15:06:36 -08001357 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001358
Sunny Goyald3864fa2017-11-14 15:06:36 -08001359 // In all these cases, only animate if we're already on home
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001360 AbstractFloatingView.closeAllOpenViews(this, isStarted());
Jon Mirandafeba90f2016-10-06 10:53:29 -07001361
Sunny Goyala2467272018-03-16 14:53:05 -07001362 if (!isInState(NORMAL)) {
1363 // Only change state, if not already the same. This prevents cancelling any
1364 // animations running as part of resume
1365 mStateManager.goToState(NORMAL);
1366 }
Sunny Goyald3864fa2017-11-14 15:06:36 -08001367
1368 // Reset the apps view
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001369 if (!alreadyOnHome) {
Sunny Goyal7185dd62018-03-14 17:51:49 -07001370 mAppsView.reset(isStarted() /* animate */);
Sunny Goyald3864fa2017-11-14 15:06:36 -08001371 }
1372
Sunny Goyalaad33592018-08-07 17:20:35 -07001373 if (shouldMoveToDefaultScreen && !mWorkspace.isHandlingTouch()) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001374 mWorkspace.post(mWorkspace::moveToDefaultScreen);
1375 }
1376 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001377
1378 final View v = getWindow().peekDecorView();
1379 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001380 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001381 }
1382
Adam Cohen9211d422014-10-07 18:14:53 -07001383 if (mLauncherCallbacks != null) {
Sunny Goyal85462132018-04-24 11:39:37 -07001384 mLauncherCallbacks.onHomeIntent(internalStateHandled);
Adam Cohen9211d422014-10-07 18:14:53 -07001385 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001386 }
Winson15f8b172015-08-19 11:02:39 -07001387
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001388 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001389 }
1390
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001391 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001392 public void onRestoreInstanceState(Bundle state) {
1393 super.onRestoreInstanceState(state);
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001394 mWorkspace.restoreInstanceStateForChild(mSynchronouslyBoundPage);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001395 }
1396
1397 @Override
1398 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001399 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001400 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001401
Adam Cohen21cd0022013-10-09 18:57:02 -07001402 }
Sunny Goyalea609262017-10-25 15:47:38 -07001403 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001404
1405
1406 AbstractFloatingView widgets = AbstractFloatingView
1407 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1408 if (widgets != null) {
1409 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1410 widgets.saveHierarchyState(widgetsState);
1411 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1412 } else {
1413 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1414 }
1415
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001416 // We close any open folders and shortcut containers since they will not be re-opened,
1417 // and we need to make sure this state is reflected.
1418 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001419
Sunny Goyal2100c782016-08-22 16:00:03 -07001420 if (mPendingRequestArgs != null) {
1421 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1422 }
Sunny Goyaldedda052019-05-14 15:23:48 -07001423 outState.putInt(RUNTIME_STATE_PENDING_REQUEST_CODE, mPendingActivityRequestCode);
1424
Sunny Goyal2100c782016-08-22 16:00:03 -07001425 if (mPendingActivityResult != null) {
1426 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001427 }
1428
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001429 super.onSaveInstanceState(outState);
1430
Adam Cohen9211d422014-10-07 18:14:53 -07001431 if (mLauncherCallbacks != null) {
1432 mLauncherCallbacks.onSaveInstanceState(outState);
1433 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001434 }
1435
1436 @Override
1437 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001438 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001439
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001440 unregisterReceiver(mScreenOffReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001441 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001442
Sunny Goyal5743f862019-03-28 15:35:32 -07001443 if (mCancelTouchController != null) {
1444 mCancelTouchController.run();
1445 mCancelTouchController = null;
1446 }
Tony Wickhama0068302018-04-11 16:16:11 -07001447
Winson Chungcd2b0142011-06-08 16:02:26 -07001448 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001449 // It's possible to receive onDestroy after a new Launcher activity has
1450 // been created. In this case, don't interfere with the new Launcher.
1451 if (mModel.isCurrentCallbacks(this)) {
1452 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001453 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001454 }
Sunny Goyal623eddd2018-03-02 12:24:41 -08001455 mRotationHelper.destroy();
Sunny Goyal745bad92016-05-02 10:54:12 -07001456
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001457 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001458 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001459 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001460 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001461 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001462
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001463 TextKeyListener.getInstance().release();
Tony2917a8b2017-07-31 23:29:42 -07001464 clearPendingBinds();
Hyunyoung Song31971a32019-01-27 12:31:12 -08001465 LauncherAppState.getIDP(this).removeOnChangeListener(this);
Adam Cohen9211d422014-10-07 18:14:53 -07001466 if (mLauncherCallbacks != null) {
1467 mLauncherCallbacks.onDestroy();
1468 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001469 }
1470
Sunny Goyalae502842016-06-17 08:43:56 -07001471 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1472 return mAccessibilityDelegate;
1473 }
1474
Adam Cohena9cf38f2011-05-02 15:36:58 -07001475 public DragController getDragController() {
1476 return mDragController;
1477 }
1478
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001479 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001480 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyaldedda052019-05-14 15:23:48 -07001481 if (requestCode != -1) {
1482 mPendingActivityRequestCode = requestCode;
1483 }
1484 if (requestCode == -1
1485 || !UiFactory.startActivityForResult(this, intent, requestCode, options)) {
1486 super.startActivityForResult(intent, requestCode, options);
1487 }
Adam Cohen173f7112015-03-27 15:14:00 -07001488 }
1489
1490 @Override
Sunny Goyaldedda052019-05-14 15:23:48 -07001491 public void startIntentSenderForResult(IntentSender intent, int requestCode,
Adam Cohen173f7112015-03-27 15:14:00 -07001492 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Sunny Goyaldedda052019-05-14 15:23:48 -07001493 if (requestCode != -1) {
1494 mPendingActivityRequestCode = requestCode;
1495 }
1496 if (requestCode == -1 || !UiFactory.startIntentSenderForResult(this, intent, requestCode,
1497 fillInIntent, flagsMask, flagsValues, extraFlags, options)) {
1498 try {
1499 super.startIntentSenderForResult(intent, requestCode,
1500 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1501 } catch (IntentSender.SendIntentException e) {
1502 throw new ActivityNotFoundException();
1503 }
Adam Cohen173f7112015-03-27 15:14:00 -07001504 }
1505 }
1506
Winson Chung70d72102011-08-12 11:24:35 -07001507 /**
1508 * Indicates that we want global search for this activity by setting the globalSearch
1509 * argument for {@link #startSearch} to true.
1510 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001512 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001513 Bundle appSearchData, boolean globalSearch) {
1514 if (appSearchData == null) {
1515 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001516 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001517 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001518
Sunny Goyal6f28e712016-09-13 14:19:29 -07001519 if (mLauncherCallbacks == null ||
1520 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1521 // Starting search from the callbacks failed. Start the default global search.
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001522 super.startSearch(initialQuery, selectInitialQuery, appSearchData, true);
Ian Parkinson047036e2014-09-01 15:40:53 +01001523 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001524
1525 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001526 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001527 }
1528
Joe Onorato9c1289c2009-08-17 11:03:03 -04001529 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001530 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001531 }
1532
Adam Cohen517a7f52014-03-01 12:12:59 -08001533 public boolean isWorkspaceLoading() {
1534 return mWorkspaceLoading;
1535 }
1536
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001537 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001538 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001539 }
1540
Sunny Goyal04a324a2017-01-20 21:08:59 -08001541 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001542 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001543 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001544
Sunny Goyal2100c782016-08-22 16:00:03 -07001545 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001546 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001547 if (LOGD) {
1548 Log.d(TAG, "Adding widget from drop");
1549 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001550 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001551 }
1552
Sunny Goyal2100c782016-08-22 16:00:03 -07001553 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001554 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1555 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1556 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001557
Adam Cohenad4e15c2013-10-17 16:21:35 -07001558 Runnable onComplete = new Runnable() {
1559 @Override
1560 public void run() {
1561 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001562 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001563 }
1564 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001565 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001566 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001567 }
1568 }
Romain Guycbb89e42009-06-08 15:52:54 -07001569
Sunny Goyalefb7e842018-10-04 15:11:00 -07001570 public void addPendingItem(PendingAddItemInfo info, int container, int screenId,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001571 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001572 info.container = container;
1573 info.screenId = screenId;
1574 if (cell != null) {
1575 info.cellX = cell[0];
1576 info.cellY = cell[1];
1577 }
1578 info.spanX = spanX;
1579 info.spanY = spanY;
1580
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001581 switch (info.itemType) {
1582 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1583 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001584 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001585 break;
1586 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001587 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001588 break;
1589 default:
1590 throw new IllegalStateException("Unknown item type: " + info.itemType);
1591 }
1592 }
1593
Adam Cohenfbba09b2011-07-18 21:43:05 -07001594 /**
1595 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001596 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001597 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001598 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1599 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001600 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1601 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1602 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001603 }
1604
Adam Cohenfbba09b2011-07-18 21:43:05 -07001605 /**
1606 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001607 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001608 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001609 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001610 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001611 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001612 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001613 // In the case where we've prebound the widget, we remove it from the DragLayer
1614 if (LOGD) {
1615 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1616 }
1617 getDragLayer().removeView(hostView);
1618
Adam Cohened66b2b2012-01-23 17:28:51 -08001619 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001620 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001621
1622 // Clear the boundWidget so that it doesn't get destroyed.
1623 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001624 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001625 // In this case, we either need to start an activity to get permission to bind
1626 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001627 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1628 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1629 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1630 this, info.componentName);
1631 } else {
1632 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1633 }
Adam Cohendd70d662012-10-04 16:53:44 -07001634 Bundle options = info.bindOptions;
1635
Sunny Goyalffe83f12014-08-14 17:39:34 -07001636 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1637 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001638 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001639 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001640 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001641 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001642 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001643 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001644 }
1645
Sunny Goyalefb7e842018-10-04 15:11:00 -07001646 FolderIcon addFolder(CellLayout layout, int container, final int screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001647 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001648 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001649 folderInfo.title = getText(R.string.folder_name);
1650
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001651 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001652 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001653
1654 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001655 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301656 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001657 // Force measure the new folder icon
1658 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1659 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001660 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001661 }
Romain Guycbb89e42009-06-08 15:52:54 -07001662
Winsonc0b52fe2015-09-09 16:38:15 -07001663 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001664 * Unbinds the view for the specified item, and removes the item and all its children.
1665 *
1666 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001667 * @param itemInfo the {@link ItemInfo} for this view.
1668 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001669 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001670 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Sunny Goyal95899162019-03-27 16:03:06 -07001671 if (itemInfo instanceof WorkspaceItemInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001672 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001673 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1674 if (folderIcon instanceof FolderIcon) {
Sunny Goyal95899162019-03-27 16:03:06 -07001675 ((FolderInfo) folderIcon.getTag()).remove((WorkspaceItemInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001676 } else {
1677 mWorkspace.removeWorkspaceItem(v);
1678 }
Winsonc0b52fe2015-09-09 16:38:15 -07001679 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001680 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001681 }
1682 } else if (itemInfo instanceof FolderInfo) {
1683 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001684 if (v instanceof FolderIcon) {
1685 ((FolderIcon) v).removeListeners();
1686 }
Winsonc0b52fe2015-09-09 16:38:15 -07001687 mWorkspace.removeWorkspaceItem(v);
1688 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001689 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001690 }
1691 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1692 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001693 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001694 if (deleteFromDb) {
Tony Wickham6a71a5b2018-08-21 11:40:23 -07001695 getModelWriter().deleteWidgetInfo(widgetInfo, getAppWidgetHost());
Winsonc0b52fe2015-09-09 16:38:15 -07001696 }
1697 } else {
1698 return false;
1699 }
1700 return true;
1701 }
1702
Tony Wickham6a71a5b2018-08-21 11:40:23 -07001703
Winsonc0b52fe2015-09-09 16:38:15 -07001704
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001705 @Override
1706 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001707 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001708 }
1709
Joe Onorato88ec0992009-11-19 13:16:06 -08001710 @Override
1711 public void onBackPressed() {
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08001712 if (finishAutoCancelActionMode()) {
1713 return;
1714 }
Adam Cohen9211d422014-10-07 18:14:53 -07001715 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1716 return;
1717 }
1718
Sunny Goyal45478022015-06-08 16:52:41 -07001719 if (mDragController.isDragging()) {
1720 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001721 return;
1722 }
1723
Jon Mirandafeba90f2016-10-06 10:53:29 -07001724 // Note: There should be at most one log per method call. This is enforced implicitly
1725 // by using if-else statements.
1726 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001727 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
Tony Wickham52c1b662018-05-21 13:13:58 -07001728 if (topView != null && topView.onBackPressed()) {
1729 // Handled by the floating view.
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001730 } else {
Sunny Goyale39690b2018-08-02 13:35:07 -07001731 mStateManager.getState().onBackPressed(this);
Michael Jurkaaf442092010-06-10 17:01:57 -07001732 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001733 }
1734
Sunny Goyalfe770c92016-09-27 14:38:58 -07001735 @TargetApi(Build.VERSION_CODES.M)
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001736 @Override
Jon Miranda73c27ec2018-05-16 18:06:23 -07001737 public ActivityOptions getActivityLaunchOptions(View v) {
1738 return mAppTransitionManager.getActivityLaunchOptions(this, v);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001739 }
1740
Tony Wickham3a6746a2018-03-29 09:39:56 -07001741 public LauncherAppTransitionManager getAppTransitionManager() {
1742 return mAppTransitionManager;
1743 }
1744
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001745 @TargetApi(Build.VERSION_CODES.M)
1746 @Override
1747 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
1748 // Due to legacy reasons, direct call shortcuts require Launchers to have the
1749 // corresponding permission. Show the appropriate permission prompt if that
1750 // is the case.
1751 if (intent.getComponent() == null
1752 && Intent.ACTION_CALL.equals(intent.getAction())
1753 && checkSelfPermission(android.Manifest.permission.CALL_PHONE) !=
1754 PackageManager.PERMISSION_GRANTED) {
1755
1756 setWaitingForResult(PendingRequestArgs
1757 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
1758 requestPermissions(new String[]{android.Manifest.permission.CALL_PHONE},
1759 REQUEST_PERMISSION_CALL_PHONE);
1760 return true;
1761 } else {
1762 return false;
1763 }
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001764 }
1765
Hyunyoung Song46d07f72018-05-22 15:41:25 -07001766 @Override
Hyunyoung Songfc007472018-10-25 14:09:50 -07001767 public int getCurrentState() {
1768 if(mStateManager.getState() == LauncherState.ALL_APPS) {
1769 return StatsLogUtils.LAUNCHER_STATE_ALLAPPS;
Govinda Wassermanc06e1512019-04-09 09:56:53 -04001770 } else if (mStateManager.getState() == OVERVIEW) {
Hyunyoung Songfc007472018-10-25 14:09:50 -07001771 return StatsLogUtils.LAUNCHER_STATE_OVERVIEW;
1772 }
Hyunyoung Song0ae38882018-11-05 14:45:19 -08001773 return StatsLogUtils.LAUNCHER_STATE_HOME;
Hyunyoung Songfc007472018-10-25 14:09:50 -07001774 }
1775
1776 @Override
Hyunyoung Song46d07f72018-05-22 15:41:25 -07001777 public void modifyUserEvent(LauncherLogProto.LauncherEvent event) {
1778 if (event.srcTarget != null && event.srcTarget.length > 0 &&
1779 event.srcTarget[1].containerType == ContainerType.PREDICTION) {
1780 Target[] targets = new Target[3];
1781 targets[0] = event.srcTarget[0];
1782 targets[1] = event.srcTarget[1];
1783 targets[2] = newTarget(Target.Type.CONTAINER);
1784 event.srcTarget = targets;
1785 LauncherState state = mStateManager.getState();
1786 if (state == LauncherState.ALL_APPS) {
1787 event.srcTarget[2].containerType = ContainerType.ALLAPPS;
Govinda Wassermanc06e1512019-04-09 09:56:53 -04001788 } else if (state == OVERVIEW) {
Hyunyoung Song46d07f72018-05-22 15:41:25 -07001789 event.srcTarget[2].containerType = ContainerType.TASKSWITCHER;
1790 }
1791 }
1792 }
1793
Sunny Goyal369212a2019-03-26 15:03:57 -07001794 public boolean startActivitySafely(View v, Intent intent, ItemInfo item,
1795 @Nullable String sourceContainer) {
Sunny Goyalab3963d2019-05-23 00:50:08 -07001796 if (TestProtocol.sDebugTracing) {
1797 android.util.Log.d(TestProtocol.NO_START_TAG,
vadimt3ab80bb2019-05-22 12:58:35 -07001798 "startActivitySafely outer");
1799 }
Sunny Goyal369212a2019-03-26 15:03:57 -07001800 boolean success = super.startActivitySafely(v, intent, item, sourceContainer);
Hyunyoung Song1241e612018-05-15 21:46:51 -07001801 if (success && v instanceof BubbleTextView) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001802 // This is set to the view that launched the activity that navigated the user away
1803 // from launcher. Since there is no callback for when the activity has finished
1804 // launching, enable the press state and keep this reference to reset the press
1805 // state when we return to launcher.
1806 BubbleTextView btv = (BubbleTextView) v;
1807 btv.setStayPressed(true);
1808 setOnResumeCallback(btv);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001809 }
Hyunyoung Song1241e612018-05-15 21:46:51 -07001810 return success;
Michael Jurka86a720e2012-05-09 11:23:23 -07001811 }
1812
Winson Chung3d503fb2011-07-13 17:25:49 -07001813 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001814 // TODO: Remove this method
Sunny Goyal876e4622018-11-02 13:50:40 -07001815 return mHotseat != null && (layout == mHotseat);
Winson Chung3d503fb2011-07-13 17:25:49 -07001816 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001817
Winson Chung3d503fb2011-07-13 17:25:49 -07001818 /**
1819 * Returns the CellLayout of the specified container at the specified screen.
1820 */
Sunny Goyalefb7e842018-10-04 15:11:00 -07001821 public CellLayout getCellLayout(int container, int screenId) {
Sunny Goyal876e4622018-11-02 13:50:40 -07001822 return (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1823 ? mHotseat : mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08001824 }
1825
Michael Jurkae326f182011-11-21 14:05:46 -08001826 @Override
1827 public void onTrimMemory(int level) {
1828 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07001829 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
1830 // The widget preview db can result in holding onto over
1831 // 3MB of memory for caching which isn't necessary.
1832 SQLiteDatabase.releaseMemory();
1833
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08001834 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07001835 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08001836 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01001837 if (mLauncherCallbacks != null) {
1838 mLauncherCallbacks.onTrimMemory(level);
1839 }
Winson Chung62a70be2018-02-23 15:10:05 -08001840 UiFactory.onTrimMemory(this, level);
Michael Jurkae326f182011-11-21 14:05:46 -08001841 }
1842
Michael Jurkad7c28052012-04-27 15:43:36 -07001843 @Override
1844 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07001845 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07001846 final List<CharSequence> text = event.getText();
1847 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07001848 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001849 // TODO: When can workspace be null?
1850 text.add(mWorkspace == null
1851 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07001852 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07001853 return result;
1854 }
1855
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001856 public void setOnResumeCallback(OnResumeCallback callback) {
1857 if (mOnResumeCallback != null) {
1858 mOnResumeCallback.onLauncherResume();
1859 }
1860 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07001861 }
1862
Michael Jurka7607c2f2013-04-03 14:33:19 -07001863 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001864 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001865 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07001866 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001867 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07001868 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001869 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07001870 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07001871 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07001872 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001873 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001874
Joe Onorato9c1289c2009-08-17 11:03:03 -04001875 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07001876 * Clear any pending bind callbacks. This is called when is loader is planning to
1877 * perform a full rebind from scratch.
1878 */
1879 @Override
1880 public void clearPendingBinds() {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001881 if (mPendingExecutor != null) {
1882 mPendingExecutor.markCompleted();
1883 mPendingExecutor = null;
1884 }
1885 }
1886
1887 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001888 * Refreshes the shortcuts shown on the workspace.
1889 *
1890 * Implementation of the method from LauncherModel.Callbacks.
1891 */
1892 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001893 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001894 // Floating panels (except the full widget sheet) are associated with individual icons. If
1895 // we are starting a fresh bind, close all such panels as all the icons are about
1896 // to go away.
1897 AbstractFloatingView.closeOpenViews(this, true,
Sunny Goyal7ede6112017-12-05 15:11:21 -08001898 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_REBIND_SAFE);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08001899
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001900 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08001901
Winson Chungd64d1762013-08-20 14:37:16 -07001902 // Clear the workspace because it's going to be rebound
Sunny Goyal2db53422019-03-01 16:06:12 -08001903 mDragController.cancelDrag();
1904
Adam Cohendf035382011-04-11 17:22:04 -07001905 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07001906 mWorkspace.removeAllWorkspaceScreens();
Sunny Goyalc11fac32018-02-27 19:20:15 -08001907 mAppWidgetHost.clearViews();
Winson Chungd64d1762013-08-20 14:37:16 -07001908
Winson Chung3d503fb2011-07-13 17:25:49 -07001909 if (mHotseat != null) {
Sunny Goyalae6e3182019-04-30 12:04:37 -07001910 mHotseat.resetLayout(getWallpaperDeviceProfile().isVerticalBarLayout());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001911 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001912 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001913 }
1914
Adam Cohendcd297f2013-06-18 13:13:40 -07001915 @Override
Sunny Goyalefb7e842018-10-04 15:11:00 -07001916 public void bindScreens(IntArray orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001917 // Make sure the first screen is always at the start.
Jon Miranda7143ba62019-03-15 09:00:05 -07001918 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001919 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalefb7e842018-10-04 15:11:00 -07001920 orderedScreenIds.removeValue(Workspace.FIRST_SCREEN_ID);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001921 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Jon Miranda7143ba62019-03-15 09:00:05 -07001922 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001923 // If there are no screens, we need to have an empty screen
1924 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07001925 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001926 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07001927
Winsonc7d2e832016-07-28 12:24:55 -07001928 // After we have added all the screens, if the wallpaper was locked to the default state,
1929 // then notify to indicate that it can be released and a proper wallpaper offset can be
1930 // computed before the next layout
1931 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07001932 }
1933
Sunny Goyalefb7e842018-10-04 15:11:00 -07001934 private void bindAddScreens(IntArray orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001935 int count = orderedScreenIds.size();
1936 for (int i = 0; i < count; i++) {
Sunny Goyalefb7e842018-10-04 15:11:00 -07001937 int screenId = orderedScreenIds.get(i);
Jon Miranda7143ba62019-03-15 09:00:05 -07001938 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001939 // No need to bind the first screen, as its always bound.
1940 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
1941 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001942 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001943 }
1944
Sunny Goyalb23980c2017-08-17 07:45:25 -07001945 @Override
Tony Wickham6a71a5b2018-08-21 11:40:23 -07001946 public void preAddApps() {
1947 // If there's an undo snackbar, force it to complete to ensure empty screens are removed
1948 // before trying to add new items.
1949 mModelWriter.commitDelete();
1950 AbstractFloatingView snackbar = AbstractFloatingView.getOpenView(this, TYPE_SNACKBAR);
1951 if (snackbar != null) {
1952 snackbar.post(() -> snackbar.close(true));
1953 }
1954 }
1955
1956 @Override
Sunny Goyalefb7e842018-10-04 15:11:00 -07001957 public void bindAppsAdded(IntArray newScreens, ArrayList<ItemInfo> addNotAnimated,
Sunny Goyal29947f02017-12-18 13:49:44 -08001958 ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07001959 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08001960 if (newScreens != null) {
1961 bindAddScreens(newScreens);
1962 }
Winson Chungd64d1762013-08-20 14:37:16 -07001963
1964 // We add the items without animation on non-visible pages, and with
1965 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08001966 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001967 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001968 }
Adam Cohen76a47a12014-02-05 11:47:43 -08001969 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001970 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07001971 }
Winson Chungc58497e2013-09-03 17:48:37 -07001972
Winson Chung87412982013-10-03 18:34:14 -07001973 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07001974 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001975 }
1976
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001977 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001978 * Bind the items start-end from the list.
1979 *
1980 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001981 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07001982 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07001983 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001984 // Get the list of added items and intersect them with the set of items here
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001985 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07001986 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001987 Workspace workspace = mWorkspace;
Sunny Goyalefb7e842018-10-04 15:11:00 -07001988 int newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07001989 int end = items.size();
1990 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001991 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07001992
1993 // Short circuit if we are loading dock items for a configuration which has no dock
1994 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
1995 mHotseat == null) {
1996 continue;
1997 }
1998
Sunny Goyal639e9062015-08-19 19:17:06 -07001999 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002000 switch (item.itemType) {
2001 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2002 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08002003 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Sunny Goyal95899162019-03-27 16:03:06 -07002004 WorkspaceItemInfo info = (WorkspaceItemInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07002005 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002006 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002007 }
2008 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07002009 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002010 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002011 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002012 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002013 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07002014 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2015 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07002016 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002017 if (view == null) {
2018 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002019 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08002020 break;
2021 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002022 default:
2023 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002024 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002025
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002026 /*
2027 * Remove colliding items.
2028 */
2029 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
2030 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
2031 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
2032 View v = cl.getChildAt(item.cellX, item.cellY);
2033 Object tag = v.getTag();
2034 String desc = "Collision while binding workspace item: " + item
2035 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08002036 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002037 throw (new RuntimeException(desc));
2038 } else {
2039 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002040 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002041 continue;
2042 }
2043 }
2044 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302045 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07002046 if (animateIcons) {
2047 // Animate all the applications up now
2048 view.setAlpha(0f);
2049 view.setScaleX(0f);
2050 view.setScaleY(0f);
2051 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08002052 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07002053 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002054 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07002055
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002056 // Animate to the correct page
2057 if (animateIcons && newItemsScreenId > -1) {
2058 AnimatorSet anim = new AnimatorSet();
2059 anim.playTogether(bounceAnims);
Jon Miranda64eb8ea2018-03-29 11:43:58 -07002060
Sunny Goyalefb7e842018-10-04 15:11:00 -07002061 int currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002062 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
2063 final Runnable startBounceAnimRunnable = anim::start;
2064
2065 if (newItemsScreenId != currentScreenId) {
2066 // We post the animation slightly delayed to prevent slowdowns
2067 // when we are loading right after we return to launcher.
2068 mWorkspace.postDelayed(new Runnable() {
2069 public void run() {
2070 if (mWorkspace != null) {
2071 AbstractFloatingView.closeAllOpenViews(Launcher.this, false);
2072
2073 mWorkspace.snapToPage(newScreenIndex);
2074 mWorkspace.postDelayed(startBounceAnimRunnable,
2075 NEW_APPS_ANIMATION_DELAY);
Winson Chung94d67682013-09-25 16:29:40 -07002076 }
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002077 }
2078 }, NEW_APPS_PAGE_MOVE_DELAY);
2079 } else {
2080 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07002081 }
Winson Chung64359a52013-07-08 17:17:08 -07002082 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002083 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002084 }
2085
Joe Onorato9c1289c2009-08-17 11:03:03 -04002086 /**
2087 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002088 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07002089 public void bindAppWidget(LauncherAppWidgetInfo item) {
2090 View view = inflateAppWidget(item);
2091 if (view != null) {
2092 mWorkspace.addInScreen(view, item);
2093 mWorkspace.requestLayout();
2094 }
2095 }
2096
2097 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002098 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302099 PendingAppWidgetHostView view =
2100 new PendingAppWidgetHostView(this, item, mIconCache, true);
2101 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002102 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002103 }
2104
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002105 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002106
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002107 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08002108
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002109 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2110 // If the provider is not ready, bind as a pending widget.
2111 appWidgetInfo = null;
2112 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2113 // The widget id is not valid. Try to find the widget based on the provider info.
2114 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
2115 } else {
2116 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
2117 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002118
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002119 // If the provider is ready, but the width is not yet restored, try to restore it.
2120 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
2121 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07002122 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002123 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
2124 + " belongs to component " + item.providerName
2125 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002126 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002127 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002128 }
Sunny Goyalff572272014-07-23 13:58:07 -07002129
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002130 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002131 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07002132 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2133 // Id has not been allocated yet. Allocate a new id.
2134 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
2135 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07002136
Sunny Goyald478c832016-04-01 12:04:16 -07002137 // Also try to bind the widget. If the bind fails, the user will be shown
2138 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08002139 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07002140 pendingInfo.spanX = item.spanX;
2141 pendingInfo.spanY = item.spanY;
2142 pendingInfo.minSpanX = item.minSpanX;
2143 pendingInfo.minSpanY = item.minSpanY;
2144 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07002145
2146 boolean isDirectConfig =
2147 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2148 if (isDirectConfig && item.bindOptions != null) {
2149 Bundle newOptions = item.bindOptions.getExtras();
2150 if (options != null) {
2151 newOptions.putAll(options);
2152 }
2153 options = newOptions;
2154 }
Sunny Goyald478c832016-04-01 12:04:16 -07002155 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2156 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002157
Sunny Goyal86df1382016-08-10 15:03:22 -07002158 // We tried to bind once. If we were not able to bind, we would need to
2159 // go through the permission dialog, which means we cannot skip the config
2160 // activity.
2161 item.bindOptions = null;
2162 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2163
Sunny Goyald478c832016-04-01 12:04:16 -07002164 // Bind succeeded
2165 if (success) {
2166 // If the widget has a configure activity, it is still needs to set it up,
2167 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002168 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002169 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2170 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002171 }
Sunny Goyald478c832016-04-01 12:04:16 -07002172
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002173 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002174 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002175 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002176 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002177 // The widget was marked as UI not ready, but there is no configure activity to
2178 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002179 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002180 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002181 }
Sunny Goyalff572272014-07-23 13:58:07 -07002182 }
2183
Sunny Goyald5462aa2016-11-22 16:52:39 +05302184 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002185 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002186 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002187 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002188 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Tony Wickham6a71a5b2018-08-21 11:40:23 -07002189 getModelWriter().deleteWidgetInfo(item, getAppWidgetHost());
Sunny Goyalb23980c2017-08-17 07:45:25 -07002190 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002191 }
2192
Sunny Goyal233ee962015-08-03 13:05:01 -07002193 item.minSpanX = appWidgetInfo.minSpanX;
2194 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302195 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002196 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302197 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002198 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302199 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002200
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002201 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002202 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002203 }
2204
Sunny Goyalff572272014-07-23 13:58:07 -07002205 /**
2206 * Restores a pending widget.
2207 *
2208 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002209 */
Sunny Goyald478c832016-04-01 12:04:16 -07002210 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002211 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2212 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2213 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002214 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002215 }
2216
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002217 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002218 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002219 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2220 info.pendingItemInfo = null;
2221 }
Sunny Goyalff572272014-07-23 13:58:07 -07002222
Sunny Goyalba47faa2017-10-04 16:50:57 -07002223 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002224 view.reInflate();
Sunny Goyalba47faa2017-10-04 16:50:57 -07002225 }
2226
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002227 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002228 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002229 }
2230
Adam Cohen1462de32012-07-24 22:34:36 -07002231 public void onPageBoundSynchronously(int page) {
Sunny Goyalf4aceab2017-12-06 16:16:31 -08002232 mSynchronouslyBoundPage = page;
Adam Cohen1462de32012-07-24 22:34:36 -07002233 }
2234
Sunny Goyal527c7d32015-08-28 15:19:36 -07002235 @Override
2236 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2237 if (mPendingExecutor != null) {
2238 mPendingExecutor.markCompleted();
2239 }
2240 mPendingExecutor = executor;
Sunny Goyal60180b02018-02-07 12:56:30 -08002241 if (!isInState(ALL_APPS)) {
vadimtd4c90e12019-06-04 13:59:43 -07002242 mAppsView.getAppsStore().enableDeferUpdates(AllAppsStore.DEFER_UPDATES_NEXT_DRAW);
2243 mPendingExecutor.execute(() -> mAppsView.getAppsStore().disableDeferUpdates(
2244 AllAppsStore.DEFER_UPDATES_NEXT_DRAW));
Sunny Goyal60180b02018-02-07 12:56:30 -08002245 }
2246
Sunny Goyal527c7d32015-08-28 15:19:36 -07002247 executor.attachTo(this);
2248 }
2249
2250 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2251 if (mPendingExecutor == executor) {
2252 mPendingExecutor = null;
2253 }
2254 }
2255
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002256 @Override
2257 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
Sunny Goyal6001ea22018-05-10 16:31:00 -07002258 AlphaProperty property = mDragLayer.getAlphaProperty(ALPHA_INDEX_LAUNCHER_LOAD);
2259 if (property.getValue() < 1) {
2260 ObjectAnimator anim = ObjectAnimator.ofFloat(property, MultiValueAlpha.VALUE, 1);
2261 if (executor != null) {
2262 anim.addListener(new AnimatorListenerAdapter() {
2263 @Override
2264 public void onAnimationEnd(Animator animation) {
2265 executor.onLoadAnimationCompleted();
2266 }
2267 });
2268 }
2269 anim.start();
Sunny Goyal29947f02017-12-18 13:49:44 -08002270 } else if (executor != null) {
2271 executor.onLoadAnimationCompleted();
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002272 }
2273 }
2274
Joe Onorato9c1289c2009-08-17 11:03:03 -04002275 /**
2276 * Callback saying that there aren't any more items to bind.
2277 *
2278 * Implementation of the method from LauncherModel.Callbacks.
2279 */
Tonyf80e8932018-12-21 11:58:04 -08002280 public void finishBindingItems(int pageBoundFirst) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002281 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002282 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002283
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002284 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002285
Sunny Goyal2100c782016-08-22 16:00:03 -07002286 if (mPendingActivityResult != null) {
2287 handleActivityResult(mPendingActivityResult.requestCode,
2288 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2289 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002290 }
2291
Sunny Goyala474a9b2017-05-04 16:47:11 -07002292 InstallShortcutReceiver.disableAndFlushInstallQueue(
2293 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002294
Tonyf80e8932018-12-21 11:58:04 -08002295 // When undoing the removal of the last item on a page, return to that page.
Tony Wickham03f27012019-04-25 14:22:54 -07002296 // Since we are just resetting the current page without user interaction,
2297 // override the previous page so we don't log the page switch.
2298 mWorkspace.setCurrentPage(pageBoundFirst, pageBoundFirst /* overridePrevPage */);
Tony Wickham6a71a5b2018-08-21 11:40:23 -07002299
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002300 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002301 }
2302
Winson Chunga2413752012-04-03 14:22:34 -07002303 private boolean canRunNewAppsAnimation() {
2304 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002305 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002306 }
2307
Winson Chungc9168342013-06-26 14:54:55 -07002308 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002309 ValueAnimator bounceAnim = new PropertyListBuilder().alpha(1).scale(1).build(v)
2310 .setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
Winson Chungc9168342013-06-26 14:54:55 -07002311 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002312 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002313 return bounceAnim;
2314 }
2315
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002316 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002317 * Add the icons for all apps.
2318 *
2319 * Implementation of the method from LauncherModel.Callbacks.
2320 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002321 public void bindAllApplications(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002322 mAppsView.getAppsStore().setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002323 }
2324
2325 /**
Zak Cohen658c67a2018-10-19 14:21:05 -07002326 * Copies LauncherModel's map of activities to shortcut counts to Launcher's. This is necessary
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002327 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2328 */
2329 @Override
Zak Cohen658c67a2018-10-19 14:21:05 -07002330 public void bindDeepShortcutMap(HashMap<ComponentKey, Integer> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002331 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002332 }
2333
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002334 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002335 * A package was updated.
2336 *
2337 * Implementation of the method from LauncherModel.Callbacks.
2338 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002339 @Override
2340 public void bindAppsAddedOrUpdated(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002341 mAppsView.getAppsStore().addOrUpdateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002342 }
2343
Sunny Goyal4390ace2014-10-13 11:33:11 -07002344 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002345 public void bindPromiseAppProgressUpdated(PromiseAppInfo app) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002346 mAppsView.getAppsStore().updatePromiseAppProgress(app);
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002347 }
2348
2349 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002350 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002351 mWorkspace.widgetsRestored(widgets);
2352 }
2353
Joe Onorato9c1289c2009-08-17 11:03:03 -04002354 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002355 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002356 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002357 *
2358 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002359 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002360 @Override
Sunny Goyal95899162019-03-27 16:03:06 -07002361 public void bindWorkspaceItemsChanged(ArrayList<WorkspaceItemInfo> updated) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002362 if (!updated.isEmpty()) {
2363 mWorkspace.updateShortcuts(updated);
2364 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002365 }
2366
2367 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002368 * Update the state of a package, typically related to install state.
2369 *
2370 * Implementation of the method from LauncherModel.Callbacks.
2371 */
Sunny Goyale755d462014-07-22 13:48:29 -07002372 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002373 public void bindRestoreItemsChange(HashSet<ItemInfo> updates) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07002374 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07002375 }
2376
2377 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002378 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07002379 * in addition to specific applications to remove, the reason being that
2380 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002381 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07002382 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002383 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07002384 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002385 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002386 mWorkspace.removeItemsByMatcher(matcher);
2387 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002388 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002389
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002390 @Override
2391 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002392 mAppsView.getAppsStore().removeApps(appInfos);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002393 }
Joe Onoratobe386092009-11-17 17:32:16 -08002394
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002395 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002396 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
2397 mPopupDataProvider.setAllWidgets(allWidgets);
Tony Wickham26b17462017-03-20 17:12:24 -07002398 }
2399
Tony Wickham86222d22017-03-29 15:30:43 -07002400 /**
2401 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
2402 * refreshes the widgets and shortcuts associated with the given package/user
2403 */
2404 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07002405 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002406 }
2407
Winson Chung80baf5a2010-08-09 16:03:15 -07002408 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002409 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08002410 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002411 @Override
2412 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
2413 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002414
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002415 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
2416 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07002417 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002418 writer.println(prefix + " Homescreen " + i);
2419
2420 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
2421 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 }
2426 }
2427 }
2428
2429 writer.println(prefix + " Hotseat");
Sunny Goyal876e4622018-11-02 13:50:40 -07002430 ViewGroup layout = mHotseat.getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07002431 for (int j = 0; j < layout.getChildCount(); j++) {
2432 Object tag = layout.getChildAt(j).getTag();
2433 if (tag != null) {
2434 writer.println(prefix + " " + tag.toString());
2435 }
Adam Cohen4caf2982013-08-20 18:54:31 -07002436 }
Sunny Goyala1365452015-10-01 15:46:24 -07002437 }
2438
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002439 writer.println(prefix + "Misc:");
2440 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
2441 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
2442 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
Sunny Goyale43d00d2018-05-14 14:23:18 -07002443 writer.println(" mRotationHelper: " + mRotationHelper);
Sunny Goyalcb2c5052018-10-15 12:30:29 -07002444 // Extra logging for b/116853349
Hyunyoung Songa310a802019-04-25 13:57:16 -07002445 mDragLayer.dump(prefix, writer);
2446 mStateManager.dump(prefix, writer);
Sunny Goyale43d00d2018-05-14 14:23:18 -07002447 dumpMisc(writer);
Sunny Goyal2bba1902018-02-27 12:20:50 -08002448
2449 try {
2450 FileLog.flushAll(writer);
2451 } catch (Exception e) {
2452 // Ignore
2453 }
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002454
2455 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002456
Adam Cohen9211d422014-10-07 18:14:53 -07002457 if (mLauncherCallbacks != null) {
2458 mLauncherCallbacks.dump(prefix, fd, writer, args);
2459 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002460 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002461
Sunny Goyal66b24572016-09-21 15:57:55 -07002462 @Override
Sunny Goyal66b24572016-09-21 15:57:55 -07002463 public void onProvideKeyboardShortcuts(
2464 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
2465
2466 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002467 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002468 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
2469 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
Vadim Tryshev74c261c2018-06-12 13:44:43 -07002470 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.widget_button_text),
2471 KeyEvent.KEYCODE_W, KeyEvent.META_CTRL_ON));
Sunny Goyal66b24572016-09-21 15:57:55 -07002472 }
Vadim Tryshev3455f8d2018-05-11 20:02:20 -07002473 final View currentFocus = getCurrentFocus();
2474 if (currentFocus != null) {
2475 if (new CustomActionsPopup(this, currentFocus).canShow()) {
2476 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
2477 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
2478 }
2479 if (currentFocus.getTag() instanceof ItemInfo
2480 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
2481 shortcutInfos.add(new KeyboardShortcutInfo(
2482 getString(R.string.shortcuts_menu_with_notifications_description),
2483 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
2484 }
Sunny Goyal66b24572016-09-21 15:57:55 -07002485 }
2486 if (!shortcutInfos.isEmpty()) {
2487 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
2488 }
2489
2490 super.onProvideKeyboardShortcuts(data, menu, deviceId);
2491 }
2492
2493 @Override
2494 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
2495 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
2496 switch (keyCode) {
2497 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002498 if (isInState(NORMAL)) {
2499 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07002500 return true;
2501 }
2502 break;
2503 case KeyEvent.KEYCODE_S: {
2504 View focusedView = getCurrentFocus();
2505 if (focusedView instanceof BubbleTextView
2506 && focusedView.getTag() instanceof ItemInfo
2507 && mAccessibilityDelegate.performAction(focusedView,
2508 (ItemInfo) focusedView.getTag(),
2509 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal00ac9202017-11-09 15:24:06 -08002510 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07002511 return true;
2512 }
2513 break;
2514 }
2515 case KeyEvent.KEYCODE_O:
2516 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
2517 return true;
2518 }
2519 break;
Vadim Tryshev74c261c2018-06-12 13:44:43 -07002520 case KeyEvent.KEYCODE_W:
2521 if (isInState(NORMAL)) {
2522 OptionsPopupView.openWidgets(this);
2523 return true;
2524 }
2525 break;
Sunny Goyal66b24572016-09-21 15:57:55 -07002526 }
2527 }
2528 return super.onKeyShortcut(keyCode, event);
2529 }
2530
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002531 @Override
2532 public boolean onKeyUp(int keyCode, KeyEvent event) {
2533 if (keyCode == KeyEvent.KEYCODE_MENU) {
2534 // KEYCODE_MENU is sent by some tests, for example
2535 // LauncherJankTests#testWidgetsContainerFling. Don't just remove its handling.
2536 if (!mDragController.isDragging() && !mWorkspace.isSwitchingState() &&
2537 isInState(NORMAL)) {
2538 // Close any open floating views.
2539 AbstractFloatingView.closeAllOpenViews(this);
2540
2541 // Setting the touch point to (-1, -1) will show the options popup in the center of
2542 // the screen.
Sunny Goyal2fd7a8b2018-03-30 17:10:13 -07002543 OptionsPopupView.showDefaultOptions(this, -1, -1);
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002544 }
2545 return true;
2546 }
2547 return super.onKeyUp(keyCode, event);
2548 }
2549
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002550 public static Launcher getLauncher(Context context) {
Sunny Goyal87b5eb62018-07-03 15:53:39 -07002551 return (Launcher) fromContext(context);
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002552 }
2553
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002554 /**
Sunny Goyalab770a12018-11-14 15:17:26 -08002555 * Just a wrapper around the type cast to allow easier tracking of calls.
2556 */
2557 public static <T extends Launcher> T cast(ActivityContext activityContext) {
2558 return (T) activityContext;
2559 }
2560
2561 /**
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002562 * Callback for listening for onResume
2563 */
2564 public interface OnResumeCallback {
2565
2566 void onLauncherResume();
2567 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002568}