blob: ddaaa5d0828b91fdb37b2aa3601e5aa9dde6c3e1 [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;
vadimt00d42552018-12-11 17:59:36 -080032import static com.android.launcher3.util.RaceConditionTracker.ENTER;
33import static com.android.launcher3.util.RaceConditionTracker.EXIT;
Sunny Goyal326403e2017-10-02 12:45:10 -070034
Gilles Debunnedd6c9922010-10-25 11:23:41 -070035import android.animation.Animator;
Sunny Goyal6001ea22018-05-10 16:31:00 -070036import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070037import android.animation.AnimatorSet;
Sunny Goyal6001ea22018-05-10 16:31:00 -070038import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070039import android.animation.ValueAnimator;
Adam Cohen0b395352014-06-09 22:54:36 +000040import android.annotation.TargetApi;
Tony Wickham005df0b2018-02-13 11:28:12 -080041import android.app.ActivityOptions;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080042import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070043import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080045import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080046import android.content.ComponentCallbacks2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070049import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070050import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070051import android.content.SharedPreferences;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052import android.content.pm.PackageManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080053import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070054import android.database.sqlite.SQLiteDatabase;
Sunny Goyal59d086c2018-05-07 17:31:40 -070055import android.graphics.Point;
Adam Cohen0f668f32014-09-08 19:54:17 +020056import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057import android.os.Bundle;
Hyunyoung Songbd6fba92018-05-16 15:54:31 -070058import android.os.Handler;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070059import android.os.Parcelable;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080060import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070061import android.os.StrictMode;
Michael Jurkaa33411c2012-06-14 16:18:21 -070062import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070064import android.util.Log;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070065import android.util.SparseArray;
Sunny Goyal59d086c2018-05-07 17:31:40 -070066import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070067import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070068import android.view.KeyboardShortcutGroup;
69import android.view.KeyboardShortcutInfo;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080070import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -070071import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070072import android.view.View;
Adam Cohen96d30a12013-07-16 18:13:21 -070073import android.view.ViewGroup;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080074import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070075import android.view.animation.OvershootInterpolator;
Adam Cohen96d30a12013-07-16 18:13:21 -070076import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070077
Tony Wickham03f27012019-04-25 14:22:54 -070078import androidx.annotation.Nullable;
79
Sunny Goyal651077b2014-06-30 14:15:31 -070080import com.android.launcher3.DropTarget.DragObject;
Sunny Goyalae502842016-06-17 08:43:56 -070081import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070082import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070083import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal7ede6112017-12-05 15:11:21 -080084import com.android.launcher3.allapps.DiscoveryBounce;
Sunny Goyalf0b6db72018-08-13 16:10:14 -070085import com.android.launcher3.anim.PropertyListBuilder;
Sunny Goyalffe83f12014-08-14 17:39:34 -070086import com.android.launcher3.compat.AppWidgetManagerCompat;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -070087import com.android.launcher3.compat.LauncherAppsCompatVO;
Tony Wickhame0c33232016-02-08 11:37:04 -080088import com.android.launcher3.config.FeatureFlags;
vadimt00d42552018-12-11 17:59:36 -080089import com.android.launcher3.dot.DotInfo;
Vadim Tryshevfedca432015-08-19 17:55:02 -070090import com.android.launcher3.dragndrop.DragController;
91import com.android.launcher3.dragndrop.DragLayer;
92import com.android.launcher3.dragndrop.DragView;
Jon Mirandaaeb4dd02018-07-11 14:16:23 -070093import com.android.launcher3.folder.Folder;
Sunny Goyal26119432016-02-18 22:09:23 +000094import com.android.launcher3.folder.FolderIcon;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070095import com.android.launcher3.folder.FolderIconPreviewVerifier;
Sunny Goyalf840f102018-09-21 14:41:05 -070096import com.android.launcher3.icons.IconCache;
Sunny Goyal66b24572016-09-21 15:57:55 -070097import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -070098import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -070099import com.android.launcher3.logging.FileLog;
Hyunyoung Songfc007472018-10-25 14:09:50 -0700100import com.android.launcher3.logging.StatsLogUtils;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700101import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700102import com.android.launcher3.logging.UserEventDispatcher.UserEventDelegate;
Sunny Goyal369212a2019-03-26 15:03:57 -0700103import com.android.launcher3.model.AppLaunchTracker;
Sunny Goyala535ae42017-02-27 10:07:13 -0800104import com.android.launcher3.model.ModelWriter;
Sunny Goyala535ae42017-02-27 10:07:13 -0800105import com.android.launcher3.notification.NotificationListener;
Tony Wickham540913e2017-01-23 11:47:51 -0800106import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -0800107import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700108import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyalf8088ee2017-11-10 14:52:00 -0800109import com.android.launcher3.states.InternalStateHandler;
Sunny Goyal623eddd2018-03-02 12:24:41 -0800110import com.android.launcher3.states.RotationHelper;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800111import com.android.launcher3.touch.ItemClickHandler;
Sunny Goyala5ace712017-11-15 17:12:51 -0800112import com.android.launcher3.uioverrides.UiFactory;
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700113import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530114import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
115import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
Sunny Goyalaeb16432017-10-16 11:46:41 -0700116import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyal2100c782016-08-22 16:00:03 -0700117import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700118import com.android.launcher3.util.ComponentKey;
Sunny Goyalefb7e842018-10-04 15:11:00 -0700119import com.android.launcher3.util.IntArray;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700120import com.android.launcher3.util.ItemInfoMatcher;
Sunny Goyal6001ea22018-05-10 16:31:00 -0700121import com.android.launcher3.util.MultiValueAlpha;
122import com.android.launcher3.util.MultiValueAlpha.AlphaProperty;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700123import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800124import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700125import com.android.launcher3.util.PendingRequestArgs;
vadimt00d42552018-12-11 17:59:36 -0800126import com.android.launcher3.util.RaceConditionTracker;
Sunny Goyal8392c822017-06-20 10:03:56 -0700127import com.android.launcher3.util.SystemUiController;
Mario Bertschlera6936942017-05-31 14:48:19 -0700128import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700129import com.android.launcher3.util.Thunk;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700130import com.android.launcher3.util.TraceHelper;
Sunny Goyal326403e2017-10-02 12:45:10 -0700131import com.android.launcher3.util.UiThreadHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700132import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyalab770a12018-11-14 15:17:26 -0800133import com.android.launcher3.views.ActivityContext;
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800134import com.android.launcher3.views.OptionsPopupView;
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400135import com.android.launcher3.views.ScrimView;
Sunny Goyal29947f02017-12-18 13:49:44 -0800136import com.android.launcher3.widget.LauncherAppWidgetHostView;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800137import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700138import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal29947f02017-12-18 13:49:44 -0800139import com.android.launcher3.widget.PendingAppWidgetHostView;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800140import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700141import com.android.launcher3.widget.WidgetHostViewLoader;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700142import com.android.launcher3.widget.WidgetListRowEntry;
143import com.android.launcher3.widget.WidgetsFullSheet;
Sunny Goyal952e63d2017-08-16 04:59:08 -0700144import com.android.launcher3.widget.custom.CustomWidgetParser;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700145
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700146import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700147import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700148import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700149import java.util.Collection;
Zak Cohen658c67a2018-10-19 14:21:05 -0700150import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700151import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700152import java.util.List;
Sunny Goyalaae6fbb2019-01-31 16:05:58 -0800153import java.util.function.Predicate;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700154
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155/**
156 * Default launcher application.
157 */
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700158public class Launcher extends BaseDraggingActivity implements LauncherExterns,
Hyunyoung Song31971a32019-01-27 12:31:12 -0800159 LauncherModel.Callbacks, LauncherProviderChangeListener, UserEventDelegate,
160 InvariantDeviceProfile.OnIDPChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700161 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700162 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163
Winson Chunga2413752012-04-03 14:22:34 -0700164 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700165
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700167 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700168
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700169 private static final int REQUEST_PICK_APPWIDGET = 9;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170
Michael Jurka8b805b12012-04-18 14:23:14 -0700171 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800172 public static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
173 public static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700174
Jon Mirandac476d6e2017-05-04 16:30:01 -0700175 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700176
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700177 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
178
Mathew Inwood876a8462013-06-14 14:12:41 +0100179 /**
180 * IntentStarter uses request codes starting with this. This must be greater than all activity
181 * request codes used internally.
182 */
183 protected static final int REQUEST_LAST = 100;
184
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185 // Type: int
186 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700187 // Type: int
188 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700189 // Type: PendingRequestArgs
190 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
191 // Type: ActivityResultInfo
192 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700193 // Type: SparseArray<Parcelable>
194 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
vadimt00d42552018-12-11 17:59:36 -0800195 public static final String ON_CREATE_EVT = "Launcher.onCreate";
vadimtcb863752018-12-19 17:44:57 -0800196 private static final String ON_START_EVT = "Launcher.onStart";
197 private static final String ON_RESUME_EVT = "Launcher.onResume";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700199 private LauncherStateManager mStateManager;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700200
Adam Cohenad4e15c2013-10-17 16:21:35 -0700201 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800202
Winson Chunga2413752012-04-03 14:22:34 -0700203 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700204 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
205 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
206 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700207
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400208 private static final int APPS_VIEW_ALPHA_CHANNEL_INDEX = 1;
209 private static final int SCRIM_VIEW_ALPHA_CHANNEL_INDEX = 0;
210
Jon Miranda54441f52018-01-24 15:38:25 -0800211 private LauncherAppTransitionManager mAppTransitionManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800212 private Configuration mOldConfig;
Jon Miranda54441f52018-01-24 15:38:25 -0800213
Adam Cohen091440a2015-03-18 14:16:05 -0700214 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700215 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700216 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800217 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700218
Sunny Goyalffe83f12014-08-14 17:39:34 -0700219 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700220 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700221
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700222 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700223
Sunny Goyal316490e2015-06-02 09:38:28 -0700224 @Thunk Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700225
Sunny Goyal47328fd2016-05-25 18:56:41 -0700226 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700227
228 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700229 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700230 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700231
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400232 // Scrim view for the all apps and overview state.
233 @Thunk ScrimView mScrimView;
234
Winson Chung8ae41982017-11-10 11:42:13 -0800235 // UI and state for the overview panel
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800236 private View mOverviewPanel;
Winson Chung8ae41982017-11-10 11:42:13 -0800237
Adam Cohen091440a2015-03-18 14:16:05 -0700238 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400239
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700240 private OnResumeCallback mOnResumeCallback;
241
Sunny Goyal527c7d32015-08-28 15:19:36 -0700242 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700243
Joe Onorato9c1289c2009-08-17 11:03:03 -0400244 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800245 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800246 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700247 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400248
Tony Wickham010d2552017-01-20 08:15:28 -0800249 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700250
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800251 private int mSynchronouslyBoundPage = PagedView.INVALID_PAGE;
Adam Cohen1462de32012-07-24 22:34:36 -0700252
Winson Chung46353de2012-02-16 14:05:10 -0800253 // We only want to get the SharedPreferences once since it does an FS stat each time we get
254 // it from the context.
255 private SharedPreferences mSharedPrefs;
256
Sunny Goyal2100c782016-08-22 16:00:03 -0700257 // Activity result which needs to be processed after workspace has loaded.
258 private ActivityResultInfo mPendingActivityResult;
259 /**
260 * Holds extra information required to handle a result from an external call, like
261 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
262 */
263 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800264
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700265 public ViewGroupFocusHelper mFocusHandler;
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700266
Sunny Goyal623eddd2018-03-02 12:24:41 -0800267 private RotationHelper mRotationHelper;
Sunny Goyal5743f862019-03-28 15:35:32 -0700268 private Runnable mCancelTouchController;
Sunny Goyal7779d622015-06-11 16:18:39 -0700269
Sunny Goyal2db53422019-03-01 16:06:12 -0800270 final Handler mHandler = new Handler();
Sunny Goyala002c6c2019-02-12 16:20:10 -0800271 private final Runnable mHandleDeferredResume = this::handleDeferredResume;
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700272
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400273 private float mCurrentAssistantVisibility = 0f;
274
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800275 @Override
276 protected void onCreate(Bundle savedInstanceState) {
vadimt00d42552018-12-11 17:59:36 -0800277 RaceConditionTracker.onEvent(ON_CREATE_EVT, ENTER);
Winson Chunga2413752012-04-03 14:22:34 -0700278 if (DEBUG_STRICT_MODE) {
279 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
280 .detectDiskReads()
281 .detectDiskWrites()
282 .detectNetwork() // or .detectAll() for all detectable problems
283 .penaltyLog()
284 .build());
285 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
286 .detectLeakedSqlLiteObjects()
287 .detectLeakedClosableObjects()
288 .penaltyLog()
289 .penaltyDeath()
290 .build());
291 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700292 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700293
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800294 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700295 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400296
Sunny Goyal87f784c2017-01-11 10:48:34 -0800297 LauncherAppState app = LauncherAppState.getInstance(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800298 mOldConfig = new Configuration(getResources().getConfiguration());
Sunny Goyal0e7724c2018-02-22 13:22:21 -0800299 mModel = app.setLauncher(this);
Hyunyoung Song31971a32019-01-27 12:31:12 -0800300 InvariantDeviceProfile idp = app.getInvariantDeviceProfile();
301 initDeviceProfile(idp);
302 idp.addOnChangeListener(this);
Sunny Goyalf7258242015-10-19 16:59:07 -0700303 mSharedPrefs = Utilities.getPrefs(this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800304 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700305 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700306
Joe Onorato41a12d22009-10-31 18:30:00 -0400307 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700308 mAllAppsController = new AllAppsTransitionController(this);
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800309 mStateManager = new LauncherStateManager(this);
Tracy Zhou8b23c6f2018-04-30 17:13:27 -0700310 UiFactory.onCreate(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700311
Sunny Goyalffe83f12014-08-14 17:39:34 -0700312 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700313
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700314 mAppWidgetHost = new LauncherAppWidgetHost(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700315 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700316
Sunny Goyal60820d72017-05-09 12:40:11 -0700317 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700318
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800319 setupViews();
Tony Wickham010d2552017-01-20 08:15:28 -0800320 mPopupDataProvider = new PopupDataProvider(this);
321
Sunny Goyal623eddd2018-03-02 12:24:41 -0800322 mRotationHelper = new RotationHelper(this);
Sunny Goyal90548432018-04-04 17:17:00 -0700323 mAppTransitionManager = LauncherAppTransitionManager.newInstance(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800324
Sunny Goyald3864fa2017-11-14 15:06:36 -0800325 boolean internalStateHandled = InternalStateHandler.handleCreate(this, getIntent());
326 if (internalStateHandled) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800327 if (savedInstanceState != null) {
328 // InternalStateHandler has already set the appropriate state.
329 // We dont need to do anything.
330 savedInstanceState.remove(RUNTIME_STATE);
331 }
332 }
333 restoreState(savedInstanceState);
Sunny Goyalbbece862019-03-01 13:13:52 -0800334 mStateManager.reapplyState();
Winson Chungb63b44c2017-11-10 17:54:44 -0800335
Sunny Goyal2100c782016-08-22 16:00:03 -0700336 // We only load the page synchronously if the user rotates (or triggers a
337 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700338 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
339 if (savedInstanceState != null) {
340 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
341 }
Sunny Goyald3864fa2017-11-14 15:06:36 -0800342
Sunny Goyalfe770c92016-09-27 14:38:58 -0700343 if (!mModel.startLoader(currentScreen)) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800344 if (!internalStateHandled) {
345 // If we are not binding synchronously, show a fade in animation when
346 // the first page bind completes.
Sunny Goyal6001ea22018-05-10 16:31:00 -0700347 mDragLayer.getAlphaProperty(ALPHA_INDEX_LAUNCHER_LOAD).setValue(0);
Sunny Goyald3864fa2017-11-14 15:06:36 -0800348 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700349 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700350 // Pages bound synchronously.
351 mWorkspace.setCurrentPage(currentScreen);
352
Sunny Goyal2100c782016-08-22 16:00:03 -0700353 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800354 }
355
356 // For handling default keys
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800357 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800358
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800359 setContentView(mLauncherView);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800360 getRootView().dispatchInsets();
Mario Bertschlera6936942017-05-31 14:48:19 -0700361
Jon Miranda7fda2852017-07-19 16:07:01 -0700362 // Listen for broadcasts
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700363 registerReceiver(mScreenOffReceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));
Sunny Goyala616d2b2017-06-12 13:54:01 -0700364
Sunny Goyal8392c822017-06-20 10:03:56 -0700365 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
366 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700367
368 if (mLauncherCallbacks != null) {
369 mLauncherCallbacks.onCreate(savedInstanceState);
370 }
Sunny Goyal623eddd2018-03-02 12:24:41 -0800371 mRotationHelper.initialize();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700372
373 TraceHelper.endSection("Launcher-onCreate");
vadimt00d42552018-12-11 17:59:36 -0800374 RaceConditionTracker.onEvent(ON_CREATE_EVT, EXIT);
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400375 mStateManager.addStateListener(new LauncherStateManager.StateListener() {
376 @Override
377 public void onStateTransitionStart(LauncherState toState) {}
378
379 @Override
380 public void onStateTransitionComplete(LauncherState finalState) {
381 float alpha = 1f - mCurrentAssistantVisibility;
382 if (finalState == NORMAL) {
383 mAppsView.getAlphaProperty(APPS_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
384 } else if (finalState == OVERVIEW || finalState == OVERVIEW_PEEK) {
385 mAppsView.getAlphaProperty(APPS_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
386 mScrimView.getAlphaProperty(SCRIM_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
387 } else {
388 mAppsView.getAlphaProperty(APPS_VIEW_ALPHA_CHANNEL_INDEX).setValue(1f);
389 mScrimView.getAlphaProperty(SCRIM_VIEW_ALPHA_CHANNEL_INDEX).setValue(1f);
390 }
391 }
392 });
Adam Cohen9211d422014-10-07 18:14:53 -0700393 }
394
Sunny Goyal7779d622015-06-11 16:18:39 -0700395 @Override
Winson Chung8eb49e02018-07-16 13:33:31 -0700396 public void onEnterAnimationComplete() {
397 super.onEnterAnimationComplete();
398 UiFactory.onEnterAnimationComplete(this);
arangelovad783052018-08-17 14:46:45 +0100399 mAllAppsController.highlightWorkTabIfNecessary();
Winson Chung8eb49e02018-07-16 13:33:31 -0700400 }
401
402 @Override
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800403 public void onConfigurationChanged(Configuration newConfig) {
404 int diff = newConfig.diff(mOldConfig);
Jon Mirandaaeb4dd02018-07-11 14:16:23 -0700405
406 if ((diff & CONFIG_LOCALE) != 0) {
407 Folder.setLocaleDependentFields(getResources(), true /* force */);
408 }
409
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800410 if ((diff & (CONFIG_ORIENTATION | CONFIG_SCREEN_SIZE)) != 0) {
Sunny Goyal371ea052019-03-06 15:38:32 -0800411 onIdpChanged(mDeviceProfile.inv);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800412 }
413
414 mOldConfig.setTo(newConfig);
Tracy Zhoua706f002018-03-28 13:55:19 -0700415 UiFactory.onLauncherStateOrResumeChanged(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800416 super.onConfigurationChanged(newConfig);
417 }
418
Sunny Goyal605bcf32018-03-02 15:16:12 -0800419 @Override
Sunny Goyal59d086c2018-05-07 17:31:40 -0700420 protected void reapplyUi() {
421 getRootView().dispatchInsets();
422 getStateManager().reapplyState(true /* cancelCurrentAnimation */);
423 }
424
425 @Override
Sunny Goyal605bcf32018-03-02 15:16:12 -0800426 public void rebindModel() {
427 int currentPage = mWorkspace.getNextPage();
428 if (mModel.startLoader(currentPage)) {
429 mWorkspace.setCurrentPage(currentPage);
430 setWorkspaceLoading(true);
431 }
432 }
433
Hyunyoung Song31971a32019-01-27 12:31:12 -0800434 @Override
435 public void onIdpChanged(int changeFlags, InvariantDeviceProfile idp) {
Sunny Goyal371ea052019-03-06 15:38:32 -0800436 onIdpChanged(idp);
437 }
438
439 private void onIdpChanged(InvariantDeviceProfile idp) {
440 mUserEventDispatcher = null;
441
Hyunyoung Song31971a32019-01-27 12:31:12 -0800442 initDeviceProfile(idp);
Sunny Goyal371ea052019-03-06 15:38:32 -0800443 dispatchDeviceProfileChanged();
444 reapplyUi();
445 mDragLayer.recreateControllers();
446
447 // TODO: We can probably avoid rebind when only screen size changed.
448 rebindModel();
Hyunyoung Song31971a32019-01-27 12:31:12 -0800449 }
450
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400451 public void onAssistantVisibilityChanged(float visibility) {
452 mCurrentAssistantVisibility = visibility;
453 float alpha = 1f - visibility;
454 LauncherState state = mStateManager.getState();
455 if (state == NORMAL) {
456 mAppsView.getAlphaProperty(APPS_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
457 } else if (state == OVERVIEW || state == OVERVIEW_PEEK) {
458 mAppsView.getAlphaProperty(APPS_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
459 mScrimView.getAlphaProperty(SCRIM_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
460 }
461 }
462
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800463 private void initDeviceProfile(InvariantDeviceProfile idp) {
464 // Load configuration-specific DeviceProfile
Sunny Goyal59d086c2018-05-07 17:31:40 -0700465 mDeviceProfile = idp.getDeviceProfile(this);
Sunny Goyal8c48d8b2019-01-25 15:10:18 -0800466 if (isInMultiWindowMode()) {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700467 Display display = getWindowManager().getDefaultDisplay();
468 Point mwSize = new Point();
469 display.getSize(mwSize);
470 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
471 }
472 onDeviceProfileInitiated();
Sunny Goyal605bcf32018-03-02 15:16:12 -0800473 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout(), true);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800474 }
475
Sunny Goyal623eddd2018-03-02 12:24:41 -0800476 public RotationHelper getRotationHelper() {
477 return mRotationHelper;
478 }
479
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700480 public LauncherStateManager getStateManager() {
481 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700482 }
483
Mario Bertschler27288382017-05-24 15:35:09 -0700484 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700485 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800486 return mLauncherView.findViewById(id);
487 }
488
489 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700490 public void onAppWidgetHostReset() {
491 if (mAppWidgetHost != null) {
492 mAppWidgetHost.startListening();
493 }
494 }
495
Adam Cohen9211d422014-10-07 18:14:53 -0700496 private LauncherCallbacks mLauncherCallbacks;
497
Sunny Goyal32554d12015-12-03 15:31:25 -0800498 /**
499 * Call this after onCreate to set or clear overlay.
500 */
501 public void setLauncherOverlay(LauncherOverlay overlay) {
502 if (overlay != null) {
503 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
504 }
505 mWorkspace.setLauncherOverlay(overlay);
506 }
507
Adam Cohen9211d422014-10-07 18:14:53 -0700508 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
509 mLauncherCallbacks = callbacks;
510 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700511 }
512
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700513 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700514 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700515 if (mLauncherCallbacks != null) {
516 mLauncherCallbacks.onLauncherProviderChange();
517 }
518 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700519
Hyunyoung Song3f471442015-04-08 19:01:34 -0700520 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700521 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
522 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700523 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700524 }
525
Tony Wickham010d2552017-01-20 08:15:28 -0800526 public PopupDataProvider getPopupDataProvider() {
527 return mPopupDataProvider;
528 }
529
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700530 @Override
Tony Wickhamf34bee82018-12-03 18:11:39 -0800531 public DotInfo getDotInfoForItem(ItemInfo info) {
532 return mPopupDataProvider.getDotInfoForItem(info);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700533 }
534
535 @Override
536 public void invalidateParent(ItemInfo info) {
Jon Mirandae6f3fa42019-03-04 09:14:40 -0800537 if (info.container >= 0) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700538 View folderIcon = getWorkspace().getHomescreenIconByItemId(info.container);
Jon Mirandae6f3fa42019-03-04 09:14:40 -0800539 if (folderIcon instanceof FolderIcon && folderIcon.getTag() instanceof FolderInfo) {
540 FolderIconPreviewVerifier verifier =
541 new FolderIconPreviewVerifier(getDeviceProfile().inv);
542 verifier.setFolderInfo((FolderInfo) folderIcon.getTag());
543 if (verifier.isItemInPreview(info.rank)) {
544 folderIcon.invalidate();
545 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700546 }
547 }
548 }
549
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000550 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700551 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
552 * a configuration step, this allows the proper animations to run after other transitions.
553 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700554 private int completeAdd(
Sunny Goyal2100c782016-08-22 16:00:03 -0700555 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
Sunny Goyalefb7e842018-10-04 15:11:00 -0700556 int screenId = info.screenId;
Sunny Goyal2100c782016-08-22 16:00:03 -0700557 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700558 // When the screen id represents an actual screen (as opposed to a rank) we make sure
559 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700560 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700561 }
Adam Cohendb364c32014-05-20 14:23:40 -0700562
Sunny Goyal2100c782016-08-22 16:00:03 -0700563 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800564 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700565 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700566 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800567 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700568 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700569 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700570 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700571 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700572 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700573 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700574 int widgetId = appWidgetId;
575 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700576 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700577 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700578 // Since the view was just bound, also launch the configure activity if needed
579 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
580 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800581 if (provider != null) {
582 new WidgetAddFlowHandler(provider)
583 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700584 }
585 }
586 break;
587 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800588 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700589
Adam Cohendb364c32014-05-20 14:23:40 -0700590 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800591 }
592
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800593 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700594 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700595 if (isWorkspaceLoading()) {
596 // process the result once the workspace has loaded.
597 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
598 return;
599 }
600 mPendingActivityResult = null;
601
Winson Chunge341d302013-08-16 14:31:00 -0700602 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700603 final PendingRequestArgs requestArgs = mPendingRequestArgs;
604 setWaitingForResult(null);
605 if (requestArgs == null) {
606 return;
607 }
608
609 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700610
Adam Cohenad4e15c2013-10-17 16:21:35 -0700611 Runnable exitSpringLoaded = new Runnable() {
612 @Override
613 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700614 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700615 }
616 };
617
Michael Jurka8b805b12012-04-18 14:23:14 -0700618 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700619 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700620 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700621 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
622 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700623 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700624 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700625 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700626 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700627 addAppWidgetImpl(
628 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800629 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700630 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700631 }
632 return;
633 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200634
Adam Cohened66b2b2012-01-23 17:28:51 -0800635 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700636 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700637
Adam Cohened66b2b2012-01-23 17:28:51 -0800638 // We have special handling for widgets
639 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800640 final int appWidgetId;
641 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
642 : -1;
643 if (widgetId < 0) {
644 appWidgetId = pendingAddWidgetId;
645 } else {
646 appWidgetId = widgetId;
647 }
648
Adam Cohenad4e15c2013-10-17 16:21:35 -0700649 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800650 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700651 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
652 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700653 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700654 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700655 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700656 @Override
657 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700658 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700659 }
660 };
Adam Cohendb364c32014-05-20 14:23:40 -0700661
Sunny Goyal2100c782016-08-22 16:00:03 -0700662 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
663 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
664 } else {
665 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
666 // When the screen id represents an actual screen (as opposed to a rank)
667 // we make sure that the drop page actually exists.
668 requestArgs.screenId =
669 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700670 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700671 final CellLayout dropLayout =
672 mWorkspace.getScreenWithId(requestArgs.screenId);
673
674 dropLayout.setDropPending(true);
675 final Runnable onComplete = new Runnable() {
676 @Override
677 public void run() {
678 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
679 dropLayout.setDropPending(false);
680 }
681 };
682 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
683 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700684 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800685 return;
686 }
687
Sunny Goyald478c832016-04-01 12:04:16 -0700688 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
689 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700690 if (resultCode == RESULT_OK) {
691 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700692 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700693 }
694 // Leave the widget in the pending state if the user canceled the configure.
695 return;
696 }
697
Sunny Goyalaad90582015-07-09 14:22:50 -0700698 if (requestCode == REQUEST_CREATE_SHORTCUT) {
699 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700700 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
701 completeAdd(requestCode, data, -1, requestArgs);
702 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
703 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
704
Sunny Goyalaad90582015-07-09 14:22:50 -0700705 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700706 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
707 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800708 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800709 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800710 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800711 }
712
713 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700714 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800715 final int requestCode, final int resultCode, final Intent data) {
716 handleActivityResult(requestCode, resultCode, data);
717 if (mLauncherCallbacks != null) {
718 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
719 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800720 }
721
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700722 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700723 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600724 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700725 PendingRequestArgs pendingArgs = mPendingRequestArgs;
726 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
727 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
728 setWaitingForResult(null);
729
Sunny Goyal28c6b962015-10-12 11:42:05 -0700730 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700731 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700732 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700733 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700734 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700735 Intent intent = pendingArgs.getPendingIntent();
736
Sunny Goyal28c6b962015-10-12 11:42:05 -0700737 if (grantResults.length > 0
738 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal369212a2019-03-26 15:03:57 -0700739 startActivitySafely(v, intent, null, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700740 } else {
741 // TODO: Show a snack bar with link to settings
742 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700743 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700744 }
745 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600746 if (mLauncherCallbacks != null) {
747 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
748 grantResults);
749 }
750 }
751
Adam Cohendb364c32014-05-20 14:23:40 -0700752 /**
753 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
754 *
755 * @param screenId the screen id to check
756 * @return the new screen, or screenId if it exists
757 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700758 private int ensurePendingDropLayoutExists(int screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800759 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700760 if (dropLayout == null) {
761 // it's possible that the add screen was removed because it was
762 // empty and a re-bind occurred
763 mWorkspace.addExtraEmptyScreen();
764 return mWorkspace.commitExtraEmptyScreen();
765 } else {
766 return screenId;
767 }
768 }
769
Sunny Goyal2100c782016-08-22 16:00:03 -0700770 @Thunk void completeTwoStageWidgetDrop(
771 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
772 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800773 Runnable onCompleteRunnable = null;
774 int animationType = 0;
775
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700776 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800777 if (resultCode == RESULT_OK) {
778 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
779 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800780 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700781 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800782 onCompleteRunnable = new Runnable() {
783 @Override
784 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700785 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700786 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800787 }
788 };
789 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800790 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800791 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800792 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700793 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700794 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700795 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
796 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700797 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700798 // The animated view may be null in the case of a rotation during widget configuration
799 onCompleteRunnable.run();
800 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800801 }
802
803 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700804 protected void onStop() {
805 super.onStop();
Adam Cohen9211d422014-10-07 18:14:53 -0700806 if (mLauncherCallbacks != null) {
807 mLauncherCallbacks.onStop();
808 }
Miranda Kephart1a359a22019-02-11 13:07:04 -0500809
Hyunyoung Song1241e612018-05-15 21:46:51 -0700810 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
811 mStateManager.getState().containerType, -1);
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700812
813 mAppWidgetHost.setListenIfResumed(false);
814
Tony Wickham010d2552017-01-20 08:15:28 -0800815 NotificationListener.removeNotificationsChangedListener();
Sunny Goyalaf161fd2018-02-05 16:34:57 -0800816 getStateManager().moveToRestState();
Winson Chungedb0d6b2018-03-21 17:32:55 -0700817
Tracy Zhou62646712018-06-26 14:19:02 -0700818 UiFactory.onLauncherStateOrResumeChanged(this);
819
Winson Chungdea51352018-04-24 13:02:10 -0700820 // Workaround for b/78520668, explicitly trim memory once UI is hidden
Winson Chung473730f2018-04-26 11:54:22 -0700821 onTrimMemory(TRIM_MEMORY_UI_HIDDEN);
Michael Jurkacd496d72013-04-11 11:32:45 -0700822 }
823
824 @Override
825 protected void onStart() {
vadimtcb863752018-12-19 17:44:57 -0800826 RaceConditionTracker.onEvent(ON_START_EVT, ENTER);
Michael Jurkacd496d72013-04-11 11:32:45 -0700827 super.onStart();
Adam Cohen9211d422014-10-07 18:14:53 -0700828 if (mLauncherCallbacks != null) {
829 mLauncherCallbacks.onStart();
830 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800831 mAppWidgetHost.setListenIfResumed(true);
Sunny Goyalcac002d2018-01-04 09:51:45 -0800832 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
vadimtcb863752018-12-19 17:44:57 -0800833 RaceConditionTracker.onEvent(ON_START_EVT, EXIT);
Michael Jurkacd496d72013-04-11 11:32:45 -0700834 }
835
Sunny Goyala002c6c2019-02-12 16:20:10 -0800836 private void handleDeferredResume() {
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700837 if (hasBeenResumed()) {
838 getUserEventDispatcher().logActionCommand(Action.Command.RESUME,
839 mStateManager.getState().containerType, -1);
840 getUserEventDispatcher().startSession();
Sunny Goyala002c6c2019-02-12 16:20:10 -0800841
842 UiFactory.onLauncherStateOrResumeChanged(this);
Sunny Goyal369212a2019-03-26 15:03:57 -0700843 AppLaunchTracker.INSTANCE.get(this).onReturnedToHome();
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700844 }
845 }
846
Michael Jurkacd496d72013-04-11 11:32:45 -0700847 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800848 protected void onResume() {
vadimtcb863752018-12-19 17:44:57 -0800849 RaceConditionTracker.onEvent(ON_RESUME_EVT, ENTER);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700850 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800851 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700852 TraceHelper.partitionSection("ON_RESUME", "superCall");
853
Sunny Goyala002c6c2019-02-12 16:20:10 -0800854 mHandler.removeCallbacks(mHandleDeferredResume);
855 Utilities.postAsyncCallback(mHandler, mHandleDeferredResume);
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700856
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700857 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700858 // Process any items that were added while Launcher was away.
859 InstallShortcutReceiver.disableAndFlushInstallQueue(
860 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700861
Sunny Goyala474a9b2017-05-04 16:47:11 -0700862 // Refresh shortcuts if the permission changed.
863 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700864
Tracy Zhoue5575f92018-04-19 11:12:53 -0700865 DiscoveryBounce.showForHomeIfNeeded(this);
Adam Cohen9211d422014-10-07 18:14:53 -0700866 if (mLauncherCallbacks != null) {
867 mLauncherCallbacks.onResume();
868 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800869
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700870 TraceHelper.endSection("ON_RESUME");
vadimtcb863752018-12-19 17:44:57 -0800871 RaceConditionTracker.onEvent(ON_RESUME_EVT, EXIT);
Adam Cohen06dff352012-06-01 17:17:08 -0700872 }
873
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800874 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700875 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700876 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700877 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700878
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700879 super.onPause();
Joe Onorato24b6fd82009-11-12 13:47:09 -0800880 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700881 mDragController.resetLastGestureUpTime();
Hyunyoung Song497708c2019-05-01 16:16:53 -0700882 mDropTargetBar.animateToVisibility(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700883 if (mLauncherCallbacks != null) {
884 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700885 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700886 }
887
Winson Chungd7823942018-02-16 03:23:47 +0000888 @Override
Tracy Zhoua706f002018-03-28 13:55:19 -0700889 protected void onUserLeaveHint() {
890 super.onUserLeaveHint();
891 UiFactory.onLauncherStateOrResumeChanged(this);
892 }
893
894 @Override
Winson Chungd7823942018-02-16 03:23:47 +0000895 public void onWindowFocusChanged(boolean hasFocus) {
896 super.onWindowFocusChanged(hasFocus);
897 mStateManager.onWindowFocusChanged();
898 }
899
Adam Cohenc2d6e892014-10-16 09:49:24 -0700900 public interface LauncherOverlay {
901
902 /**
903 * Touch interaction leading to overscroll has begun
904 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700905 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700906
907 /**
908 * Touch interaction related to overscroll has ended
909 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700910 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700911
912 /**
913 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
914 * screen (or in the case of RTL, the rightmost screen).
915 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700916 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700917
918 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800919 * Called when the launcher is ready to use the overlay
920 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700921 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700922 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700923 }
924
925 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700926 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700927 }
928
929 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
930
Sunny Goyalc86df472016-02-25 09:19:38 -0800931 public void onScrollChanged(float progress) {
932 if (mWorkspace != null) {
933 mWorkspace.onOverlayScrollChanged(progress);
934 }
935 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700936 }
937
Sunny Goyalf9403d92017-10-18 10:55:56 -0700938 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -0700939 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700940 }
941
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800942 /**
943 * Restores the previous state, if it exists.
944 *
945 * @param savedState The previous state.
946 */
947 private void restoreState(Bundle savedState) {
948 if (savedState == null) {
949 return;
950 }
951
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700952 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -0700953 LauncherState[] stateValues = LauncherState.values();
954 LauncherState state = stateValues[stateOrdinal];
Sunny Goyalbc683e92017-12-06 10:25:07 -0800955 if (!state.disableRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700956 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800957 }
958
Sunny Goyal2100c782016-08-22 16:00:03 -0700959 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
960 if (requestArgs != null) {
961 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800962 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700963
964 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700965
966 SparseArray<Parcelable> widgetsState =
967 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
968 if (widgetsState != null) {
969 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
970 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800971 }
972
973 /**
974 * Finds all the views we need and configure them properly.
975 */
976 private void setupViews() {
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800977 mDragLayer = findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700978 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -0700979 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700980 mWorkspace.initParentViews(mDragLayer);
Sunny Goyal16764582017-11-06 16:00:34 -0800981 mOverviewPanel = findViewById(R.id.overview_panel);
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800982 mHotseat = findViewById(R.id.hotseat);
Craig Mautner360310b2012-10-26 15:13:08 -0700983
Sunny Goyal784f9c32016-03-23 16:56:54 -0700984 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
985 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
986 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800987
Winson Chung4c98d922011-05-31 16:50:48 -0700988 // Setup the drag layer
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700989 mDragLayer.setup(mDragController, mWorkspace);
Sunny Goyal5743f862019-03-28 15:35:32 -0700990 mCancelTouchController = UiFactory.enableLiveTouchControllerChanges(mDragLayer);
Winson Chung4c98d922011-05-31 16:50:48 -0700991
Hyunyoung Song645764e2016-06-06 14:19:02 -0700992 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -0700993 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
994 // default state, otherwise we will update to the wrong offsets in RTL
995 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700996 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700997 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700998
Tony Wickham34d2c912015-09-11 09:27:58 -0700999 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001000 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001001
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001002 // Setup Apps
1003 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001004
Govinda Wassermanc06e1512019-04-09 09:56:53 -04001005 // Setup Scrim
1006 mScrimView = findViewById(R.id.scrim_view);
1007
Winson Chung3d503fb2011-07-13 17:25:49 -07001008 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001009 mDragController.setMoveTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001010 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001011
Sunny Goyal7185dd62018-03-14 17:51:49 -07001012 mAllAppsController.setupViews(mAppsView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001013 }
1014
1015 /**
1016 * Creates a view representing a shortcut.
1017 *
1018 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001019 */
Sunny Goyal95899162019-03-27 16:03:06 -07001020 View createShortcut(WorkspaceItemInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001021 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001022 }
1023
1024 /**
1025 * Creates a view representing a shortcut inflated from the specified resource.
1026 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001027 * @param parent The group the shortcut belongs to.
1028 * @param info The data structure describing the shortcut.
1029 *
1030 * @return A View inflated from layoutResId.
1031 */
Sunny Goyal95899162019-03-27 16:03:06 -07001032 public View createShortcut(ViewGroup parent, WorkspaceItemInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -08001033 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
1034 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal95899162019-03-27 16:03:06 -07001035 favorite.applyFromWorkspaceItem(info);
Sunny Goyal8a2a63b2018-03-06 22:15:18 -08001036 favorite.setOnClickListener(ItemClickHandler.INSTANCE);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001037 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001038 return favorite;
1039 }
1040
1041 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -07001042 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001043 *
1044 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001045 */
Sunny Goyalefb7e842018-10-04 15:11:00 -07001046 private void completeAddShortcut(Intent data, int container, int screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001047 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001048 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
1049 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001050 return;
1051 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001052
Jon Mirandac476d6e2017-05-04 16:30:01 -07001053 int[] cellXY = mTmpAddItemCellCoordinates;
1054 CellLayout layout = getCellLayout(container, screenId);
1055
Sunny Goyal95899162019-03-27 16:03:06 -07001056 WorkspaceItemInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001057 if (Utilities.ATLEAST_OREO) {
Sunny Goyal95899162019-03-27 16:03:06 -07001058 info = LauncherAppsCompatVO.createWorkspaceItemFromPinItemRequest(
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001059 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001060 }
1061
1062 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001063 // Legacy shortcuts are only supported for primary profile.
1064 info = Process.myUserHandle().equals(args.user)
1065 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001066
Sunny Goyalb57645f2017-03-20 13:57:28 -07001067 if (info == null) {
1068 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1069 return;
1070 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001071 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001072 // The app is trying to add a shortcut without sufficient permissions
1073 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1074 return;
1075 }
1076 }
1077
Jon Mirandac476d6e2017-05-04 16:30:01 -07001078 if (container < 0) {
1079 // Adding a shortcut to the Workspace.
1080 final View view = createShortcut(info);
1081 boolean foundCellSpan = false;
1082 // First we check if we already know the exact location where we want to add this item.
1083 if (cellX >= 0 && cellY >= 0) {
1084 cellXY[0] = cellX;
1085 cellXY[1] = cellY;
1086 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001087
Jon Mirandac476d6e2017-05-04 16:30:01 -07001088 // If appropriate, either create a folder or add to an existing folder
1089 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001090 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001091 return;
1092 }
1093 DragObject dragObject = new DragObject();
1094 dragObject.dragInfo = info;
1095 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1096 true)) {
1097 return;
1098 }
1099 } else {
1100 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1101 }
1102
1103 if (!foundCellSpan) {
1104 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001105 return;
1106 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001107
1108 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1109 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001110 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001111 // Adding a shortcut to a Folder.
Sunny Goyale29897f52017-07-20 10:09:42 -07001112 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001113 if (folderIcon != null) {
1114 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1115 folderInfo.add(info, args.rank, false);
1116 } else {
Sunny Goyale29897f52017-07-20 10:09:42 -07001117 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001118 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001119 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001120 }
1121
Sunny Goyalefb7e842018-10-04 15:11:00 -07001122 public FolderIcon findFolderIcon(final int folderIconId) {
Sunny Goyal83fd25e2018-07-09 16:47:01 -07001123 return (FolderIcon) mWorkspace.getHomescreenIconByItemId(folderIconId);
Sunny Goyale29897f52017-07-20 10:09:42 -07001124 }
1125
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001126 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001127 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001128 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001129 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001130 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001131 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001132 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1133
Adam Cohened66b2b2012-01-23 17:28:51 -08001134 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001135 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001136 }
Romain Guycbb89e42009-06-08 15:52:54 -07001137
Adam Cohen59400422014-03-05 18:07:04 -08001138 LauncherAppWidgetInfo launcherInfo;
1139 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001140 launcherInfo.spanX = itemInfo.spanX;
1141 launcherInfo.spanY = itemInfo.spanY;
1142 launcherInfo.minSpanX = itemInfo.minSpanX;
1143 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001144 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001145
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001146 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001147 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001148
Sunny Goyal2100c782016-08-22 16:00:03 -07001149 if (hostView == null) {
1150 // Perform actual inflation because we're live
1151 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001152 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001153 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301154 prepareAppWidget(hostView, launcherInfo);
1155 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001156 }
Romain Guycbb89e42009-06-08 15:52:54 -07001157
Sunny Goyald5462aa2016-11-22 16:52:39 +05301158 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001159 hostView.setTag(item);
1160 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001161 hostView.setFocusable(true);
1162 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001163 }
1164
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001165 private final BroadcastReceiver mScreenOffReceiver = new BroadcastReceiver() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001166 @Override
1167 public void onReceive(Context context, Intent intent) {
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001168 // Reset AllApps to its initial state only if we are not in the middle of
1169 // processing a multi-step drop
1170 if (mPendingRequestArgs == null) {
1171 mStateManager.goToState(NORMAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001172 }
1173 }
1174 };
1175
Sunny Goyalaae6fbb2019-01-31 16:05:58 -08001176 public void updateNotificationDots(Predicate<PackageUserKey> updatedDots) {
Tony Wickhamf34bee82018-12-03 18:11:39 -08001177 mWorkspace.updateNotificationDots(updatedDots);
1178 mAppsView.getAppsStore().updateNotificationDots(updatedDots);
Tony Wickham010d2552017-01-20 08:15:28 -08001179 }
1180
Adam Cohended9f8d2010-11-03 13:25:16 -07001181 @Override
1182 public void onAttachedToWindow() {
1183 super.onAttachedToWindow();
1184
Sunny Goyalc86df472016-02-25 09:19:38 -08001185 if (mLauncherCallbacks != null) {
1186 mLauncherCallbacks.onAttachedToWindow();
1187 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001188 }
1189
1190 @Override
1191 public void onDetachedFromWindow() {
1192 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001193
1194 if (mLauncherCallbacks != null) {
1195 mLauncherCallbacks.onDetachedFromWindow();
1196 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001197 }
1198
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001199 public AllAppsTransitionController getAllAppsController() {
1200 return mAllAppsController;
1201 }
1202
Sunny Goyal9d69c8d2018-03-19 13:41:31 -07001203 @Override
Winson Chung1a341002018-01-17 10:00:23 -08001204 public LauncherRootView getRootView() {
1205 return (LauncherRootView) mLauncherView;
1206 }
1207
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001208 @Override
Winson Chunga6945242014-01-08 14:04:34 -08001209 public DragLayer getDragLayer() {
1210 return mDragLayer;
1211 }
1212
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001213 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001214 return mAppsView;
1215 }
1216
Winson Chunga6945242014-01-08 14:04:34 -08001217 public Workspace getWorkspace() {
1218 return mWorkspace;
1219 }
1220
1221 public Hotseat getHotseat() {
1222 return mHotseat;
1223 }
1224
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001225 public <T extends View> T getOverviewPanel() {
Winson Chung8ae41982017-11-10 11:42:13 -08001226 return (T) mOverviewPanel;
Winson Chunga6945242014-01-08 14:04:34 -08001227 }
1228
Sunny Goyal47328fd2016-05-25 18:56:41 -07001229 public DropTargetBar getDropTargetBar() {
1230 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001231 }
1232
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001233 public LauncherAppWidgetHost getAppWidgetHost() {
1234 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001235 }
Romain Guycbb89e42009-06-08 15:52:54 -07001236
Winson Chunga9abd0e2010-10-27 17:18:37 -07001237 public LauncherModel getModel() {
1238 return mModel;
1239 }
1240
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001241 public ModelWriter getModelWriter() {
1242 return mModelWriter;
1243 }
1244
Adam Cohen79d90c52016-04-22 13:29:20 -07001245 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001246 return mSharedPrefs;
1247 }
1248
Sunny Goyalf8d56fc2018-01-31 15:18:11 -08001249 public int getOrientation() { return mOldConfig.orientation; }
Jon Miranda6bed3502017-09-19 14:43:17 -07001250
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001251 @Override
1252 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001253 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001254 super.onNewIntent(intent);
1255
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001256 boolean alreadyOnHome = hasWindowFocus() && ((intent.getFlags() &
Winson15f8b172015-08-19 11:02:39 -07001257 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1258 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001259
1260 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001261 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001262 && AbstractFloatingView.getTopOpenView(this) == null;
Winson15f8b172015-08-19 11:02:39 -07001263 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001264 boolean internalStateHandled = InternalStateHandler
Sunny Goyal8b264562018-02-03 23:00:13 -08001265 .handleNewIntent(this, intent, isStarted());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001266
Winson15f8b172015-08-19 11:02:39 -07001267 if (isActionMain) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001268 if (!internalStateHandled) {
1269 // Note: There should be at most one log per method call. This is enforced
1270 // implicitly by using if-else statements.
1271 UserEventDispatcher ued = getUserEventDispatcher();
1272 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
1273 if (topOpenView != null) {
1274 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
1275 } else if (alreadyOnHome) {
1276 Target target = newContainerTarget(mStateManager.getState().containerType);
1277 target.pageIndex = mWorkspace.getCurrentPage();
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001278 ued.logActionCommand(Action.Command.HOME_INTENT, target,
1279 newContainerTarget(ContainerType.WORKSPACE));
Sunny Goyald3864fa2017-11-14 15:06:36 -08001280 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001281
Sunny Goyald3864fa2017-11-14 15:06:36 -08001282 // In all these cases, only animate if we're already on home
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001283 AbstractFloatingView.closeAllOpenViews(this, isStarted());
Jon Mirandafeba90f2016-10-06 10:53:29 -07001284
Sunny Goyala2467272018-03-16 14:53:05 -07001285 if (!isInState(NORMAL)) {
1286 // Only change state, if not already the same. This prevents cancelling any
1287 // animations running as part of resume
1288 mStateManager.goToState(NORMAL);
1289 }
Sunny Goyald3864fa2017-11-14 15:06:36 -08001290
1291 // Reset the apps view
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001292 if (!alreadyOnHome) {
Sunny Goyal7185dd62018-03-14 17:51:49 -07001293 mAppsView.reset(isStarted() /* animate */);
Sunny Goyald3864fa2017-11-14 15:06:36 -08001294 }
1295
Sunny Goyalaad33592018-08-07 17:20:35 -07001296 if (shouldMoveToDefaultScreen && !mWorkspace.isHandlingTouch()) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001297 mWorkspace.post(mWorkspace::moveToDefaultScreen);
1298 }
1299 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001300
1301 final View v = getWindow().peekDecorView();
1302 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001303 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001304 }
1305
Adam Cohen9211d422014-10-07 18:14:53 -07001306 if (mLauncherCallbacks != null) {
Sunny Goyal85462132018-04-24 11:39:37 -07001307 mLauncherCallbacks.onHomeIntent(internalStateHandled);
Adam Cohen9211d422014-10-07 18:14:53 -07001308 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001309 }
Winson15f8b172015-08-19 11:02:39 -07001310
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001311 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001312 }
1313
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001314 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001315 public void onRestoreInstanceState(Bundle state) {
1316 super.onRestoreInstanceState(state);
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001317 mWorkspace.restoreInstanceStateForChild(mSynchronouslyBoundPage);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001318 }
1319
1320 @Override
1321 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001322 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001323 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001324
Adam Cohen21cd0022013-10-09 18:57:02 -07001325 }
Sunny Goyalea609262017-10-25 15:47:38 -07001326 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001327
1328
1329 AbstractFloatingView widgets = AbstractFloatingView
1330 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1331 if (widgets != null) {
1332 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1333 widgets.saveHierarchyState(widgetsState);
1334 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1335 } else {
1336 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1337 }
1338
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001339 // We close any open folders and shortcut containers since they will not be re-opened,
1340 // and we need to make sure this state is reflected.
1341 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001342
Sunny Goyal2100c782016-08-22 16:00:03 -07001343 if (mPendingRequestArgs != null) {
1344 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1345 }
1346 if (mPendingActivityResult != null) {
1347 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001348 }
1349
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001350 super.onSaveInstanceState(outState);
1351
Adam Cohen9211d422014-10-07 18:14:53 -07001352 if (mLauncherCallbacks != null) {
1353 mLauncherCallbacks.onSaveInstanceState(outState);
1354 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001355 }
1356
1357 @Override
1358 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001359 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001360
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001361 unregisterReceiver(mScreenOffReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001362 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001363
Sunny Goyal5743f862019-03-28 15:35:32 -07001364 if (mCancelTouchController != null) {
1365 mCancelTouchController.run();
1366 mCancelTouchController = null;
1367 }
Tony Wickhama0068302018-04-11 16:16:11 -07001368
Winson Chungcd2b0142011-06-08 16:02:26 -07001369 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001370 // It's possible to receive onDestroy after a new Launcher activity has
1371 // been created. In this case, don't interfere with the new Launcher.
1372 if (mModel.isCurrentCallbacks(this)) {
1373 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001374 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001375 }
Sunny Goyal623eddd2018-03-02 12:24:41 -08001376 mRotationHelper.destroy();
Sunny Goyal745bad92016-05-02 10:54:12 -07001377
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001378 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001379 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001380 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001381 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001382 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001383
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001384 TextKeyListener.getInstance().release();
Tony2917a8b2017-07-31 23:29:42 -07001385 clearPendingBinds();
Hyunyoung Song31971a32019-01-27 12:31:12 -08001386 LauncherAppState.getIDP(this).removeOnChangeListener(this);
Adam Cohen9211d422014-10-07 18:14:53 -07001387 if (mLauncherCallbacks != null) {
1388 mLauncherCallbacks.onDestroy();
1389 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001390 }
1391
Sunny Goyalae502842016-06-17 08:43:56 -07001392 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1393 return mAccessibilityDelegate;
1394 }
1395
Adam Cohena9cf38f2011-05-02 15:36:58 -07001396 public DragController getDragController() {
1397 return mDragController;
1398 }
1399
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001400 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001401 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001402 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001403 }
1404
1405 @Override
1406 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1407 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001408 try {
1409 super.startIntentSenderForResult(intent, requestCode,
1410 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1411 } catch (IntentSender.SendIntentException e) {
1412 throw new ActivityNotFoundException();
1413 }
1414 }
1415
Winson Chung70d72102011-08-12 11:24:35 -07001416 /**
1417 * Indicates that we want global search for this activity by setting the globalSearch
1418 * argument for {@link #startSearch} to true.
1419 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001420 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001421 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001422 Bundle appSearchData, boolean globalSearch) {
1423 if (appSearchData == null) {
1424 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001425 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001426 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001427
Sunny Goyal6f28e712016-09-13 14:19:29 -07001428 if (mLauncherCallbacks == null ||
1429 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1430 // Starting search from the callbacks failed. Start the default global search.
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001431 super.startSearch(initialQuery, selectInitialQuery, appSearchData, true);
Ian Parkinson047036e2014-09-01 15:40:53 +01001432 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001433
1434 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001435 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001436 }
1437
Joe Onorato9c1289c2009-08-17 11:03:03 -04001438 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001439 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001440 }
1441
Adam Cohen517a7f52014-03-01 12:12:59 -08001442 public boolean isWorkspaceLoading() {
1443 return mWorkspaceLoading;
1444 }
1445
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001446 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001447 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001448 }
1449
Sunny Goyal04a324a2017-01-20 21:08:59 -08001450 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001451 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001452 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001453
Sunny Goyal2100c782016-08-22 16:00:03 -07001454 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001455 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001456 if (LOGD) {
1457 Log.d(TAG, "Adding widget from drop");
1458 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001459 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001460 }
1461
Sunny Goyal2100c782016-08-22 16:00:03 -07001462 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001463 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1464 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1465 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001466
Adam Cohenad4e15c2013-10-17 16:21:35 -07001467 Runnable onComplete = new Runnable() {
1468 @Override
1469 public void run() {
1470 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001471 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001472 }
1473 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001474 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001475 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476 }
1477 }
Romain Guycbb89e42009-06-08 15:52:54 -07001478
Sunny Goyalefb7e842018-10-04 15:11:00 -07001479 public void addPendingItem(PendingAddItemInfo info, int container, int screenId,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001480 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001481 info.container = container;
1482 info.screenId = screenId;
1483 if (cell != null) {
1484 info.cellX = cell[0];
1485 info.cellY = cell[1];
1486 }
1487 info.spanX = spanX;
1488 info.spanY = spanY;
1489
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001490 switch (info.itemType) {
1491 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1492 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001493 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001494 break;
1495 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001496 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001497 break;
1498 default:
1499 throw new IllegalStateException("Unknown item type: " + info.itemType);
1500 }
1501 }
1502
Adam Cohenfbba09b2011-07-18 21:43:05 -07001503 /**
1504 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001505 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001506 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001507 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1508 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001509 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1510 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1511 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001512 }
1513
Adam Cohenfbba09b2011-07-18 21:43:05 -07001514 /**
1515 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001516 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001517 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001518 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001519 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001520 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001521 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001522 // In the case where we've prebound the widget, we remove it from the DragLayer
1523 if (LOGD) {
1524 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1525 }
1526 getDragLayer().removeView(hostView);
1527
Adam Cohened66b2b2012-01-23 17:28:51 -08001528 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001529 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001530
1531 // Clear the boundWidget so that it doesn't get destroyed.
1532 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001533 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001534 // In this case, we either need to start an activity to get permission to bind
1535 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001536 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1537 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1538 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1539 this, info.componentName);
1540 } else {
1541 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1542 }
Adam Cohendd70d662012-10-04 16:53:44 -07001543 Bundle options = info.bindOptions;
1544
Sunny Goyalffe83f12014-08-14 17:39:34 -07001545 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1546 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001547 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001548 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001549 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001550 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001551 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001552 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001553 }
1554
Sunny Goyalefb7e842018-10-04 15:11:00 -07001555 FolderIcon addFolder(CellLayout layout, int container, final int screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001556 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001557 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001558 folderInfo.title = getText(R.string.folder_name);
1559
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001560 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001561 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001562
1563 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001564 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301565 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001566 // Force measure the new folder icon
1567 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1568 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001569 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001570 }
Romain Guycbb89e42009-06-08 15:52:54 -07001571
Winsonc0b52fe2015-09-09 16:38:15 -07001572 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001573 * Unbinds the view for the specified item, and removes the item and all its children.
1574 *
1575 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001576 * @param itemInfo the {@link ItemInfo} for this view.
1577 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001578 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001579 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Sunny Goyal95899162019-03-27 16:03:06 -07001580 if (itemInfo instanceof WorkspaceItemInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001581 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001582 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1583 if (folderIcon instanceof FolderIcon) {
Sunny Goyal95899162019-03-27 16:03:06 -07001584 ((FolderInfo) folderIcon.getTag()).remove((WorkspaceItemInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001585 } else {
1586 mWorkspace.removeWorkspaceItem(v);
1587 }
Winsonc0b52fe2015-09-09 16:38:15 -07001588 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001589 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001590 }
1591 } else if (itemInfo instanceof FolderInfo) {
1592 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001593 if (v instanceof FolderIcon) {
1594 ((FolderIcon) v).removeListeners();
1595 }
Winsonc0b52fe2015-09-09 16:38:15 -07001596 mWorkspace.removeWorkspaceItem(v);
1597 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001598 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001599 }
1600 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1601 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001602 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001603 if (deleteFromDb) {
Tony Wickham6a71a5b2018-08-21 11:40:23 -07001604 getModelWriter().deleteWidgetInfo(widgetInfo, getAppWidgetHost());
Winsonc0b52fe2015-09-09 16:38:15 -07001605 }
1606 } else {
1607 return false;
1608 }
1609 return true;
1610 }
1611
Tony Wickham6a71a5b2018-08-21 11:40:23 -07001612
Winsonc0b52fe2015-09-09 16:38:15 -07001613
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001614 @Override
1615 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001616 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001617 }
1618
Joe Onorato88ec0992009-11-19 13:16:06 -08001619 @Override
1620 public void onBackPressed() {
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08001621 if (finishAutoCancelActionMode()) {
1622 return;
1623 }
Adam Cohen9211d422014-10-07 18:14:53 -07001624 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1625 return;
1626 }
1627
Sunny Goyal45478022015-06-08 16:52:41 -07001628 if (mDragController.isDragging()) {
1629 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001630 return;
1631 }
1632
Jon Mirandafeba90f2016-10-06 10:53:29 -07001633 // Note: There should be at most one log per method call. This is enforced implicitly
1634 // by using if-else statements.
1635 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001636 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
Tony Wickham52c1b662018-05-21 13:13:58 -07001637 if (topView != null && topView.onBackPressed()) {
1638 // Handled by the floating view.
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001639 } else {
Sunny Goyale39690b2018-08-02 13:35:07 -07001640 mStateManager.getState().onBackPressed(this);
Michael Jurkaaf442092010-06-10 17:01:57 -07001641 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001642 }
1643
Sunny Goyalfe770c92016-09-27 14:38:58 -07001644 @TargetApi(Build.VERSION_CODES.M)
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001645 @Override
Jon Miranda73c27ec2018-05-16 18:06:23 -07001646 public ActivityOptions getActivityLaunchOptions(View v) {
1647 return mAppTransitionManager.getActivityLaunchOptions(this, v);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001648 }
1649
Tony Wickham3a6746a2018-03-29 09:39:56 -07001650 public LauncherAppTransitionManager getAppTransitionManager() {
1651 return mAppTransitionManager;
1652 }
1653
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001654 @TargetApi(Build.VERSION_CODES.M)
1655 @Override
1656 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
1657 // Due to legacy reasons, direct call shortcuts require Launchers to have the
1658 // corresponding permission. Show the appropriate permission prompt if that
1659 // is the case.
1660 if (intent.getComponent() == null
1661 && Intent.ACTION_CALL.equals(intent.getAction())
1662 && checkSelfPermission(android.Manifest.permission.CALL_PHONE) !=
1663 PackageManager.PERMISSION_GRANTED) {
1664
1665 setWaitingForResult(PendingRequestArgs
1666 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
1667 requestPermissions(new String[]{android.Manifest.permission.CALL_PHONE},
1668 REQUEST_PERMISSION_CALL_PHONE);
1669 return true;
1670 } else {
1671 return false;
1672 }
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001673 }
1674
Hyunyoung Song46d07f72018-05-22 15:41:25 -07001675 @Override
Hyunyoung Songfc007472018-10-25 14:09:50 -07001676 public int getCurrentState() {
1677 if(mStateManager.getState() == LauncherState.ALL_APPS) {
1678 return StatsLogUtils.LAUNCHER_STATE_ALLAPPS;
Govinda Wassermanc06e1512019-04-09 09:56:53 -04001679 } else if (mStateManager.getState() == OVERVIEW) {
Hyunyoung Songfc007472018-10-25 14:09:50 -07001680 return StatsLogUtils.LAUNCHER_STATE_OVERVIEW;
1681 }
Hyunyoung Song0ae38882018-11-05 14:45:19 -08001682 return StatsLogUtils.LAUNCHER_STATE_HOME;
Hyunyoung Songfc007472018-10-25 14:09:50 -07001683 }
1684
1685 @Override
Hyunyoung Song46d07f72018-05-22 15:41:25 -07001686 public void modifyUserEvent(LauncherLogProto.LauncherEvent event) {
1687 if (event.srcTarget != null && event.srcTarget.length > 0 &&
1688 event.srcTarget[1].containerType == ContainerType.PREDICTION) {
1689 Target[] targets = new Target[3];
1690 targets[0] = event.srcTarget[0];
1691 targets[1] = event.srcTarget[1];
1692 targets[2] = newTarget(Target.Type.CONTAINER);
1693 event.srcTarget = targets;
1694 LauncherState state = mStateManager.getState();
1695 if (state == LauncherState.ALL_APPS) {
1696 event.srcTarget[2].containerType = ContainerType.ALLAPPS;
Govinda Wassermanc06e1512019-04-09 09:56:53 -04001697 } else if (state == OVERVIEW) {
Hyunyoung Song46d07f72018-05-22 15:41:25 -07001698 event.srcTarget[2].containerType = ContainerType.TASKSWITCHER;
1699 }
1700 }
1701 }
1702
Sunny Goyal369212a2019-03-26 15:03:57 -07001703 public boolean startActivitySafely(View v, Intent intent, ItemInfo item,
1704 @Nullable String sourceContainer) {
1705 boolean success = super.startActivitySafely(v, intent, item, sourceContainer);
Hyunyoung Song1241e612018-05-15 21:46:51 -07001706 if (success && v instanceof BubbleTextView) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001707 // This is set to the view that launched the activity that navigated the user away
1708 // from launcher. Since there is no callback for when the activity has finished
1709 // launching, enable the press state and keep this reference to reset the press
1710 // state when we return to launcher.
1711 BubbleTextView btv = (BubbleTextView) v;
1712 btv.setStayPressed(true);
1713 setOnResumeCallback(btv);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001714 }
Hyunyoung Song1241e612018-05-15 21:46:51 -07001715 return success;
Michael Jurka86a720e2012-05-09 11:23:23 -07001716 }
1717
Winson Chung3d503fb2011-07-13 17:25:49 -07001718 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001719 // TODO: Remove this method
Sunny Goyal876e4622018-11-02 13:50:40 -07001720 return mHotseat != null && (layout == mHotseat);
Winson Chung3d503fb2011-07-13 17:25:49 -07001721 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001722
Winson Chung3d503fb2011-07-13 17:25:49 -07001723 /**
1724 * Returns the CellLayout of the specified container at the specified screen.
1725 */
Sunny Goyalefb7e842018-10-04 15:11:00 -07001726 public CellLayout getCellLayout(int container, int screenId) {
Sunny Goyal876e4622018-11-02 13:50:40 -07001727 return (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1728 ? mHotseat : mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08001729 }
1730
Michael Jurkae326f182011-11-21 14:05:46 -08001731 @Override
1732 public void onTrimMemory(int level) {
1733 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07001734 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
1735 // The widget preview db can result in holding onto over
1736 // 3MB of memory for caching which isn't necessary.
1737 SQLiteDatabase.releaseMemory();
1738
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08001739 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07001740 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08001741 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01001742 if (mLauncherCallbacks != null) {
1743 mLauncherCallbacks.onTrimMemory(level);
1744 }
Winson Chung62a70be2018-02-23 15:10:05 -08001745 UiFactory.onTrimMemory(this, level);
Michael Jurkae326f182011-11-21 14:05:46 -08001746 }
1747
Michael Jurkad7c28052012-04-27 15:43:36 -07001748 @Override
1749 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07001750 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07001751 final List<CharSequence> text = event.getText();
1752 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07001753 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001754 // TODO: When can workspace be null?
1755 text.add(mWorkspace == null
1756 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07001757 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07001758 return result;
1759 }
1760
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001761 public void setOnResumeCallback(OnResumeCallback callback) {
1762 if (mOnResumeCallback != null) {
1763 mOnResumeCallback.onLauncherResume();
1764 }
1765 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07001766 }
1767
Michael Jurka7607c2f2013-04-03 14:33:19 -07001768 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001769 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001770 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07001771 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001772 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07001773 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001774 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07001775 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07001776 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07001777 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001778 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001779
Joe Onorato9c1289c2009-08-17 11:03:03 -04001780 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07001781 * Clear any pending bind callbacks. This is called when is loader is planning to
1782 * perform a full rebind from scratch.
1783 */
1784 @Override
1785 public void clearPendingBinds() {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001786 if (mPendingExecutor != null) {
1787 mPendingExecutor.markCompleted();
1788 mPendingExecutor = null;
1789 }
1790 }
1791
1792 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001793 * Refreshes the shortcuts shown on the workspace.
1794 *
1795 * Implementation of the method from LauncherModel.Callbacks.
1796 */
1797 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001798 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001799 // Floating panels (except the full widget sheet) are associated with individual icons. If
1800 // we are starting a fresh bind, close all such panels as all the icons are about
1801 // to go away.
1802 AbstractFloatingView.closeOpenViews(this, true,
Sunny Goyal7ede6112017-12-05 15:11:21 -08001803 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_REBIND_SAFE);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08001804
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001805 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08001806
Winson Chungd64d1762013-08-20 14:37:16 -07001807 // Clear the workspace because it's going to be rebound
Sunny Goyal2db53422019-03-01 16:06:12 -08001808 mDragController.cancelDrag();
1809
Adam Cohendf035382011-04-11 17:22:04 -07001810 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07001811 mWorkspace.removeAllWorkspaceScreens();
Sunny Goyalc11fac32018-02-27 19:20:15 -08001812 mAppWidgetHost.clearViews();
Winson Chungd64d1762013-08-20 14:37:16 -07001813
Winson Chung3d503fb2011-07-13 17:25:49 -07001814 if (mHotseat != null) {
Sunny Goyal11e96492018-03-23 17:06:00 -07001815 mHotseat.resetLayout(mDeviceProfile.isVerticalBarLayout());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001816 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001817 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001818 }
1819
Adam Cohendcd297f2013-06-18 13:13:40 -07001820 @Override
Sunny Goyalefb7e842018-10-04 15:11:00 -07001821 public void bindScreens(IntArray orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001822 // Make sure the first screen is always at the start.
Jon Miranda7143ba62019-03-15 09:00:05 -07001823 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001824 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalefb7e842018-10-04 15:11:00 -07001825 orderedScreenIds.removeValue(Workspace.FIRST_SCREEN_ID);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001826 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Jon Miranda7143ba62019-03-15 09:00:05 -07001827 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001828 // If there are no screens, we need to have an empty screen
1829 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07001830 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001831 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07001832
Winsonc7d2e832016-07-28 12:24:55 -07001833 // After we have added all the screens, if the wallpaper was locked to the default state,
1834 // then notify to indicate that it can be released and a proper wallpaper offset can be
1835 // computed before the next layout
1836 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07001837 }
1838
Sunny Goyalefb7e842018-10-04 15:11:00 -07001839 private void bindAddScreens(IntArray orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001840 int count = orderedScreenIds.size();
1841 for (int i = 0; i < count; i++) {
Sunny Goyalefb7e842018-10-04 15:11:00 -07001842 int screenId = orderedScreenIds.get(i);
Jon Miranda7143ba62019-03-15 09:00:05 -07001843 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001844 // No need to bind the first screen, as its always bound.
1845 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
1846 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001847 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001848 }
1849
Sunny Goyalb23980c2017-08-17 07:45:25 -07001850 @Override
Tony Wickham6a71a5b2018-08-21 11:40:23 -07001851 public void preAddApps() {
1852 // If there's an undo snackbar, force it to complete to ensure empty screens are removed
1853 // before trying to add new items.
1854 mModelWriter.commitDelete();
1855 AbstractFloatingView snackbar = AbstractFloatingView.getOpenView(this, TYPE_SNACKBAR);
1856 if (snackbar != null) {
1857 snackbar.post(() -> snackbar.close(true));
1858 }
1859 }
1860
1861 @Override
Sunny Goyalefb7e842018-10-04 15:11:00 -07001862 public void bindAppsAdded(IntArray newScreens, ArrayList<ItemInfo> addNotAnimated,
Sunny Goyal29947f02017-12-18 13:49:44 -08001863 ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07001864 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08001865 if (newScreens != null) {
1866 bindAddScreens(newScreens);
1867 }
Winson Chungd64d1762013-08-20 14:37:16 -07001868
1869 // We add the items without animation on non-visible pages, and with
1870 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08001871 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001872 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001873 }
Adam Cohen76a47a12014-02-05 11:47:43 -08001874 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001875 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07001876 }
Winson Chungc58497e2013-09-03 17:48:37 -07001877
Winson Chung87412982013-10-03 18:34:14 -07001878 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07001879 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001880 }
1881
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001882 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001883 * Bind the items start-end from the list.
1884 *
1885 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001886 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07001887 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07001888 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001889 // Get the list of added items and intersect them with the set of items here
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001890 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07001891 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001892 Workspace workspace = mWorkspace;
Sunny Goyalefb7e842018-10-04 15:11:00 -07001893 int newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07001894 int end = items.size();
1895 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001896 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07001897
1898 // Short circuit if we are loading dock items for a configuration which has no dock
1899 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
1900 mHotseat == null) {
1901 continue;
1902 }
1903
Sunny Goyal639e9062015-08-19 19:17:06 -07001904 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001905 switch (item.itemType) {
1906 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1907 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08001908 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Sunny Goyal95899162019-03-27 16:03:06 -07001909 WorkspaceItemInfo info = (WorkspaceItemInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07001910 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001911 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001912 }
1913 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07001914 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001915 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001916 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001917 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001918 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07001919 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1920 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07001921 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07001922 if (view == null) {
1923 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001924 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08001925 break;
1926 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001927 default:
1928 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001929 }
Sunny Goyal639e9062015-08-19 19:17:06 -07001930
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001931 /*
1932 * Remove colliding items.
1933 */
1934 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1935 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
1936 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
1937 View v = cl.getChildAt(item.cellX, item.cellY);
1938 Object tag = v.getTag();
1939 String desc = "Collision while binding workspace item: " + item
1940 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08001941 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001942 throw (new RuntimeException(desc));
1943 } else {
1944 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001945 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001946 continue;
1947 }
1948 }
1949 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05301950 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07001951 if (animateIcons) {
1952 // Animate all the applications up now
1953 view.setAlpha(0f);
1954 view.setScaleX(0f);
1955 view.setScaleY(0f);
1956 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08001957 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07001958 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001959 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07001960
Sunny Goyalf0b6db72018-08-13 16:10:14 -07001961 // Animate to the correct page
1962 if (animateIcons && newItemsScreenId > -1) {
1963 AnimatorSet anim = new AnimatorSet();
1964 anim.playTogether(bounceAnims);
Jon Miranda64eb8ea2018-03-29 11:43:58 -07001965
Sunny Goyalefb7e842018-10-04 15:11:00 -07001966 int currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyalf0b6db72018-08-13 16:10:14 -07001967 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
1968 final Runnable startBounceAnimRunnable = anim::start;
1969
1970 if (newItemsScreenId != currentScreenId) {
1971 // We post the animation slightly delayed to prevent slowdowns
1972 // when we are loading right after we return to launcher.
1973 mWorkspace.postDelayed(new Runnable() {
1974 public void run() {
1975 if (mWorkspace != null) {
1976 AbstractFloatingView.closeAllOpenViews(Launcher.this, false);
1977
1978 mWorkspace.snapToPage(newScreenIndex);
1979 mWorkspace.postDelayed(startBounceAnimRunnable,
1980 NEW_APPS_ANIMATION_DELAY);
Winson Chung94d67682013-09-25 16:29:40 -07001981 }
Sunny Goyalf0b6db72018-08-13 16:10:14 -07001982 }
1983 }, NEW_APPS_PAGE_MOVE_DELAY);
1984 } else {
1985 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07001986 }
Winson Chung64359a52013-07-08 17:17:08 -07001987 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001988 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001989 }
1990
Joe Onorato9c1289c2009-08-17 11:03:03 -04001991 /**
1992 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001993 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07001994 public void bindAppWidget(LauncherAppWidgetInfo item) {
1995 View view = inflateAppWidget(item);
1996 if (view != null) {
1997 mWorkspace.addInScreen(view, item);
1998 mWorkspace.requestLayout();
1999 }
2000 }
2001
2002 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002003 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302004 PendingAppWidgetHostView view =
2005 new PendingAppWidgetHostView(this, item, mIconCache, true);
2006 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002007 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002008 }
2009
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002010 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002011
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002012 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08002013
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002014 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2015 // If the provider is not ready, bind as a pending widget.
2016 appWidgetInfo = null;
2017 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2018 // The widget id is not valid. Try to find the widget based on the provider info.
2019 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
2020 } else {
2021 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
2022 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002023
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002024 // If the provider is ready, but the width is not yet restored, try to restore it.
2025 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
2026 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07002027 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002028 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
2029 + " belongs to component " + item.providerName
2030 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002031 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002032 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002033 }
Sunny Goyalff572272014-07-23 13:58:07 -07002034
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002035 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002036 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07002037 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2038 // Id has not been allocated yet. Allocate a new id.
2039 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
2040 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07002041
Sunny Goyald478c832016-04-01 12:04:16 -07002042 // Also try to bind the widget. If the bind fails, the user will be shown
2043 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08002044 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07002045 pendingInfo.spanX = item.spanX;
2046 pendingInfo.spanY = item.spanY;
2047 pendingInfo.minSpanX = item.minSpanX;
2048 pendingInfo.minSpanY = item.minSpanY;
2049 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07002050
2051 boolean isDirectConfig =
2052 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2053 if (isDirectConfig && item.bindOptions != null) {
2054 Bundle newOptions = item.bindOptions.getExtras();
2055 if (options != null) {
2056 newOptions.putAll(options);
2057 }
2058 options = newOptions;
2059 }
Sunny Goyald478c832016-04-01 12:04:16 -07002060 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2061 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002062
Sunny Goyal86df1382016-08-10 15:03:22 -07002063 // We tried to bind once. If we were not able to bind, we would need to
2064 // go through the permission dialog, which means we cannot skip the config
2065 // activity.
2066 item.bindOptions = null;
2067 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2068
Sunny Goyald478c832016-04-01 12:04:16 -07002069 // Bind succeeded
2070 if (success) {
2071 // If the widget has a configure activity, it is still needs to set it up,
2072 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002073 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002074 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2075 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002076 }
Sunny Goyald478c832016-04-01 12:04:16 -07002077
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002078 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002079 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002080 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002081 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002082 // The widget was marked as UI not ready, but there is no configure activity to
2083 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002084 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002085 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002086 }
Sunny Goyalff572272014-07-23 13:58:07 -07002087 }
2088
Sunny Goyald5462aa2016-11-22 16:52:39 +05302089 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002090 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002091 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002092 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002093 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Tony Wickham6a71a5b2018-08-21 11:40:23 -07002094 getModelWriter().deleteWidgetInfo(item, getAppWidgetHost());
Sunny Goyalb23980c2017-08-17 07:45:25 -07002095 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002096 }
2097
Sunny Goyal233ee962015-08-03 13:05:01 -07002098 item.minSpanX = appWidgetInfo.minSpanX;
2099 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302100 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002101 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302102 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002103 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302104 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002105
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002106 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002107 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002108 }
2109
Sunny Goyalff572272014-07-23 13:58:07 -07002110 /**
2111 * Restores a pending widget.
2112 *
2113 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002114 */
Sunny Goyald478c832016-04-01 12:04:16 -07002115 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002116 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2117 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2118 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002119 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002120 }
2121
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002122 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002123 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002124 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2125 info.pendingItemInfo = null;
2126 }
Sunny Goyalff572272014-07-23 13:58:07 -07002127
Sunny Goyalba47faa2017-10-04 16:50:57 -07002128 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002129 view.reInflate();
Sunny Goyalba47faa2017-10-04 16:50:57 -07002130 }
2131
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002132 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002133 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002134 }
2135
Adam Cohen1462de32012-07-24 22:34:36 -07002136 public void onPageBoundSynchronously(int page) {
Sunny Goyalf4aceab2017-12-06 16:16:31 -08002137 mSynchronouslyBoundPage = page;
Adam Cohen1462de32012-07-24 22:34:36 -07002138 }
2139
Sunny Goyal527c7d32015-08-28 15:19:36 -07002140 @Override
2141 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2142 if (mPendingExecutor != null) {
2143 mPendingExecutor.markCompleted();
2144 }
2145 mPendingExecutor = executor;
Sunny Goyal60180b02018-02-07 12:56:30 -08002146 if (!isInState(ALL_APPS)) {
2147 mAppsView.getAppsStore().setDeferUpdates(true);
2148 mPendingExecutor.execute(() -> mAppsView.getAppsStore().setDeferUpdates(false));
2149 }
2150
Sunny Goyal527c7d32015-08-28 15:19:36 -07002151 executor.attachTo(this);
2152 }
2153
2154 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2155 if (mPendingExecutor == executor) {
2156 mPendingExecutor = null;
2157 }
2158 }
2159
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002160 @Override
2161 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
Sunny Goyal6001ea22018-05-10 16:31:00 -07002162 AlphaProperty property = mDragLayer.getAlphaProperty(ALPHA_INDEX_LAUNCHER_LOAD);
2163 if (property.getValue() < 1) {
2164 ObjectAnimator anim = ObjectAnimator.ofFloat(property, MultiValueAlpha.VALUE, 1);
2165 if (executor != null) {
2166 anim.addListener(new AnimatorListenerAdapter() {
2167 @Override
2168 public void onAnimationEnd(Animator animation) {
2169 executor.onLoadAnimationCompleted();
2170 }
2171 });
2172 }
2173 anim.start();
Sunny Goyal29947f02017-12-18 13:49:44 -08002174 } else if (executor != null) {
2175 executor.onLoadAnimationCompleted();
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002176 }
2177 }
2178
Joe Onorato9c1289c2009-08-17 11:03:03 -04002179 /**
2180 * Callback saying that there aren't any more items to bind.
2181 *
2182 * Implementation of the method from LauncherModel.Callbacks.
2183 */
Tonyf80e8932018-12-21 11:58:04 -08002184 public void finishBindingItems(int pageBoundFirst) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002185 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002186 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002187
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002188 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002189
Sunny Goyal2100c782016-08-22 16:00:03 -07002190 if (mPendingActivityResult != null) {
2191 handleActivityResult(mPendingActivityResult.requestCode,
2192 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2193 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002194 }
2195
Sunny Goyala474a9b2017-05-04 16:47:11 -07002196 InstallShortcutReceiver.disableAndFlushInstallQueue(
2197 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002198
Tonyf80e8932018-12-21 11:58:04 -08002199 // When undoing the removal of the last item on a page, return to that page.
Tony Wickham03f27012019-04-25 14:22:54 -07002200 // Since we are just resetting the current page without user interaction,
2201 // override the previous page so we don't log the page switch.
2202 mWorkspace.setCurrentPage(pageBoundFirst, pageBoundFirst /* overridePrevPage */);
Tony Wickham6a71a5b2018-08-21 11:40:23 -07002203
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002204 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002205 }
2206
Winson Chunga2413752012-04-03 14:22:34 -07002207 private boolean canRunNewAppsAnimation() {
2208 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002209 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002210 }
2211
Winson Chungc9168342013-06-26 14:54:55 -07002212 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002213 ValueAnimator bounceAnim = new PropertyListBuilder().alpha(1).scale(1).build(v)
2214 .setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
Winson Chungc9168342013-06-26 14:54:55 -07002215 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002216 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002217 return bounceAnim;
2218 }
2219
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002220 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002221 * Add the icons for all apps.
2222 *
2223 * Implementation of the method from LauncherModel.Callbacks.
2224 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002225 public void bindAllApplications(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002226 mAppsView.getAppsStore().setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002227 }
2228
2229 /**
Zak Cohen658c67a2018-10-19 14:21:05 -07002230 * Copies LauncherModel's map of activities to shortcut counts to Launcher's. This is necessary
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002231 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2232 */
2233 @Override
Zak Cohen658c67a2018-10-19 14:21:05 -07002234 public void bindDeepShortcutMap(HashMap<ComponentKey, Integer> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002235 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002236 }
2237
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002238 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002239 * A package was updated.
2240 *
2241 * Implementation of the method from LauncherModel.Callbacks.
2242 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002243 @Override
2244 public void bindAppsAddedOrUpdated(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002245 mAppsView.getAppsStore().addOrUpdateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002246 }
2247
Sunny Goyal4390ace2014-10-13 11:33:11 -07002248 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002249 public void bindPromiseAppProgressUpdated(PromiseAppInfo app) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002250 mAppsView.getAppsStore().updatePromiseAppProgress(app);
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002251 }
2252
2253 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002254 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002255 mWorkspace.widgetsRestored(widgets);
2256 }
2257
Joe Onorato9c1289c2009-08-17 11:03:03 -04002258 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002259 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002260 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002261 *
2262 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002263 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002264 @Override
Sunny Goyal95899162019-03-27 16:03:06 -07002265 public void bindWorkspaceItemsChanged(ArrayList<WorkspaceItemInfo> updated) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002266 if (!updated.isEmpty()) {
2267 mWorkspace.updateShortcuts(updated);
2268 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002269 }
2270
2271 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002272 * Update the state of a package, typically related to install state.
2273 *
2274 * Implementation of the method from LauncherModel.Callbacks.
2275 */
Sunny Goyale755d462014-07-22 13:48:29 -07002276 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002277 public void bindRestoreItemsChange(HashSet<ItemInfo> updates) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07002278 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07002279 }
2280
2281 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002282 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07002283 * in addition to specific applications to remove, the reason being that
2284 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002285 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07002286 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002287 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07002288 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002289 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002290 mWorkspace.removeItemsByMatcher(matcher);
2291 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002292 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002293
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002294 @Override
2295 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002296 mAppsView.getAppsStore().removeApps(appInfos);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002297 }
Joe Onoratobe386092009-11-17 17:32:16 -08002298
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002299 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002300 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
2301 mPopupDataProvider.setAllWidgets(allWidgets);
Tony Wickham26b17462017-03-20 17:12:24 -07002302 }
2303
Tony Wickham86222d22017-03-29 15:30:43 -07002304 /**
2305 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
2306 * refreshes the widgets and shortcuts associated with the given package/user
2307 */
2308 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07002309 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002310 }
2311
Winson Chung80baf5a2010-08-09 16:03:15 -07002312 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002313 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08002314 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002315 @Override
2316 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
2317 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002318
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002319 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
2320 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07002321 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002322 writer.println(prefix + " Homescreen " + i);
2323
2324 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
2325 for (int j = 0; j < layout.getChildCount(); j++) {
2326 Object tag = layout.getChildAt(j).getTag();
2327 if (tag != null) {
2328 writer.println(prefix + " " + tag.toString());
2329 }
2330 }
2331 }
2332
2333 writer.println(prefix + " Hotseat");
Sunny Goyal876e4622018-11-02 13:50:40 -07002334 ViewGroup layout = mHotseat.getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07002335 for (int j = 0; j < layout.getChildCount(); j++) {
2336 Object tag = layout.getChildAt(j).getTag();
2337 if (tag != null) {
2338 writer.println(prefix + " " + tag.toString());
2339 }
Adam Cohen4caf2982013-08-20 18:54:31 -07002340 }
Sunny Goyala1365452015-10-01 15:46:24 -07002341 }
2342
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002343 writer.println(prefix + "Misc:");
2344 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
2345 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
2346 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
Sunny Goyale43d00d2018-05-14 14:23:18 -07002347 writer.println(" mRotationHelper: " + mRotationHelper);
Sunny Goyalcb2c5052018-10-15 12:30:29 -07002348 // Extra logging for b/116853349
Hyunyoung Songa310a802019-04-25 13:57:16 -07002349 mDragLayer.dump(prefix, writer);
2350 mStateManager.dump(prefix, writer);
Sunny Goyale43d00d2018-05-14 14:23:18 -07002351 dumpMisc(writer);
Sunny Goyal2bba1902018-02-27 12:20:50 -08002352
2353 try {
2354 FileLog.flushAll(writer);
2355 } catch (Exception e) {
2356 // Ignore
2357 }
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002358
2359 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002360
Adam Cohen9211d422014-10-07 18:14:53 -07002361 if (mLauncherCallbacks != null) {
2362 mLauncherCallbacks.dump(prefix, fd, writer, args);
2363 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002364 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002365
Sunny Goyal66b24572016-09-21 15:57:55 -07002366 @Override
Sunny Goyal66b24572016-09-21 15:57:55 -07002367 public void onProvideKeyboardShortcuts(
2368 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
2369
2370 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002371 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002372 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
2373 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
Vadim Tryshev74c261c2018-06-12 13:44:43 -07002374 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.widget_button_text),
2375 KeyEvent.KEYCODE_W, KeyEvent.META_CTRL_ON));
Sunny Goyal66b24572016-09-21 15:57:55 -07002376 }
Vadim Tryshev3455f8d2018-05-11 20:02:20 -07002377 final View currentFocus = getCurrentFocus();
2378 if (currentFocus != null) {
2379 if (new CustomActionsPopup(this, currentFocus).canShow()) {
2380 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
2381 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
2382 }
2383 if (currentFocus.getTag() instanceof ItemInfo
2384 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
2385 shortcutInfos.add(new KeyboardShortcutInfo(
2386 getString(R.string.shortcuts_menu_with_notifications_description),
2387 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
2388 }
Sunny Goyal66b24572016-09-21 15:57:55 -07002389 }
2390 if (!shortcutInfos.isEmpty()) {
2391 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
2392 }
2393
2394 super.onProvideKeyboardShortcuts(data, menu, deviceId);
2395 }
2396
2397 @Override
2398 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
2399 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
2400 switch (keyCode) {
2401 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002402 if (isInState(NORMAL)) {
2403 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07002404 return true;
2405 }
2406 break;
2407 case KeyEvent.KEYCODE_S: {
2408 View focusedView = getCurrentFocus();
2409 if (focusedView instanceof BubbleTextView
2410 && focusedView.getTag() instanceof ItemInfo
2411 && mAccessibilityDelegate.performAction(focusedView,
2412 (ItemInfo) focusedView.getTag(),
2413 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal00ac9202017-11-09 15:24:06 -08002414 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07002415 return true;
2416 }
2417 break;
2418 }
2419 case KeyEvent.KEYCODE_O:
2420 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
2421 return true;
2422 }
2423 break;
Vadim Tryshev74c261c2018-06-12 13:44:43 -07002424 case KeyEvent.KEYCODE_W:
2425 if (isInState(NORMAL)) {
2426 OptionsPopupView.openWidgets(this);
2427 return true;
2428 }
2429 break;
Sunny Goyal66b24572016-09-21 15:57:55 -07002430 }
2431 }
2432 return super.onKeyShortcut(keyCode, event);
2433 }
2434
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002435 @Override
2436 public boolean onKeyUp(int keyCode, KeyEvent event) {
2437 if (keyCode == KeyEvent.KEYCODE_MENU) {
2438 // KEYCODE_MENU is sent by some tests, for example
2439 // LauncherJankTests#testWidgetsContainerFling. Don't just remove its handling.
2440 if (!mDragController.isDragging() && !mWorkspace.isSwitchingState() &&
2441 isInState(NORMAL)) {
2442 // Close any open floating views.
2443 AbstractFloatingView.closeAllOpenViews(this);
2444
2445 // Setting the touch point to (-1, -1) will show the options popup in the center of
2446 // the screen.
Sunny Goyal2fd7a8b2018-03-30 17:10:13 -07002447 OptionsPopupView.showDefaultOptions(this, -1, -1);
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002448 }
2449 return true;
2450 }
2451 return super.onKeyUp(keyCode, event);
2452 }
2453
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002454 public static Launcher getLauncher(Context context) {
Sunny Goyal87b5eb62018-07-03 15:53:39 -07002455 return (Launcher) fromContext(context);
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002456 }
2457
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002458 /**
Sunny Goyalab770a12018-11-14 15:17:26 -08002459 * Just a wrapper around the type cast to allow easier tracking of calls.
2460 */
2461 public static <T extends Launcher> T cast(ActivityContext activityContext) {
2462 return (T) activityContext;
2463 }
2464
2465 /**
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002466 * Callback for listening for onResume
2467 */
2468 public interface OnResumeCallback {
2469
2470 void onLauncherResume();
2471 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002472}