blob: 73fba4eb61b2a0e29dec363ce4c59cc1a39d7e04 [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;
Sunny Goyal6001ea22018-05-10 16:31:00 -070027import static com.android.launcher3.dragndrop.DragLayer.ALPHA_INDEX_LAUNCHER_LOAD;
Sunny Goyalaeb16432017-10-16 11:46:41 -070028import static com.android.launcher3.logging.LoggerUtils.newContainerTarget;
Hyunyoung Song46d07f72018-05-22 15:41:25 -070029import static com.android.launcher3.logging.LoggerUtils.newTarget;
vadimt00d42552018-12-11 17:59:36 -080030import static com.android.launcher3.util.RaceConditionTracker.ENTER;
31import static com.android.launcher3.util.RaceConditionTracker.EXIT;
Sunny Goyal326403e2017-10-02 12:45:10 -070032
Gilles Debunnedd6c9922010-10-25 11:23:41 -070033import android.animation.Animator;
Sunny Goyal6001ea22018-05-10 16:31:00 -070034import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070035import android.animation.AnimatorSet;
Sunny Goyal6001ea22018-05-10 16:31:00 -070036import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070037import android.animation.ValueAnimator;
Adam Cohen0b395352014-06-09 22:54:36 +000038import android.annotation.TargetApi;
Tony Wickham005df0b2018-02-13 11:28:12 -080039import android.app.ActivityOptions;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080040import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070041import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080043import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080044import android.content.ComponentCallbacks2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070047import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070048import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070049import android.content.SharedPreferences;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.content.pm.PackageManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080051import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070052import android.database.sqlite.SQLiteDatabase;
Sunny Goyal59d086c2018-05-07 17:31:40 -070053import android.graphics.Point;
Adam Cohen0f668f32014-09-08 19:54:17 +020054import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055import android.os.Bundle;
Hyunyoung Songbd6fba92018-05-16 15:54:31 -070056import android.os.Handler;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070057import android.os.Parcelable;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080058import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070059import android.os.StrictMode;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070060import android.os.UserHandle;
Michael Jurkaa33411c2012-06-14 16:18:21 -070061import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070063import android.util.Log;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070064import android.util.SparseArray;
Sunny Goyal59d086c2018-05-07 17:31:40 -070065import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070066import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070067import android.view.KeyboardShortcutGroup;
68import android.view.KeyboardShortcutInfo;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080069import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -070070import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070071import android.view.View;
Adam Cohen96d30a12013-07-16 18:13:21 -070072import android.view.ViewGroup;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080073import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070074import android.view.animation.OvershootInterpolator;
Adam Cohen96d30a12013-07-16 18:13:21 -070075import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070076
Sunny Goyal651077b2014-06-30 14:15:31 -070077import com.android.launcher3.DropTarget.DragObject;
Sunny Goyalae502842016-06-17 08:43:56 -070078import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070079import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070080import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal7ede6112017-12-05 15:11:21 -080081import com.android.launcher3.allapps.DiscoveryBounce;
Sunny Goyalf0b6db72018-08-13 16:10:14 -070082import com.android.launcher3.anim.PropertyListBuilder;
Sunny Goyalffe83f12014-08-14 17:39:34 -070083import com.android.launcher3.compat.AppWidgetManagerCompat;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -070084import com.android.launcher3.compat.LauncherAppsCompatVO;
Tony Wickhame0c33232016-02-08 11:37:04 -080085import com.android.launcher3.config.FeatureFlags;
vadimt00d42552018-12-11 17:59:36 -080086import com.android.launcher3.dot.DotInfo;
Vadim Tryshevfedca432015-08-19 17:55:02 -070087import com.android.launcher3.dragndrop.DragController;
88import com.android.launcher3.dragndrop.DragLayer;
89import com.android.launcher3.dragndrop.DragView;
Jon Mirandaaeb4dd02018-07-11 14:16:23 -070090import com.android.launcher3.folder.Folder;
Sunny Goyal26119432016-02-18 22:09:23 +000091import com.android.launcher3.folder.FolderIcon;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070092import com.android.launcher3.folder.FolderIconPreviewVerifier;
Sunny Goyalf840f102018-09-21 14:41:05 -070093import com.android.launcher3.icons.IconCache;
Sunny Goyal66b24572016-09-21 15:57:55 -070094import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -070095import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -070096import com.android.launcher3.logging.FileLog;
Hyunyoung Songfc007472018-10-25 14:09:50 -070097import com.android.launcher3.logging.StatsLogUtils;
Hyunyoung Songaa953652016-04-19 18:30:24 -070098import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Song46d07f72018-05-22 15:41:25 -070099import com.android.launcher3.logging.UserEventDispatcher.UserEventDelegate;
Sunny Goyala535ae42017-02-27 10:07:13 -0800100import com.android.launcher3.model.ModelWriter;
Sunny Goyala535ae42017-02-27 10:07:13 -0800101import com.android.launcher3.notification.NotificationListener;
Tony Wickham540913e2017-01-23 11:47:51 -0800102import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -0800103import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700104import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyalf8088ee2017-11-10 14:52:00 -0800105import com.android.launcher3.states.InternalStateHandler;
Sunny Goyal623eddd2018-03-02 12:24:41 -0800106import com.android.launcher3.states.RotationHelper;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800107import com.android.launcher3.touch.ItemClickHandler;
Sunny Goyala5ace712017-11-15 17:12:51 -0800108import com.android.launcher3.uioverrides.UiFactory;
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700109import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530110import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
111import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
Sunny Goyalaeb16432017-10-16 11:46:41 -0700112import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyal2100c782016-08-22 16:00:03 -0700113import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700114import com.android.launcher3.util.ComponentKey;
Sunny Goyalefb7e842018-10-04 15:11:00 -0700115import com.android.launcher3.util.IntArray;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700116import com.android.launcher3.util.ItemInfoMatcher;
Sunny Goyal6001ea22018-05-10 16:31:00 -0700117import com.android.launcher3.util.MultiValueAlpha;
118import com.android.launcher3.util.MultiValueAlpha.AlphaProperty;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700119import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800120import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700121import com.android.launcher3.util.PendingRequestArgs;
vadimt00d42552018-12-11 17:59:36 -0800122import com.android.launcher3.util.RaceConditionTracker;
Sunny Goyal8392c822017-06-20 10:03:56 -0700123import com.android.launcher3.util.SystemUiController;
Mario Bertschlera6936942017-05-31 14:48:19 -0700124import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700125import com.android.launcher3.util.Thunk;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700126import com.android.launcher3.util.TraceHelper;
Sunny Goyal326403e2017-10-02 12:45:10 -0700127import com.android.launcher3.util.UiThreadHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700128import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyalab770a12018-11-14 15:17:26 -0800129import com.android.launcher3.views.ActivityContext;
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800130import com.android.launcher3.views.OptionsPopupView;
Sunny Goyal29947f02017-12-18 13:49:44 -0800131import com.android.launcher3.widget.LauncherAppWidgetHostView;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800132import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700133import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal29947f02017-12-18 13:49:44 -0800134import com.android.launcher3.widget.PendingAppWidgetHostView;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800135import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700136import com.android.launcher3.widget.WidgetHostViewLoader;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700137import com.android.launcher3.widget.WidgetListRowEntry;
138import com.android.launcher3.widget.WidgetsFullSheet;
Sunny Goyal952e63d2017-08-16 04:59:08 -0700139import com.android.launcher3.widget.custom.CustomWidgetParser;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700140
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700141import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700142import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700143import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700144import java.util.Collection;
Zak Cohen658c67a2018-10-19 14:21:05 -0700145import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700146import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700147import java.util.List;
Tony Wickham010d2552017-01-20 08:15:28 -0800148import java.util.Set;
Sunny Goyalaae6fbb2019-01-31 16:05:58 -0800149import java.util.function.Predicate;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700150
Sunny Goyal8c48d8b2019-01-25 15:10:18 -0800151import androidx.annotation.Nullable;
152
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153/**
154 * Default launcher application.
155 */
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700156public class Launcher extends BaseDraggingActivity implements LauncherExterns,
Hyunyoung Song31971a32019-01-27 12:31:12 -0800157 LauncherModel.Callbacks, LauncherProviderChangeListener, UserEventDelegate,
158 InvariantDeviceProfile.OnIDPChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700159 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700160 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161
Winson Chunga2413752012-04-03 14:22:34 -0700162 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700163
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700165 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700166
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700167 private static final int REQUEST_PICK_APPWIDGET = 9;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168
Michael Jurka8b805b12012-04-18 14:23:14 -0700169 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800170 public static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
171 public static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700172
Jon Mirandac476d6e2017-05-04 16:30:01 -0700173 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700174
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700175 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
176
Mathew Inwood876a8462013-06-14 14:12:41 +0100177 /**
178 * IntentStarter uses request codes starting with this. This must be greater than all activity
179 * request codes used internally.
180 */
181 protected static final int REQUEST_LAST = 100;
182
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 // Type: int
184 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700185 // Type: int
186 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700187 // Type: PendingRequestArgs
188 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
189 // Type: ActivityResultInfo
190 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700191 // Type: SparseArray<Parcelable>
192 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
vadimt00d42552018-12-11 17:59:36 -0800193 public static final String ON_CREATE_EVT = "Launcher.onCreate";
vadimtcb863752018-12-19 17:44:57 -0800194 private static final String ON_START_EVT = "Launcher.onStart";
195 private static final String ON_RESUME_EVT = "Launcher.onResume";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700197 private LauncherStateManager mStateManager;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700198
Adam Cohenad4e15c2013-10-17 16:21:35 -0700199 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200
Winson Chunga2413752012-04-03 14:22:34 -0700201 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700202 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
203 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
204 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700205
Jon Miranda54441f52018-01-24 15:38:25 -0800206 private LauncherAppTransitionManager mAppTransitionManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800207 private Configuration mOldConfig;
Jon Miranda54441f52018-01-24 15:38:25 -0800208
Adam Cohen091440a2015-03-18 14:16:05 -0700209 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700210 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700211 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800212 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700213
Sunny Goyalffe83f12014-08-14 17:39:34 -0700214 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700215 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700216
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700217 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700218
Sunny Goyal316490e2015-06-02 09:38:28 -0700219 @Thunk Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700220
Sunny Goyal47328fd2016-05-25 18:56:41 -0700221 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700222
223 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700224 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700225 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700226
Winson Chung8ae41982017-11-10 11:42:13 -0800227 // UI and state for the overview panel
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800228 private View mOverviewPanel;
Winson Chung8ae41982017-11-10 11:42:13 -0800229
Adam Cohen091440a2015-03-18 14:16:05 -0700230 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400231
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700232 private OnResumeCallback mOnResumeCallback;
233
Sunny Goyal527c7d32015-08-28 15:19:36 -0700234 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700235
Joe Onorato9c1289c2009-08-17 11:03:03 -0400236 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800237 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800238 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700239 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400240
Tony Wickham010d2552017-01-20 08:15:28 -0800241 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700242
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800243 private int mSynchronouslyBoundPage = PagedView.INVALID_PAGE;
Adam Cohen1462de32012-07-24 22:34:36 -0700244
Winson Chung46353de2012-02-16 14:05:10 -0800245 // We only want to get the SharedPreferences once since it does an FS stat each time we get
246 // it from the context.
247 private SharedPreferences mSharedPrefs;
248
Sunny Goyal2100c782016-08-22 16:00:03 -0700249 // Activity result which needs to be processed after workspace has loaded.
250 private ActivityResultInfo mPendingActivityResult;
251 /**
252 * Holds extra information required to handle a result from an external call, like
253 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
254 */
255 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800256
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700257 public ViewGroupFocusHelper mFocusHandler;
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700258
Sunny Goyal623eddd2018-03-02 12:24:41 -0800259 private RotationHelper mRotationHelper;
Sunny Goyal7779d622015-06-11 16:18:39 -0700260
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700261
262 private final Handler mHandler = new Handler();
263 private final Runnable mLogOnDelayedResume = this::logOnDelayedResume;
264
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800265 @Override
266 protected void onCreate(Bundle savedInstanceState) {
vadimt00d42552018-12-11 17:59:36 -0800267 RaceConditionTracker.onEvent(ON_CREATE_EVT, ENTER);
Winson Chunga2413752012-04-03 14:22:34 -0700268 if (DEBUG_STRICT_MODE) {
269 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
270 .detectDiskReads()
271 .detectDiskWrites()
272 .detectNetwork() // or .detectAll() for all detectable problems
273 .penaltyLog()
274 .build());
275 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
276 .detectLeakedSqlLiteObjects()
277 .detectLeakedClosableObjects()
278 .penaltyLog()
279 .penaltyDeath()
280 .build());
281 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700282 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700283
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800284 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700285 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400286
Sunny Goyal87f784c2017-01-11 10:48:34 -0800287 LauncherAppState app = LauncherAppState.getInstance(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800288 mOldConfig = new Configuration(getResources().getConfiguration());
Sunny Goyal0e7724c2018-02-22 13:22:21 -0800289 mModel = app.setLauncher(this);
Hyunyoung Song31971a32019-01-27 12:31:12 -0800290 InvariantDeviceProfile idp = app.getInvariantDeviceProfile();
291 initDeviceProfile(idp);
292 idp.addOnChangeListener(this);
Sunny Goyalf7258242015-10-19 16:59:07 -0700293 mSharedPrefs = Utilities.getPrefs(this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800294 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700295 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700296
Joe Onorato41a12d22009-10-31 18:30:00 -0400297 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700298 mAllAppsController = new AllAppsTransitionController(this);
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800299 mStateManager = new LauncherStateManager(this);
Tracy Zhou8b23c6f2018-04-30 17:13:27 -0700300 UiFactory.onCreate(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700301
Sunny Goyalffe83f12014-08-14 17:39:34 -0700302 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700303
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700304 mAppWidgetHost = new LauncherAppWidgetHost(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700305 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700306
Sunny Goyal60820d72017-05-09 12:40:11 -0700307 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700308
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800309 setupViews();
Tony Wickham010d2552017-01-20 08:15:28 -0800310 mPopupDataProvider = new PopupDataProvider(this);
311
Sunny Goyal623eddd2018-03-02 12:24:41 -0800312 mRotationHelper = new RotationHelper(this);
Sunny Goyal90548432018-04-04 17:17:00 -0700313 mAppTransitionManager = LauncherAppTransitionManager.newInstance(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800314
Sunny Goyald3864fa2017-11-14 15:06:36 -0800315 boolean internalStateHandled = InternalStateHandler.handleCreate(this, getIntent());
316 if (internalStateHandled) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800317 if (savedInstanceState != null) {
318 // InternalStateHandler has already set the appropriate state.
319 // We dont need to do anything.
320 savedInstanceState.remove(RUNTIME_STATE);
321 }
322 }
323 restoreState(savedInstanceState);
Winson Chungb63b44c2017-11-10 17:54:44 -0800324
Sunny Goyal2100c782016-08-22 16:00:03 -0700325 // We only load the page synchronously if the user rotates (or triggers a
326 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700327 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
328 if (savedInstanceState != null) {
329 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
330 }
Sunny Goyald3864fa2017-11-14 15:06:36 -0800331
Sunny Goyalfe770c92016-09-27 14:38:58 -0700332 if (!mModel.startLoader(currentScreen)) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800333 if (!internalStateHandled) {
334 // If we are not binding synchronously, show a fade in animation when
335 // the first page bind completes.
Sunny Goyal6001ea22018-05-10 16:31:00 -0700336 mDragLayer.getAlphaProperty(ALPHA_INDEX_LAUNCHER_LOAD).setValue(0);
Sunny Goyald3864fa2017-11-14 15:06:36 -0800337 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700338 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700339 // Pages bound synchronously.
340 mWorkspace.setCurrentPage(currentScreen);
341
Sunny Goyal2100c782016-08-22 16:00:03 -0700342 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800343 }
344
345 // For handling default keys
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800346 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800347
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800348 setContentView(mLauncherView);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800349 getRootView().dispatchInsets();
Mario Bertschlera6936942017-05-31 14:48:19 -0700350
Jon Miranda7fda2852017-07-19 16:07:01 -0700351 // Listen for broadcasts
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700352 registerReceiver(mScreenOffReceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));
Sunny Goyala616d2b2017-06-12 13:54:01 -0700353
Sunny Goyal8392c822017-06-20 10:03:56 -0700354 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
355 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700356
357 if (mLauncherCallbacks != null) {
358 mLauncherCallbacks.onCreate(savedInstanceState);
359 }
Sunny Goyal623eddd2018-03-02 12:24:41 -0800360 mRotationHelper.initialize();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700361
362 TraceHelper.endSection("Launcher-onCreate");
vadimt00d42552018-12-11 17:59:36 -0800363 RaceConditionTracker.onEvent(ON_CREATE_EVT, EXIT);
Adam Cohen9211d422014-10-07 18:14:53 -0700364 }
365
Sunny Goyal7779d622015-06-11 16:18:39 -0700366 @Override
Winson Chung8eb49e02018-07-16 13:33:31 -0700367 public void onEnterAnimationComplete() {
368 super.onEnterAnimationComplete();
369 UiFactory.onEnterAnimationComplete(this);
arangelovad783052018-08-17 14:46:45 +0100370 mAllAppsController.highlightWorkTabIfNecessary();
Winson Chung8eb49e02018-07-16 13:33:31 -0700371 }
372
373 @Override
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800374 public void onConfigurationChanged(Configuration newConfig) {
375 int diff = newConfig.diff(mOldConfig);
Jon Mirandaaeb4dd02018-07-11 14:16:23 -0700376
377 if ((diff & CONFIG_LOCALE) != 0) {
378 Folder.setLocaleDependentFields(getResources(), true /* force */);
379 }
380
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800381 if ((diff & (CONFIG_ORIENTATION | CONFIG_SCREEN_SIZE)) != 0) {
382 mUserEventDispatcher = null;
383 initDeviceProfile(mDeviceProfile.inv);
Sunny Goyalfde55052018-02-01 14:46:13 -0800384 dispatchDeviceProfileChanged();
Sunny Goyal59d086c2018-05-07 17:31:40 -0700385 reapplyUi();
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700386 mDragLayer.recreateControllers();
Sunny Goyal7185dd62018-03-14 17:51:49 -0700387
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800388 // TODO: We can probably avoid rebind when only screen size changed.
Sunny Goyal605bcf32018-03-02 15:16:12 -0800389 rebindModel();
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800390 }
391
392 mOldConfig.setTo(newConfig);
Tracy Zhoua706f002018-03-28 13:55:19 -0700393 UiFactory.onLauncherStateOrResumeChanged(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800394 super.onConfigurationChanged(newConfig);
395 }
396
Sunny Goyal605bcf32018-03-02 15:16:12 -0800397 @Override
Sunny Goyal59d086c2018-05-07 17:31:40 -0700398 protected void reapplyUi() {
399 getRootView().dispatchInsets();
400 getStateManager().reapplyState(true /* cancelCurrentAnimation */);
401 }
402
403 @Override
Sunny Goyal605bcf32018-03-02 15:16:12 -0800404 public void rebindModel() {
405 int currentPage = mWorkspace.getNextPage();
406 if (mModel.startLoader(currentPage)) {
407 mWorkspace.setCurrentPage(currentPage);
408 setWorkspaceLoading(true);
409 }
410 }
411
Hyunyoung Song31971a32019-01-27 12:31:12 -0800412 @Override
413 public void onIdpChanged(int changeFlags, InvariantDeviceProfile idp) {
414 initDeviceProfile(idp);
415 getRootView().dispatchInsets();
416 }
417
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800418 private void initDeviceProfile(InvariantDeviceProfile idp) {
419 // Load configuration-specific DeviceProfile
Sunny Goyal59d086c2018-05-07 17:31:40 -0700420 mDeviceProfile = idp.getDeviceProfile(this);
Sunny Goyal8c48d8b2019-01-25 15:10:18 -0800421 if (isInMultiWindowMode()) {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700422 Display display = getWindowManager().getDefaultDisplay();
423 Point mwSize = new Point();
424 display.getSize(mwSize);
425 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
426 }
427 onDeviceProfileInitiated();
Sunny Goyal605bcf32018-03-02 15:16:12 -0800428 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout(), true);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800429 }
430
Sunny Goyal623eddd2018-03-02 12:24:41 -0800431 public RotationHelper getRotationHelper() {
432 return mRotationHelper;
433 }
434
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700435 public LauncherStateManager getStateManager() {
436 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700437 }
438
Mario Bertschler27288382017-05-24 15:35:09 -0700439 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700440 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800441 return mLauncherView.findViewById(id);
442 }
443
444 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700445 public void onAppWidgetHostReset() {
446 if (mAppWidgetHost != null) {
447 mAppWidgetHost.startListening();
448 }
449 }
450
Adam Cohen9211d422014-10-07 18:14:53 -0700451 private LauncherCallbacks mLauncherCallbacks;
452
Sunny Goyal32554d12015-12-03 15:31:25 -0800453 /**
454 * Call this after onCreate to set or clear overlay.
455 */
456 public void setLauncherOverlay(LauncherOverlay overlay) {
457 if (overlay != null) {
458 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
459 }
460 mWorkspace.setLauncherOverlay(overlay);
461 }
462
Adam Cohen9211d422014-10-07 18:14:53 -0700463 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
464 mLauncherCallbacks = callbacks;
465 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700466 }
467
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700468 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700469 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700470 if (mLauncherCallbacks != null) {
471 mLauncherCallbacks.onLauncherProviderChange();
472 }
473 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700474
Hyunyoung Song3f471442015-04-08 19:01:34 -0700475 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700476 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
477 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700478 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700479 }
480
Tony Wickham010d2552017-01-20 08:15:28 -0800481 public PopupDataProvider getPopupDataProvider() {
482 return mPopupDataProvider;
483 }
484
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700485 @Override
Tony Wickhamf34bee82018-12-03 18:11:39 -0800486 public DotInfo getDotInfoForItem(ItemInfo info) {
487 return mPopupDataProvider.getDotInfoForItem(info);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700488 }
489
490 @Override
491 public void invalidateParent(ItemInfo info) {
492 FolderIconPreviewVerifier verifier = new FolderIconPreviewVerifier(getDeviceProfile().inv);
493 if (verifier.isItemInPreview(info.rank) && (info.container >= 0)) {
494 View folderIcon = getWorkspace().getHomescreenIconByItemId(info.container);
495 if (folderIcon != null) {
496 folderIcon.invalidate();
497 }
498 }
499 }
500
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000501 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700502 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
503 * a configuration step, this allows the proper animations to run after other transitions.
504 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700505 private int completeAdd(
Sunny Goyal2100c782016-08-22 16:00:03 -0700506 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
Sunny Goyalefb7e842018-10-04 15:11:00 -0700507 int screenId = info.screenId;
Sunny Goyal2100c782016-08-22 16:00:03 -0700508 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700509 // When the screen id represents an actual screen (as opposed to a rank) we make sure
510 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700511 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700512 }
Adam Cohendb364c32014-05-20 14:23:40 -0700513
Sunny Goyal2100c782016-08-22 16:00:03 -0700514 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800515 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700516 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700517 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800518 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700519 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700520 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700521 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700522 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700523 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700524 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700525 int widgetId = appWidgetId;
526 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700527 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700528 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700529 // Since the view was just bound, also launch the configure activity if needed
530 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
531 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800532 if (provider != null) {
533 new WidgetAddFlowHandler(provider)
534 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700535 }
536 }
537 break;
538 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800539 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700540
Adam Cohendb364c32014-05-20 14:23:40 -0700541 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800542 }
543
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800544 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700545 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700546 if (isWorkspaceLoading()) {
547 // process the result once the workspace has loaded.
548 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
549 return;
550 }
551 mPendingActivityResult = null;
552
Winson Chunge341d302013-08-16 14:31:00 -0700553 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700554 final PendingRequestArgs requestArgs = mPendingRequestArgs;
555 setWaitingForResult(null);
556 if (requestArgs == null) {
557 return;
558 }
559
560 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700561
Adam Cohenad4e15c2013-10-17 16:21:35 -0700562 Runnable exitSpringLoaded = new Runnable() {
563 @Override
564 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700565 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700566 }
567 };
568
Michael Jurka8b805b12012-04-18 14:23:14 -0700569 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700570 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700571 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700572 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
573 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700574 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700575 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700576 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700577 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700578 addAppWidgetImpl(
579 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800580 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700581 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700582 }
583 return;
584 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200585
Adam Cohened66b2b2012-01-23 17:28:51 -0800586 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700587 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700588
Adam Cohened66b2b2012-01-23 17:28:51 -0800589 // We have special handling for widgets
590 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800591 final int appWidgetId;
592 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
593 : -1;
594 if (widgetId < 0) {
595 appWidgetId = pendingAddWidgetId;
596 } else {
597 appWidgetId = widgetId;
598 }
599
Adam Cohenad4e15c2013-10-17 16:21:35 -0700600 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800601 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700602 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
603 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700604 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700605 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700606 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700607 @Override
608 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700609 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700610 }
611 };
Adam Cohendb364c32014-05-20 14:23:40 -0700612
Sunny Goyal2100c782016-08-22 16:00:03 -0700613 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
614 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
615 } else {
616 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
617 // When the screen id represents an actual screen (as opposed to a rank)
618 // we make sure that the drop page actually exists.
619 requestArgs.screenId =
620 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700621 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700622 final CellLayout dropLayout =
623 mWorkspace.getScreenWithId(requestArgs.screenId);
624
625 dropLayout.setDropPending(true);
626 final Runnable onComplete = new Runnable() {
627 @Override
628 public void run() {
629 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
630 dropLayout.setDropPending(false);
631 }
632 };
633 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
634 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700635 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800636 return;
637 }
638
Sunny Goyald478c832016-04-01 12:04:16 -0700639 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
640 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700641 if (resultCode == RESULT_OK) {
642 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700643 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700644 }
645 // Leave the widget in the pending state if the user canceled the configure.
646 return;
647 }
648
Sunny Goyalaad90582015-07-09 14:22:50 -0700649 if (requestCode == REQUEST_CREATE_SHORTCUT) {
650 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700651 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
652 completeAdd(requestCode, data, -1, requestArgs);
653 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
654 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
655
Sunny Goyalaad90582015-07-09 14:22:50 -0700656 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700657 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
658 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800659 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800661 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800662 }
663
664 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700665 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800666 final int requestCode, final int resultCode, final Intent data) {
667 handleActivityResult(requestCode, resultCode, data);
668 if (mLauncherCallbacks != null) {
669 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
670 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800671 }
672
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700673 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700674 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600675 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700676 PendingRequestArgs pendingArgs = mPendingRequestArgs;
677 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
678 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
679 setWaitingForResult(null);
680
Sunny Goyal28c6b962015-10-12 11:42:05 -0700681 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700682 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700683 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700684 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700685 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700686 Intent intent = pendingArgs.getPendingIntent();
687
Sunny Goyal28c6b962015-10-12 11:42:05 -0700688 if (grantResults.length > 0
689 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700690 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700691 } else {
692 // TODO: Show a snack bar with link to settings
693 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700694 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700695 }
696 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600697 if (mLauncherCallbacks != null) {
698 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
699 grantResults);
700 }
701 }
702
Adam Cohendb364c32014-05-20 14:23:40 -0700703 /**
704 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
705 *
706 * @param screenId the screen id to check
707 * @return the new screen, or screenId if it exists
708 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700709 private int ensurePendingDropLayoutExists(int screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800710 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700711 if (dropLayout == null) {
712 // it's possible that the add screen was removed because it was
713 // empty and a re-bind occurred
714 mWorkspace.addExtraEmptyScreen();
715 return mWorkspace.commitExtraEmptyScreen();
716 } else {
717 return screenId;
718 }
719 }
720
Sunny Goyal2100c782016-08-22 16:00:03 -0700721 @Thunk void completeTwoStageWidgetDrop(
722 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
723 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800724 Runnable onCompleteRunnable = null;
725 int animationType = 0;
726
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700727 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800728 if (resultCode == RESULT_OK) {
729 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
730 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800731 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700732 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800733 onCompleteRunnable = new Runnable() {
734 @Override
735 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700736 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700737 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800738 }
739 };
740 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800741 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800742 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800743 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700744 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700745 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700746 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
747 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700748 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700749 // The animated view may be null in the case of a rotation during widget configuration
750 onCompleteRunnable.run();
751 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800752 }
753
754 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700755 protected void onStop() {
756 super.onStop();
Adam Cohen9211d422014-10-07 18:14:53 -0700757 if (mLauncherCallbacks != null) {
758 mLauncherCallbacks.onStop();
759 }
Hyunyoung Song1241e612018-05-15 21:46:51 -0700760 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
761 mStateManager.getState().containerType, -1);
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700762
763 mAppWidgetHost.setListenIfResumed(false);
764
Tony Wickham010d2552017-01-20 08:15:28 -0800765 NotificationListener.removeNotificationsChangedListener();
Sunny Goyalaf161fd2018-02-05 16:34:57 -0800766 getStateManager().moveToRestState();
Winson Chungedb0d6b2018-03-21 17:32:55 -0700767
Tracy Zhou62646712018-06-26 14:19:02 -0700768 UiFactory.onLauncherStateOrResumeChanged(this);
769
Winson Chungdea51352018-04-24 13:02:10 -0700770 // Workaround for b/78520668, explicitly trim memory once UI is hidden
Winson Chung473730f2018-04-26 11:54:22 -0700771 onTrimMemory(TRIM_MEMORY_UI_HIDDEN);
Michael Jurkacd496d72013-04-11 11:32:45 -0700772 }
773
774 @Override
775 protected void onStart() {
vadimtcb863752018-12-19 17:44:57 -0800776 RaceConditionTracker.onEvent(ON_START_EVT, ENTER);
Michael Jurkacd496d72013-04-11 11:32:45 -0700777 super.onStart();
Adam Cohen9211d422014-10-07 18:14:53 -0700778 if (mLauncherCallbacks != null) {
779 mLauncherCallbacks.onStart();
780 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800781 mAppWidgetHost.setListenIfResumed(true);
Sunny Goyalcac002d2018-01-04 09:51:45 -0800782 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
vadimtcb863752018-12-19 17:44:57 -0800783 RaceConditionTracker.onEvent(ON_START_EVT, EXIT);
Michael Jurkacd496d72013-04-11 11:32:45 -0700784 }
785
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700786 private void logOnDelayedResume() {
787 if (hasBeenResumed()) {
788 getUserEventDispatcher().logActionCommand(Action.Command.RESUME,
789 mStateManager.getState().containerType, -1);
790 getUserEventDispatcher().startSession();
791 }
792 }
793
Michael Jurkacd496d72013-04-11 11:32:45 -0700794 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800795 protected void onResume() {
vadimtcb863752018-12-19 17:44:57 -0800796 RaceConditionTracker.onEvent(ON_RESUME_EVT, ENTER);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700797 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800798 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700799 TraceHelper.partitionSection("ON_RESUME", "superCall");
800
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700801 mHandler.removeCallbacks(mLogOnDelayedResume);
802 Utilities.postAsyncCallback(mHandler, mLogOnDelayedResume);
803
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700804 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700805 // Process any items that were added while Launcher was away.
806 InstallShortcutReceiver.disableAndFlushInstallQueue(
807 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700808
Sunny Goyala474a9b2017-05-04 16:47:11 -0700809 // Refresh shortcuts if the permission changed.
810 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700811
Tracy Zhoue5575f92018-04-19 11:12:53 -0700812 DiscoveryBounce.showForHomeIfNeeded(this);
Adam Cohen9211d422014-10-07 18:14:53 -0700813 if (mLauncherCallbacks != null) {
814 mLauncherCallbacks.onResume();
815 }
Tracy Zhoua706f002018-03-28 13:55:19 -0700816 UiFactory.onLauncherStateOrResumeChanged(this);
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800817
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700818 TraceHelper.endSection("ON_RESUME");
vadimtcb863752018-12-19 17:44:57 -0800819 RaceConditionTracker.onEvent(ON_RESUME_EVT, EXIT);
Adam Cohen06dff352012-06-01 17:17:08 -0700820 }
821
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800822 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700823 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700824 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700825 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700826
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700827 super.onPause();
Joe Onorato24b6fd82009-11-12 13:47:09 -0800828 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700829 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700830
Adam Cohen9211d422014-10-07 18:14:53 -0700831 if (mLauncherCallbacks != null) {
832 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700833 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700834 }
835
Winson Chungd7823942018-02-16 03:23:47 +0000836 @Override
Tracy Zhoua706f002018-03-28 13:55:19 -0700837 protected void onUserLeaveHint() {
838 super.onUserLeaveHint();
839 UiFactory.onLauncherStateOrResumeChanged(this);
840 }
841
842 @Override
Winson Chungd7823942018-02-16 03:23:47 +0000843 public void onWindowFocusChanged(boolean hasFocus) {
844 super.onWindowFocusChanged(hasFocus);
845 mStateManager.onWindowFocusChanged();
846 }
847
Adam Cohenc2d6e892014-10-16 09:49:24 -0700848 public interface LauncherOverlay {
849
850 /**
851 * Touch interaction leading to overscroll has begun
852 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700853 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700854
855 /**
856 * Touch interaction related to overscroll has ended
857 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700858 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700859
860 /**
861 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
862 * screen (or in the case of RTL, the rightmost screen).
863 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700864 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700865
866 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800867 * Called when the launcher is ready to use the overlay
868 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700869 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700870 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700871 }
872
873 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700874 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700875 }
876
877 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
878
Sunny Goyalc86df472016-02-25 09:19:38 -0800879 public void onScrollChanged(float progress) {
880 if (mWorkspace != null) {
881 mWorkspace.onOverlayScrollChanged(progress);
882 }
883 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700884 }
885
Sunny Goyalf9403d92017-10-18 10:55:56 -0700886 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -0700887 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700888 }
889
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800890 /**
891 * Restores the previous state, if it exists.
892 *
893 * @param savedState The previous state.
894 */
895 private void restoreState(Bundle savedState) {
896 if (savedState == null) {
897 return;
898 }
899
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700900 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -0700901 LauncherState[] stateValues = LauncherState.values();
902 LauncherState state = stateValues[stateOrdinal];
Sunny Goyalbc683e92017-12-06 10:25:07 -0800903 if (!state.disableRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700904 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800905 }
906
Sunny Goyal2100c782016-08-22 16:00:03 -0700907 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
908 if (requestArgs != null) {
909 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800910 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700911
912 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700913
914 SparseArray<Parcelable> widgetsState =
915 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
916 if (widgetsState != null) {
917 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
918 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800919 }
920
921 /**
922 * Finds all the views we need and configure them properly.
923 */
924 private void setupViews() {
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800925 mDragLayer = findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700926 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -0700927 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700928 mWorkspace.initParentViews(mDragLayer);
Sunny Goyal16764582017-11-06 16:00:34 -0800929 mOverviewPanel = findViewById(R.id.overview_panel);
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800930 mHotseat = findViewById(R.id.hotseat);
Craig Mautner360310b2012-10-26 15:13:08 -0700931
Sunny Goyal784f9c32016-03-23 16:56:54 -0700932 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
933 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
934 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800935
Winson Chung4c98d922011-05-31 16:50:48 -0700936 // Setup the drag layer
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700937 mDragLayer.setup(mDragController, mWorkspace);
938 UiFactory.setOnTouchControllersChangedListener(this, mDragLayer::recreateControllers);
Winson Chung4c98d922011-05-31 16:50:48 -0700939
Hyunyoung Song645764e2016-06-06 14:19:02 -0700940 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -0700941 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
942 // default state, otherwise we will update to the wrong offsets in RTL
943 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700944 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700945 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700946
Tony Wickham34d2c912015-09-11 09:27:58 -0700947 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700948 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700949
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700950 // Setup Apps
951 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -0700952
Winson Chung3d503fb2011-07-13 17:25:49 -0700953 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -0700954 mDragController.setMoveTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700955 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -0400956
Sunny Goyal7185dd62018-03-14 17:51:49 -0700957 mAllAppsController.setupViews(mAppsView);
Sunny Goyalab770a12018-11-14 15:17:26 -0800958 mHotseat.setOnInterceptTouchListener(mWorkspace::onInterceptHotseatTouch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800959 }
960
961 /**
962 * Creates a view representing a shortcut.
963 *
964 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800965 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800966 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700967 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800968 }
969
970 /**
971 * Creates a view representing a shortcut inflated from the specified resource.
972 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800973 * @param parent The group the shortcut belongs to.
974 * @param info The data structure describing the shortcut.
975 *
976 * @return A View inflated from layoutResId.
977 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700978 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -0800979 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
980 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800981 favorite.applyFromShortcutInfo(info);
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800982 favorite.setOnClickListener(ItemClickHandler.INSTANCE);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700983 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800984 return favorite;
985 }
986
987 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -0700988 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800989 *
990 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800991 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700992 private void completeAddShortcut(Intent data, int container, int screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700993 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -0700994 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
995 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700996 return;
997 }
Adam Cohen558baaf2011-08-15 15:22:57 -0700998
Jon Mirandac476d6e2017-05-04 16:30:01 -0700999 int[] cellXY = mTmpAddItemCellCoordinates;
1000 CellLayout layout = getCellLayout(container, screenId);
1001
Sunny Goyal782f0c92017-01-19 10:27:54 -08001002 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001003 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001004 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
1005 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001006 }
1007
1008 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001009 // Legacy shortcuts are only supported for primary profile.
1010 info = Process.myUserHandle().equals(args.user)
1011 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001012
Sunny Goyalb57645f2017-03-20 13:57:28 -07001013 if (info == null) {
1014 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1015 return;
1016 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001017 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001018 // The app is trying to add a shortcut without sufficient permissions
1019 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1020 return;
1021 }
1022 }
1023
Jon Mirandac476d6e2017-05-04 16:30:01 -07001024 if (container < 0) {
1025 // Adding a shortcut to the Workspace.
1026 final View view = createShortcut(info);
1027 boolean foundCellSpan = false;
1028 // First we check if we already know the exact location where we want to add this item.
1029 if (cellX >= 0 && cellY >= 0) {
1030 cellXY[0] = cellX;
1031 cellXY[1] = cellY;
1032 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001033
Jon Mirandac476d6e2017-05-04 16:30:01 -07001034 // If appropriate, either create a folder or add to an existing folder
1035 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001036 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001037 return;
1038 }
1039 DragObject dragObject = new DragObject();
1040 dragObject.dragInfo = info;
1041 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1042 true)) {
1043 return;
1044 }
1045 } else {
1046 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1047 }
1048
1049 if (!foundCellSpan) {
1050 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001051 return;
1052 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001053
1054 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1055 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001056 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001057 // Adding a shortcut to a Folder.
Sunny Goyale29897f52017-07-20 10:09:42 -07001058 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001059 if (folderIcon != null) {
1060 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1061 folderInfo.add(info, args.rank, false);
1062 } else {
Sunny Goyale29897f52017-07-20 10:09:42 -07001063 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001064 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001065 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001066 }
1067
Sunny Goyalefb7e842018-10-04 15:11:00 -07001068 public FolderIcon findFolderIcon(final int folderIconId) {
Sunny Goyal83fd25e2018-07-09 16:47:01 -07001069 return (FolderIcon) mWorkspace.getHomescreenIconByItemId(folderIconId);
Sunny Goyale29897f52017-07-20 10:09:42 -07001070 }
1071
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001072 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001073 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001074 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001075 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001076 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001077 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001078 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1079
Adam Cohened66b2b2012-01-23 17:28:51 -08001080 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001081 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001082 }
Romain Guycbb89e42009-06-08 15:52:54 -07001083
Adam Cohen59400422014-03-05 18:07:04 -08001084 LauncherAppWidgetInfo launcherInfo;
1085 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001086 launcherInfo.spanX = itemInfo.spanX;
1087 launcherInfo.spanY = itemInfo.spanY;
1088 launcherInfo.minSpanX = itemInfo.minSpanX;
1089 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001090 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001091
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001092 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001093 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001094
Sunny Goyal2100c782016-08-22 16:00:03 -07001095 if (hostView == null) {
1096 // Perform actual inflation because we're live
1097 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001098 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001099 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301100 prepareAppWidget(hostView, launcherInfo);
1101 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001102 }
Romain Guycbb89e42009-06-08 15:52:54 -07001103
Sunny Goyald5462aa2016-11-22 16:52:39 +05301104 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001105 hostView.setTag(item);
1106 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001107 hostView.setFocusable(true);
1108 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001109 }
1110
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001111 private final BroadcastReceiver mScreenOffReceiver = new BroadcastReceiver() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001112 @Override
1113 public void onReceive(Context context, Intent intent) {
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001114 // Reset AllApps to its initial state only if we are not in the middle of
1115 // processing a multi-step drop
1116 if (mPendingRequestArgs == null) {
1117 mStateManager.goToState(NORMAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001118 }
1119 }
1120 };
1121
Sunny Goyalaae6fbb2019-01-31 16:05:58 -08001122 public void updateNotificationDots(Predicate<PackageUserKey> updatedDots) {
Tony Wickhamf34bee82018-12-03 18:11:39 -08001123 mWorkspace.updateNotificationDots(updatedDots);
1124 mAppsView.getAppsStore().updateNotificationDots(updatedDots);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001125
Sunny Goyal29947f02017-12-18 13:49:44 -08001126 PopupContainerWithArrow popup = PopupContainerWithArrow.getOpen(Launcher.this);
1127 if (popup != null) {
Tony Wickhamf34bee82018-12-03 18:11:39 -08001128 popup.updateNotificationHeader(updatedDots);
Tony Wickham010d2552017-01-20 08:15:28 -08001129 }
1130 }
1131
Adam Cohended9f8d2010-11-03 13:25:16 -07001132 @Override
1133 public void onAttachedToWindow() {
1134 super.onAttachedToWindow();
1135
Sunny Goyalc86df472016-02-25 09:19:38 -08001136 if (mLauncherCallbacks != null) {
1137 mLauncherCallbacks.onAttachedToWindow();
1138 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001139 }
1140
1141 @Override
1142 public void onDetachedFromWindow() {
1143 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001144
1145 if (mLauncherCallbacks != null) {
1146 mLauncherCallbacks.onDetachedFromWindow();
1147 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001148 }
1149
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001150 public AllAppsTransitionController getAllAppsController() {
1151 return mAllAppsController;
1152 }
1153
Sunny Goyal9d69c8d2018-03-19 13:41:31 -07001154 @Override
Winson Chung1a341002018-01-17 10:00:23 -08001155 public LauncherRootView getRootView() {
1156 return (LauncherRootView) mLauncherView;
1157 }
1158
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001159 @Override
Winson Chunga6945242014-01-08 14:04:34 -08001160 public DragLayer getDragLayer() {
1161 return mDragLayer;
1162 }
1163
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001164 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001165 return mAppsView;
1166 }
1167
Winson Chunga6945242014-01-08 14:04:34 -08001168 public Workspace getWorkspace() {
1169 return mWorkspace;
1170 }
1171
1172 public Hotseat getHotseat() {
1173 return mHotseat;
1174 }
1175
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001176 public <T extends View> T getOverviewPanel() {
Winson Chung8ae41982017-11-10 11:42:13 -08001177 return (T) mOverviewPanel;
Winson Chunga6945242014-01-08 14:04:34 -08001178 }
1179
Sunny Goyal47328fd2016-05-25 18:56:41 -07001180 public DropTargetBar getDropTargetBar() {
1181 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001182 }
1183
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001184 public LauncherAppWidgetHost getAppWidgetHost() {
1185 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001186 }
Romain Guycbb89e42009-06-08 15:52:54 -07001187
Winson Chunga9abd0e2010-10-27 17:18:37 -07001188 public LauncherModel getModel() {
1189 return mModel;
1190 }
1191
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001192 public ModelWriter getModelWriter() {
1193 return mModelWriter;
1194 }
1195
Adam Cohen79d90c52016-04-22 13:29:20 -07001196 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001197 return mSharedPrefs;
1198 }
1199
Sunny Goyalf8d56fc2018-01-31 15:18:11 -08001200 public int getOrientation() { return mOldConfig.orientation; }
Jon Miranda6bed3502017-09-19 14:43:17 -07001201
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001202 @Override
1203 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001204 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001205 super.onNewIntent(intent);
1206
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001207 boolean alreadyOnHome = hasWindowFocus() && ((intent.getFlags() &
Winson15f8b172015-08-19 11:02:39 -07001208 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1209 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001210
1211 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001212 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001213 && AbstractFloatingView.getTopOpenView(this) == null;
Winson15f8b172015-08-19 11:02:39 -07001214 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001215 boolean internalStateHandled = InternalStateHandler
Sunny Goyal8b264562018-02-03 23:00:13 -08001216 .handleNewIntent(this, intent, isStarted());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001217
Winson15f8b172015-08-19 11:02:39 -07001218 if (isActionMain) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001219 if (!internalStateHandled) {
1220 // Note: There should be at most one log per method call. This is enforced
1221 // implicitly by using if-else statements.
1222 UserEventDispatcher ued = getUserEventDispatcher();
1223 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
1224 if (topOpenView != null) {
1225 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
1226 } else if (alreadyOnHome) {
1227 Target target = newContainerTarget(mStateManager.getState().containerType);
1228 target.pageIndex = mWorkspace.getCurrentPage();
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001229 ued.logActionCommand(Action.Command.HOME_INTENT, target,
1230 newContainerTarget(ContainerType.WORKSPACE));
Sunny Goyald3864fa2017-11-14 15:06:36 -08001231 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001232
Sunny Goyald3864fa2017-11-14 15:06:36 -08001233 // In all these cases, only animate if we're already on home
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001234 AbstractFloatingView.closeAllOpenViews(this, isStarted());
Jon Mirandafeba90f2016-10-06 10:53:29 -07001235
Sunny Goyala2467272018-03-16 14:53:05 -07001236 if (!isInState(NORMAL)) {
1237 // Only change state, if not already the same. This prevents cancelling any
1238 // animations running as part of resume
1239 mStateManager.goToState(NORMAL);
1240 }
Sunny Goyald3864fa2017-11-14 15:06:36 -08001241
1242 // Reset the apps view
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001243 if (!alreadyOnHome) {
Sunny Goyal7185dd62018-03-14 17:51:49 -07001244 mAppsView.reset(isStarted() /* animate */);
Sunny Goyald3864fa2017-11-14 15:06:36 -08001245 }
1246
Sunny Goyalaad33592018-08-07 17:20:35 -07001247 if (shouldMoveToDefaultScreen && !mWorkspace.isHandlingTouch()) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001248 mWorkspace.post(mWorkspace::moveToDefaultScreen);
1249 }
1250 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001251
1252 final View v = getWindow().peekDecorView();
1253 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001254 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001255 }
1256
Adam Cohen9211d422014-10-07 18:14:53 -07001257 if (mLauncherCallbacks != null) {
Sunny Goyal85462132018-04-24 11:39:37 -07001258 mLauncherCallbacks.onHomeIntent(internalStateHandled);
Adam Cohen9211d422014-10-07 18:14:53 -07001259 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001260 }
Winson15f8b172015-08-19 11:02:39 -07001261
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001262 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001263 }
1264
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001265 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001266 public void onRestoreInstanceState(Bundle state) {
1267 super.onRestoreInstanceState(state);
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001268 mWorkspace.restoreInstanceStateForChild(mSynchronouslyBoundPage);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001269 }
1270
1271 @Override
1272 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001273 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001274 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001275
Adam Cohen21cd0022013-10-09 18:57:02 -07001276 }
Sunny Goyalea609262017-10-25 15:47:38 -07001277 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001278
1279
1280 AbstractFloatingView widgets = AbstractFloatingView
1281 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1282 if (widgets != null) {
1283 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1284 widgets.saveHierarchyState(widgetsState);
1285 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1286 } else {
1287 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1288 }
1289
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001290 // We close any open folders and shortcut containers since they will not be re-opened,
1291 // and we need to make sure this state is reflected.
1292 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001293
Sunny Goyal2100c782016-08-22 16:00:03 -07001294 if (mPendingRequestArgs != null) {
1295 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1296 }
1297 if (mPendingActivityResult != null) {
1298 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001299 }
1300
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001301 super.onSaveInstanceState(outState);
1302
Adam Cohen9211d422014-10-07 18:14:53 -07001303 if (mLauncherCallbacks != null) {
1304 mLauncherCallbacks.onSaveInstanceState(outState);
1305 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001306 }
1307
1308 @Override
1309 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001310 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001311
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001312 unregisterReceiver(mScreenOffReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001313 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001314
Tony Wickhama0068302018-04-11 16:16:11 -07001315 UiFactory.setOnTouchControllersChangedListener(this, null);
1316
Winson Chungcd2b0142011-06-08 16:02:26 -07001317 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001318 // It's possible to receive onDestroy after a new Launcher activity has
1319 // been created. In this case, don't interfere with the new Launcher.
1320 if (mModel.isCurrentCallbacks(this)) {
1321 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001322 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001323 }
Sunny Goyal623eddd2018-03-02 12:24:41 -08001324 mRotationHelper.destroy();
Sunny Goyal745bad92016-05-02 10:54:12 -07001325
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001326 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001327 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001328 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001329 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001330 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001331
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001332 TextKeyListener.getInstance().release();
Tony2917a8b2017-07-31 23:29:42 -07001333 clearPendingBinds();
Hyunyoung Song31971a32019-01-27 12:31:12 -08001334 LauncherAppState.getIDP(this).removeOnChangeListener(this);
Adam Cohen9211d422014-10-07 18:14:53 -07001335 if (mLauncherCallbacks != null) {
1336 mLauncherCallbacks.onDestroy();
1337 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001338 }
1339
Sunny Goyalae502842016-06-17 08:43:56 -07001340 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1341 return mAccessibilityDelegate;
1342 }
1343
Adam Cohena9cf38f2011-05-02 15:36:58 -07001344 public DragController getDragController() {
1345 return mDragController;
1346 }
1347
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001348 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001349 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001350 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001351 }
1352
1353 @Override
1354 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1355 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001356 try {
1357 super.startIntentSenderForResult(intent, requestCode,
1358 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1359 } catch (IntentSender.SendIntentException e) {
1360 throw new ActivityNotFoundException();
1361 }
1362 }
1363
Winson Chung70d72102011-08-12 11:24:35 -07001364 /**
1365 * Indicates that we want global search for this activity by setting the globalSearch
1366 * argument for {@link #startSearch} to true.
1367 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001368 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001369 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001370 Bundle appSearchData, boolean globalSearch) {
1371 if (appSearchData == null) {
1372 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001373 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001374 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001375
Sunny Goyal6f28e712016-09-13 14:19:29 -07001376 if (mLauncherCallbacks == null ||
1377 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1378 // Starting search from the callbacks failed. Start the default global search.
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001379 super.startSearch(initialQuery, selectInitialQuery, appSearchData, true);
Ian Parkinson047036e2014-09-01 15:40:53 +01001380 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001381
1382 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001383 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001384 }
1385
Joe Onorato9c1289c2009-08-17 11:03:03 -04001386 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001387 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001388 }
1389
Adam Cohen517a7f52014-03-01 12:12:59 -08001390 public boolean isWorkspaceLoading() {
1391 return mWorkspaceLoading;
1392 }
1393
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001394 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001395 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001396 }
1397
Sunny Goyal04a324a2017-01-20 21:08:59 -08001398 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001399 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001400 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001401
Sunny Goyal2100c782016-08-22 16:00:03 -07001402 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001403 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001404 if (LOGD) {
1405 Log.d(TAG, "Adding widget from drop");
1406 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001407 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001408 }
1409
Sunny Goyal2100c782016-08-22 16:00:03 -07001410 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001411 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1412 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1413 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001414
Adam Cohenad4e15c2013-10-17 16:21:35 -07001415 Runnable onComplete = new Runnable() {
1416 @Override
1417 public void run() {
1418 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001419 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001420 }
1421 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001422 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001423 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001424 }
1425 }
Romain Guycbb89e42009-06-08 15:52:54 -07001426
Sunny Goyalefb7e842018-10-04 15:11:00 -07001427 public void addPendingItem(PendingAddItemInfo info, int container, int screenId,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001428 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001429 info.container = container;
1430 info.screenId = screenId;
1431 if (cell != null) {
1432 info.cellX = cell[0];
1433 info.cellY = cell[1];
1434 }
1435 info.spanX = spanX;
1436 info.spanY = spanY;
1437
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001438 switch (info.itemType) {
1439 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1440 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001441 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001442 break;
1443 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001444 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001445 break;
1446 default:
1447 throw new IllegalStateException("Unknown item type: " + info.itemType);
1448 }
1449 }
1450
Adam Cohenfbba09b2011-07-18 21:43:05 -07001451 /**
1452 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001453 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001454 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001455 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1456 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001457 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1458 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1459 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001460 }
1461
Adam Cohenfbba09b2011-07-18 21:43:05 -07001462 /**
1463 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001464 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001465 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001466 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001467 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001468 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001469 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001470 // In the case where we've prebound the widget, we remove it from the DragLayer
1471 if (LOGD) {
1472 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1473 }
1474 getDragLayer().removeView(hostView);
1475
Adam Cohened66b2b2012-01-23 17:28:51 -08001476 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001477 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001478
1479 // Clear the boundWidget so that it doesn't get destroyed.
1480 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001481 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001482 // In this case, we either need to start an activity to get permission to bind
1483 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001484 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1485 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1486 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1487 this, info.componentName);
1488 } else {
1489 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1490 }
Adam Cohendd70d662012-10-04 16:53:44 -07001491 Bundle options = info.bindOptions;
1492
Sunny Goyalffe83f12014-08-14 17:39:34 -07001493 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1494 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001495 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001496 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001497 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001498 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001499 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001500 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001501 }
1502
Sunny Goyalefb7e842018-10-04 15:11:00 -07001503 FolderIcon addFolder(CellLayout layout, int container, final int screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001504 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001505 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001506 folderInfo.title = getText(R.string.folder_name);
1507
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001508 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001509 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001510
1511 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001512 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301513 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001514 // Force measure the new folder icon
1515 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1516 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001517 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001518 }
Romain Guycbb89e42009-06-08 15:52:54 -07001519
Winsonc0b52fe2015-09-09 16:38:15 -07001520 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001521 * Unbinds the view for the specified item, and removes the item and all its children.
1522 *
1523 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001524 * @param itemInfo the {@link ItemInfo} for this view.
1525 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001526 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001527 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001528 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001529 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001530 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1531 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001532 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001533 } else {
1534 mWorkspace.removeWorkspaceItem(v);
1535 }
Winsonc0b52fe2015-09-09 16:38:15 -07001536 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001537 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001538 }
1539 } else if (itemInfo instanceof FolderInfo) {
1540 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001541 if (v instanceof FolderIcon) {
1542 ((FolderIcon) v).removeListeners();
1543 }
Winsonc0b52fe2015-09-09 16:38:15 -07001544 mWorkspace.removeWorkspaceItem(v);
1545 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001546 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001547 }
1548 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1549 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001550 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001551 if (deleteFromDb) {
Tony Wickham6a71a5b2018-08-21 11:40:23 -07001552 getModelWriter().deleteWidgetInfo(widgetInfo, getAppWidgetHost());
Winsonc0b52fe2015-09-09 16:38:15 -07001553 }
1554 } else {
1555 return false;
1556 }
1557 return true;
1558 }
1559
Tony Wickham6a71a5b2018-08-21 11:40:23 -07001560
Winsonc0b52fe2015-09-09 16:38:15 -07001561
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001562 @Override
1563 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001564 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001565 }
1566
Joe Onorato88ec0992009-11-19 13:16:06 -08001567 @Override
1568 public void onBackPressed() {
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08001569 if (finishAutoCancelActionMode()) {
1570 return;
1571 }
Adam Cohen9211d422014-10-07 18:14:53 -07001572 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1573 return;
1574 }
1575
Sunny Goyal45478022015-06-08 16:52:41 -07001576 if (mDragController.isDragging()) {
1577 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001578 return;
1579 }
1580
Jon Mirandafeba90f2016-10-06 10:53:29 -07001581 // Note: There should be at most one log per method call. This is enforced implicitly
1582 // by using if-else statements.
1583 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001584 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
Tony Wickham52c1b662018-05-21 13:13:58 -07001585 if (topView != null && topView.onBackPressed()) {
1586 // Handled by the floating view.
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001587 } else {
Sunny Goyale39690b2018-08-02 13:35:07 -07001588 mStateManager.getState().onBackPressed(this);
Michael Jurkaaf442092010-06-10 17:01:57 -07001589 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001590 }
1591
Sunny Goyalfe770c92016-09-27 14:38:58 -07001592 @TargetApi(Build.VERSION_CODES.M)
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001593 @Override
Jon Miranda73c27ec2018-05-16 18:06:23 -07001594 public ActivityOptions getActivityLaunchOptions(View v) {
1595 return mAppTransitionManager.getActivityLaunchOptions(this, v);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001596 }
1597
Tony Wickham3a6746a2018-03-29 09:39:56 -07001598 public LauncherAppTransitionManager getAppTransitionManager() {
1599 return mAppTransitionManager;
1600 }
1601
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001602 @TargetApi(Build.VERSION_CODES.M)
1603 @Override
1604 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
1605 // Due to legacy reasons, direct call shortcuts require Launchers to have the
1606 // corresponding permission. Show the appropriate permission prompt if that
1607 // is the case.
1608 if (intent.getComponent() == null
1609 && Intent.ACTION_CALL.equals(intent.getAction())
1610 && checkSelfPermission(android.Manifest.permission.CALL_PHONE) !=
1611 PackageManager.PERMISSION_GRANTED) {
1612
1613 setWaitingForResult(PendingRequestArgs
1614 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
1615 requestPermissions(new String[]{android.Manifest.permission.CALL_PHONE},
1616 REQUEST_PERMISSION_CALL_PHONE);
1617 return true;
1618 } else {
1619 return false;
1620 }
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001621 }
1622
Hyunyoung Song46d07f72018-05-22 15:41:25 -07001623 @Override
Hyunyoung Songfc007472018-10-25 14:09:50 -07001624 public int getCurrentState() {
1625 if(mStateManager.getState() == LauncherState.ALL_APPS) {
1626 return StatsLogUtils.LAUNCHER_STATE_ALLAPPS;
1627 } else if (mStateManager.getState() == LauncherState.OVERVIEW) {
1628 return StatsLogUtils.LAUNCHER_STATE_OVERVIEW;
1629 }
Hyunyoung Song0ae38882018-11-05 14:45:19 -08001630 return StatsLogUtils.LAUNCHER_STATE_HOME;
Hyunyoung Songfc007472018-10-25 14:09:50 -07001631 }
1632
1633 @Override
Hyunyoung Song46d07f72018-05-22 15:41:25 -07001634 public void modifyUserEvent(LauncherLogProto.LauncherEvent event) {
1635 if (event.srcTarget != null && event.srcTarget.length > 0 &&
1636 event.srcTarget[1].containerType == ContainerType.PREDICTION) {
1637 Target[] targets = new Target[3];
1638 targets[0] = event.srcTarget[0];
1639 targets[1] = event.srcTarget[1];
1640 targets[2] = newTarget(Target.Type.CONTAINER);
1641 event.srcTarget = targets;
1642 LauncherState state = mStateManager.getState();
1643 if (state == LauncherState.ALL_APPS) {
1644 event.srcTarget[2].containerType = ContainerType.ALLAPPS;
1645 } else if (state == LauncherState.OVERVIEW) {
1646 event.srcTarget[2].containerType = ContainerType.TASKSWITCHER;
1647 }
1648 }
1649 }
1650
Sunny Goyala7ce1662016-05-31 15:01:35 -07001651 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Hyunyoung Song1241e612018-05-15 21:46:51 -07001652 boolean success = super.startActivitySafely(v, intent, item);
1653 if (success && v instanceof BubbleTextView) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001654 // This is set to the view that launched the activity that navigated the user away
1655 // from launcher. Since there is no callback for when the activity has finished
1656 // launching, enable the press state and keep this reference to reset the press
1657 // state when we return to launcher.
1658 BubbleTextView btv = (BubbleTextView) v;
1659 btv.setStayPressed(true);
1660 setOnResumeCallback(btv);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001661 }
Hyunyoung Song1241e612018-05-15 21:46:51 -07001662 return success;
Michael Jurka86a720e2012-05-09 11:23:23 -07001663 }
1664
Winson Chung3d503fb2011-07-13 17:25:49 -07001665 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001666 // TODO: Remove this method
Sunny Goyal876e4622018-11-02 13:50:40 -07001667 return mHotseat != null && (layout == mHotseat);
Winson Chung3d503fb2011-07-13 17:25:49 -07001668 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001669
Winson Chung3d503fb2011-07-13 17:25:49 -07001670 /**
1671 * Returns the CellLayout of the specified container at the specified screen.
1672 */
Sunny Goyalefb7e842018-10-04 15:11:00 -07001673 public CellLayout getCellLayout(int container, int screenId) {
Sunny Goyal876e4622018-11-02 13:50:40 -07001674 return (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1675 ? mHotseat : mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08001676 }
1677
Michael Jurkae326f182011-11-21 14:05:46 -08001678 @Override
1679 public void onTrimMemory(int level) {
1680 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07001681 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
1682 // The widget preview db can result in holding onto over
1683 // 3MB of memory for caching which isn't necessary.
1684 SQLiteDatabase.releaseMemory();
1685
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08001686 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07001687 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08001688 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01001689 if (mLauncherCallbacks != null) {
1690 mLauncherCallbacks.onTrimMemory(level);
1691 }
Winson Chung62a70be2018-02-23 15:10:05 -08001692 UiFactory.onTrimMemory(this, level);
Michael Jurkae326f182011-11-21 14:05:46 -08001693 }
1694
Michael Jurkad7c28052012-04-27 15:43:36 -07001695 @Override
1696 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07001697 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07001698 final List<CharSequence> text = event.getText();
1699 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07001700 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001701 // TODO: When can workspace be null?
1702 text.add(mWorkspace == null
1703 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07001704 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07001705 return result;
1706 }
1707
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001708 public void setOnResumeCallback(OnResumeCallback callback) {
1709 if (mOnResumeCallback != null) {
1710 mOnResumeCallback.onLauncherResume();
1711 }
1712 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07001713 }
1714
Michael Jurka7607c2f2013-04-03 14:33:19 -07001715 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001716 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001717 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07001718 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001719 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07001720 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001721 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07001722 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07001723 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07001724 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001725 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001726
Joe Onorato9c1289c2009-08-17 11:03:03 -04001727 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07001728 * Clear any pending bind callbacks. This is called when is loader is planning to
1729 * perform a full rebind from scratch.
1730 */
1731 @Override
1732 public void clearPendingBinds() {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001733 if (mPendingExecutor != null) {
1734 mPendingExecutor.markCompleted();
1735 mPendingExecutor = null;
1736 }
1737 }
1738
1739 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001740 * Refreshes the shortcuts shown on the workspace.
1741 *
1742 * Implementation of the method from LauncherModel.Callbacks.
1743 */
1744 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001745 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001746 // Floating panels (except the full widget sheet) are associated with individual icons. If
1747 // we are starting a fresh bind, close all such panels as all the icons are about
1748 // to go away.
1749 AbstractFloatingView.closeOpenViews(this, true,
Sunny Goyal7ede6112017-12-05 15:11:21 -08001750 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_REBIND_SAFE);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08001751
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001752 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08001753
Winson Chungd64d1762013-08-20 14:37:16 -07001754 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07001755 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07001756 mWorkspace.removeAllWorkspaceScreens();
Sunny Goyalc11fac32018-02-27 19:20:15 -08001757 mAppWidgetHost.clearViews();
Winson Chungd64d1762013-08-20 14:37:16 -07001758
Winson Chung3d503fb2011-07-13 17:25:49 -07001759 if (mHotseat != null) {
Sunny Goyal11e96492018-03-23 17:06:00 -07001760 mHotseat.resetLayout(mDeviceProfile.isVerticalBarLayout());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001761 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001762 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001763 }
1764
Adam Cohendcd297f2013-06-18 13:13:40 -07001765 @Override
Sunny Goyalefb7e842018-10-04 15:11:00 -07001766 public void bindScreens(IntArray orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001767 // Make sure the first screen is always at the start.
Ryan Lothianfa530cd2018-10-12 14:14:16 -04001768 if (FeatureFlags.QSB_ON_FIRST_SCREEN.get() &&
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001769 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalefb7e842018-10-04 15:11:00 -07001770 orderedScreenIds.removeValue(Workspace.FIRST_SCREEN_ID);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001771 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Ryan Lothianfa530cd2018-10-12 14:14:16 -04001772 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN.get()
Ryan Lothian688e9892018-10-03 13:32:01 -04001773 && orderedScreenIds.isEmpty()) {
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001774 // If there are no screens, we need to have an empty screen
1775 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07001776 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001777 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07001778
Winsonc7d2e832016-07-28 12:24:55 -07001779 // After we have added all the screens, if the wallpaper was locked to the default state,
1780 // then notify to indicate that it can be released and a proper wallpaper offset can be
1781 // computed before the next layout
1782 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07001783 }
1784
Sunny Goyalefb7e842018-10-04 15:11:00 -07001785 private void bindAddScreens(IntArray orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001786 int count = orderedScreenIds.size();
1787 for (int i = 0; i < count; i++) {
Sunny Goyalefb7e842018-10-04 15:11:00 -07001788 int screenId = orderedScreenIds.get(i);
Ryan Lothianfa530cd2018-10-12 14:14:16 -04001789 if (!FeatureFlags.QSB_ON_FIRST_SCREEN.get() || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001790 // No need to bind the first screen, as its always bound.
1791 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
1792 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001793 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001794 }
1795
Sunny Goyalb23980c2017-08-17 07:45:25 -07001796 @Override
Tony Wickham6a71a5b2018-08-21 11:40:23 -07001797 public void preAddApps() {
1798 // If there's an undo snackbar, force it to complete to ensure empty screens are removed
1799 // before trying to add new items.
1800 mModelWriter.commitDelete();
1801 AbstractFloatingView snackbar = AbstractFloatingView.getOpenView(this, TYPE_SNACKBAR);
1802 if (snackbar != null) {
1803 snackbar.post(() -> snackbar.close(true));
1804 }
1805 }
1806
1807 @Override
Sunny Goyalefb7e842018-10-04 15:11:00 -07001808 public void bindAppsAdded(IntArray newScreens, ArrayList<ItemInfo> addNotAnimated,
Sunny Goyal29947f02017-12-18 13:49:44 -08001809 ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07001810 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08001811 if (newScreens != null) {
1812 bindAddScreens(newScreens);
1813 }
Winson Chungd64d1762013-08-20 14:37:16 -07001814
1815 // We add the items without animation on non-visible pages, and with
1816 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08001817 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001818 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001819 }
Adam Cohen76a47a12014-02-05 11:47:43 -08001820 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001821 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07001822 }
Winson Chungc58497e2013-09-03 17:48:37 -07001823
Winson Chung87412982013-10-03 18:34:14 -07001824 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07001825 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001826 }
1827
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001828 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001829 * Bind the items start-end from the list.
1830 *
1831 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001832 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07001833 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07001834 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001835 // Get the list of added items and intersect them with the set of items here
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001836 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07001837 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001838 Workspace workspace = mWorkspace;
Sunny Goyalefb7e842018-10-04 15:11:00 -07001839 int newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07001840 int end = items.size();
1841 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001842 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07001843
1844 // Short circuit if we are loading dock items for a configuration which has no dock
1845 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
1846 mHotseat == null) {
1847 continue;
1848 }
1849
Sunny Goyal639e9062015-08-19 19:17:06 -07001850 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001851 switch (item.itemType) {
1852 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1853 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08001854 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001855 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07001856 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001857 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001858 }
1859 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07001860 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001861 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001862 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001863 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001864 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07001865 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1866 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07001867 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07001868 if (view == null) {
1869 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001870 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08001871 break;
1872 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001873 default:
1874 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001875 }
Sunny Goyal639e9062015-08-19 19:17:06 -07001876
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001877 /*
1878 * Remove colliding items.
1879 */
1880 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1881 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
1882 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
1883 View v = cl.getChildAt(item.cellX, item.cellY);
1884 Object tag = v.getTag();
1885 String desc = "Collision while binding workspace item: " + item
1886 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08001887 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001888 throw (new RuntimeException(desc));
1889 } else {
1890 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001891 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001892 continue;
1893 }
1894 }
1895 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05301896 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07001897 if (animateIcons) {
1898 // Animate all the applications up now
1899 view.setAlpha(0f);
1900 view.setScaleX(0f);
1901 view.setScaleY(0f);
1902 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08001903 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07001904 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001905 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07001906
Sunny Goyalf0b6db72018-08-13 16:10:14 -07001907 // Animate to the correct page
1908 if (animateIcons && newItemsScreenId > -1) {
1909 AnimatorSet anim = new AnimatorSet();
1910 anim.playTogether(bounceAnims);
Jon Miranda64eb8ea2018-03-29 11:43:58 -07001911
Sunny Goyalefb7e842018-10-04 15:11:00 -07001912 int currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyalf0b6db72018-08-13 16:10:14 -07001913 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
1914 final Runnable startBounceAnimRunnable = anim::start;
1915
1916 if (newItemsScreenId != currentScreenId) {
1917 // We post the animation slightly delayed to prevent slowdowns
1918 // when we are loading right after we return to launcher.
1919 mWorkspace.postDelayed(new Runnable() {
1920 public void run() {
1921 if (mWorkspace != null) {
1922 AbstractFloatingView.closeAllOpenViews(Launcher.this, false);
1923
1924 mWorkspace.snapToPage(newScreenIndex);
1925 mWorkspace.postDelayed(startBounceAnimRunnable,
1926 NEW_APPS_ANIMATION_DELAY);
Winson Chung94d67682013-09-25 16:29:40 -07001927 }
Sunny Goyalf0b6db72018-08-13 16:10:14 -07001928 }
1929 }, NEW_APPS_PAGE_MOVE_DELAY);
1930 } else {
1931 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07001932 }
Winson Chung64359a52013-07-08 17:17:08 -07001933 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001934 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001935 }
1936
Joe Onorato9c1289c2009-08-17 11:03:03 -04001937 /**
1938 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001939 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07001940 public void bindAppWidget(LauncherAppWidgetInfo item) {
1941 View view = inflateAppWidget(item);
1942 if (view != null) {
1943 mWorkspace.addInScreen(view, item);
1944 mWorkspace.requestLayout();
1945 }
1946 }
1947
1948 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001949 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05301950 PendingAppWidgetHostView view =
1951 new PendingAppWidgetHostView(this, item, mIconCache, true);
1952 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07001953 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001954 }
1955
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001956 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001957
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001958 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08001959
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001960 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
1961 // If the provider is not ready, bind as a pending widget.
1962 appWidgetInfo = null;
1963 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
1964 // The widget id is not valid. Try to find the widget based on the provider info.
1965 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
1966 } else {
1967 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
1968 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07001969
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001970 // If the provider is ready, but the width is not yet restored, try to restore it.
1971 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
1972 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07001973 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001974 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
1975 + " belongs to component " + item.providerName
1976 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001977 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07001978 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07001979 }
Sunny Goyalff572272014-07-23 13:58:07 -07001980
Sunny Goyal84b4adc2015-08-12 15:12:16 -07001981 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001982 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07001983 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
1984 // Id has not been allocated yet. Allocate a new id.
1985 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
1986 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07001987
Sunny Goyald478c832016-04-01 12:04:16 -07001988 // Also try to bind the widget. If the bind fails, the user will be shown
1989 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08001990 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07001991 pendingInfo.spanX = item.spanX;
1992 pendingInfo.spanY = item.spanY;
1993 pendingInfo.minSpanX = item.minSpanX;
1994 pendingInfo.minSpanY = item.minSpanY;
1995 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07001996
1997 boolean isDirectConfig =
1998 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
1999 if (isDirectConfig && item.bindOptions != null) {
2000 Bundle newOptions = item.bindOptions.getExtras();
2001 if (options != null) {
2002 newOptions.putAll(options);
2003 }
2004 options = newOptions;
2005 }
Sunny Goyald478c832016-04-01 12:04:16 -07002006 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2007 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002008
Sunny Goyal86df1382016-08-10 15:03:22 -07002009 // We tried to bind once. If we were not able to bind, we would need to
2010 // go through the permission dialog, which means we cannot skip the config
2011 // activity.
2012 item.bindOptions = null;
2013 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2014
Sunny Goyald478c832016-04-01 12:04:16 -07002015 // Bind succeeded
2016 if (success) {
2017 // If the widget has a configure activity, it is still needs to set it up,
2018 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002019 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002020 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2021 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002022 }
Sunny Goyald478c832016-04-01 12:04:16 -07002023
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002024 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002025 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002026 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002027 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002028 // The widget was marked as UI not ready, but there is no configure activity to
2029 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002030 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002031 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002032 }
Sunny Goyalff572272014-07-23 13:58:07 -07002033 }
2034
Sunny Goyald5462aa2016-11-22 16:52:39 +05302035 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002036 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002037 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002038 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002039 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Tony Wickham6a71a5b2018-08-21 11:40:23 -07002040 getModelWriter().deleteWidgetInfo(item, getAppWidgetHost());
Sunny Goyalb23980c2017-08-17 07:45:25 -07002041 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002042 }
2043
Sunny Goyal233ee962015-08-03 13:05:01 -07002044 item.minSpanX = appWidgetInfo.minSpanX;
2045 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302046 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002047 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302048 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002049 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302050 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002051
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002052 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002053 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002054 }
2055
Sunny Goyalff572272014-07-23 13:58:07 -07002056 /**
2057 * Restores a pending widget.
2058 *
2059 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002060 */
Sunny Goyald478c832016-04-01 12:04:16 -07002061 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002062 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2063 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2064 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002065 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002066 }
2067
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002068 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002069 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002070 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2071 info.pendingItemInfo = null;
2072 }
Sunny Goyalff572272014-07-23 13:58:07 -07002073
Sunny Goyalba47faa2017-10-04 16:50:57 -07002074 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002075 view.reInflate();
Sunny Goyalba47faa2017-10-04 16:50:57 -07002076 }
2077
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002078 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002079 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002080 }
2081
Adam Cohen1462de32012-07-24 22:34:36 -07002082 public void onPageBoundSynchronously(int page) {
Sunny Goyalf4aceab2017-12-06 16:16:31 -08002083 mSynchronouslyBoundPage = page;
Adam Cohen1462de32012-07-24 22:34:36 -07002084 }
2085
Sunny Goyal527c7d32015-08-28 15:19:36 -07002086 @Override
2087 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2088 if (mPendingExecutor != null) {
2089 mPendingExecutor.markCompleted();
2090 }
2091 mPendingExecutor = executor;
Sunny Goyal60180b02018-02-07 12:56:30 -08002092 if (!isInState(ALL_APPS)) {
2093 mAppsView.getAppsStore().setDeferUpdates(true);
2094 mPendingExecutor.execute(() -> mAppsView.getAppsStore().setDeferUpdates(false));
2095 }
2096
Sunny Goyal527c7d32015-08-28 15:19:36 -07002097 executor.attachTo(this);
2098 }
2099
2100 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2101 if (mPendingExecutor == executor) {
2102 mPendingExecutor = null;
2103 }
2104 }
2105
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002106 @Override
2107 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
Sunny Goyal6001ea22018-05-10 16:31:00 -07002108 AlphaProperty property = mDragLayer.getAlphaProperty(ALPHA_INDEX_LAUNCHER_LOAD);
2109 if (property.getValue() < 1) {
2110 ObjectAnimator anim = ObjectAnimator.ofFloat(property, MultiValueAlpha.VALUE, 1);
2111 if (executor != null) {
2112 anim.addListener(new AnimatorListenerAdapter() {
2113 @Override
2114 public void onAnimationEnd(Animator animation) {
2115 executor.onLoadAnimationCompleted();
2116 }
2117 });
2118 }
2119 anim.start();
Sunny Goyal29947f02017-12-18 13:49:44 -08002120 } else if (executor != null) {
2121 executor.onLoadAnimationCompleted();
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002122 }
2123 }
2124
Joe Onorato9c1289c2009-08-17 11:03:03 -04002125 /**
2126 * Callback saying that there aren't any more items to bind.
2127 *
2128 * Implementation of the method from LauncherModel.Callbacks.
2129 */
Tonyf80e8932018-12-21 11:58:04 -08002130 public void finishBindingItems(int pageBoundFirst) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002131 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002132 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002133
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002134 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002135
Sunny Goyal2100c782016-08-22 16:00:03 -07002136 if (mPendingActivityResult != null) {
2137 handleActivityResult(mPendingActivityResult.requestCode,
2138 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2139 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002140 }
2141
Sunny Goyala474a9b2017-05-04 16:47:11 -07002142 InstallShortcutReceiver.disableAndFlushInstallQueue(
2143 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002144
Tonyf80e8932018-12-21 11:58:04 -08002145 // When undoing the removal of the last item on a page, return to that page.
2146 mWorkspace.setCurrentPage(pageBoundFirst);
Tony Wickham6a71a5b2018-08-21 11:40:23 -07002147
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002148 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002149 }
2150
Winson Chunga2413752012-04-03 14:22:34 -07002151 private boolean canRunNewAppsAnimation() {
2152 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002153 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002154 }
2155
Winson Chungc9168342013-06-26 14:54:55 -07002156 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002157 ValueAnimator bounceAnim = new PropertyListBuilder().alpha(1).scale(1).build(v)
2158 .setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
Winson Chungc9168342013-06-26 14:54:55 -07002159 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002160 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002161 return bounceAnim;
2162 }
2163
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002164 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002165 * Add the icons for all apps.
2166 *
2167 * Implementation of the method from LauncherModel.Callbacks.
2168 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002169 public void bindAllApplications(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002170 mAppsView.getAppsStore().setApps(apps);
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002171
Adam Cohen9211d422014-10-07 18:14:53 -07002172 if (mLauncherCallbacks != null) {
2173 mLauncherCallbacks.bindAllApplications(apps);
2174 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002175 }
2176
2177 /**
Zak Cohen658c67a2018-10-19 14:21:05 -07002178 * Copies LauncherModel's map of activities to shortcut counts to Launcher's. This is necessary
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002179 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2180 */
2181 @Override
Zak Cohen658c67a2018-10-19 14:21:05 -07002182 public void bindDeepShortcutMap(HashMap<ComponentKey, Integer> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002183 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002184 }
2185
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002186 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002187 * A package was updated.
2188 *
2189 * Implementation of the method from LauncherModel.Callbacks.
2190 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002191 @Override
2192 public void bindAppsAddedOrUpdated(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002193 mAppsView.getAppsStore().addOrUpdateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002194 }
2195
Sunny Goyal4390ace2014-10-13 11:33:11 -07002196 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002197 public void bindPromiseAppProgressUpdated(PromiseAppInfo app) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002198 mAppsView.getAppsStore().updatePromiseAppProgress(app);
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002199 }
2200
2201 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002202 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002203 mWorkspace.widgetsRestored(widgets);
2204 }
2205
Joe Onorato9c1289c2009-08-17 11:03:03 -04002206 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002207 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002208 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002209 *
2210 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002211 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002212 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002213 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002214 if (!updated.isEmpty()) {
2215 mWorkspace.updateShortcuts(updated);
2216 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002217 }
2218
2219 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002220 * Update the state of a package, typically related to install state.
2221 *
2222 * Implementation of the method from LauncherModel.Callbacks.
2223 */
Sunny Goyale755d462014-07-22 13:48:29 -07002224 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002225 public void bindRestoreItemsChange(HashSet<ItemInfo> updates) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07002226 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07002227 }
2228
2229 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002230 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07002231 * in addition to specific applications to remove, the reason being that
2232 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002233 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07002234 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002235 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07002236 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002237 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002238 mWorkspace.removeItemsByMatcher(matcher);
2239 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002240 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002241
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002242 @Override
2243 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002244 mAppsView.getAppsStore().removeApps(appInfos);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002245 }
Joe Onoratobe386092009-11-17 17:32:16 -08002246
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002247 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002248 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
2249 mPopupDataProvider.setAllWidgets(allWidgets);
Tony Wickham26b17462017-03-20 17:12:24 -07002250 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
2251 if (topView != null) {
2252 topView.onWidgetsBound();
2253 }
2254 }
2255
Tony Wickham86222d22017-03-29 15:30:43 -07002256 /**
2257 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
2258 * refreshes the widgets and shortcuts associated with the given package/user
2259 */
2260 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07002261 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002262 }
2263
Winson Chung80baf5a2010-08-09 16:03:15 -07002264 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002265 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08002266 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002267 @Override
2268 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
2269 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002270
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002271 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
2272 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07002273 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002274 writer.println(prefix + " Homescreen " + i);
2275
2276 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
2277 for (int j = 0; j < layout.getChildCount(); j++) {
2278 Object tag = layout.getChildAt(j).getTag();
2279 if (tag != null) {
2280 writer.println(prefix + " " + tag.toString());
2281 }
2282 }
2283 }
2284
2285 writer.println(prefix + " Hotseat");
Sunny Goyal876e4622018-11-02 13:50:40 -07002286 ViewGroup layout = mHotseat.getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07002287 for (int j = 0; j < layout.getChildCount(); j++) {
2288 Object tag = layout.getChildAt(j).getTag();
2289 if (tag != null) {
2290 writer.println(prefix + " " + tag.toString());
2291 }
Adam Cohen4caf2982013-08-20 18:54:31 -07002292 }
Sunny Goyala1365452015-10-01 15:46:24 -07002293 }
2294
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002295 writer.println(prefix + "Misc:");
2296 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
2297 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
2298 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
Sunny Goyale43d00d2018-05-14 14:23:18 -07002299 writer.println(" mRotationHelper: " + mRotationHelper);
Sunny Goyalcb2c5052018-10-15 12:30:29 -07002300 // Extra logging for b/116853349
2301 mDragLayer.dumpAlpha(writer);
Sunny Goyale43d00d2018-05-14 14:23:18 -07002302 dumpMisc(writer);
Sunny Goyal2bba1902018-02-27 12:20:50 -08002303
2304 try {
2305 FileLog.flushAll(writer);
2306 } catch (Exception e) {
2307 // Ignore
2308 }
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002309
2310 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002311
Adam Cohen9211d422014-10-07 18:14:53 -07002312 if (mLauncherCallbacks != null) {
2313 mLauncherCallbacks.dump(prefix, fd, writer, args);
2314 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002315 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002316
Sunny Goyal66b24572016-09-21 15:57:55 -07002317 @Override
Sunny Goyal66b24572016-09-21 15:57:55 -07002318 public void onProvideKeyboardShortcuts(
2319 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
2320
2321 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002322 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002323 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
2324 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
Vadim Tryshev74c261c2018-06-12 13:44:43 -07002325 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.widget_button_text),
2326 KeyEvent.KEYCODE_W, KeyEvent.META_CTRL_ON));
Sunny Goyal66b24572016-09-21 15:57:55 -07002327 }
Vadim Tryshev3455f8d2018-05-11 20:02:20 -07002328 final View currentFocus = getCurrentFocus();
2329 if (currentFocus != null) {
2330 if (new CustomActionsPopup(this, currentFocus).canShow()) {
2331 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
2332 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
2333 }
2334 if (currentFocus.getTag() instanceof ItemInfo
2335 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
2336 shortcutInfos.add(new KeyboardShortcutInfo(
2337 getString(R.string.shortcuts_menu_with_notifications_description),
2338 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
2339 }
Sunny Goyal66b24572016-09-21 15:57:55 -07002340 }
2341 if (!shortcutInfos.isEmpty()) {
2342 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
2343 }
2344
2345 super.onProvideKeyboardShortcuts(data, menu, deviceId);
2346 }
2347
2348 @Override
2349 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
2350 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
2351 switch (keyCode) {
2352 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002353 if (isInState(NORMAL)) {
2354 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07002355 return true;
2356 }
2357 break;
2358 case KeyEvent.KEYCODE_S: {
2359 View focusedView = getCurrentFocus();
2360 if (focusedView instanceof BubbleTextView
2361 && focusedView.getTag() instanceof ItemInfo
2362 && mAccessibilityDelegate.performAction(focusedView,
2363 (ItemInfo) focusedView.getTag(),
2364 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal00ac9202017-11-09 15:24:06 -08002365 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07002366 return true;
2367 }
2368 break;
2369 }
2370 case KeyEvent.KEYCODE_O:
2371 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
2372 return true;
2373 }
2374 break;
Vadim Tryshev74c261c2018-06-12 13:44:43 -07002375 case KeyEvent.KEYCODE_W:
2376 if (isInState(NORMAL)) {
2377 OptionsPopupView.openWidgets(this);
2378 return true;
2379 }
2380 break;
Sunny Goyal66b24572016-09-21 15:57:55 -07002381 }
2382 }
2383 return super.onKeyShortcut(keyCode, event);
2384 }
2385
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002386 @Override
2387 public boolean onKeyUp(int keyCode, KeyEvent event) {
2388 if (keyCode == KeyEvent.KEYCODE_MENU) {
2389 // KEYCODE_MENU is sent by some tests, for example
2390 // LauncherJankTests#testWidgetsContainerFling. Don't just remove its handling.
2391 if (!mDragController.isDragging() && !mWorkspace.isSwitchingState() &&
2392 isInState(NORMAL)) {
2393 // Close any open floating views.
2394 AbstractFloatingView.closeAllOpenViews(this);
2395
2396 // Setting the touch point to (-1, -1) will show the options popup in the center of
2397 // the screen.
Sunny Goyal2fd7a8b2018-03-30 17:10:13 -07002398 OptionsPopupView.showDefaultOptions(this, -1, -1);
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002399 }
2400 return true;
2401 }
2402 return super.onKeyUp(keyCode, event);
2403 }
2404
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002405 public static Launcher getLauncher(Context context) {
Sunny Goyal87b5eb62018-07-03 15:53:39 -07002406 return (Launcher) fromContext(context);
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002407 }
2408
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002409 /**
Sunny Goyalab770a12018-11-14 15:17:26 -08002410 * Just a wrapper around the type cast to allow easier tracking of calls.
2411 */
2412 public static <T extends Launcher> T cast(ActivityContext activityContext) {
2413 return (T) activityContext;
2414 }
2415
2416 /**
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002417 * Callback for listening for onResume
2418 */
2419 public interface OnResumeCallback {
2420
2421 void onLauncherResume();
2422 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002423}