blob: f571aa30e98ca24d18755e3bcf4894e755b70215 [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;
Sunny Goyalaae6fbb2019-01-31 16:05:58 -0800148import java.util.function.Predicate;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700149
Sunny Goyal8c48d8b2019-01-25 15:10:18 -0800150import androidx.annotation.Nullable;
151
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152/**
153 * Default launcher application.
154 */
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700155public class Launcher extends BaseDraggingActivity implements LauncherExterns,
Hyunyoung Song31971a32019-01-27 12:31:12 -0800156 LauncherModel.Callbacks, LauncherProviderChangeListener, UserEventDelegate,
157 InvariantDeviceProfile.OnIDPChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700158 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700159 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160
Winson Chunga2413752012-04-03 14:22:34 -0700161 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700162
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700164 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700165
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700166 private static final int REQUEST_PICK_APPWIDGET = 9;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167
Michael Jurka8b805b12012-04-18 14:23:14 -0700168 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800169 public static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
170 public static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700171
Jon Mirandac476d6e2017-05-04 16:30:01 -0700172 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700173
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700174 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
175
Mathew Inwood876a8462013-06-14 14:12:41 +0100176 /**
177 * IntentStarter uses request codes starting with this. This must be greater than all activity
178 * request codes used internally.
179 */
180 protected static final int REQUEST_LAST = 100;
181
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 // Type: int
183 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700184 // Type: int
185 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700186 // Type: PendingRequestArgs
187 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
188 // Type: ActivityResultInfo
189 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700190 // Type: SparseArray<Parcelable>
191 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
vadimt00d42552018-12-11 17:59:36 -0800192 public static final String ON_CREATE_EVT = "Launcher.onCreate";
vadimtcb863752018-12-19 17:44:57 -0800193 private static final String ON_START_EVT = "Launcher.onStart";
194 private static final String ON_RESUME_EVT = "Launcher.onResume";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800195
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700196 private LauncherStateManager mStateManager;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700197
Adam Cohenad4e15c2013-10-17 16:21:35 -0700198 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199
Winson Chunga2413752012-04-03 14:22:34 -0700200 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700201 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
202 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
203 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700204
Jon Miranda54441f52018-01-24 15:38:25 -0800205 private LauncherAppTransitionManager mAppTransitionManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800206 private Configuration mOldConfig;
Jon Miranda54441f52018-01-24 15:38:25 -0800207
Adam Cohen091440a2015-03-18 14:16:05 -0700208 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700209 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700210 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800211 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700212
Sunny Goyalffe83f12014-08-14 17:39:34 -0700213 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700214 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700215
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700216 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700217
Sunny Goyal316490e2015-06-02 09:38:28 -0700218 @Thunk Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700219
Sunny Goyal47328fd2016-05-25 18:56:41 -0700220 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700221
222 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700223 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700224 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700225
Winson Chung8ae41982017-11-10 11:42:13 -0800226 // UI and state for the overview panel
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800227 private View mOverviewPanel;
Winson Chung8ae41982017-11-10 11:42:13 -0800228
Adam Cohen091440a2015-03-18 14:16:05 -0700229 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400230
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700231 private OnResumeCallback mOnResumeCallback;
232
Sunny Goyal527c7d32015-08-28 15:19:36 -0700233 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700234
Joe Onorato9c1289c2009-08-17 11:03:03 -0400235 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800236 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800237 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700238 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400239
Tony Wickham010d2552017-01-20 08:15:28 -0800240 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700241
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800242 private int mSynchronouslyBoundPage = PagedView.INVALID_PAGE;
Adam Cohen1462de32012-07-24 22:34:36 -0700243
Winson Chung46353de2012-02-16 14:05:10 -0800244 // We only want to get the SharedPreferences once since it does an FS stat each time we get
245 // it from the context.
246 private SharedPreferences mSharedPrefs;
247
Sunny Goyal2100c782016-08-22 16:00:03 -0700248 // Activity result which needs to be processed after workspace has loaded.
249 private ActivityResultInfo mPendingActivityResult;
250 /**
251 * Holds extra information required to handle a result from an external call, like
252 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
253 */
254 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800255
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700256 public ViewGroupFocusHelper mFocusHandler;
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700257
Sunny Goyal623eddd2018-03-02 12:24:41 -0800258 private RotationHelper mRotationHelper;
Sunny Goyal7779d622015-06-11 16:18:39 -0700259
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700260 private final Handler mHandler = new Handler();
Sunny Goyala002c6c2019-02-12 16:20:10 -0800261 private final Runnable mHandleDeferredResume = this::handleDeferredResume;
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700262
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800263 @Override
264 protected void onCreate(Bundle savedInstanceState) {
vadimt00d42552018-12-11 17:59:36 -0800265 RaceConditionTracker.onEvent(ON_CREATE_EVT, ENTER);
Winson Chunga2413752012-04-03 14:22:34 -0700266 if (DEBUG_STRICT_MODE) {
267 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
268 .detectDiskReads()
269 .detectDiskWrites()
270 .detectNetwork() // or .detectAll() for all detectable problems
271 .penaltyLog()
272 .build());
273 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
274 .detectLeakedSqlLiteObjects()
275 .detectLeakedClosableObjects()
276 .penaltyLog()
277 .penaltyDeath()
278 .build());
279 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700280 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700281
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800282 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700283 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400284
Sunny Goyal87f784c2017-01-11 10:48:34 -0800285 LauncherAppState app = LauncherAppState.getInstance(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800286 mOldConfig = new Configuration(getResources().getConfiguration());
Sunny Goyal0e7724c2018-02-22 13:22:21 -0800287 mModel = app.setLauncher(this);
Hyunyoung Song31971a32019-01-27 12:31:12 -0800288 InvariantDeviceProfile idp = app.getInvariantDeviceProfile();
289 initDeviceProfile(idp);
290 idp.addOnChangeListener(this);
Sunny Goyalf7258242015-10-19 16:59:07 -0700291 mSharedPrefs = Utilities.getPrefs(this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800292 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700293 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700294
Joe Onorato41a12d22009-10-31 18:30:00 -0400295 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700296 mAllAppsController = new AllAppsTransitionController(this);
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800297 mStateManager = new LauncherStateManager(this);
Tracy Zhou8b23c6f2018-04-30 17:13:27 -0700298 UiFactory.onCreate(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700299
Sunny Goyalffe83f12014-08-14 17:39:34 -0700300 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700301
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700302 mAppWidgetHost = new LauncherAppWidgetHost(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700303 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700304
Sunny Goyal60820d72017-05-09 12:40:11 -0700305 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700306
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800307 setupViews();
Tony Wickham010d2552017-01-20 08:15:28 -0800308 mPopupDataProvider = new PopupDataProvider(this);
309
Sunny Goyal623eddd2018-03-02 12:24:41 -0800310 mRotationHelper = new RotationHelper(this);
Sunny Goyal90548432018-04-04 17:17:00 -0700311 mAppTransitionManager = LauncherAppTransitionManager.newInstance(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800312
Sunny Goyald3864fa2017-11-14 15:06:36 -0800313 boolean internalStateHandled = InternalStateHandler.handleCreate(this, getIntent());
314 if (internalStateHandled) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800315 if (savedInstanceState != null) {
316 // InternalStateHandler has already set the appropriate state.
317 // We dont need to do anything.
318 savedInstanceState.remove(RUNTIME_STATE);
319 }
320 }
321 restoreState(savedInstanceState);
Winson Chungb63b44c2017-11-10 17:54:44 -0800322
Sunny Goyal2100c782016-08-22 16:00:03 -0700323 // We only load the page synchronously if the user rotates (or triggers a
324 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700325 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
326 if (savedInstanceState != null) {
327 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
328 }
Sunny Goyald3864fa2017-11-14 15:06:36 -0800329
Sunny Goyalfe770c92016-09-27 14:38:58 -0700330 if (!mModel.startLoader(currentScreen)) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800331 if (!internalStateHandled) {
332 // If we are not binding synchronously, show a fade in animation when
333 // the first page bind completes.
Sunny Goyal6001ea22018-05-10 16:31:00 -0700334 mDragLayer.getAlphaProperty(ALPHA_INDEX_LAUNCHER_LOAD).setValue(0);
Sunny Goyald3864fa2017-11-14 15:06:36 -0800335 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700336 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700337 // Pages bound synchronously.
338 mWorkspace.setCurrentPage(currentScreen);
339
Sunny Goyal2100c782016-08-22 16:00:03 -0700340 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800341 }
342
343 // For handling default keys
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800344 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800345
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800346 setContentView(mLauncherView);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800347 getRootView().dispatchInsets();
Mario Bertschlera6936942017-05-31 14:48:19 -0700348
Jon Miranda7fda2852017-07-19 16:07:01 -0700349 // Listen for broadcasts
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700350 registerReceiver(mScreenOffReceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));
Sunny Goyala616d2b2017-06-12 13:54:01 -0700351
Sunny Goyal8392c822017-06-20 10:03:56 -0700352 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
353 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700354
355 if (mLauncherCallbacks != null) {
356 mLauncherCallbacks.onCreate(savedInstanceState);
357 }
Sunny Goyal623eddd2018-03-02 12:24:41 -0800358 mRotationHelper.initialize();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700359
360 TraceHelper.endSection("Launcher-onCreate");
vadimt00d42552018-12-11 17:59:36 -0800361 RaceConditionTracker.onEvent(ON_CREATE_EVT, EXIT);
Adam Cohen9211d422014-10-07 18:14:53 -0700362 }
363
Sunny Goyal7779d622015-06-11 16:18:39 -0700364 @Override
Winson Chung8eb49e02018-07-16 13:33:31 -0700365 public void onEnterAnimationComplete() {
366 super.onEnterAnimationComplete();
367 UiFactory.onEnterAnimationComplete(this);
arangelovad783052018-08-17 14:46:45 +0100368 mAllAppsController.highlightWorkTabIfNecessary();
Winson Chung8eb49e02018-07-16 13:33:31 -0700369 }
370
371 @Override
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800372 public void onConfigurationChanged(Configuration newConfig) {
373 int diff = newConfig.diff(mOldConfig);
Jon Mirandaaeb4dd02018-07-11 14:16:23 -0700374
375 if ((diff & CONFIG_LOCALE) != 0) {
376 Folder.setLocaleDependentFields(getResources(), true /* force */);
377 }
378
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800379 if ((diff & (CONFIG_ORIENTATION | CONFIG_SCREEN_SIZE)) != 0) {
380 mUserEventDispatcher = null;
381 initDeviceProfile(mDeviceProfile.inv);
Sunny Goyalfde55052018-02-01 14:46:13 -0800382 dispatchDeviceProfileChanged();
Sunny Goyal59d086c2018-05-07 17:31:40 -0700383 reapplyUi();
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700384 mDragLayer.recreateControllers();
Sunny Goyal7185dd62018-03-14 17:51:49 -0700385
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800386 // TODO: We can probably avoid rebind when only screen size changed.
Sunny Goyal605bcf32018-03-02 15:16:12 -0800387 rebindModel();
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800388 }
389
390 mOldConfig.setTo(newConfig);
Tracy Zhoua706f002018-03-28 13:55:19 -0700391 UiFactory.onLauncherStateOrResumeChanged(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800392 super.onConfigurationChanged(newConfig);
393 }
394
Sunny Goyal605bcf32018-03-02 15:16:12 -0800395 @Override
Sunny Goyal59d086c2018-05-07 17:31:40 -0700396 protected void reapplyUi() {
397 getRootView().dispatchInsets();
398 getStateManager().reapplyState(true /* cancelCurrentAnimation */);
399 }
400
401 @Override
Sunny Goyal605bcf32018-03-02 15:16:12 -0800402 public void rebindModel() {
403 int currentPage = mWorkspace.getNextPage();
404 if (mModel.startLoader(currentPage)) {
405 mWorkspace.setCurrentPage(currentPage);
406 setWorkspaceLoading(true);
407 }
408 }
409
Hyunyoung Song31971a32019-01-27 12:31:12 -0800410 @Override
411 public void onIdpChanged(int changeFlags, InvariantDeviceProfile idp) {
412 initDeviceProfile(idp);
413 getRootView().dispatchInsets();
414 }
415
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800416 private void initDeviceProfile(InvariantDeviceProfile idp) {
417 // Load configuration-specific DeviceProfile
Sunny Goyal59d086c2018-05-07 17:31:40 -0700418 mDeviceProfile = idp.getDeviceProfile(this);
Sunny Goyal8c48d8b2019-01-25 15:10:18 -0800419 if (isInMultiWindowMode()) {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700420 Display display = getWindowManager().getDefaultDisplay();
421 Point mwSize = new Point();
422 display.getSize(mwSize);
423 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
424 }
425 onDeviceProfileInitiated();
Sunny Goyal605bcf32018-03-02 15:16:12 -0800426 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout(), true);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800427 }
428
Sunny Goyal623eddd2018-03-02 12:24:41 -0800429 public RotationHelper getRotationHelper() {
430 return mRotationHelper;
431 }
432
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700433 public LauncherStateManager getStateManager() {
434 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700435 }
436
Mario Bertschler27288382017-05-24 15:35:09 -0700437 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700438 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800439 return mLauncherView.findViewById(id);
440 }
441
442 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700443 public void onAppWidgetHostReset() {
444 if (mAppWidgetHost != null) {
445 mAppWidgetHost.startListening();
446 }
447 }
448
Adam Cohen9211d422014-10-07 18:14:53 -0700449 private LauncherCallbacks mLauncherCallbacks;
450
Sunny Goyal32554d12015-12-03 15:31:25 -0800451 /**
452 * Call this after onCreate to set or clear overlay.
453 */
454 public void setLauncherOverlay(LauncherOverlay overlay) {
455 if (overlay != null) {
456 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
457 }
458 mWorkspace.setLauncherOverlay(overlay);
459 }
460
Adam Cohen9211d422014-10-07 18:14:53 -0700461 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
462 mLauncherCallbacks = callbacks;
463 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700464 }
465
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700466 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700467 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700468 if (mLauncherCallbacks != null) {
469 mLauncherCallbacks.onLauncherProviderChange();
470 }
471 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700472
Hyunyoung Song3f471442015-04-08 19:01:34 -0700473 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700474 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
475 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700476 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700477 }
478
Tony Wickham010d2552017-01-20 08:15:28 -0800479 public PopupDataProvider getPopupDataProvider() {
480 return mPopupDataProvider;
481 }
482
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700483 @Override
Tony Wickhamf34bee82018-12-03 18:11:39 -0800484 public DotInfo getDotInfoForItem(ItemInfo info) {
485 return mPopupDataProvider.getDotInfoForItem(info);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700486 }
487
488 @Override
489 public void invalidateParent(ItemInfo info) {
490 FolderIconPreviewVerifier verifier = new FolderIconPreviewVerifier(getDeviceProfile().inv);
491 if (verifier.isItemInPreview(info.rank) && (info.container >= 0)) {
492 View folderIcon = getWorkspace().getHomescreenIconByItemId(info.container);
493 if (folderIcon != null) {
494 folderIcon.invalidate();
495 }
496 }
497 }
498
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000499 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700500 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
501 * a configuration step, this allows the proper animations to run after other transitions.
502 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700503 private int completeAdd(
Sunny Goyal2100c782016-08-22 16:00:03 -0700504 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
Sunny Goyalefb7e842018-10-04 15:11:00 -0700505 int screenId = info.screenId;
Sunny Goyal2100c782016-08-22 16:00:03 -0700506 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700507 // When the screen id represents an actual screen (as opposed to a rank) we make sure
508 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700509 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700510 }
Adam Cohendb364c32014-05-20 14:23:40 -0700511
Sunny Goyal2100c782016-08-22 16:00:03 -0700512 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800513 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700514 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700515 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800516 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700517 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700518 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700519 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700520 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700521 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700522 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700523 int widgetId = appWidgetId;
524 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700525 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700526 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700527 // Since the view was just bound, also launch the configure activity if needed
528 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
529 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800530 if (provider != null) {
531 new WidgetAddFlowHandler(provider)
532 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700533 }
534 }
535 break;
536 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800537 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700538
Adam Cohendb364c32014-05-20 14:23:40 -0700539 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800540 }
541
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800542 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700543 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700544 if (isWorkspaceLoading()) {
545 // process the result once the workspace has loaded.
546 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
547 return;
548 }
549 mPendingActivityResult = null;
550
Winson Chunge341d302013-08-16 14:31:00 -0700551 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700552 final PendingRequestArgs requestArgs = mPendingRequestArgs;
553 setWaitingForResult(null);
554 if (requestArgs == null) {
555 return;
556 }
557
558 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700559
Adam Cohenad4e15c2013-10-17 16:21:35 -0700560 Runnable exitSpringLoaded = new Runnable() {
561 @Override
562 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700563 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700564 }
565 };
566
Michael Jurka8b805b12012-04-18 14:23:14 -0700567 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700568 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700569 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700570 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
571 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700572 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700573 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700574 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700575 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700576 addAppWidgetImpl(
577 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800578 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700579 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700580 }
581 return;
582 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200583
Adam Cohened66b2b2012-01-23 17:28:51 -0800584 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700585 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700586
Adam Cohened66b2b2012-01-23 17:28:51 -0800587 // We have special handling for widgets
588 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800589 final int appWidgetId;
590 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
591 : -1;
592 if (widgetId < 0) {
593 appWidgetId = pendingAddWidgetId;
594 } else {
595 appWidgetId = widgetId;
596 }
597
Adam Cohenad4e15c2013-10-17 16:21:35 -0700598 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800599 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700600 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
601 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700602 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700603 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700604 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700605 @Override
606 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700607 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700608 }
609 };
Adam Cohendb364c32014-05-20 14:23:40 -0700610
Sunny Goyal2100c782016-08-22 16:00:03 -0700611 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
612 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
613 } else {
614 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
615 // When the screen id represents an actual screen (as opposed to a rank)
616 // we make sure that the drop page actually exists.
617 requestArgs.screenId =
618 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700619 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700620 final CellLayout dropLayout =
621 mWorkspace.getScreenWithId(requestArgs.screenId);
622
623 dropLayout.setDropPending(true);
624 final Runnable onComplete = new Runnable() {
625 @Override
626 public void run() {
627 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
628 dropLayout.setDropPending(false);
629 }
630 };
631 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
632 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700633 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800634 return;
635 }
636
Sunny Goyald478c832016-04-01 12:04:16 -0700637 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
638 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700639 if (resultCode == RESULT_OK) {
640 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700641 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700642 }
643 // Leave the widget in the pending state if the user canceled the configure.
644 return;
645 }
646
Sunny Goyalaad90582015-07-09 14:22:50 -0700647 if (requestCode == REQUEST_CREATE_SHORTCUT) {
648 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700649 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
650 completeAdd(requestCode, data, -1, requestArgs);
651 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
652 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
653
Sunny Goyalaad90582015-07-09 14:22:50 -0700654 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700655 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
656 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800657 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800658 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800659 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800660 }
661
662 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700663 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800664 final int requestCode, final int resultCode, final Intent data) {
665 handleActivityResult(requestCode, resultCode, data);
666 if (mLauncherCallbacks != null) {
667 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
668 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800669 }
670
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700671 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700672 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600673 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700674 PendingRequestArgs pendingArgs = mPendingRequestArgs;
675 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
676 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
677 setWaitingForResult(null);
678
Sunny Goyal28c6b962015-10-12 11:42:05 -0700679 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700680 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700681 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700682 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700683 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700684 Intent intent = pendingArgs.getPendingIntent();
685
Sunny Goyal28c6b962015-10-12 11:42:05 -0700686 if (grantResults.length > 0
687 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700688 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700689 } else {
690 // TODO: Show a snack bar with link to settings
691 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700692 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700693 }
694 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600695 if (mLauncherCallbacks != null) {
696 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
697 grantResults);
698 }
699 }
700
Adam Cohendb364c32014-05-20 14:23:40 -0700701 /**
702 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
703 *
704 * @param screenId the screen id to check
705 * @return the new screen, or screenId if it exists
706 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700707 private int ensurePendingDropLayoutExists(int screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800708 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700709 if (dropLayout == null) {
710 // it's possible that the add screen was removed because it was
711 // empty and a re-bind occurred
712 mWorkspace.addExtraEmptyScreen();
713 return mWorkspace.commitExtraEmptyScreen();
714 } else {
715 return screenId;
716 }
717 }
718
Sunny Goyal2100c782016-08-22 16:00:03 -0700719 @Thunk void completeTwoStageWidgetDrop(
720 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
721 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800722 Runnable onCompleteRunnable = null;
723 int animationType = 0;
724
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700725 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800726 if (resultCode == RESULT_OK) {
727 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
728 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800729 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700730 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800731 onCompleteRunnable = new Runnable() {
732 @Override
733 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700734 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700735 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800736 }
737 };
738 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800739 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800740 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800741 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700742 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700743 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700744 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
745 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700746 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700747 // The animated view may be null in the case of a rotation during widget configuration
748 onCompleteRunnable.run();
749 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800750 }
751
752 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700753 protected void onStop() {
754 super.onStop();
Adam Cohen9211d422014-10-07 18:14:53 -0700755 if (mLauncherCallbacks != null) {
756 mLauncherCallbacks.onStop();
757 }
Miranda Kephart1a359a22019-02-11 13:07:04 -0500758
Hyunyoung Song1241e612018-05-15 21:46:51 -0700759 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
760 mStateManager.getState().containerType, -1);
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700761
762 mAppWidgetHost.setListenIfResumed(false);
763
Tony Wickham010d2552017-01-20 08:15:28 -0800764 NotificationListener.removeNotificationsChangedListener();
Sunny Goyalaf161fd2018-02-05 16:34:57 -0800765 getStateManager().moveToRestState();
Winson Chungedb0d6b2018-03-21 17:32:55 -0700766
Tracy Zhou62646712018-06-26 14:19:02 -0700767 UiFactory.onLauncherStateOrResumeChanged(this);
768
Winson Chungdea51352018-04-24 13:02:10 -0700769 // Workaround for b/78520668, explicitly trim memory once UI is hidden
Winson Chung473730f2018-04-26 11:54:22 -0700770 onTrimMemory(TRIM_MEMORY_UI_HIDDEN);
Michael Jurkacd496d72013-04-11 11:32:45 -0700771 }
772
773 @Override
774 protected void onStart() {
vadimtcb863752018-12-19 17:44:57 -0800775 RaceConditionTracker.onEvent(ON_START_EVT, ENTER);
Michael Jurkacd496d72013-04-11 11:32:45 -0700776 super.onStart();
Adam Cohen9211d422014-10-07 18:14:53 -0700777 if (mLauncherCallbacks != null) {
778 mLauncherCallbacks.onStart();
779 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800780 mAppWidgetHost.setListenIfResumed(true);
Sunny Goyalcac002d2018-01-04 09:51:45 -0800781 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
vadimtcb863752018-12-19 17:44:57 -0800782 RaceConditionTracker.onEvent(ON_START_EVT, EXIT);
Michael Jurkacd496d72013-04-11 11:32:45 -0700783 }
784
Sunny Goyala002c6c2019-02-12 16:20:10 -0800785 private void handleDeferredResume() {
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700786 if (hasBeenResumed()) {
787 getUserEventDispatcher().logActionCommand(Action.Command.RESUME,
788 mStateManager.getState().containerType, -1);
789 getUserEventDispatcher().startSession();
Sunny Goyala002c6c2019-02-12 16:20:10 -0800790
791 UiFactory.onLauncherStateOrResumeChanged(this);
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700792 }
793 }
794
Michael Jurkacd496d72013-04-11 11:32:45 -0700795 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800796 protected void onResume() {
vadimtcb863752018-12-19 17:44:57 -0800797 RaceConditionTracker.onEvent(ON_RESUME_EVT, ENTER);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700798 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800799 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700800 TraceHelper.partitionSection("ON_RESUME", "superCall");
801
Sunny Goyala002c6c2019-02-12 16:20:10 -0800802 mHandler.removeCallbacks(mHandleDeferredResume);
803 Utilities.postAsyncCallback(mHandler, mHandleDeferredResume);
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700804
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700805 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700806 // Process any items that were added while Launcher was away.
807 InstallShortcutReceiver.disableAndFlushInstallQueue(
808 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700809
Sunny Goyala474a9b2017-05-04 16:47:11 -0700810 // Refresh shortcuts if the permission changed.
811 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700812
Tracy Zhoue5575f92018-04-19 11:12:53 -0700813 DiscoveryBounce.showForHomeIfNeeded(this);
Adam Cohen9211d422014-10-07 18:14:53 -0700814 if (mLauncherCallbacks != null) {
815 mLauncherCallbacks.onResume();
816 }
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 Goyale29897f2017-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 Goyale29897f2017-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 Goyale29897f2017-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 Wickham010d2552017-01-20 08:15:28 -08001125 }
1126
Adam Cohended9f8d2010-11-03 13:25:16 -07001127 @Override
1128 public void onAttachedToWindow() {
1129 super.onAttachedToWindow();
1130
Sunny Goyalc86df472016-02-25 09:19:38 -08001131 if (mLauncherCallbacks != null) {
1132 mLauncherCallbacks.onAttachedToWindow();
1133 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001134 }
1135
1136 @Override
1137 public void onDetachedFromWindow() {
1138 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001139
1140 if (mLauncherCallbacks != null) {
1141 mLauncherCallbacks.onDetachedFromWindow();
1142 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001143 }
1144
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001145 public AllAppsTransitionController getAllAppsController() {
1146 return mAllAppsController;
1147 }
1148
Sunny Goyal9d69c8d2018-03-19 13:41:31 -07001149 @Override
Winson Chung1a341002018-01-17 10:00:23 -08001150 public LauncherRootView getRootView() {
1151 return (LauncherRootView) mLauncherView;
1152 }
1153
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001154 @Override
Winson Chunga6945242014-01-08 14:04:34 -08001155 public DragLayer getDragLayer() {
1156 return mDragLayer;
1157 }
1158
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001159 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001160 return mAppsView;
1161 }
1162
Winson Chunga6945242014-01-08 14:04:34 -08001163 public Workspace getWorkspace() {
1164 return mWorkspace;
1165 }
1166
1167 public Hotseat getHotseat() {
1168 return mHotseat;
1169 }
1170
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001171 public <T extends View> T getOverviewPanel() {
Winson Chung8ae41982017-11-10 11:42:13 -08001172 return (T) mOverviewPanel;
Winson Chunga6945242014-01-08 14:04:34 -08001173 }
1174
Sunny Goyal47328fd2016-05-25 18:56:41 -07001175 public DropTargetBar getDropTargetBar() {
1176 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001177 }
1178
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001179 public LauncherAppWidgetHost getAppWidgetHost() {
1180 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001181 }
Romain Guycbb89e42009-06-08 15:52:54 -07001182
Winson Chunga9abd0e2010-10-27 17:18:37 -07001183 public LauncherModel getModel() {
1184 return mModel;
1185 }
1186
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001187 public ModelWriter getModelWriter() {
1188 return mModelWriter;
1189 }
1190
Adam Cohen79d90c52016-04-22 13:29:20 -07001191 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001192 return mSharedPrefs;
1193 }
1194
Sunny Goyalf8d56fc2018-01-31 15:18:11 -08001195 public int getOrientation() { return mOldConfig.orientation; }
Jon Miranda6bed3502017-09-19 14:43:17 -07001196
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001197 @Override
1198 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001199 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001200 super.onNewIntent(intent);
1201
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001202 boolean alreadyOnHome = hasWindowFocus() && ((intent.getFlags() &
Winson15f8b172015-08-19 11:02:39 -07001203 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1204 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001205
1206 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001207 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001208 && AbstractFloatingView.getTopOpenView(this) == null;
Winson15f8b172015-08-19 11:02:39 -07001209 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001210 boolean internalStateHandled = InternalStateHandler
Sunny Goyal8b264562018-02-03 23:00:13 -08001211 .handleNewIntent(this, intent, isStarted());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001212
Winson15f8b172015-08-19 11:02:39 -07001213 if (isActionMain) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001214 if (!internalStateHandled) {
1215 // Note: There should be at most one log per method call. This is enforced
1216 // implicitly by using if-else statements.
1217 UserEventDispatcher ued = getUserEventDispatcher();
1218 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
1219 if (topOpenView != null) {
1220 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
1221 } else if (alreadyOnHome) {
1222 Target target = newContainerTarget(mStateManager.getState().containerType);
1223 target.pageIndex = mWorkspace.getCurrentPage();
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001224 ued.logActionCommand(Action.Command.HOME_INTENT, target,
1225 newContainerTarget(ContainerType.WORKSPACE));
Sunny Goyald3864fa2017-11-14 15:06:36 -08001226 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001227
Sunny Goyald3864fa2017-11-14 15:06:36 -08001228 // In all these cases, only animate if we're already on home
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001229 AbstractFloatingView.closeAllOpenViews(this, isStarted());
Jon Mirandafeba90f2016-10-06 10:53:29 -07001230
Sunny Goyala2467272018-03-16 14:53:05 -07001231 if (!isInState(NORMAL)) {
1232 // Only change state, if not already the same. This prevents cancelling any
1233 // animations running as part of resume
1234 mStateManager.goToState(NORMAL);
1235 }
Sunny Goyald3864fa2017-11-14 15:06:36 -08001236
1237 // Reset the apps view
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001238 if (!alreadyOnHome) {
Sunny Goyal7185dd62018-03-14 17:51:49 -07001239 mAppsView.reset(isStarted() /* animate */);
Sunny Goyald3864fa2017-11-14 15:06:36 -08001240 }
1241
Sunny Goyalaad33592018-08-07 17:20:35 -07001242 if (shouldMoveToDefaultScreen && !mWorkspace.isHandlingTouch()) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001243 mWorkspace.post(mWorkspace::moveToDefaultScreen);
1244 }
1245 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001246
1247 final View v = getWindow().peekDecorView();
1248 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001249 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001250 }
1251
Adam Cohen9211d422014-10-07 18:14:53 -07001252 if (mLauncherCallbacks != null) {
Sunny Goyal85462132018-04-24 11:39:37 -07001253 mLauncherCallbacks.onHomeIntent(internalStateHandled);
Adam Cohen9211d422014-10-07 18:14:53 -07001254 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001255 }
Winson15f8b172015-08-19 11:02:39 -07001256
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001257 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001258 }
1259
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001260 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001261 public void onRestoreInstanceState(Bundle state) {
1262 super.onRestoreInstanceState(state);
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001263 mWorkspace.restoreInstanceStateForChild(mSynchronouslyBoundPage);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001264 }
1265
1266 @Override
1267 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001268 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001269 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001270
Adam Cohen21cd0022013-10-09 18:57:02 -07001271 }
Sunny Goyalea609262017-10-25 15:47:38 -07001272 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001273
1274
1275 AbstractFloatingView widgets = AbstractFloatingView
1276 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1277 if (widgets != null) {
1278 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1279 widgets.saveHierarchyState(widgetsState);
1280 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1281 } else {
1282 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1283 }
1284
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001285 // We close any open folders and shortcut containers since they will not be re-opened,
1286 // and we need to make sure this state is reflected.
1287 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001288
Sunny Goyal2100c782016-08-22 16:00:03 -07001289 if (mPendingRequestArgs != null) {
1290 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1291 }
1292 if (mPendingActivityResult != null) {
1293 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001294 }
1295
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001296 super.onSaveInstanceState(outState);
1297
Adam Cohen9211d422014-10-07 18:14:53 -07001298 if (mLauncherCallbacks != null) {
1299 mLauncherCallbacks.onSaveInstanceState(outState);
1300 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001301 }
1302
1303 @Override
1304 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001305 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001306
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001307 unregisterReceiver(mScreenOffReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001308 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001309
Tony Wickhama0068302018-04-11 16:16:11 -07001310 UiFactory.setOnTouchControllersChangedListener(this, null);
1311
Winson Chungcd2b0142011-06-08 16:02:26 -07001312 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001313 // It's possible to receive onDestroy after a new Launcher activity has
1314 // been created. In this case, don't interfere with the new Launcher.
1315 if (mModel.isCurrentCallbacks(this)) {
1316 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001317 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001318 }
Sunny Goyal623eddd2018-03-02 12:24:41 -08001319 mRotationHelper.destroy();
Sunny Goyal745bad92016-05-02 10:54:12 -07001320
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001321 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001322 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001323 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001324 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001325 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001326
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001327 TextKeyListener.getInstance().release();
Tony2917a8b2017-07-31 23:29:42 -07001328 clearPendingBinds();
Hyunyoung Song31971a32019-01-27 12:31:12 -08001329 LauncherAppState.getIDP(this).removeOnChangeListener(this);
Adam Cohen9211d422014-10-07 18:14:53 -07001330 if (mLauncherCallbacks != null) {
1331 mLauncherCallbacks.onDestroy();
1332 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001333 }
1334
Sunny Goyalae502842016-06-17 08:43:56 -07001335 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1336 return mAccessibilityDelegate;
1337 }
1338
Adam Cohena9cf38f2011-05-02 15:36:58 -07001339 public DragController getDragController() {
1340 return mDragController;
1341 }
1342
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001343 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001344 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001345 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001346 }
1347
1348 @Override
1349 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1350 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001351 try {
1352 super.startIntentSenderForResult(intent, requestCode,
1353 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1354 } catch (IntentSender.SendIntentException e) {
1355 throw new ActivityNotFoundException();
1356 }
1357 }
1358
Winson Chung70d72102011-08-12 11:24:35 -07001359 /**
1360 * Indicates that we want global search for this activity by setting the globalSearch
1361 * argument for {@link #startSearch} to true.
1362 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001363 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001364 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001365 Bundle appSearchData, boolean globalSearch) {
1366 if (appSearchData == null) {
1367 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001368 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001369 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001370
Sunny Goyal6f28e712016-09-13 14:19:29 -07001371 if (mLauncherCallbacks == null ||
1372 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1373 // Starting search from the callbacks failed. Start the default global search.
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001374 super.startSearch(initialQuery, selectInitialQuery, appSearchData, true);
Ian Parkinson047036e2014-09-01 15:40:53 +01001375 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001376
1377 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001378 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001379 }
1380
Joe Onorato9c1289c2009-08-17 11:03:03 -04001381 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001382 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001383 }
1384
Adam Cohen517a7f52014-03-01 12:12:59 -08001385 public boolean isWorkspaceLoading() {
1386 return mWorkspaceLoading;
1387 }
1388
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001389 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001390 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001391 }
1392
Sunny Goyal04a324a2017-01-20 21:08:59 -08001393 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001394 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001395 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001396
Sunny Goyal2100c782016-08-22 16:00:03 -07001397 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001398 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001399 if (LOGD) {
1400 Log.d(TAG, "Adding widget from drop");
1401 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001402 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001403 }
1404
Sunny Goyal2100c782016-08-22 16:00:03 -07001405 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001406 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1407 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1408 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001409
Adam Cohenad4e15c2013-10-17 16:21:35 -07001410 Runnable onComplete = new Runnable() {
1411 @Override
1412 public void run() {
1413 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001414 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001415 }
1416 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001417 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001418 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001419 }
1420 }
Romain Guycbb89e42009-06-08 15:52:54 -07001421
Sunny Goyalefb7e842018-10-04 15:11:00 -07001422 public void addPendingItem(PendingAddItemInfo info, int container, int screenId,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001423 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001424 info.container = container;
1425 info.screenId = screenId;
1426 if (cell != null) {
1427 info.cellX = cell[0];
1428 info.cellY = cell[1];
1429 }
1430 info.spanX = spanX;
1431 info.spanY = spanY;
1432
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001433 switch (info.itemType) {
1434 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1435 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001436 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001437 break;
1438 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001439 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001440 break;
1441 default:
1442 throw new IllegalStateException("Unknown item type: " + info.itemType);
1443 }
1444 }
1445
Adam Cohenfbba09b2011-07-18 21:43:05 -07001446 /**
1447 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001448 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001449 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001450 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1451 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001452 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1453 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1454 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001455 }
1456
Adam Cohenfbba09b2011-07-18 21:43:05 -07001457 /**
1458 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001459 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001460 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001461 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001462 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001463 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001464 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001465 // In the case where we've prebound the widget, we remove it from the DragLayer
1466 if (LOGD) {
1467 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1468 }
1469 getDragLayer().removeView(hostView);
1470
Adam Cohened66b2b2012-01-23 17:28:51 -08001471 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001472 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001473
1474 // Clear the boundWidget so that it doesn't get destroyed.
1475 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001476 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001477 // In this case, we either need to start an activity to get permission to bind
1478 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001479 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1480 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1481 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1482 this, info.componentName);
1483 } else {
1484 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1485 }
Adam Cohendd70d662012-10-04 16:53:44 -07001486 Bundle options = info.bindOptions;
1487
Sunny Goyalffe83f12014-08-14 17:39:34 -07001488 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1489 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001490 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001491 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001492 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001493 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001494 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001495 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001496 }
1497
Sunny Goyalefb7e842018-10-04 15:11:00 -07001498 FolderIcon addFolder(CellLayout layout, int container, final int screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001499 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001500 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001501 folderInfo.title = getText(R.string.folder_name);
1502
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001503 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001504 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001505
1506 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001507 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301508 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001509 // Force measure the new folder icon
1510 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1511 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001512 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001513 }
Romain Guycbb89e42009-06-08 15:52:54 -07001514
Winsonc0b52fe2015-09-09 16:38:15 -07001515 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001516 * Unbinds the view for the specified item, and removes the item and all its children.
1517 *
1518 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001519 * @param itemInfo the {@link ItemInfo} for this view.
1520 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001521 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001522 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001523 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001524 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001525 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1526 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001527 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001528 } else {
1529 mWorkspace.removeWorkspaceItem(v);
1530 }
Winsonc0b52fe2015-09-09 16:38:15 -07001531 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001532 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001533 }
1534 } else if (itemInfo instanceof FolderInfo) {
1535 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001536 if (v instanceof FolderIcon) {
1537 ((FolderIcon) v).removeListeners();
1538 }
Winsonc0b52fe2015-09-09 16:38:15 -07001539 mWorkspace.removeWorkspaceItem(v);
1540 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001541 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001542 }
1543 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1544 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001545 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001546 if (deleteFromDb) {
Tony Wickham6a71a5b2018-08-21 11:40:23 -07001547 getModelWriter().deleteWidgetInfo(widgetInfo, getAppWidgetHost());
Winsonc0b52fe2015-09-09 16:38:15 -07001548 }
1549 } else {
1550 return false;
1551 }
1552 return true;
1553 }
1554
Tony Wickham6a71a5b2018-08-21 11:40:23 -07001555
Winsonc0b52fe2015-09-09 16:38:15 -07001556
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001557 @Override
1558 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001559 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001560 }
1561
Joe Onorato88ec0992009-11-19 13:16:06 -08001562 @Override
1563 public void onBackPressed() {
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08001564 if (finishAutoCancelActionMode()) {
1565 return;
1566 }
Adam Cohen9211d422014-10-07 18:14:53 -07001567 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1568 return;
1569 }
1570
Sunny Goyal45478022015-06-08 16:52:41 -07001571 if (mDragController.isDragging()) {
1572 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001573 return;
1574 }
1575
Jon Mirandafeba90f2016-10-06 10:53:29 -07001576 // Note: There should be at most one log per method call. This is enforced implicitly
1577 // by using if-else statements.
1578 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001579 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
Tony Wickham52c1b662018-05-21 13:13:58 -07001580 if (topView != null && topView.onBackPressed()) {
1581 // Handled by the floating view.
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001582 } else {
Sunny Goyale39690b2018-08-02 13:35:07 -07001583 mStateManager.getState().onBackPressed(this);
Michael Jurkaaf442092010-06-10 17:01:57 -07001584 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001585 }
1586
Sunny Goyalfe770c92016-09-27 14:38:58 -07001587 @TargetApi(Build.VERSION_CODES.M)
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001588 @Override
Jon Miranda73c27ec2018-05-16 18:06:23 -07001589 public ActivityOptions getActivityLaunchOptions(View v) {
1590 return mAppTransitionManager.getActivityLaunchOptions(this, v);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001591 }
1592
Tony Wickham3a6746a2018-03-29 09:39:56 -07001593 public LauncherAppTransitionManager getAppTransitionManager() {
1594 return mAppTransitionManager;
1595 }
1596
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001597 @TargetApi(Build.VERSION_CODES.M)
1598 @Override
1599 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
1600 // Due to legacy reasons, direct call shortcuts require Launchers to have the
1601 // corresponding permission. Show the appropriate permission prompt if that
1602 // is the case.
1603 if (intent.getComponent() == null
1604 && Intent.ACTION_CALL.equals(intent.getAction())
1605 && checkSelfPermission(android.Manifest.permission.CALL_PHONE) !=
1606 PackageManager.PERMISSION_GRANTED) {
1607
1608 setWaitingForResult(PendingRequestArgs
1609 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
1610 requestPermissions(new String[]{android.Manifest.permission.CALL_PHONE},
1611 REQUEST_PERMISSION_CALL_PHONE);
1612 return true;
1613 } else {
1614 return false;
1615 }
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001616 }
1617
Hyunyoung Song46d07f72018-05-22 15:41:25 -07001618 @Override
Hyunyoung Songfc007472018-10-25 14:09:50 -07001619 public int getCurrentState() {
1620 if(mStateManager.getState() == LauncherState.ALL_APPS) {
1621 return StatsLogUtils.LAUNCHER_STATE_ALLAPPS;
1622 } else if (mStateManager.getState() == LauncherState.OVERVIEW) {
1623 return StatsLogUtils.LAUNCHER_STATE_OVERVIEW;
1624 }
Hyunyoung Song0ae38882018-11-05 14:45:19 -08001625 return StatsLogUtils.LAUNCHER_STATE_HOME;
Hyunyoung Songfc007472018-10-25 14:09:50 -07001626 }
1627
1628 @Override
Hyunyoung Song46d07f72018-05-22 15:41:25 -07001629 public void modifyUserEvent(LauncherLogProto.LauncherEvent event) {
1630 if (event.srcTarget != null && event.srcTarget.length > 0 &&
1631 event.srcTarget[1].containerType == ContainerType.PREDICTION) {
1632 Target[] targets = new Target[3];
1633 targets[0] = event.srcTarget[0];
1634 targets[1] = event.srcTarget[1];
1635 targets[2] = newTarget(Target.Type.CONTAINER);
1636 event.srcTarget = targets;
1637 LauncherState state = mStateManager.getState();
1638 if (state == LauncherState.ALL_APPS) {
1639 event.srcTarget[2].containerType = ContainerType.ALLAPPS;
1640 } else if (state == LauncherState.OVERVIEW) {
1641 event.srcTarget[2].containerType = ContainerType.TASKSWITCHER;
1642 }
1643 }
1644 }
1645
Sunny Goyala7ce1662016-05-31 15:01:35 -07001646 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Hyunyoung Song1241e612018-05-15 21:46:51 -07001647 boolean success = super.startActivitySafely(v, intent, item);
1648 if (success && v instanceof BubbleTextView) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001649 // This is set to the view that launched the activity that navigated the user away
1650 // from launcher. Since there is no callback for when the activity has finished
1651 // launching, enable the press state and keep this reference to reset the press
1652 // state when we return to launcher.
1653 BubbleTextView btv = (BubbleTextView) v;
1654 btv.setStayPressed(true);
1655 setOnResumeCallback(btv);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001656 }
Hyunyoung Song1241e612018-05-15 21:46:51 -07001657 return success;
Michael Jurka86a720e2012-05-09 11:23:23 -07001658 }
1659
Winson Chung3d503fb2011-07-13 17:25:49 -07001660 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001661 // TODO: Remove this method
Sunny Goyal876e4622018-11-02 13:50:40 -07001662 return mHotseat != null && (layout == mHotseat);
Winson Chung3d503fb2011-07-13 17:25:49 -07001663 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001664
Winson Chung3d503fb2011-07-13 17:25:49 -07001665 /**
1666 * Returns the CellLayout of the specified container at the specified screen.
1667 */
Sunny Goyalefb7e842018-10-04 15:11:00 -07001668 public CellLayout getCellLayout(int container, int screenId) {
Sunny Goyal876e4622018-11-02 13:50:40 -07001669 return (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1670 ? mHotseat : mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08001671 }
1672
Michael Jurkae326f182011-11-21 14:05:46 -08001673 @Override
1674 public void onTrimMemory(int level) {
1675 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07001676 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
1677 // The widget preview db can result in holding onto over
1678 // 3MB of memory for caching which isn't necessary.
1679 SQLiteDatabase.releaseMemory();
1680
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08001681 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07001682 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08001683 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01001684 if (mLauncherCallbacks != null) {
1685 mLauncherCallbacks.onTrimMemory(level);
1686 }
Winson Chung62a70be2018-02-23 15:10:05 -08001687 UiFactory.onTrimMemory(this, level);
Michael Jurkae326f182011-11-21 14:05:46 -08001688 }
1689
Michael Jurkad7c28052012-04-27 15:43:36 -07001690 @Override
1691 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07001692 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07001693 final List<CharSequence> text = event.getText();
1694 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07001695 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001696 // TODO: When can workspace be null?
1697 text.add(mWorkspace == null
1698 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07001699 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07001700 return result;
1701 }
1702
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001703 public void setOnResumeCallback(OnResumeCallback callback) {
1704 if (mOnResumeCallback != null) {
1705 mOnResumeCallback.onLauncherResume();
1706 }
1707 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07001708 }
1709
Michael Jurka7607c2f2013-04-03 14:33:19 -07001710 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001711 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001712 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07001713 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001714 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07001715 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001716 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07001717 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07001718 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07001719 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001720 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001721
Joe Onorato9c1289c2009-08-17 11:03:03 -04001722 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07001723 * Clear any pending bind callbacks. This is called when is loader is planning to
1724 * perform a full rebind from scratch.
1725 */
1726 @Override
1727 public void clearPendingBinds() {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001728 if (mPendingExecutor != null) {
1729 mPendingExecutor.markCompleted();
1730 mPendingExecutor = null;
1731 }
1732 }
1733
1734 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001735 * Refreshes the shortcuts shown on the workspace.
1736 *
1737 * Implementation of the method from LauncherModel.Callbacks.
1738 */
1739 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001740 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001741 // Floating panels (except the full widget sheet) are associated with individual icons. If
1742 // we are starting a fresh bind, close all such panels as all the icons are about
1743 // to go away.
1744 AbstractFloatingView.closeOpenViews(this, true,
Sunny Goyal7ede6112017-12-05 15:11:21 -08001745 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_REBIND_SAFE);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08001746
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001747 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08001748
Winson Chungd64d1762013-08-20 14:37:16 -07001749 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07001750 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07001751 mWorkspace.removeAllWorkspaceScreens();
Sunny Goyalc11fac32018-02-27 19:20:15 -08001752 mAppWidgetHost.clearViews();
Winson Chungd64d1762013-08-20 14:37:16 -07001753
Winson Chung3d503fb2011-07-13 17:25:49 -07001754 if (mHotseat != null) {
Sunny Goyal11e96492018-03-23 17:06:00 -07001755 mHotseat.resetLayout(mDeviceProfile.isVerticalBarLayout());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001756 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001757 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001758 }
1759
Adam Cohendcd297f2013-06-18 13:13:40 -07001760 @Override
Sunny Goyalefb7e842018-10-04 15:11:00 -07001761 public void bindScreens(IntArray orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001762 // Make sure the first screen is always at the start.
Ryan Lothianfa530cd2018-10-12 14:14:16 -04001763 if (FeatureFlags.QSB_ON_FIRST_SCREEN.get() &&
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001764 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalefb7e842018-10-04 15:11:00 -07001765 orderedScreenIds.removeValue(Workspace.FIRST_SCREEN_ID);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001766 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Ryan Lothianfa530cd2018-10-12 14:14:16 -04001767 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN.get()
Ryan Lothian688e9892018-10-03 13:32:01 -04001768 && orderedScreenIds.isEmpty()) {
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001769 // If there are no screens, we need to have an empty screen
1770 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07001771 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001772 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07001773
Winsonc7d2e832016-07-28 12:24:55 -07001774 // After we have added all the screens, if the wallpaper was locked to the default state,
1775 // then notify to indicate that it can be released and a proper wallpaper offset can be
1776 // computed before the next layout
1777 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07001778 }
1779
Sunny Goyalefb7e842018-10-04 15:11:00 -07001780 private void bindAddScreens(IntArray orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001781 int count = orderedScreenIds.size();
1782 for (int i = 0; i < count; i++) {
Sunny Goyalefb7e842018-10-04 15:11:00 -07001783 int screenId = orderedScreenIds.get(i);
Ryan Lothianfa530cd2018-10-12 14:14:16 -04001784 if (!FeatureFlags.QSB_ON_FIRST_SCREEN.get() || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001785 // No need to bind the first screen, as its always bound.
1786 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
1787 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001788 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001789 }
1790
Sunny Goyalb23980c2017-08-17 07:45:25 -07001791 @Override
Tony Wickham6a71a5b2018-08-21 11:40:23 -07001792 public void preAddApps() {
1793 // If there's an undo snackbar, force it to complete to ensure empty screens are removed
1794 // before trying to add new items.
1795 mModelWriter.commitDelete();
1796 AbstractFloatingView snackbar = AbstractFloatingView.getOpenView(this, TYPE_SNACKBAR);
1797 if (snackbar != null) {
1798 snackbar.post(() -> snackbar.close(true));
1799 }
1800 }
1801
1802 @Override
Sunny Goyalefb7e842018-10-04 15:11:00 -07001803 public void bindAppsAdded(IntArray newScreens, ArrayList<ItemInfo> addNotAnimated,
Sunny Goyal29947f02017-12-18 13:49:44 -08001804 ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07001805 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08001806 if (newScreens != null) {
1807 bindAddScreens(newScreens);
1808 }
Winson Chungd64d1762013-08-20 14:37:16 -07001809
1810 // We add the items without animation on non-visible pages, and with
1811 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08001812 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001813 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001814 }
Adam Cohen76a47a12014-02-05 11:47:43 -08001815 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001816 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07001817 }
Winson Chungc58497e2013-09-03 17:48:37 -07001818
Winson Chung87412982013-10-03 18:34:14 -07001819 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07001820 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001821 }
1822
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001823 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001824 * Bind the items start-end from the list.
1825 *
1826 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001827 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07001828 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07001829 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001830 // Get the list of added items and intersect them with the set of items here
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001831 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07001832 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001833 Workspace workspace = mWorkspace;
Sunny Goyalefb7e842018-10-04 15:11:00 -07001834 int newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07001835 int end = items.size();
1836 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001837 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07001838
1839 // Short circuit if we are loading dock items for a configuration which has no dock
1840 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
1841 mHotseat == null) {
1842 continue;
1843 }
1844
Sunny Goyal639e9062015-08-19 19:17:06 -07001845 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001846 switch (item.itemType) {
1847 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1848 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08001849 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001850 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07001851 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001852 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001853 }
1854 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07001855 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001856 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001857 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001858 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001859 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07001860 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1861 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07001862 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07001863 if (view == null) {
1864 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001865 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08001866 break;
1867 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001868 default:
1869 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001870 }
Sunny Goyal639e9062015-08-19 19:17:06 -07001871
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001872 /*
1873 * Remove colliding items.
1874 */
1875 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1876 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
1877 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
1878 View v = cl.getChildAt(item.cellX, item.cellY);
1879 Object tag = v.getTag();
1880 String desc = "Collision while binding workspace item: " + item
1881 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08001882 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001883 throw (new RuntimeException(desc));
1884 } else {
1885 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001886 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001887 continue;
1888 }
1889 }
1890 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05301891 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07001892 if (animateIcons) {
1893 // Animate all the applications up now
1894 view.setAlpha(0f);
1895 view.setScaleX(0f);
1896 view.setScaleY(0f);
1897 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08001898 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07001899 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001900 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07001901
Sunny Goyalf0b6db72018-08-13 16:10:14 -07001902 // Animate to the correct page
1903 if (animateIcons && newItemsScreenId > -1) {
1904 AnimatorSet anim = new AnimatorSet();
1905 anim.playTogether(bounceAnims);
Jon Miranda64eb8ea2018-03-29 11:43:58 -07001906
Sunny Goyalefb7e842018-10-04 15:11:00 -07001907 int currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyalf0b6db72018-08-13 16:10:14 -07001908 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
1909 final Runnable startBounceAnimRunnable = anim::start;
1910
1911 if (newItemsScreenId != currentScreenId) {
1912 // We post the animation slightly delayed to prevent slowdowns
1913 // when we are loading right after we return to launcher.
1914 mWorkspace.postDelayed(new Runnable() {
1915 public void run() {
1916 if (mWorkspace != null) {
1917 AbstractFloatingView.closeAllOpenViews(Launcher.this, false);
1918
1919 mWorkspace.snapToPage(newScreenIndex);
1920 mWorkspace.postDelayed(startBounceAnimRunnable,
1921 NEW_APPS_ANIMATION_DELAY);
Winson Chung94d67682013-09-25 16:29:40 -07001922 }
Sunny Goyalf0b6db72018-08-13 16:10:14 -07001923 }
1924 }, NEW_APPS_PAGE_MOVE_DELAY);
1925 } else {
1926 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07001927 }
Winson Chung64359a52013-07-08 17:17:08 -07001928 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001929 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001930 }
1931
Joe Onorato9c1289c2009-08-17 11:03:03 -04001932 /**
1933 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001934 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07001935 public void bindAppWidget(LauncherAppWidgetInfo item) {
1936 View view = inflateAppWidget(item);
1937 if (view != null) {
1938 mWorkspace.addInScreen(view, item);
1939 mWorkspace.requestLayout();
1940 }
1941 }
1942
1943 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001944 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05301945 PendingAppWidgetHostView view =
1946 new PendingAppWidgetHostView(this, item, mIconCache, true);
1947 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07001948 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001949 }
1950
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001951 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001952
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001953 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08001954
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001955 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
1956 // If the provider is not ready, bind as a pending widget.
1957 appWidgetInfo = null;
1958 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
1959 // The widget id is not valid. Try to find the widget based on the provider info.
1960 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
1961 } else {
1962 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
1963 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07001964
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001965 // If the provider is ready, but the width is not yet restored, try to restore it.
1966 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
1967 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07001968 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001969 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
1970 + " belongs to component " + item.providerName
1971 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001972 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07001973 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07001974 }
Sunny Goyalff572272014-07-23 13:58:07 -07001975
Sunny Goyal84b4adc2015-08-12 15:12:16 -07001976 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001977 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07001978 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
1979 // Id has not been allocated yet. Allocate a new id.
1980 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
1981 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07001982
Sunny Goyald478c832016-04-01 12:04:16 -07001983 // Also try to bind the widget. If the bind fails, the user will be shown
1984 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08001985 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07001986 pendingInfo.spanX = item.spanX;
1987 pendingInfo.spanY = item.spanY;
1988 pendingInfo.minSpanX = item.minSpanX;
1989 pendingInfo.minSpanY = item.minSpanY;
1990 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07001991
1992 boolean isDirectConfig =
1993 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
1994 if (isDirectConfig && item.bindOptions != null) {
1995 Bundle newOptions = item.bindOptions.getExtras();
1996 if (options != null) {
1997 newOptions.putAll(options);
1998 }
1999 options = newOptions;
2000 }
Sunny Goyald478c832016-04-01 12:04:16 -07002001 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2002 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002003
Sunny Goyal86df1382016-08-10 15:03:22 -07002004 // We tried to bind once. If we were not able to bind, we would need to
2005 // go through the permission dialog, which means we cannot skip the config
2006 // activity.
2007 item.bindOptions = null;
2008 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2009
Sunny Goyald478c832016-04-01 12:04:16 -07002010 // Bind succeeded
2011 if (success) {
2012 // If the widget has a configure activity, it is still needs to set it up,
2013 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002014 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002015 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2016 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002017 }
Sunny Goyald478c832016-04-01 12:04:16 -07002018
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002019 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002020 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002021 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002022 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002023 // The widget was marked as UI not ready, but there is no configure activity to
2024 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002025 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002026 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002027 }
Sunny Goyalff572272014-07-23 13:58:07 -07002028 }
2029
Sunny Goyald5462aa2016-11-22 16:52:39 +05302030 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002031 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002032 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002033 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002034 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Tony Wickham6a71a5b2018-08-21 11:40:23 -07002035 getModelWriter().deleteWidgetInfo(item, getAppWidgetHost());
Sunny Goyalb23980c2017-08-17 07:45:25 -07002036 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002037 }
2038
Sunny Goyal233ee962015-08-03 13:05:01 -07002039 item.minSpanX = appWidgetInfo.minSpanX;
2040 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302041 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002042 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302043 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002044 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302045 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002046
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002047 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002048 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002049 }
2050
Sunny Goyalff572272014-07-23 13:58:07 -07002051 /**
2052 * Restores a pending widget.
2053 *
2054 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002055 */
Sunny Goyald478c832016-04-01 12:04:16 -07002056 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002057 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2058 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2059 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002060 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002061 }
2062
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002063 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002064 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002065 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2066 info.pendingItemInfo = null;
2067 }
Sunny Goyalff572272014-07-23 13:58:07 -07002068
Sunny Goyalba47faa2017-10-04 16:50:57 -07002069 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002070 view.reInflate();
Sunny Goyalba47faa2017-10-04 16:50:57 -07002071 }
2072
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002073 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002074 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002075 }
2076
Adam Cohen1462de32012-07-24 22:34:36 -07002077 public void onPageBoundSynchronously(int page) {
Sunny Goyalf4aceab2017-12-06 16:16:31 -08002078 mSynchronouslyBoundPage = page;
Adam Cohen1462de32012-07-24 22:34:36 -07002079 }
2080
Sunny Goyal527c7d32015-08-28 15:19:36 -07002081 @Override
2082 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2083 if (mPendingExecutor != null) {
2084 mPendingExecutor.markCompleted();
2085 }
2086 mPendingExecutor = executor;
Sunny Goyal60180b02018-02-07 12:56:30 -08002087 if (!isInState(ALL_APPS)) {
2088 mAppsView.getAppsStore().setDeferUpdates(true);
2089 mPendingExecutor.execute(() -> mAppsView.getAppsStore().setDeferUpdates(false));
2090 }
2091
Sunny Goyal527c7d32015-08-28 15:19:36 -07002092 executor.attachTo(this);
2093 }
2094
2095 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2096 if (mPendingExecutor == executor) {
2097 mPendingExecutor = null;
2098 }
2099 }
2100
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002101 @Override
2102 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
Sunny Goyal6001ea22018-05-10 16:31:00 -07002103 AlphaProperty property = mDragLayer.getAlphaProperty(ALPHA_INDEX_LAUNCHER_LOAD);
2104 if (property.getValue() < 1) {
2105 ObjectAnimator anim = ObjectAnimator.ofFloat(property, MultiValueAlpha.VALUE, 1);
2106 if (executor != null) {
2107 anim.addListener(new AnimatorListenerAdapter() {
2108 @Override
2109 public void onAnimationEnd(Animator animation) {
2110 executor.onLoadAnimationCompleted();
2111 }
2112 });
2113 }
2114 anim.start();
Sunny Goyal29947f02017-12-18 13:49:44 -08002115 } else if (executor != null) {
2116 executor.onLoadAnimationCompleted();
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002117 }
2118 }
2119
Joe Onorato9c1289c2009-08-17 11:03:03 -04002120 /**
2121 * Callback saying that there aren't any more items to bind.
2122 *
2123 * Implementation of the method from LauncherModel.Callbacks.
2124 */
Tonyf80e8932018-12-21 11:58:04 -08002125 public void finishBindingItems(int pageBoundFirst) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002126 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002127 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002128
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002129 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002130
Sunny Goyal2100c782016-08-22 16:00:03 -07002131 if (mPendingActivityResult != null) {
2132 handleActivityResult(mPendingActivityResult.requestCode,
2133 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2134 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002135 }
2136
Sunny Goyala474a9b2017-05-04 16:47:11 -07002137 InstallShortcutReceiver.disableAndFlushInstallQueue(
2138 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002139
Tonyf80e8932018-12-21 11:58:04 -08002140 // When undoing the removal of the last item on a page, return to that page.
2141 mWorkspace.setCurrentPage(pageBoundFirst);
Tony Wickham6a71a5b2018-08-21 11:40:23 -07002142
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002143 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002144 }
2145
Winson Chunga2413752012-04-03 14:22:34 -07002146 private boolean canRunNewAppsAnimation() {
2147 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002148 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002149 }
2150
Winson Chungc9168342013-06-26 14:54:55 -07002151 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002152 ValueAnimator bounceAnim = new PropertyListBuilder().alpha(1).scale(1).build(v)
2153 .setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
Winson Chungc9168342013-06-26 14:54:55 -07002154 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002155 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002156 return bounceAnim;
2157 }
2158
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002159 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002160 * Add the icons for all apps.
2161 *
2162 * Implementation of the method from LauncherModel.Callbacks.
2163 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002164 public void bindAllApplications(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002165 mAppsView.getAppsStore().setApps(apps);
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002166
Adam Cohen9211d422014-10-07 18:14:53 -07002167 if (mLauncherCallbacks != null) {
2168 mLauncherCallbacks.bindAllApplications(apps);
2169 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002170 }
2171
2172 /**
Zak Cohen658c67a2018-10-19 14:21:05 -07002173 * Copies LauncherModel's map of activities to shortcut counts to Launcher's. This is necessary
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002174 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2175 */
2176 @Override
Zak Cohen658c67a2018-10-19 14:21:05 -07002177 public void bindDeepShortcutMap(HashMap<ComponentKey, Integer> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002178 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002179 }
2180
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002181 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002182 * A package was updated.
2183 *
2184 * Implementation of the method from LauncherModel.Callbacks.
2185 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002186 @Override
2187 public void bindAppsAddedOrUpdated(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002188 mAppsView.getAppsStore().addOrUpdateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002189 }
2190
Sunny Goyal4390ace2014-10-13 11:33:11 -07002191 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002192 public void bindPromiseAppProgressUpdated(PromiseAppInfo app) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002193 mAppsView.getAppsStore().updatePromiseAppProgress(app);
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002194 }
2195
2196 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002197 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002198 mWorkspace.widgetsRestored(widgets);
2199 }
2200
Joe Onorato9c1289c2009-08-17 11:03:03 -04002201 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002202 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002203 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002204 *
2205 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002206 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002207 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002208 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002209 if (!updated.isEmpty()) {
2210 mWorkspace.updateShortcuts(updated);
2211 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002212 }
2213
2214 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002215 * Update the state of a package, typically related to install state.
2216 *
2217 * Implementation of the method from LauncherModel.Callbacks.
2218 */
Sunny Goyale755d462014-07-22 13:48:29 -07002219 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002220 public void bindRestoreItemsChange(HashSet<ItemInfo> updates) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07002221 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07002222 }
2223
2224 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002225 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07002226 * in addition to specific applications to remove, the reason being that
2227 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002228 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07002229 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002230 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07002231 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002232 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002233 mWorkspace.removeItemsByMatcher(matcher);
2234 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002235 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002236
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002237 @Override
2238 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002239 mAppsView.getAppsStore().removeApps(appInfos);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002240 }
Joe Onoratobe386092009-11-17 17:32:16 -08002241
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002242 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002243 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
2244 mPopupDataProvider.setAllWidgets(allWidgets);
Tony Wickham26b17462017-03-20 17:12:24 -07002245 }
2246
Tony Wickham86222d22017-03-29 15:30:43 -07002247 /**
2248 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
2249 * refreshes the widgets and shortcuts associated with the given package/user
2250 */
2251 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07002252 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002253 }
2254
Winson Chung80baf5a2010-08-09 16:03:15 -07002255 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002256 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08002257 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002258 @Override
2259 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
2260 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002261
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002262 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
2263 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07002264 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002265 writer.println(prefix + " Homescreen " + i);
2266
2267 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
2268 for (int j = 0; j < layout.getChildCount(); j++) {
2269 Object tag = layout.getChildAt(j).getTag();
2270 if (tag != null) {
2271 writer.println(prefix + " " + tag.toString());
2272 }
2273 }
2274 }
2275
2276 writer.println(prefix + " Hotseat");
Sunny Goyal876e4622018-11-02 13:50:40 -07002277 ViewGroup layout = mHotseat.getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07002278 for (int j = 0; j < layout.getChildCount(); j++) {
2279 Object tag = layout.getChildAt(j).getTag();
2280 if (tag != null) {
2281 writer.println(prefix + " " + tag.toString());
2282 }
Adam Cohen4caf2982013-08-20 18:54:31 -07002283 }
Sunny Goyala1365452015-10-01 15:46:24 -07002284 }
2285
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002286 writer.println(prefix + "Misc:");
2287 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
2288 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
2289 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
Sunny Goyale43d00d2018-05-14 14:23:18 -07002290 writer.println(" mRotationHelper: " + mRotationHelper);
Sunny Goyalcb2c5052018-10-15 12:30:29 -07002291 // Extra logging for b/116853349
2292 mDragLayer.dumpAlpha(writer);
Sunny Goyale43d00d2018-05-14 14:23:18 -07002293 dumpMisc(writer);
Sunny Goyal2bba1902018-02-27 12:20:50 -08002294
2295 try {
2296 FileLog.flushAll(writer);
2297 } catch (Exception e) {
2298 // Ignore
2299 }
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002300
2301 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002302
Adam Cohen9211d422014-10-07 18:14:53 -07002303 if (mLauncherCallbacks != null) {
2304 mLauncherCallbacks.dump(prefix, fd, writer, args);
2305 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002306 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002307
Sunny Goyal66b24572016-09-21 15:57:55 -07002308 @Override
Sunny Goyal66b24572016-09-21 15:57:55 -07002309 public void onProvideKeyboardShortcuts(
2310 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
2311
2312 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002313 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002314 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
2315 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
Vadim Tryshev74c261c2018-06-12 13:44:43 -07002316 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.widget_button_text),
2317 KeyEvent.KEYCODE_W, KeyEvent.META_CTRL_ON));
Sunny Goyal66b24572016-09-21 15:57:55 -07002318 }
Vadim Tryshev3455f8d2018-05-11 20:02:20 -07002319 final View currentFocus = getCurrentFocus();
2320 if (currentFocus != null) {
2321 if (new CustomActionsPopup(this, currentFocus).canShow()) {
2322 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
2323 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
2324 }
2325 if (currentFocus.getTag() instanceof ItemInfo
2326 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
2327 shortcutInfos.add(new KeyboardShortcutInfo(
2328 getString(R.string.shortcuts_menu_with_notifications_description),
2329 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
2330 }
Sunny Goyal66b24572016-09-21 15:57:55 -07002331 }
2332 if (!shortcutInfos.isEmpty()) {
2333 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
2334 }
2335
2336 super.onProvideKeyboardShortcuts(data, menu, deviceId);
2337 }
2338
2339 @Override
2340 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
2341 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
2342 switch (keyCode) {
2343 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002344 if (isInState(NORMAL)) {
2345 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07002346 return true;
2347 }
2348 break;
2349 case KeyEvent.KEYCODE_S: {
2350 View focusedView = getCurrentFocus();
2351 if (focusedView instanceof BubbleTextView
2352 && focusedView.getTag() instanceof ItemInfo
2353 && mAccessibilityDelegate.performAction(focusedView,
2354 (ItemInfo) focusedView.getTag(),
2355 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal00ac9202017-11-09 15:24:06 -08002356 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07002357 return true;
2358 }
2359 break;
2360 }
2361 case KeyEvent.KEYCODE_O:
2362 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
2363 return true;
2364 }
2365 break;
Vadim Tryshev74c261c2018-06-12 13:44:43 -07002366 case KeyEvent.KEYCODE_W:
2367 if (isInState(NORMAL)) {
2368 OptionsPopupView.openWidgets(this);
2369 return true;
2370 }
2371 break;
Sunny Goyal66b24572016-09-21 15:57:55 -07002372 }
2373 }
2374 return super.onKeyShortcut(keyCode, event);
2375 }
2376
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002377 @Override
2378 public boolean onKeyUp(int keyCode, KeyEvent event) {
2379 if (keyCode == KeyEvent.KEYCODE_MENU) {
2380 // KEYCODE_MENU is sent by some tests, for example
2381 // LauncherJankTests#testWidgetsContainerFling. Don't just remove its handling.
2382 if (!mDragController.isDragging() && !mWorkspace.isSwitchingState() &&
2383 isInState(NORMAL)) {
2384 // Close any open floating views.
2385 AbstractFloatingView.closeAllOpenViews(this);
2386
2387 // Setting the touch point to (-1, -1) will show the options popup in the center of
2388 // the screen.
Sunny Goyal2fd7a8b2018-03-30 17:10:13 -07002389 OptionsPopupView.showDefaultOptions(this, -1, -1);
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002390 }
2391 return true;
2392 }
2393 return super.onKeyUp(keyCode, event);
2394 }
2395
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002396 public static Launcher getLauncher(Context context) {
Sunny Goyal87b5eb62018-07-03 15:53:39 -07002397 return (Launcher) fromContext(context);
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002398 }
2399
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002400 /**
Sunny Goyalab770a12018-11-14 15:17:26 -08002401 * Just a wrapper around the type cast to allow easier tracking of calls.
2402 */
2403 public static <T extends Launcher> T cast(ActivityContext activityContext) {
2404 return (T) activityContext;
2405 }
2406
2407 /**
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002408 * Callback for listening for onResume
2409 */
2410 public interface OnResumeCallback {
2411
2412 void onLauncherResume();
2413 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002414}