blob: f09b6e852d6fa75c68a300aaf91114838f880dc0 [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
vadimt00d42552018-12-11 17:59:36 -080077import androidx.annotation.Nullable;
78
Sunny Goyal651077b2014-06-30 14:15:31 -070079import com.android.launcher3.DropTarget.DragObject;
Sunny Goyalae502842016-06-17 08:43:56 -070080import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070081import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070082import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal7ede6112017-12-05 15:11:21 -080083import com.android.launcher3.allapps.DiscoveryBounce;
Sunny Goyalf0b6db72018-08-13 16:10:14 -070084import com.android.launcher3.anim.PropertyListBuilder;
Sunny Goyalffe83f12014-08-14 17:39:34 -070085import com.android.launcher3.compat.AppWidgetManagerCompat;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -070086import com.android.launcher3.compat.LauncherAppsCompatVO;
Tony Wickhame0c33232016-02-08 11:37:04 -080087import com.android.launcher3.config.FeatureFlags;
vadimt00d42552018-12-11 17:59:36 -080088import com.android.launcher3.dot.DotInfo;
Vadim Tryshevfedca432015-08-19 17:55:02 -070089import com.android.launcher3.dragndrop.DragController;
90import com.android.launcher3.dragndrop.DragLayer;
91import com.android.launcher3.dragndrop.DragView;
Jon Mirandaaeb4dd02018-07-11 14:16:23 -070092import com.android.launcher3.folder.Folder;
Sunny Goyal26119432016-02-18 22:09:23 +000093import com.android.launcher3.folder.FolderIcon;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070094import com.android.launcher3.folder.FolderIconPreviewVerifier;
Sunny Goyalf840f102018-09-21 14:41:05 -070095import com.android.launcher3.icons.IconCache;
Sunny Goyal66b24572016-09-21 15:57:55 -070096import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -070097import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -070098import com.android.launcher3.logging.FileLog;
Hyunyoung Songfc007472018-10-25 14:09:50 -070099import com.android.launcher3.logging.StatsLogUtils;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700100import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700101import com.android.launcher3.logging.UserEventDispatcher.UserEventDelegate;
Sunny Goyala535ae42017-02-27 10:07:13 -0800102import com.android.launcher3.model.ModelWriter;
Sunny Goyala535ae42017-02-27 10:07:13 -0800103import com.android.launcher3.notification.NotificationListener;
Tony Wickham540913e2017-01-23 11:47:51 -0800104import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -0800105import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700106import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyalf8088ee2017-11-10 14:52:00 -0800107import com.android.launcher3.states.InternalStateHandler;
Sunny Goyal623eddd2018-03-02 12:24:41 -0800108import com.android.launcher3.states.RotationHelper;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800109import com.android.launcher3.touch.ItemClickHandler;
Sunny Goyala5ace712017-11-15 17:12:51 -0800110import com.android.launcher3.uioverrides.UiFactory;
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700111import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530112import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
113import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
Sunny Goyalaeb16432017-10-16 11:46:41 -0700114import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyal2100c782016-08-22 16:00:03 -0700115import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700116import com.android.launcher3.util.ComponentKey;
Sunny Goyalefb7e842018-10-04 15:11:00 -0700117import com.android.launcher3.util.IntArray;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700118import com.android.launcher3.util.ItemInfoMatcher;
Sunny Goyal6001ea22018-05-10 16:31:00 -0700119import com.android.launcher3.util.MultiValueAlpha;
120import com.android.launcher3.util.MultiValueAlpha.AlphaProperty;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700121import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800122import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700123import com.android.launcher3.util.PendingRequestArgs;
vadimt00d42552018-12-11 17:59:36 -0800124import com.android.launcher3.util.RaceConditionTracker;
Sunny Goyal8392c822017-06-20 10:03:56 -0700125import com.android.launcher3.util.SystemUiController;
Mario Bertschlera6936942017-05-31 14:48:19 -0700126import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700127import com.android.launcher3.util.Thunk;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700128import com.android.launcher3.util.TraceHelper;
Sunny Goyal326403e2017-10-02 12:45:10 -0700129import com.android.launcher3.util.UiThreadHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700130import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyalab770a12018-11-14 15:17:26 -0800131import com.android.launcher3.views.ActivityContext;
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800132import com.android.launcher3.views.OptionsPopupView;
Sunny Goyal29947f02017-12-18 13:49:44 -0800133import com.android.launcher3.widget.LauncherAppWidgetHostView;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800134import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700135import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal29947f02017-12-18 13:49:44 -0800136import com.android.launcher3.widget.PendingAppWidgetHostView;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800137import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700138import com.android.launcher3.widget.WidgetHostViewLoader;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700139import com.android.launcher3.widget.WidgetListRowEntry;
140import com.android.launcher3.widget.WidgetsFullSheet;
Sunny Goyal952e63d2017-08-16 04:59:08 -0700141import com.android.launcher3.widget.custom.CustomWidgetParser;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700142
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700143import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700144import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700145import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700146import java.util.Collection;
Zak Cohen658c67a2018-10-19 14:21:05 -0700147import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700148import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700149import java.util.List;
Tony Wickham010d2552017-01-20 08:15:28 -0800150import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700151
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,
156 LauncherModel.Callbacks, LauncherProviderChangeListener, UserEventDelegate{
Vadim Tryshevfedca432015-08-19 17:55:02 -0700157 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700158 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159
Winson Chunga2413752012-04-03 14:22:34 -0700160 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700161
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700163 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700164
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700165 private static final int REQUEST_PICK_APPWIDGET = 9;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166
Michael Jurka8b805b12012-04-18 14:23:14 -0700167 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800168 public static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
169 public static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700170
Jon Mirandac476d6e2017-05-04 16:30:01 -0700171 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700172
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700173 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
174
Mathew Inwood876a8462013-06-14 14:12:41 +0100175 /**
176 * IntentStarter uses request codes starting with this. This must be greater than all activity
177 * request codes used internally.
178 */
179 protected static final int REQUEST_LAST = 100;
180
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181 // Type: int
182 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700183 // Type: int
184 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700185 // Type: PendingRequestArgs
186 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
187 // Type: ActivityResultInfo
188 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700189 // Type: SparseArray<Parcelable>
190 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
vadimt00d42552018-12-11 17:59:36 -0800191 public static final String ON_CREATE_EVT = "Launcher.onCreate";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700193 private LauncherStateManager mStateManager;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700194
Adam Cohenad4e15c2013-10-17 16:21:35 -0700195 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196
Winson Chunga2413752012-04-03 14:22:34 -0700197 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700198 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
199 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
200 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700201
Jon Miranda54441f52018-01-24 15:38:25 -0800202 private LauncherAppTransitionManager mAppTransitionManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800203 private Configuration mOldConfig;
Jon Miranda54441f52018-01-24 15:38:25 -0800204
Adam Cohen091440a2015-03-18 14:16:05 -0700205 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700206 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700207 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800208 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700209
Sunny Goyalffe83f12014-08-14 17:39:34 -0700210 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700211 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700212
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700213 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700214
Sunny Goyal316490e2015-06-02 09:38:28 -0700215 @Thunk Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700216
Sunny Goyal47328fd2016-05-25 18:56:41 -0700217 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700218
219 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700220 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700221 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700222
Winson Chung8ae41982017-11-10 11:42:13 -0800223 // UI and state for the overview panel
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800224 private View mOverviewPanel;
Winson Chung8ae41982017-11-10 11:42:13 -0800225
Adam Cohen091440a2015-03-18 14:16:05 -0700226 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400227
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700228 private OnResumeCallback mOnResumeCallback;
229
Sunny Goyal527c7d32015-08-28 15:19:36 -0700230 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700231
Joe Onorato9c1289c2009-08-17 11:03:03 -0400232 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800233 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800234 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700235 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400236
Tony Wickham010d2552017-01-20 08:15:28 -0800237 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700238
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800239 private int mSynchronouslyBoundPage = PagedView.INVALID_PAGE;
Adam Cohen1462de32012-07-24 22:34:36 -0700240
Winson Chung46353de2012-02-16 14:05:10 -0800241 // We only want to get the SharedPreferences once since it does an FS stat each time we get
242 // it from the context.
243 private SharedPreferences mSharedPrefs;
244
Sunny Goyal2100c782016-08-22 16:00:03 -0700245 // Activity result which needs to be processed after workspace has loaded.
246 private ActivityResultInfo mPendingActivityResult;
247 /**
248 * Holds extra information required to handle a result from an external call, like
249 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
250 */
251 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800252
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700253 public ViewGroupFocusHelper mFocusHandler;
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700254
Sunny Goyal623eddd2018-03-02 12:24:41 -0800255 private RotationHelper mRotationHelper;
Sunny Goyal7779d622015-06-11 16:18:39 -0700256
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700257
258 private final Handler mHandler = new Handler();
259 private final Runnable mLogOnDelayedResume = this::logOnDelayedResume;
260
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800261 @Override
262 protected void onCreate(Bundle savedInstanceState) {
vadimt00d42552018-12-11 17:59:36 -0800263 RaceConditionTracker.onEvent(ON_CREATE_EVT, ENTER);
Winson Chunga2413752012-04-03 14:22:34 -0700264 if (DEBUG_STRICT_MODE) {
265 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
266 .detectDiskReads()
267 .detectDiskWrites()
268 .detectNetwork() // or .detectAll() for all detectable problems
269 .penaltyLog()
270 .build());
271 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
272 .detectLeakedSqlLiteObjects()
273 .detectLeakedClosableObjects()
274 .penaltyLog()
275 .penaltyDeath()
276 .build());
277 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700278 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700279
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800280 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700281 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400282
Sunny Goyal87f784c2017-01-11 10:48:34 -0800283 LauncherAppState app = LauncherAppState.getInstance(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800284 mOldConfig = new Configuration(getResources().getConfiguration());
Sunny Goyal0e7724c2018-02-22 13:22:21 -0800285 mModel = app.setLauncher(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800286 initDeviceProfile(app.getInvariantDeviceProfile());
Winson Chung6e1c0d32013-10-25 15:24:24 -0700287
Sunny Goyalf7258242015-10-19 16:59:07 -0700288 mSharedPrefs = Utilities.getPrefs(this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800289 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700290 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700291
Joe Onorato41a12d22009-10-31 18:30:00 -0400292 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700293 mAllAppsController = new AllAppsTransitionController(this);
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800294 mStateManager = new LauncherStateManager(this);
Tracy Zhou8b23c6f2018-04-30 17:13:27 -0700295 UiFactory.onCreate(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700296
Sunny Goyalffe83f12014-08-14 17:39:34 -0700297 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700298
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700299 mAppWidgetHost = new LauncherAppWidgetHost(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700300 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700301
Sunny Goyal60820d72017-05-09 12:40:11 -0700302 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700303
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800304 setupViews();
Tony Wickham010d2552017-01-20 08:15:28 -0800305 mPopupDataProvider = new PopupDataProvider(this);
306
Sunny Goyal623eddd2018-03-02 12:24:41 -0800307 mRotationHelper = new RotationHelper(this);
Sunny Goyal90548432018-04-04 17:17:00 -0700308 mAppTransitionManager = LauncherAppTransitionManager.newInstance(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800309
Sunny Goyald3864fa2017-11-14 15:06:36 -0800310 boolean internalStateHandled = InternalStateHandler.handleCreate(this, getIntent());
311 if (internalStateHandled) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800312 if (savedInstanceState != null) {
313 // InternalStateHandler has already set the appropriate state.
314 // We dont need to do anything.
315 savedInstanceState.remove(RUNTIME_STATE);
316 }
317 }
318 restoreState(savedInstanceState);
Winson Chungb63b44c2017-11-10 17:54:44 -0800319
Sunny Goyal2100c782016-08-22 16:00:03 -0700320 // We only load the page synchronously if the user rotates (or triggers a
321 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700322 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
323 if (savedInstanceState != null) {
324 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
325 }
Sunny Goyald3864fa2017-11-14 15:06:36 -0800326
Sunny Goyalfe770c92016-09-27 14:38:58 -0700327 if (!mModel.startLoader(currentScreen)) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800328 if (!internalStateHandled) {
329 // If we are not binding synchronously, show a fade in animation when
330 // the first page bind completes.
Sunny Goyal6001ea22018-05-10 16:31:00 -0700331 mDragLayer.getAlphaProperty(ALPHA_INDEX_LAUNCHER_LOAD).setValue(0);
Sunny Goyald3864fa2017-11-14 15:06:36 -0800332 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700333 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700334 // Pages bound synchronously.
335 mWorkspace.setCurrentPage(currentScreen);
336
Sunny Goyal2100c782016-08-22 16:00:03 -0700337 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800338 }
339
340 // For handling default keys
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800341 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800342
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800343 setContentView(mLauncherView);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800344 getRootView().dispatchInsets();
Mario Bertschlera6936942017-05-31 14:48:19 -0700345
Jon Miranda7fda2852017-07-19 16:07:01 -0700346 // Listen for broadcasts
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700347 registerReceiver(mScreenOffReceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));
Sunny Goyala616d2b2017-06-12 13:54:01 -0700348
Sunny Goyal8392c822017-06-20 10:03:56 -0700349 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
350 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700351
352 if (mLauncherCallbacks != null) {
353 mLauncherCallbacks.onCreate(savedInstanceState);
354 }
Sunny Goyal623eddd2018-03-02 12:24:41 -0800355 mRotationHelper.initialize();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700356
357 TraceHelper.endSection("Launcher-onCreate");
vadimt00d42552018-12-11 17:59:36 -0800358 RaceConditionTracker.onEvent(ON_CREATE_EVT, EXIT);
Adam Cohen9211d422014-10-07 18:14:53 -0700359 }
360
Sunny Goyal7779d622015-06-11 16:18:39 -0700361 @Override
Winson Chung8eb49e02018-07-16 13:33:31 -0700362 public void onEnterAnimationComplete() {
363 super.onEnterAnimationComplete();
364 UiFactory.onEnterAnimationComplete(this);
arangelovad783052018-08-17 14:46:45 +0100365 mAllAppsController.highlightWorkTabIfNecessary();
Winson Chung8eb49e02018-07-16 13:33:31 -0700366 }
367
368 @Override
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800369 public void onConfigurationChanged(Configuration newConfig) {
370 int diff = newConfig.diff(mOldConfig);
Jon Mirandaaeb4dd02018-07-11 14:16:23 -0700371
372 if ((diff & CONFIG_LOCALE) != 0) {
373 Folder.setLocaleDependentFields(getResources(), true /* force */);
374 }
375
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800376 if ((diff & (CONFIG_ORIENTATION | CONFIG_SCREEN_SIZE)) != 0) {
377 mUserEventDispatcher = null;
378 initDeviceProfile(mDeviceProfile.inv);
Sunny Goyalfde55052018-02-01 14:46:13 -0800379 dispatchDeviceProfileChanged();
Sunny Goyal59d086c2018-05-07 17:31:40 -0700380 reapplyUi();
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700381 mDragLayer.recreateControllers();
Sunny Goyal7185dd62018-03-14 17:51:49 -0700382
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800383 // TODO: We can probably avoid rebind when only screen size changed.
Sunny Goyal605bcf32018-03-02 15:16:12 -0800384 rebindModel();
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800385 }
386
387 mOldConfig.setTo(newConfig);
Tracy Zhoua706f002018-03-28 13:55:19 -0700388 UiFactory.onLauncherStateOrResumeChanged(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800389 super.onConfigurationChanged(newConfig);
390 }
391
Sunny Goyal605bcf32018-03-02 15:16:12 -0800392 @Override
Sunny Goyal59d086c2018-05-07 17:31:40 -0700393 protected void reapplyUi() {
394 getRootView().dispatchInsets();
395 getStateManager().reapplyState(true /* cancelCurrentAnimation */);
396 }
397
398 @Override
Sunny Goyal605bcf32018-03-02 15:16:12 -0800399 public void rebindModel() {
400 int currentPage = mWorkspace.getNextPage();
401 if (mModel.startLoader(currentPage)) {
402 mWorkspace.setCurrentPage(currentPage);
403 setWorkspaceLoading(true);
404 }
405 }
406
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800407 private void initDeviceProfile(InvariantDeviceProfile idp) {
408 // Load configuration-specific DeviceProfile
Sunny Goyal59d086c2018-05-07 17:31:40 -0700409 mDeviceProfile = idp.getDeviceProfile(this);
410 if (isInMultiWindowModeCompat()) {
411 Display display = getWindowManager().getDefaultDisplay();
412 Point mwSize = new Point();
413 display.getSize(mwSize);
414 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
415 }
416 onDeviceProfileInitiated();
Sunny Goyal605bcf32018-03-02 15:16:12 -0800417 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout(), true);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800418 }
419
Sunny Goyal623eddd2018-03-02 12:24:41 -0800420 public RotationHelper getRotationHelper() {
421 return mRotationHelper;
422 }
423
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700424 public LauncherStateManager getStateManager() {
425 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700426 }
427
Mario Bertschler27288382017-05-24 15:35:09 -0700428 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700429 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800430 return mLauncherView.findViewById(id);
431 }
432
433 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700434 public void onAppWidgetHostReset() {
435 if (mAppWidgetHost != null) {
436 mAppWidgetHost.startListening();
437 }
438 }
439
Adam Cohen9211d422014-10-07 18:14:53 -0700440 private LauncherCallbacks mLauncherCallbacks;
441
Sunny Goyal32554d12015-12-03 15:31:25 -0800442 /**
443 * Call this after onCreate to set or clear overlay.
444 */
445 public void setLauncherOverlay(LauncherOverlay overlay) {
446 if (overlay != null) {
447 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
448 }
449 mWorkspace.setLauncherOverlay(overlay);
450 }
451
Adam Cohen9211d422014-10-07 18:14:53 -0700452 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
453 mLauncherCallbacks = callbacks;
454 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700455 }
456
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700457 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700458 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700459 if (mLauncherCallbacks != null) {
460 mLauncherCallbacks.onLauncherProviderChange();
461 }
462 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700463
Hyunyoung Song3f471442015-04-08 19:01:34 -0700464 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700465 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
466 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700467 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700468 }
469
Tony Wickham010d2552017-01-20 08:15:28 -0800470 public PopupDataProvider getPopupDataProvider() {
471 return mPopupDataProvider;
472 }
473
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700474 @Override
Tony Wickhamf34bee82018-12-03 18:11:39 -0800475 public DotInfo getDotInfoForItem(ItemInfo info) {
476 return mPopupDataProvider.getDotInfoForItem(info);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700477 }
478
479 @Override
480 public void invalidateParent(ItemInfo info) {
481 FolderIconPreviewVerifier verifier = new FolderIconPreviewVerifier(getDeviceProfile().inv);
482 if (verifier.isItemInPreview(info.rank) && (info.container >= 0)) {
483 View folderIcon = getWorkspace().getHomescreenIconByItemId(info.container);
484 if (folderIcon != null) {
485 folderIcon.invalidate();
486 }
487 }
488 }
489
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000490 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700491 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
492 * a configuration step, this allows the proper animations to run after other transitions.
493 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700494 private int completeAdd(
Sunny Goyal2100c782016-08-22 16:00:03 -0700495 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
Sunny Goyalefb7e842018-10-04 15:11:00 -0700496 int screenId = info.screenId;
Sunny Goyal2100c782016-08-22 16:00:03 -0700497 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700498 // When the screen id represents an actual screen (as opposed to a rank) we make sure
499 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700500 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700501 }
Adam Cohendb364c32014-05-20 14:23:40 -0700502
Sunny Goyal2100c782016-08-22 16:00:03 -0700503 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800504 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700505 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700506 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800507 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700508 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700509 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700510 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700511 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700512 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700513 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700514 int widgetId = appWidgetId;
515 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700516 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700517 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700518 // Since the view was just bound, also launch the configure activity if needed
519 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
520 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800521 if (provider != null) {
522 new WidgetAddFlowHandler(provider)
523 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700524 }
525 }
526 break;
527 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800528 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700529
Adam Cohendb364c32014-05-20 14:23:40 -0700530 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800531 }
532
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800533 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700534 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700535 if (isWorkspaceLoading()) {
536 // process the result once the workspace has loaded.
537 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
538 return;
539 }
540 mPendingActivityResult = null;
541
Winson Chunge341d302013-08-16 14:31:00 -0700542 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700543 final PendingRequestArgs requestArgs = mPendingRequestArgs;
544 setWaitingForResult(null);
545 if (requestArgs == null) {
546 return;
547 }
548
549 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700550
Adam Cohenad4e15c2013-10-17 16:21:35 -0700551 Runnable exitSpringLoaded = new Runnable() {
552 @Override
553 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700554 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700555 }
556 };
557
Michael Jurka8b805b12012-04-18 14:23:14 -0700558 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700559 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700560 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700561 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
562 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700563 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700564 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700565 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700566 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700567 addAppWidgetImpl(
568 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800569 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700570 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700571 }
572 return;
573 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200574
Adam Cohened66b2b2012-01-23 17:28:51 -0800575 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700576 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700577
Adam Cohened66b2b2012-01-23 17:28:51 -0800578 // We have special handling for widgets
579 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800580 final int appWidgetId;
581 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
582 : -1;
583 if (widgetId < 0) {
584 appWidgetId = pendingAddWidgetId;
585 } else {
586 appWidgetId = widgetId;
587 }
588
Adam Cohenad4e15c2013-10-17 16:21:35 -0700589 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800590 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700591 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
592 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700593 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700594 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700595 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700596 @Override
597 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700598 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700599 }
600 };
Adam Cohendb364c32014-05-20 14:23:40 -0700601
Sunny Goyal2100c782016-08-22 16:00:03 -0700602 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
603 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
604 } else {
605 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
606 // When the screen id represents an actual screen (as opposed to a rank)
607 // we make sure that the drop page actually exists.
608 requestArgs.screenId =
609 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700610 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700611 final CellLayout dropLayout =
612 mWorkspace.getScreenWithId(requestArgs.screenId);
613
614 dropLayout.setDropPending(true);
615 final Runnable onComplete = new Runnable() {
616 @Override
617 public void run() {
618 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
619 dropLayout.setDropPending(false);
620 }
621 };
622 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
623 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700624 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800625 return;
626 }
627
Sunny Goyald478c832016-04-01 12:04:16 -0700628 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
629 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700630 if (resultCode == RESULT_OK) {
631 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700632 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700633 }
634 // Leave the widget in the pending state if the user canceled the configure.
635 return;
636 }
637
Sunny Goyalaad90582015-07-09 14:22:50 -0700638 if (requestCode == REQUEST_CREATE_SHORTCUT) {
639 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700640 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
641 completeAdd(requestCode, data, -1, requestArgs);
642 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
643 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
644
Sunny Goyalaad90582015-07-09 14:22:50 -0700645 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700646 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
647 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800648 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800649 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800650 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800651 }
652
653 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700654 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800655 final int requestCode, final int resultCode, final Intent data) {
656 handleActivityResult(requestCode, resultCode, data);
657 if (mLauncherCallbacks != null) {
658 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
659 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800660 }
661
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700662 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700663 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600664 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700665 PendingRequestArgs pendingArgs = mPendingRequestArgs;
666 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
667 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
668 setWaitingForResult(null);
669
Sunny Goyal28c6b962015-10-12 11:42:05 -0700670 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700671 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700672 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700673 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700674 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700675 Intent intent = pendingArgs.getPendingIntent();
676
Sunny Goyal28c6b962015-10-12 11:42:05 -0700677 if (grantResults.length > 0
678 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700679 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700680 } else {
681 // TODO: Show a snack bar with link to settings
682 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700683 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700684 }
685 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600686 if (mLauncherCallbacks != null) {
687 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
688 grantResults);
689 }
690 }
691
Adam Cohendb364c32014-05-20 14:23:40 -0700692 /**
693 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
694 *
695 * @param screenId the screen id to check
696 * @return the new screen, or screenId if it exists
697 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700698 private int ensurePendingDropLayoutExists(int screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800699 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700700 if (dropLayout == null) {
701 // it's possible that the add screen was removed because it was
702 // empty and a re-bind occurred
703 mWorkspace.addExtraEmptyScreen();
704 return mWorkspace.commitExtraEmptyScreen();
705 } else {
706 return screenId;
707 }
708 }
709
Sunny Goyal2100c782016-08-22 16:00:03 -0700710 @Thunk void completeTwoStageWidgetDrop(
711 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
712 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800713 Runnable onCompleteRunnable = null;
714 int animationType = 0;
715
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700716 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800717 if (resultCode == RESULT_OK) {
718 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
719 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800720 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700721 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800722 onCompleteRunnable = new Runnable() {
723 @Override
724 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700725 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700726 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800727 }
728 };
729 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800730 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800731 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800732 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700733 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700734 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700735 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
736 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700737 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700738 // The animated view may be null in the case of a rotation during widget configuration
739 onCompleteRunnable.run();
740 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800741 }
742
743 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700744 protected void onStop() {
745 super.onStop();
Adam Cohen9211d422014-10-07 18:14:53 -0700746 if (mLauncherCallbacks != null) {
747 mLauncherCallbacks.onStop();
748 }
Hyunyoung Song1241e612018-05-15 21:46:51 -0700749 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
750 mStateManager.getState().containerType, -1);
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700751
752 mAppWidgetHost.setListenIfResumed(false);
753
Tony Wickham010d2552017-01-20 08:15:28 -0800754 NotificationListener.removeNotificationsChangedListener();
Sunny Goyalaf161fd2018-02-05 16:34:57 -0800755 getStateManager().moveToRestState();
Winson Chungedb0d6b2018-03-21 17:32:55 -0700756
Tracy Zhou62646712018-06-26 14:19:02 -0700757 UiFactory.onLauncherStateOrResumeChanged(this);
758
Winson Chungdea51352018-04-24 13:02:10 -0700759 // Workaround for b/78520668, explicitly trim memory once UI is hidden
Winson Chung473730f2018-04-26 11:54:22 -0700760 onTrimMemory(TRIM_MEMORY_UI_HIDDEN);
Michael Jurkacd496d72013-04-11 11:32:45 -0700761 }
762
763 @Override
764 protected void onStart() {
765 super.onStart();
Adam Cohen9211d422014-10-07 18:14:53 -0700766 if (mLauncherCallbacks != null) {
767 mLauncherCallbacks.onStart();
768 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800769 mAppWidgetHost.setListenIfResumed(true);
Sunny Goyalcac002d2018-01-04 09:51:45 -0800770 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
Michael Jurkacd496d72013-04-11 11:32:45 -0700771 }
772
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700773 private void logOnDelayedResume() {
774 if (hasBeenResumed()) {
775 getUserEventDispatcher().logActionCommand(Action.Command.RESUME,
776 mStateManager.getState().containerType, -1);
777 getUserEventDispatcher().startSession();
778 }
779 }
780
Michael Jurkacd496d72013-04-11 11:32:45 -0700781 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800782 protected void onResume() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700783 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800784 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700785 TraceHelper.partitionSection("ON_RESUME", "superCall");
786
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700787 mHandler.removeCallbacks(mLogOnDelayedResume);
788 Utilities.postAsyncCallback(mHandler, mLogOnDelayedResume);
789
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700790 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700791 // Process any items that were added while Launcher was away.
792 InstallShortcutReceiver.disableAndFlushInstallQueue(
793 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700794
Sunny Goyala474a9b2017-05-04 16:47:11 -0700795 // Refresh shortcuts if the permission changed.
796 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700797
Tracy Zhoue5575f92018-04-19 11:12:53 -0700798 DiscoveryBounce.showForHomeIfNeeded(this);
Adam Cohen9211d422014-10-07 18:14:53 -0700799 if (mLauncherCallbacks != null) {
800 mLauncherCallbacks.onResume();
801 }
Tracy Zhoua706f002018-03-28 13:55:19 -0700802 UiFactory.onLauncherStateOrResumeChanged(this);
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800803
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700804 TraceHelper.endSection("ON_RESUME");
Adam Cohen06dff352012-06-01 17:17:08 -0700805 }
806
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800807 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700808 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700809 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700810 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700811
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700812 super.onPause();
Joe Onorato24b6fd82009-11-12 13:47:09 -0800813 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700814 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700815
Adam Cohen9211d422014-10-07 18:14:53 -0700816 if (mLauncherCallbacks != null) {
817 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700818 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700819 }
820
Winson Chungd7823942018-02-16 03:23:47 +0000821 @Override
Tracy Zhoua706f002018-03-28 13:55:19 -0700822 protected void onUserLeaveHint() {
823 super.onUserLeaveHint();
824 UiFactory.onLauncherStateOrResumeChanged(this);
825 }
826
827 @Override
Winson Chungd7823942018-02-16 03:23:47 +0000828 public void onWindowFocusChanged(boolean hasFocus) {
829 super.onWindowFocusChanged(hasFocus);
830 mStateManager.onWindowFocusChanged();
831 }
832
Adam Cohenc2d6e892014-10-16 09:49:24 -0700833 public interface LauncherOverlay {
834
835 /**
836 * Touch interaction leading to overscroll has begun
837 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700838 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700839
840 /**
841 * Touch interaction related to overscroll has ended
842 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700843 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700844
845 /**
846 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
847 * screen (or in the case of RTL, the rightmost screen).
848 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700849 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700850
851 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800852 * Called when the launcher is ready to use the overlay
853 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700854 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700855 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700856 }
857
858 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700859 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700860 }
861
862 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
863
Sunny Goyalc86df472016-02-25 09:19:38 -0800864 public void onScrollChanged(float progress) {
865 if (mWorkspace != null) {
866 mWorkspace.onOverlayScrollChanged(progress);
867 }
868 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700869 }
870
Sunny Goyalf9403d92017-10-18 10:55:56 -0700871 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -0700872 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700873 }
874
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800875 /**
876 * Restores the previous state, if it exists.
877 *
878 * @param savedState The previous state.
879 */
880 private void restoreState(Bundle savedState) {
881 if (savedState == null) {
882 return;
883 }
884
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700885 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -0700886 LauncherState[] stateValues = LauncherState.values();
887 LauncherState state = stateValues[stateOrdinal];
Sunny Goyalbc683e92017-12-06 10:25:07 -0800888 if (!state.disableRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700889 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800890 }
891
Sunny Goyal2100c782016-08-22 16:00:03 -0700892 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
893 if (requestArgs != null) {
894 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800895 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700896
897 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700898
899 SparseArray<Parcelable> widgetsState =
900 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
901 if (widgetsState != null) {
902 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
903 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800904 }
905
906 /**
907 * Finds all the views we need and configure them properly.
908 */
909 private void setupViews() {
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800910 mDragLayer = findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700911 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -0700912 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700913 mWorkspace.initParentViews(mDragLayer);
Sunny Goyal16764582017-11-06 16:00:34 -0800914 mOverviewPanel = findViewById(R.id.overview_panel);
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800915 mHotseat = findViewById(R.id.hotseat);
Craig Mautner360310b2012-10-26 15:13:08 -0700916
Sunny Goyal784f9c32016-03-23 16:56:54 -0700917 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
918 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
919 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800920
Winson Chung4c98d922011-05-31 16:50:48 -0700921 // Setup the drag layer
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700922 mDragLayer.setup(mDragController, mWorkspace);
923 UiFactory.setOnTouchControllersChangedListener(this, mDragLayer::recreateControllers);
Winson Chung4c98d922011-05-31 16:50:48 -0700924
Hyunyoung Song645764e2016-06-06 14:19:02 -0700925 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -0700926 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
927 // default state, otherwise we will update to the wrong offsets in RTL
928 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700929 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700930 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700931
Tony Wickham34d2c912015-09-11 09:27:58 -0700932 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700933 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700934
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700935 // Setup Apps
936 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -0700937
Winson Chung3d503fb2011-07-13 17:25:49 -0700938 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -0700939 mDragController.setMoveTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700940 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -0400941
Sunny Goyal7185dd62018-03-14 17:51:49 -0700942 mAllAppsController.setupViews(mAppsView);
Sunny Goyalab770a12018-11-14 15:17:26 -0800943 mHotseat.setOnInterceptTouchListener(mWorkspace::onInterceptHotseatTouch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800944 }
945
946 /**
947 * Creates a view representing a shortcut.
948 *
949 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800950 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800951 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700952 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800953 }
954
955 /**
956 * Creates a view representing a shortcut inflated from the specified resource.
957 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800958 * @param parent The group the shortcut belongs to.
959 * @param info The data structure describing the shortcut.
960 *
961 * @return A View inflated from layoutResId.
962 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700963 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -0800964 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
965 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800966 favorite.applyFromShortcutInfo(info);
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800967 favorite.setOnClickListener(ItemClickHandler.INSTANCE);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700968 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800969 return favorite;
970 }
971
972 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -0700973 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800974 *
975 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800976 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700977 private void completeAddShortcut(Intent data, int container, int screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700978 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -0700979 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
980 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700981 return;
982 }
Adam Cohen558baaf2011-08-15 15:22:57 -0700983
Jon Mirandac476d6e2017-05-04 16:30:01 -0700984 int[] cellXY = mTmpAddItemCellCoordinates;
985 CellLayout layout = getCellLayout(container, screenId);
986
Sunny Goyal782f0c92017-01-19 10:27:54 -0800987 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700988 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -0700989 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
990 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -0800991 }
992
993 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -0700994 // Legacy shortcuts are only supported for primary profile.
995 info = Process.myUserHandle().equals(args.user)
996 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800997
Sunny Goyalb57645f2017-03-20 13:57:28 -0700998 if (info == null) {
999 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1000 return;
1001 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001002 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001003 // The app is trying to add a shortcut without sufficient permissions
1004 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1005 return;
1006 }
1007 }
1008
Jon Mirandac476d6e2017-05-04 16:30:01 -07001009 if (container < 0) {
1010 // Adding a shortcut to the Workspace.
1011 final View view = createShortcut(info);
1012 boolean foundCellSpan = false;
1013 // First we check if we already know the exact location where we want to add this item.
1014 if (cellX >= 0 && cellY >= 0) {
1015 cellXY[0] = cellX;
1016 cellXY[1] = cellY;
1017 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001018
Jon Mirandac476d6e2017-05-04 16:30:01 -07001019 // If appropriate, either create a folder or add to an existing folder
1020 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001021 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001022 return;
1023 }
1024 DragObject dragObject = new DragObject();
1025 dragObject.dragInfo = info;
1026 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1027 true)) {
1028 return;
1029 }
1030 } else {
1031 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1032 }
1033
1034 if (!foundCellSpan) {
1035 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001036 return;
1037 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001038
1039 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1040 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001041 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001042 // Adding a shortcut to a Folder.
Sunny Goyale29897f2017-07-20 10:09:42 -07001043 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001044 if (folderIcon != null) {
1045 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1046 folderInfo.add(info, args.rank, false);
1047 } else {
Sunny Goyale29897f2017-07-20 10:09:42 -07001048 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001049 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001050 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001051 }
1052
Sunny Goyalefb7e842018-10-04 15:11:00 -07001053 public FolderIcon findFolderIcon(final int folderIconId) {
Sunny Goyal83fd25e2018-07-09 16:47:01 -07001054 return (FolderIcon) mWorkspace.getHomescreenIconByItemId(folderIconId);
Sunny Goyale29897f2017-07-20 10:09:42 -07001055 }
1056
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001057 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001058 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001059 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001060 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001061 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001062 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001063 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1064
Adam Cohened66b2b2012-01-23 17:28:51 -08001065 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001066 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001067 }
Romain Guycbb89e42009-06-08 15:52:54 -07001068
Adam Cohen59400422014-03-05 18:07:04 -08001069 LauncherAppWidgetInfo launcherInfo;
1070 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001071 launcherInfo.spanX = itemInfo.spanX;
1072 launcherInfo.spanY = itemInfo.spanY;
1073 launcherInfo.minSpanX = itemInfo.minSpanX;
1074 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001075 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001076
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001077 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001078 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001079
Sunny Goyal2100c782016-08-22 16:00:03 -07001080 if (hostView == null) {
1081 // Perform actual inflation because we're live
1082 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001083 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001084 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301085 prepareAppWidget(hostView, launcherInfo);
1086 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001087 }
Romain Guycbb89e42009-06-08 15:52:54 -07001088
Sunny Goyald5462aa2016-11-22 16:52:39 +05301089 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001090 hostView.setTag(item);
1091 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001092 hostView.setFocusable(true);
1093 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001094 }
1095
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001096 private final BroadcastReceiver mScreenOffReceiver = new BroadcastReceiver() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001097 @Override
1098 public void onReceive(Context context, Intent intent) {
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001099 // Reset AllApps to its initial state only if we are not in the middle of
1100 // processing a multi-step drop
1101 if (mPendingRequestArgs == null) {
1102 mStateManager.goToState(NORMAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001103 }
1104 }
1105 };
1106
Tony Wickhamf34bee82018-12-03 18:11:39 -08001107 public void updateNotificationDots(final Set<PackageUserKey> updatedDots) {
1108 mWorkspace.updateNotificationDots(updatedDots);
1109 mAppsView.getAppsStore().updateNotificationDots(updatedDots);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001110
Sunny Goyal29947f02017-12-18 13:49:44 -08001111 PopupContainerWithArrow popup = PopupContainerWithArrow.getOpen(Launcher.this);
1112 if (popup != null) {
Tony Wickhamf34bee82018-12-03 18:11:39 -08001113 popup.updateNotificationHeader(updatedDots);
Tony Wickham010d2552017-01-20 08:15:28 -08001114 }
1115 }
1116
Adam Cohended9f8d2010-11-03 13:25:16 -07001117 @Override
1118 public void onAttachedToWindow() {
1119 super.onAttachedToWindow();
1120
Sunny Goyalc86df472016-02-25 09:19:38 -08001121 if (mLauncherCallbacks != null) {
1122 mLauncherCallbacks.onAttachedToWindow();
1123 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001124 }
1125
1126 @Override
1127 public void onDetachedFromWindow() {
1128 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001129
1130 if (mLauncherCallbacks != null) {
1131 mLauncherCallbacks.onDetachedFromWindow();
1132 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001133 }
1134
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001135 public AllAppsTransitionController getAllAppsController() {
1136 return mAllAppsController;
1137 }
1138
Sunny Goyal9d69c8d2018-03-19 13:41:31 -07001139 @Override
Winson Chung1a341002018-01-17 10:00:23 -08001140 public LauncherRootView getRootView() {
1141 return (LauncherRootView) mLauncherView;
1142 }
1143
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001144 @Override
Winson Chunga6945242014-01-08 14:04:34 -08001145 public DragLayer getDragLayer() {
1146 return mDragLayer;
1147 }
1148
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001149 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001150 return mAppsView;
1151 }
1152
Winson Chunga6945242014-01-08 14:04:34 -08001153 public Workspace getWorkspace() {
1154 return mWorkspace;
1155 }
1156
1157 public Hotseat getHotseat() {
1158 return mHotseat;
1159 }
1160
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001161 public <T extends View> T getOverviewPanel() {
Winson Chung8ae41982017-11-10 11:42:13 -08001162 return (T) mOverviewPanel;
Winson Chunga6945242014-01-08 14:04:34 -08001163 }
1164
Sunny Goyal47328fd2016-05-25 18:56:41 -07001165 public DropTargetBar getDropTargetBar() {
1166 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001167 }
1168
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001169 public LauncherAppWidgetHost getAppWidgetHost() {
1170 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001171 }
Romain Guycbb89e42009-06-08 15:52:54 -07001172
Winson Chunga9abd0e2010-10-27 17:18:37 -07001173 public LauncherModel getModel() {
1174 return mModel;
1175 }
1176
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001177 public ModelWriter getModelWriter() {
1178 return mModelWriter;
1179 }
1180
Adam Cohen79d90c52016-04-22 13:29:20 -07001181 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001182 return mSharedPrefs;
1183 }
1184
Sunny Goyalf8d56fc2018-01-31 15:18:11 -08001185 public int getOrientation() { return mOldConfig.orientation; }
Jon Miranda6bed3502017-09-19 14:43:17 -07001186
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001187 @Override
1188 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001189 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001190 super.onNewIntent(intent);
1191
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001192 boolean alreadyOnHome = hasWindowFocus() && ((intent.getFlags() &
Winson15f8b172015-08-19 11:02:39 -07001193 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1194 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001195
1196 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001197 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001198 && AbstractFloatingView.getTopOpenView(this) == null;
Winson15f8b172015-08-19 11:02:39 -07001199 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001200 boolean internalStateHandled = InternalStateHandler
Sunny Goyal8b264562018-02-03 23:00:13 -08001201 .handleNewIntent(this, intent, isStarted());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001202
Winson15f8b172015-08-19 11:02:39 -07001203 if (isActionMain) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001204 if (!internalStateHandled) {
1205 // Note: There should be at most one log per method call. This is enforced
1206 // implicitly by using if-else statements.
1207 UserEventDispatcher ued = getUserEventDispatcher();
1208 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
1209 if (topOpenView != null) {
1210 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
1211 } else if (alreadyOnHome) {
1212 Target target = newContainerTarget(mStateManager.getState().containerType);
1213 target.pageIndex = mWorkspace.getCurrentPage();
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001214 ued.logActionCommand(Action.Command.HOME_INTENT, target,
1215 newContainerTarget(ContainerType.WORKSPACE));
Sunny Goyald3864fa2017-11-14 15:06:36 -08001216 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001217
Sunny Goyald3864fa2017-11-14 15:06:36 -08001218 // In all these cases, only animate if we're already on home
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001219 AbstractFloatingView.closeAllOpenViews(this, isStarted());
Jon Mirandafeba90f2016-10-06 10:53:29 -07001220
Sunny Goyala2467272018-03-16 14:53:05 -07001221 if (!isInState(NORMAL)) {
1222 // Only change state, if not already the same. This prevents cancelling any
1223 // animations running as part of resume
1224 mStateManager.goToState(NORMAL);
1225 }
Sunny Goyald3864fa2017-11-14 15:06:36 -08001226
1227 // Reset the apps view
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001228 if (!alreadyOnHome) {
Sunny Goyal7185dd62018-03-14 17:51:49 -07001229 mAppsView.reset(isStarted() /* animate */);
Sunny Goyald3864fa2017-11-14 15:06:36 -08001230 }
1231
Sunny Goyalaad33592018-08-07 17:20:35 -07001232 if (shouldMoveToDefaultScreen && !mWorkspace.isHandlingTouch()) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001233 mWorkspace.post(mWorkspace::moveToDefaultScreen);
1234 }
1235 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001236
1237 final View v = getWindow().peekDecorView();
1238 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001239 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001240 }
1241
Adam Cohen9211d422014-10-07 18:14:53 -07001242 if (mLauncherCallbacks != null) {
Sunny Goyal85462132018-04-24 11:39:37 -07001243 mLauncherCallbacks.onHomeIntent(internalStateHandled);
Adam Cohen9211d422014-10-07 18:14:53 -07001244 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001245 }
Winson15f8b172015-08-19 11:02:39 -07001246
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001247 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001248 }
1249
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001250 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001251 public void onRestoreInstanceState(Bundle state) {
1252 super.onRestoreInstanceState(state);
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001253 mWorkspace.restoreInstanceStateForChild(mSynchronouslyBoundPage);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001254 }
1255
1256 @Override
1257 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001258 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001259 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001260
Adam Cohen21cd0022013-10-09 18:57:02 -07001261 }
Sunny Goyalea609262017-10-25 15:47:38 -07001262 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001263
1264
1265 AbstractFloatingView widgets = AbstractFloatingView
1266 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1267 if (widgets != null) {
1268 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1269 widgets.saveHierarchyState(widgetsState);
1270 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1271 } else {
1272 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1273 }
1274
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001275 // We close any open folders and shortcut containers since they will not be re-opened,
1276 // and we need to make sure this state is reflected.
1277 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001278
Sunny Goyal2100c782016-08-22 16:00:03 -07001279 if (mPendingRequestArgs != null) {
1280 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1281 }
1282 if (mPendingActivityResult != null) {
1283 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001284 }
1285
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001286 super.onSaveInstanceState(outState);
1287
Adam Cohen9211d422014-10-07 18:14:53 -07001288 if (mLauncherCallbacks != null) {
1289 mLauncherCallbacks.onSaveInstanceState(outState);
1290 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001291 }
1292
1293 @Override
1294 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001295 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001296
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001297 unregisterReceiver(mScreenOffReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001298 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001299
Tony Wickhama0068302018-04-11 16:16:11 -07001300 UiFactory.setOnTouchControllersChangedListener(this, null);
1301
Winson Chungcd2b0142011-06-08 16:02:26 -07001302 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001303 // It's possible to receive onDestroy after a new Launcher activity has
1304 // been created. In this case, don't interfere with the new Launcher.
1305 if (mModel.isCurrentCallbacks(this)) {
1306 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001307 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001308 }
Sunny Goyal623eddd2018-03-02 12:24:41 -08001309 mRotationHelper.destroy();
Sunny Goyal745bad92016-05-02 10:54:12 -07001310
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001311 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001312 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001313 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001314 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001315 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001316
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001317 TextKeyListener.getInstance().release();
Tony2917a8b2017-07-31 23:29:42 -07001318 clearPendingBinds();
1319
Adam Cohen9211d422014-10-07 18:14:53 -07001320 if (mLauncherCallbacks != null) {
1321 mLauncherCallbacks.onDestroy();
1322 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001323 }
1324
Sunny Goyalae502842016-06-17 08:43:56 -07001325 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1326 return mAccessibilityDelegate;
1327 }
1328
Adam Cohena9cf38f2011-05-02 15:36:58 -07001329 public DragController getDragController() {
1330 return mDragController;
1331 }
1332
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001333 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001334 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001335 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001336 }
1337
1338 @Override
1339 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1340 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001341 try {
1342 super.startIntentSenderForResult(intent, requestCode,
1343 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1344 } catch (IntentSender.SendIntentException e) {
1345 throw new ActivityNotFoundException();
1346 }
1347 }
1348
Winson Chung70d72102011-08-12 11:24:35 -07001349 /**
1350 * Indicates that we want global search for this activity by setting the globalSearch
1351 * argument for {@link #startSearch} to true.
1352 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001353 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001354 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001355 Bundle appSearchData, boolean globalSearch) {
1356 if (appSearchData == null) {
1357 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001358 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001359 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001360
Sunny Goyal6f28e712016-09-13 14:19:29 -07001361 if (mLauncherCallbacks == null ||
1362 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1363 // Starting search from the callbacks failed. Start the default global search.
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001364 super.startSearch(initialQuery, selectInitialQuery, appSearchData, true);
Ian Parkinson047036e2014-09-01 15:40:53 +01001365 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001366
1367 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001368 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001369 }
1370
Joe Onorato9c1289c2009-08-17 11:03:03 -04001371 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001372 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001373 }
1374
Adam Cohen517a7f52014-03-01 12:12:59 -08001375 public boolean isWorkspaceLoading() {
1376 return mWorkspaceLoading;
1377 }
1378
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001379 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001380 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001381 }
1382
Sunny Goyal04a324a2017-01-20 21:08:59 -08001383 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001384 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001385 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001386
Sunny Goyal2100c782016-08-22 16:00:03 -07001387 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001388 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001389 if (LOGD) {
1390 Log.d(TAG, "Adding widget from drop");
1391 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001392 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001393 }
1394
Sunny Goyal2100c782016-08-22 16:00:03 -07001395 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001396 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1397 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1398 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001399
Adam Cohenad4e15c2013-10-17 16:21:35 -07001400 Runnable onComplete = new Runnable() {
1401 @Override
1402 public void run() {
1403 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001404 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001405 }
1406 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001407 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001408 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001409 }
1410 }
Romain Guycbb89e42009-06-08 15:52:54 -07001411
Sunny Goyalefb7e842018-10-04 15:11:00 -07001412 public void addPendingItem(PendingAddItemInfo info, int container, int screenId,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001413 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001414 info.container = container;
1415 info.screenId = screenId;
1416 if (cell != null) {
1417 info.cellX = cell[0];
1418 info.cellY = cell[1];
1419 }
1420 info.spanX = spanX;
1421 info.spanY = spanY;
1422
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001423 switch (info.itemType) {
1424 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1425 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001426 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001427 break;
1428 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001429 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001430 break;
1431 default:
1432 throw new IllegalStateException("Unknown item type: " + info.itemType);
1433 }
1434 }
1435
Adam Cohenfbba09b2011-07-18 21:43:05 -07001436 /**
1437 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001438 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001439 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001440 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1441 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001442 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1443 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1444 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001445 }
1446
Adam Cohenfbba09b2011-07-18 21:43:05 -07001447 /**
1448 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001449 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001450 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001451 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001452 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001453 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001454 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001455 // In the case where we've prebound the widget, we remove it from the DragLayer
1456 if (LOGD) {
1457 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1458 }
1459 getDragLayer().removeView(hostView);
1460
Adam Cohened66b2b2012-01-23 17:28:51 -08001461 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001462 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001463
1464 // Clear the boundWidget so that it doesn't get destroyed.
1465 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001466 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001467 // In this case, we either need to start an activity to get permission to bind
1468 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001469 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1470 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1471 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1472 this, info.componentName);
1473 } else {
1474 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1475 }
Adam Cohendd70d662012-10-04 16:53:44 -07001476 Bundle options = info.bindOptions;
1477
Sunny Goyalffe83f12014-08-14 17:39:34 -07001478 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1479 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001480 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001481 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001482 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001483 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001484 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001485 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001486 }
1487
Sunny Goyalefb7e842018-10-04 15:11:00 -07001488 FolderIcon addFolder(CellLayout layout, int container, final int screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001489 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001490 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001491 folderInfo.title = getText(R.string.folder_name);
1492
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001493 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001494 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001495
1496 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001497 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301498 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001499 // Force measure the new folder icon
1500 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1501 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001502 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001503 }
Romain Guycbb89e42009-06-08 15:52:54 -07001504
Winsonc0b52fe2015-09-09 16:38:15 -07001505 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001506 * Unbinds the view for the specified item, and removes the item and all its children.
1507 *
1508 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001509 * @param itemInfo the {@link ItemInfo} for this view.
1510 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001511 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001512 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001513 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001514 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001515 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1516 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001517 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001518 } else {
1519 mWorkspace.removeWorkspaceItem(v);
1520 }
Winsonc0b52fe2015-09-09 16:38:15 -07001521 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001522 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001523 }
1524 } else if (itemInfo instanceof FolderInfo) {
1525 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001526 if (v instanceof FolderIcon) {
1527 ((FolderIcon) v).removeListeners();
1528 }
Winsonc0b52fe2015-09-09 16:38:15 -07001529 mWorkspace.removeWorkspaceItem(v);
1530 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001531 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001532 }
1533 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1534 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001535 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001536 if (deleteFromDb) {
Tony Wickham6a71a5b2018-08-21 11:40:23 -07001537 getModelWriter().deleteWidgetInfo(widgetInfo, getAppWidgetHost());
Winsonc0b52fe2015-09-09 16:38:15 -07001538 }
1539 } else {
1540 return false;
1541 }
1542 return true;
1543 }
1544
Tony Wickham6a71a5b2018-08-21 11:40:23 -07001545
Winsonc0b52fe2015-09-09 16:38:15 -07001546
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001547 @Override
1548 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001549 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001550 }
1551
Joe Onorato88ec0992009-11-19 13:16:06 -08001552 @Override
1553 public void onBackPressed() {
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08001554 if (finishAutoCancelActionMode()) {
1555 return;
1556 }
Adam Cohen9211d422014-10-07 18:14:53 -07001557 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1558 return;
1559 }
1560
Sunny Goyal45478022015-06-08 16:52:41 -07001561 if (mDragController.isDragging()) {
1562 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001563 return;
1564 }
1565
Jon Mirandafeba90f2016-10-06 10:53:29 -07001566 // Note: There should be at most one log per method call. This is enforced implicitly
1567 // by using if-else statements.
1568 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001569 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
Tony Wickham52c1b662018-05-21 13:13:58 -07001570 if (topView != null && topView.onBackPressed()) {
1571 // Handled by the floating view.
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001572 } else {
Sunny Goyale39690b2018-08-02 13:35:07 -07001573 mStateManager.getState().onBackPressed(this);
Michael Jurkaaf442092010-06-10 17:01:57 -07001574 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001575 }
1576
Sunny Goyalfe770c92016-09-27 14:38:58 -07001577 @TargetApi(Build.VERSION_CODES.M)
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001578 @Override
Jon Miranda73c27ec2018-05-16 18:06:23 -07001579 public ActivityOptions getActivityLaunchOptions(View v) {
1580 return mAppTransitionManager.getActivityLaunchOptions(this, v);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001581 }
1582
Tony Wickham3a6746a2018-03-29 09:39:56 -07001583 public LauncherAppTransitionManager getAppTransitionManager() {
1584 return mAppTransitionManager;
1585 }
1586
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001587 @TargetApi(Build.VERSION_CODES.M)
1588 @Override
1589 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
1590 // Due to legacy reasons, direct call shortcuts require Launchers to have the
1591 // corresponding permission. Show the appropriate permission prompt if that
1592 // is the case.
1593 if (intent.getComponent() == null
1594 && Intent.ACTION_CALL.equals(intent.getAction())
1595 && checkSelfPermission(android.Manifest.permission.CALL_PHONE) !=
1596 PackageManager.PERMISSION_GRANTED) {
1597
1598 setWaitingForResult(PendingRequestArgs
1599 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
1600 requestPermissions(new String[]{android.Manifest.permission.CALL_PHONE},
1601 REQUEST_PERMISSION_CALL_PHONE);
1602 return true;
1603 } else {
1604 return false;
1605 }
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001606 }
1607
Hyunyoung Song46d07f72018-05-22 15:41:25 -07001608 @Override
Hyunyoung Songfc007472018-10-25 14:09:50 -07001609 public int getCurrentState() {
1610 if(mStateManager.getState() == LauncherState.ALL_APPS) {
1611 return StatsLogUtils.LAUNCHER_STATE_ALLAPPS;
1612 } else if (mStateManager.getState() == LauncherState.OVERVIEW) {
1613 return StatsLogUtils.LAUNCHER_STATE_OVERVIEW;
1614 }
Hyunyoung Song0ae38882018-11-05 14:45:19 -08001615 return StatsLogUtils.LAUNCHER_STATE_HOME;
Hyunyoung Songfc007472018-10-25 14:09:50 -07001616 }
1617
1618 @Override
Hyunyoung Song46d07f72018-05-22 15:41:25 -07001619 public void modifyUserEvent(LauncherLogProto.LauncherEvent event) {
1620 if (event.srcTarget != null && event.srcTarget.length > 0 &&
1621 event.srcTarget[1].containerType == ContainerType.PREDICTION) {
1622 Target[] targets = new Target[3];
1623 targets[0] = event.srcTarget[0];
1624 targets[1] = event.srcTarget[1];
1625 targets[2] = newTarget(Target.Type.CONTAINER);
1626 event.srcTarget = targets;
1627 LauncherState state = mStateManager.getState();
1628 if (state == LauncherState.ALL_APPS) {
1629 event.srcTarget[2].containerType = ContainerType.ALLAPPS;
1630 } else if (state == LauncherState.OVERVIEW) {
1631 event.srcTarget[2].containerType = ContainerType.TASKSWITCHER;
1632 }
1633 }
1634 }
1635
Sunny Goyala7ce1662016-05-31 15:01:35 -07001636 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Hyunyoung Song1241e612018-05-15 21:46:51 -07001637 boolean success = super.startActivitySafely(v, intent, item);
1638 if (success && v instanceof BubbleTextView) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001639 // This is set to the view that launched the activity that navigated the user away
1640 // from launcher. Since there is no callback for when the activity has finished
1641 // launching, enable the press state and keep this reference to reset the press
1642 // state when we return to launcher.
1643 BubbleTextView btv = (BubbleTextView) v;
1644 btv.setStayPressed(true);
1645 setOnResumeCallback(btv);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001646 }
Hyunyoung Song1241e612018-05-15 21:46:51 -07001647 return success;
Michael Jurka86a720e2012-05-09 11:23:23 -07001648 }
1649
Winson Chung3d503fb2011-07-13 17:25:49 -07001650 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001651 // TODO: Remove this method
Sunny Goyal876e4622018-11-02 13:50:40 -07001652 return mHotseat != null && (layout == mHotseat);
Winson Chung3d503fb2011-07-13 17:25:49 -07001653 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001654
Winson Chung3d503fb2011-07-13 17:25:49 -07001655 /**
1656 * Returns the CellLayout of the specified container at the specified screen.
1657 */
Sunny Goyalefb7e842018-10-04 15:11:00 -07001658 public CellLayout getCellLayout(int container, int screenId) {
Sunny Goyal876e4622018-11-02 13:50:40 -07001659 return (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1660 ? mHotseat : mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08001661 }
1662
Michael Jurkae326f182011-11-21 14:05:46 -08001663 @Override
1664 public void onTrimMemory(int level) {
1665 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07001666 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
1667 // The widget preview db can result in holding onto over
1668 // 3MB of memory for caching which isn't necessary.
1669 SQLiteDatabase.releaseMemory();
1670
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08001671 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07001672 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08001673 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01001674 if (mLauncherCallbacks != null) {
1675 mLauncherCallbacks.onTrimMemory(level);
1676 }
Winson Chung62a70be2018-02-23 15:10:05 -08001677 UiFactory.onTrimMemory(this, level);
Michael Jurkae326f182011-11-21 14:05:46 -08001678 }
1679
Michael Jurkad7c28052012-04-27 15:43:36 -07001680 @Override
1681 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07001682 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07001683 final List<CharSequence> text = event.getText();
1684 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07001685 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001686 // TODO: When can workspace be null?
1687 text.add(mWorkspace == null
1688 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07001689 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07001690 return result;
1691 }
1692
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001693 public void setOnResumeCallback(OnResumeCallback callback) {
1694 if (mOnResumeCallback != null) {
1695 mOnResumeCallback.onLauncherResume();
1696 }
1697 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07001698 }
1699
Michael Jurka7607c2f2013-04-03 14:33:19 -07001700 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001701 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001702 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07001703 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001704 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07001705 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001706 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07001707 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07001708 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07001709 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001710 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001711
Joe Onorato9c1289c2009-08-17 11:03:03 -04001712 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07001713 * Clear any pending bind callbacks. This is called when is loader is planning to
1714 * perform a full rebind from scratch.
1715 */
1716 @Override
1717 public void clearPendingBinds() {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001718 if (mPendingExecutor != null) {
1719 mPendingExecutor.markCompleted();
1720 mPendingExecutor = null;
1721 }
1722 }
1723
1724 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001725 * Refreshes the shortcuts shown on the workspace.
1726 *
1727 * Implementation of the method from LauncherModel.Callbacks.
1728 */
1729 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001730 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001731 // Floating panels (except the full widget sheet) are associated with individual icons. If
1732 // we are starting a fresh bind, close all such panels as all the icons are about
1733 // to go away.
1734 AbstractFloatingView.closeOpenViews(this, true,
Sunny Goyal7ede6112017-12-05 15:11:21 -08001735 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_REBIND_SAFE);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08001736
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001737 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08001738
Winson Chungd64d1762013-08-20 14:37:16 -07001739 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07001740 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07001741 mWorkspace.removeAllWorkspaceScreens();
Sunny Goyalc11fac32018-02-27 19:20:15 -08001742 mAppWidgetHost.clearViews();
Winson Chungd64d1762013-08-20 14:37:16 -07001743
Winson Chung3d503fb2011-07-13 17:25:49 -07001744 if (mHotseat != null) {
Sunny Goyal11e96492018-03-23 17:06:00 -07001745 mHotseat.resetLayout(mDeviceProfile.isVerticalBarLayout());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001746 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001747 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001748 }
1749
Adam Cohendcd297f2013-06-18 13:13:40 -07001750 @Override
Sunny Goyalefb7e842018-10-04 15:11:00 -07001751 public void bindScreens(IntArray orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001752 // Make sure the first screen is always at the start.
Ryan Lothianfa530cd2018-10-12 14:14:16 -04001753 if (FeatureFlags.QSB_ON_FIRST_SCREEN.get() &&
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001754 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalefb7e842018-10-04 15:11:00 -07001755 orderedScreenIds.removeValue(Workspace.FIRST_SCREEN_ID);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001756 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Ryan Lothianfa530cd2018-10-12 14:14:16 -04001757 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN.get()
Ryan Lothian688e9892018-10-03 13:32:01 -04001758 && orderedScreenIds.isEmpty()) {
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001759 // If there are no screens, we need to have an empty screen
1760 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07001761 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001762 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07001763
Winsonc7d2e832016-07-28 12:24:55 -07001764 // After we have added all the screens, if the wallpaper was locked to the default state,
1765 // then notify to indicate that it can be released and a proper wallpaper offset can be
1766 // computed before the next layout
1767 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07001768 }
1769
Sunny Goyalefb7e842018-10-04 15:11:00 -07001770 private void bindAddScreens(IntArray orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001771 int count = orderedScreenIds.size();
1772 for (int i = 0; i < count; i++) {
Sunny Goyalefb7e842018-10-04 15:11:00 -07001773 int screenId = orderedScreenIds.get(i);
Ryan Lothianfa530cd2018-10-12 14:14:16 -04001774 if (!FeatureFlags.QSB_ON_FIRST_SCREEN.get() || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001775 // No need to bind the first screen, as its always bound.
1776 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
1777 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001778 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001779 }
1780
Sunny Goyalb23980c2017-08-17 07:45:25 -07001781 @Override
Tony Wickham6a71a5b2018-08-21 11:40:23 -07001782 public void preAddApps() {
1783 // If there's an undo snackbar, force it to complete to ensure empty screens are removed
1784 // before trying to add new items.
1785 mModelWriter.commitDelete();
1786 AbstractFloatingView snackbar = AbstractFloatingView.getOpenView(this, TYPE_SNACKBAR);
1787 if (snackbar != null) {
1788 snackbar.post(() -> snackbar.close(true));
1789 }
1790 }
1791
1792 @Override
Sunny Goyalefb7e842018-10-04 15:11:00 -07001793 public void bindAppsAdded(IntArray newScreens, ArrayList<ItemInfo> addNotAnimated,
Sunny Goyal29947f02017-12-18 13:49:44 -08001794 ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07001795 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08001796 if (newScreens != null) {
1797 bindAddScreens(newScreens);
1798 }
Winson Chungd64d1762013-08-20 14:37:16 -07001799
1800 // We add the items without animation on non-visible pages, and with
1801 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08001802 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001803 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001804 }
Adam Cohen76a47a12014-02-05 11:47:43 -08001805 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001806 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07001807 }
Winson Chungc58497e2013-09-03 17:48:37 -07001808
Winson Chung87412982013-10-03 18:34:14 -07001809 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07001810 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001811 }
1812
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001813 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001814 * Bind the items start-end from the list.
1815 *
1816 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001817 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07001818 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07001819 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001820 // Get the list of added items and intersect them with the set of items here
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001821 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07001822 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001823 Workspace workspace = mWorkspace;
Sunny Goyalefb7e842018-10-04 15:11:00 -07001824 int newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07001825 int end = items.size();
1826 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001827 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07001828
1829 // Short circuit if we are loading dock items for a configuration which has no dock
1830 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
1831 mHotseat == null) {
1832 continue;
1833 }
1834
Sunny Goyal639e9062015-08-19 19:17:06 -07001835 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001836 switch (item.itemType) {
1837 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1838 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08001839 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001840 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07001841 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001842 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001843 }
1844 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07001845 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001846 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001847 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001848 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001849 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07001850 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1851 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07001852 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07001853 if (view == null) {
1854 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001855 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08001856 break;
1857 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001858 default:
1859 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001860 }
Sunny Goyal639e9062015-08-19 19:17:06 -07001861
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001862 /*
1863 * Remove colliding items.
1864 */
1865 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1866 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
1867 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
1868 View v = cl.getChildAt(item.cellX, item.cellY);
1869 Object tag = v.getTag();
1870 String desc = "Collision while binding workspace item: " + item
1871 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08001872 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001873 throw (new RuntimeException(desc));
1874 } else {
1875 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001876 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001877 continue;
1878 }
1879 }
1880 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05301881 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07001882 if (animateIcons) {
1883 // Animate all the applications up now
1884 view.setAlpha(0f);
1885 view.setScaleX(0f);
1886 view.setScaleY(0f);
1887 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08001888 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07001889 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001890 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07001891
Sunny Goyalf0b6db72018-08-13 16:10:14 -07001892 // Animate to the correct page
1893 if (animateIcons && newItemsScreenId > -1) {
1894 AnimatorSet anim = new AnimatorSet();
1895 anim.playTogether(bounceAnims);
Jon Miranda64eb8ea2018-03-29 11:43:58 -07001896
Sunny Goyalefb7e842018-10-04 15:11:00 -07001897 int currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyalf0b6db72018-08-13 16:10:14 -07001898 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
1899 final Runnable startBounceAnimRunnable = anim::start;
1900
1901 if (newItemsScreenId != currentScreenId) {
1902 // We post the animation slightly delayed to prevent slowdowns
1903 // when we are loading right after we return to launcher.
1904 mWorkspace.postDelayed(new Runnable() {
1905 public void run() {
1906 if (mWorkspace != null) {
1907 AbstractFloatingView.closeAllOpenViews(Launcher.this, false);
1908
1909 mWorkspace.snapToPage(newScreenIndex);
1910 mWorkspace.postDelayed(startBounceAnimRunnable,
1911 NEW_APPS_ANIMATION_DELAY);
Winson Chung94d67682013-09-25 16:29:40 -07001912 }
Sunny Goyalf0b6db72018-08-13 16:10:14 -07001913 }
1914 }, NEW_APPS_PAGE_MOVE_DELAY);
1915 } else {
1916 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07001917 }
Winson Chung64359a52013-07-08 17:17:08 -07001918 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001919 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001920 }
1921
Joe Onorato9c1289c2009-08-17 11:03:03 -04001922 /**
1923 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001924 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07001925 public void bindAppWidget(LauncherAppWidgetInfo item) {
1926 View view = inflateAppWidget(item);
1927 if (view != null) {
1928 mWorkspace.addInScreen(view, item);
1929 mWorkspace.requestLayout();
1930 }
1931 }
1932
1933 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001934 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05301935 PendingAppWidgetHostView view =
1936 new PendingAppWidgetHostView(this, item, mIconCache, true);
1937 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07001938 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001939 }
1940
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001941 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001942
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001943 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08001944
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001945 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
1946 // If the provider is not ready, bind as a pending widget.
1947 appWidgetInfo = null;
1948 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
1949 // The widget id is not valid. Try to find the widget based on the provider info.
1950 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
1951 } else {
1952 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
1953 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07001954
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001955 // If the provider is ready, but the width is not yet restored, try to restore it.
1956 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
1957 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07001958 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001959 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
1960 + " belongs to component " + item.providerName
1961 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001962 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07001963 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07001964 }
Sunny Goyalff572272014-07-23 13:58:07 -07001965
Sunny Goyal84b4adc2015-08-12 15:12:16 -07001966 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001967 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07001968 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
1969 // Id has not been allocated yet. Allocate a new id.
1970 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
1971 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07001972
Sunny Goyald478c832016-04-01 12:04:16 -07001973 // Also try to bind the widget. If the bind fails, the user will be shown
1974 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08001975 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07001976 pendingInfo.spanX = item.spanX;
1977 pendingInfo.spanY = item.spanY;
1978 pendingInfo.minSpanX = item.minSpanX;
1979 pendingInfo.minSpanY = item.minSpanY;
1980 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07001981
1982 boolean isDirectConfig =
1983 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
1984 if (isDirectConfig && item.bindOptions != null) {
1985 Bundle newOptions = item.bindOptions.getExtras();
1986 if (options != null) {
1987 newOptions.putAll(options);
1988 }
1989 options = newOptions;
1990 }
Sunny Goyald478c832016-04-01 12:04:16 -07001991 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1992 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07001993
Sunny Goyal86df1382016-08-10 15:03:22 -07001994 // We tried to bind once. If we were not able to bind, we would need to
1995 // go through the permission dialog, which means we cannot skip the config
1996 // activity.
1997 item.bindOptions = null;
1998 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
1999
Sunny Goyald478c832016-04-01 12:04:16 -07002000 // Bind succeeded
2001 if (success) {
2002 // If the widget has a configure activity, it is still needs to set it up,
2003 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002004 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002005 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2006 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002007 }
Sunny Goyald478c832016-04-01 12:04:16 -07002008
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002009 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002010 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002011 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002012 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002013 // The widget was marked as UI not ready, but there is no configure activity to
2014 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002015 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002016 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002017 }
Sunny Goyalff572272014-07-23 13:58:07 -07002018 }
2019
Sunny Goyald5462aa2016-11-22 16:52:39 +05302020 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002021 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002022 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002023 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002024 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Tony Wickham6a71a5b2018-08-21 11:40:23 -07002025 getModelWriter().deleteWidgetInfo(item, getAppWidgetHost());
Sunny Goyalb23980c2017-08-17 07:45:25 -07002026 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002027 }
2028
Sunny Goyal233ee962015-08-03 13:05:01 -07002029 item.minSpanX = appWidgetInfo.minSpanX;
2030 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302031 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002032 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302033 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002034 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302035 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002036
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002037 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002038 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002039 }
2040
Sunny Goyalff572272014-07-23 13:58:07 -07002041 /**
2042 * Restores a pending widget.
2043 *
2044 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002045 */
Sunny Goyald478c832016-04-01 12:04:16 -07002046 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002047 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2048 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2049 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002050 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002051 }
2052
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002053 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002054 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002055 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2056 info.pendingItemInfo = null;
2057 }
Sunny Goyalff572272014-07-23 13:58:07 -07002058
Sunny Goyalba47faa2017-10-04 16:50:57 -07002059 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002060 view.reInflate();
Sunny Goyalba47faa2017-10-04 16:50:57 -07002061 }
2062
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002063 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002064 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002065 }
2066
Adam Cohen1462de32012-07-24 22:34:36 -07002067 public void onPageBoundSynchronously(int page) {
Sunny Goyalf4aceab2017-12-06 16:16:31 -08002068 mSynchronouslyBoundPage = page;
Adam Cohen1462de32012-07-24 22:34:36 -07002069 }
2070
Sunny Goyal527c7d32015-08-28 15:19:36 -07002071 @Override
2072 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2073 if (mPendingExecutor != null) {
2074 mPendingExecutor.markCompleted();
2075 }
2076 mPendingExecutor = executor;
Sunny Goyal60180b02018-02-07 12:56:30 -08002077 if (!isInState(ALL_APPS)) {
2078 mAppsView.getAppsStore().setDeferUpdates(true);
2079 mPendingExecutor.execute(() -> mAppsView.getAppsStore().setDeferUpdates(false));
2080 }
2081
Sunny Goyal527c7d32015-08-28 15:19:36 -07002082 executor.attachTo(this);
2083 }
2084
2085 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2086 if (mPendingExecutor == executor) {
2087 mPendingExecutor = null;
2088 }
2089 }
2090
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002091 @Override
2092 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
Sunny Goyal6001ea22018-05-10 16:31:00 -07002093 AlphaProperty property = mDragLayer.getAlphaProperty(ALPHA_INDEX_LAUNCHER_LOAD);
2094 if (property.getValue() < 1) {
2095 ObjectAnimator anim = ObjectAnimator.ofFloat(property, MultiValueAlpha.VALUE, 1);
2096 if (executor != null) {
2097 anim.addListener(new AnimatorListenerAdapter() {
2098 @Override
2099 public void onAnimationEnd(Animator animation) {
2100 executor.onLoadAnimationCompleted();
2101 }
2102 });
2103 }
2104 anim.start();
Sunny Goyal29947f02017-12-18 13:49:44 -08002105 } else if (executor != null) {
2106 executor.onLoadAnimationCompleted();
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002107 }
2108 }
2109
Joe Onorato9c1289c2009-08-17 11:03:03 -04002110 /**
2111 * Callback saying that there aren't any more items to bind.
2112 *
2113 * Implementation of the method from LauncherModel.Callbacks.
2114 */
Tonyf80e8932018-12-21 11:58:04 -08002115 public void finishBindingItems(int pageBoundFirst) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002116 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002117 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002118
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002119 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002120
Sunny Goyal2100c782016-08-22 16:00:03 -07002121 if (mPendingActivityResult != null) {
2122 handleActivityResult(mPendingActivityResult.requestCode,
2123 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2124 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002125 }
2126
Sunny Goyala474a9b2017-05-04 16:47:11 -07002127 InstallShortcutReceiver.disableAndFlushInstallQueue(
2128 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002129
Tonyf80e8932018-12-21 11:58:04 -08002130 // When undoing the removal of the last item on a page, return to that page.
2131 mWorkspace.setCurrentPage(pageBoundFirst);
Tony Wickham6a71a5b2018-08-21 11:40:23 -07002132
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002133 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002134 }
2135
Winson Chunga2413752012-04-03 14:22:34 -07002136 private boolean canRunNewAppsAnimation() {
2137 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002138 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002139 }
2140
Winson Chungc9168342013-06-26 14:54:55 -07002141 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002142 ValueAnimator bounceAnim = new PropertyListBuilder().alpha(1).scale(1).build(v)
2143 .setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
Winson Chungc9168342013-06-26 14:54:55 -07002144 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002145 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002146 return bounceAnim;
2147 }
2148
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002149 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002150 * Add the icons for all apps.
2151 *
2152 * Implementation of the method from LauncherModel.Callbacks.
2153 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002154 public void bindAllApplications(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002155 mAppsView.getAppsStore().setApps(apps);
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002156
Adam Cohen9211d422014-10-07 18:14:53 -07002157 if (mLauncherCallbacks != null) {
2158 mLauncherCallbacks.bindAllApplications(apps);
2159 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002160 }
2161
2162 /**
Zak Cohen658c67a2018-10-19 14:21:05 -07002163 * Copies LauncherModel's map of activities to shortcut counts to Launcher's. This is necessary
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002164 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2165 */
2166 @Override
Zak Cohen658c67a2018-10-19 14:21:05 -07002167 public void bindDeepShortcutMap(HashMap<ComponentKey, Integer> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002168 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002169 }
2170
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002171 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002172 * A package was updated.
2173 *
2174 * Implementation of the method from LauncherModel.Callbacks.
2175 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002176 @Override
2177 public void bindAppsAddedOrUpdated(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002178 mAppsView.getAppsStore().addOrUpdateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002179 }
2180
Sunny Goyal4390ace2014-10-13 11:33:11 -07002181 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002182 public void bindPromiseAppProgressUpdated(PromiseAppInfo app) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002183 mAppsView.getAppsStore().updatePromiseAppProgress(app);
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002184 }
2185
2186 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002187 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002188 mWorkspace.widgetsRestored(widgets);
2189 }
2190
Joe Onorato9c1289c2009-08-17 11:03:03 -04002191 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002192 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002193 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002194 *
2195 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002196 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002197 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002198 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002199 if (!updated.isEmpty()) {
2200 mWorkspace.updateShortcuts(updated);
2201 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002202 }
2203
2204 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002205 * Update the state of a package, typically related to install state.
2206 *
2207 * Implementation of the method from LauncherModel.Callbacks.
2208 */
Sunny Goyale755d462014-07-22 13:48:29 -07002209 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002210 public void bindRestoreItemsChange(HashSet<ItemInfo> updates) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07002211 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07002212 }
2213
2214 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002215 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07002216 * in addition to specific applications to remove, the reason being that
2217 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002218 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07002219 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002220 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07002221 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002222 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002223 mWorkspace.removeItemsByMatcher(matcher);
2224 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002225 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002226
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002227 @Override
2228 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002229 mAppsView.getAppsStore().removeApps(appInfos);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002230 }
Joe Onoratobe386092009-11-17 17:32:16 -08002231
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002232 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002233 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
2234 mPopupDataProvider.setAllWidgets(allWidgets);
Tony Wickham26b17462017-03-20 17:12:24 -07002235 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
2236 if (topView != null) {
2237 topView.onWidgetsBound();
2238 }
2239 }
2240
Tony Wickham86222d22017-03-29 15:30:43 -07002241 /**
2242 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
2243 * refreshes the widgets and shortcuts associated with the given package/user
2244 */
2245 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07002246 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002247 }
2248
Winson Chung80baf5a2010-08-09 16:03:15 -07002249 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002250 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08002251 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002252 @Override
2253 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
2254 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002255
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002256 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
2257 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07002258 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002259 writer.println(prefix + " Homescreen " + i);
2260
2261 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
2262 for (int j = 0; j < layout.getChildCount(); j++) {
2263 Object tag = layout.getChildAt(j).getTag();
2264 if (tag != null) {
2265 writer.println(prefix + " " + tag.toString());
2266 }
2267 }
2268 }
2269
2270 writer.println(prefix + " Hotseat");
Sunny Goyal876e4622018-11-02 13:50:40 -07002271 ViewGroup layout = mHotseat.getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07002272 for (int j = 0; j < layout.getChildCount(); j++) {
2273 Object tag = layout.getChildAt(j).getTag();
2274 if (tag != null) {
2275 writer.println(prefix + " " + tag.toString());
2276 }
Adam Cohen4caf2982013-08-20 18:54:31 -07002277 }
Sunny Goyala1365452015-10-01 15:46:24 -07002278 }
2279
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002280 writer.println(prefix + "Misc:");
2281 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
2282 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
2283 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
Sunny Goyale43d00d2018-05-14 14:23:18 -07002284 writer.println(" mRotationHelper: " + mRotationHelper);
Sunny Goyalcb2c5052018-10-15 12:30:29 -07002285 // Extra logging for b/116853349
2286 mDragLayer.dumpAlpha(writer);
Sunny Goyale43d00d2018-05-14 14:23:18 -07002287 dumpMisc(writer);
Sunny Goyal2bba1902018-02-27 12:20:50 -08002288
2289 try {
2290 FileLog.flushAll(writer);
2291 } catch (Exception e) {
2292 // Ignore
2293 }
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002294
2295 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002296
Adam Cohen9211d422014-10-07 18:14:53 -07002297 if (mLauncherCallbacks != null) {
2298 mLauncherCallbacks.dump(prefix, fd, writer, args);
2299 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002300 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002301
Sunny Goyal66b24572016-09-21 15:57:55 -07002302 @Override
2303 @TargetApi(Build.VERSION_CODES.N)
2304 public void onProvideKeyboardShortcuts(
2305 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
2306
2307 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002308 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002309 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
2310 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
Vadim Tryshev74c261c2018-06-12 13:44:43 -07002311 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.widget_button_text),
2312 KeyEvent.KEYCODE_W, KeyEvent.META_CTRL_ON));
Sunny Goyal66b24572016-09-21 15:57:55 -07002313 }
Vadim Tryshev3455f8d2018-05-11 20:02:20 -07002314 final View currentFocus = getCurrentFocus();
2315 if (currentFocus != null) {
2316 if (new CustomActionsPopup(this, currentFocus).canShow()) {
2317 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
2318 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
2319 }
2320 if (currentFocus.getTag() instanceof ItemInfo
2321 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
2322 shortcutInfos.add(new KeyboardShortcutInfo(
2323 getString(R.string.shortcuts_menu_with_notifications_description),
2324 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
2325 }
Sunny Goyal66b24572016-09-21 15:57:55 -07002326 }
2327 if (!shortcutInfos.isEmpty()) {
2328 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
2329 }
2330
2331 super.onProvideKeyboardShortcuts(data, menu, deviceId);
2332 }
2333
2334 @Override
2335 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
2336 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
2337 switch (keyCode) {
2338 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002339 if (isInState(NORMAL)) {
2340 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07002341 return true;
2342 }
2343 break;
2344 case KeyEvent.KEYCODE_S: {
2345 View focusedView = getCurrentFocus();
2346 if (focusedView instanceof BubbleTextView
2347 && focusedView.getTag() instanceof ItemInfo
2348 && mAccessibilityDelegate.performAction(focusedView,
2349 (ItemInfo) focusedView.getTag(),
2350 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal00ac9202017-11-09 15:24:06 -08002351 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07002352 return true;
2353 }
2354 break;
2355 }
2356 case KeyEvent.KEYCODE_O:
2357 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
2358 return true;
2359 }
2360 break;
Vadim Tryshev74c261c2018-06-12 13:44:43 -07002361 case KeyEvent.KEYCODE_W:
2362 if (isInState(NORMAL)) {
2363 OptionsPopupView.openWidgets(this);
2364 return true;
2365 }
2366 break;
Sunny Goyal66b24572016-09-21 15:57:55 -07002367 }
2368 }
2369 return super.onKeyShortcut(keyCode, event);
2370 }
2371
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002372 @Override
2373 public boolean onKeyUp(int keyCode, KeyEvent event) {
2374 if (keyCode == KeyEvent.KEYCODE_MENU) {
2375 // KEYCODE_MENU is sent by some tests, for example
2376 // LauncherJankTests#testWidgetsContainerFling. Don't just remove its handling.
2377 if (!mDragController.isDragging() && !mWorkspace.isSwitchingState() &&
2378 isInState(NORMAL)) {
2379 // Close any open floating views.
2380 AbstractFloatingView.closeAllOpenViews(this);
2381
2382 // Setting the touch point to (-1, -1) will show the options popup in the center of
2383 // the screen.
Sunny Goyal2fd7a8b2018-03-30 17:10:13 -07002384 OptionsPopupView.showDefaultOptions(this, -1, -1);
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002385 }
2386 return true;
2387 }
2388 return super.onKeyUp(keyCode, event);
2389 }
2390
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002391 public static Launcher getLauncher(Context context) {
Sunny Goyal87b5eb62018-07-03 15:53:39 -07002392 return (Launcher) fromContext(context);
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002393 }
2394
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002395 /**
Sunny Goyalab770a12018-11-14 15:17:26 -08002396 * Just a wrapper around the type cast to allow easier tracking of calls.
2397 */
2398 public static <T extends Launcher> T cast(ActivityContext activityContext) {
2399 return (T) activityContext;
2400 }
2401
2402 /**
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002403 * Callback for listening for onResume
2404 */
2405 public interface OnResumeCallback {
2406
2407 void onLauncherResume();
2408 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002409}