blob: c3c4f5e5ed51ef340a20316b65d3a91b857723b4 [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
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080019import static android.content.pm.ActivityInfo.CONFIG_ORIENTATION;
20import static android.content.pm.ActivityInfo.CONFIG_SCREEN_SIZE;
Vadim Tryshev6d2321c2018-04-11 17:19:08 -070021
Sunny Goyal4c7f2152017-10-17 17:17:16 -070022import static com.android.launcher3.LauncherAnimUtils.SPRING_LOADED_EXIT_DELAY;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -070023import static com.android.launcher3.LauncherState.ALL_APPS;
24import static com.android.launcher3.LauncherState.NORMAL;
Sunny Goyal6001ea22018-05-10 16:31:00 -070025import static com.android.launcher3.dragndrop.DragLayer.ALPHA_INDEX_LAUNCHER_LOAD;
Sunny Goyalaeb16432017-10-16 11:46:41 -070026import static com.android.launcher3.logging.LoggerUtils.newContainerTarget;
Hyunyoung Song46d07f72018-05-22 15:41:25 -070027import static com.android.launcher3.logging.LoggerUtils.newTarget;
Sunny Goyal326403e2017-10-02 12:45:10 -070028
Gilles Debunnedd6c9922010-10-25 11:23:41 -070029import android.animation.Animator;
Sunny Goyal6001ea22018-05-10 16:31:00 -070030import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070031import android.animation.AnimatorSet;
Sunny Goyal6001ea22018-05-10 16:31:00 -070032import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070033import android.animation.ValueAnimator;
Adam Cohen0b395352014-06-09 22:54:36 +000034import android.annotation.TargetApi;
Tony Wickham005df0b2018-02-13 11:28:12 -080035import android.app.ActivityOptions;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080036import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070037import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080039import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080040import android.content.ComponentCallbacks2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Context;
Andrew Sappersteinabef55a2016-06-19 12:49:00 -070042import android.content.ContextWrapper;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070044import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070045import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070046import android.content.SharedPreferences;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.content.pm.PackageManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080048import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070049import android.database.sqlite.SQLiteDatabase;
Sunny Goyal59d086c2018-05-07 17:31:40 -070050import android.graphics.Point;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070051import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020052import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053import android.os.Bundle;
Hyunyoung Songbd6fba92018-05-16 15:54:31 -070054import android.os.Handler;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070055import android.os.Parcelable;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080056import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070057import android.os.StrictMode;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070058import android.os.UserHandle;
Tony Wickham86222d22017-03-29 15:30:43 -070059import android.support.annotation.Nullable;
Michael Jurkaa33411c2012-06-14 16:18:21 -070060import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070062import android.util.Log;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070063import android.util.SparseArray;
Sunny Goyal59d086c2018-05-07 17:31:40 -070064import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070065import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070066import android.view.KeyboardShortcutGroup;
67import android.view.KeyboardShortcutInfo;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080068import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -070069import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070070import android.view.View;
Adam Cohen96d30a12013-07-16 18:13:21 -070071import android.view.ViewGroup;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080072import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070073import android.view.animation.OvershootInterpolator;
Adam Cohen96d30a12013-07-16 18:13:21 -070074import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070075
Sunny Goyal651077b2014-06-30 14:15:31 -070076import com.android.launcher3.DropTarget.DragObject;
Jon Mirandac476d6e2017-05-04 16:30:01 -070077import com.android.launcher3.Workspace.ItemOperator;
Sunny Goyalae502842016-06-17 08:43:56 -070078import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070079import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070080import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal7ede6112017-12-05 15:11:21 -080081import com.android.launcher3.allapps.DiscoveryBounce;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070082import com.android.launcher3.badge.BadgeInfo;
Sunny Goyalffe83f12014-08-14 17:39:34 -070083import com.android.launcher3.compat.AppWidgetManagerCompat;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -070084import com.android.launcher3.compat.LauncherAppsCompatVO;
Tony Wickhame0c33232016-02-08 11:37:04 -080085import com.android.launcher3.config.FeatureFlags;
Vadim Tryshevfedca432015-08-19 17:55:02 -070086import com.android.launcher3.dragndrop.DragController;
87import com.android.launcher3.dragndrop.DragLayer;
88import com.android.launcher3.dragndrop.DragView;
Sunny Goyal26119432016-02-18 22:09:23 +000089import com.android.launcher3.folder.FolderIcon;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070090import com.android.launcher3.folder.FolderIconPreviewVerifier;
Sunny Goyal66b24572016-09-21 15:57:55 -070091import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -070092import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -070093import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -070094import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Song46d07f72018-05-22 15:41:25 -070095import com.android.launcher3.logging.UserEventDispatcher.UserEventDelegate;
Sunny Goyala535ae42017-02-27 10:07:13 -080096import com.android.launcher3.model.ModelWriter;
Sunny Goyala535ae42017-02-27 10:07:13 -080097import com.android.launcher3.notification.NotificationListener;
Tony Wickham540913e2017-01-23 11:47:51 -080098import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -080099import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700100import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyalf8088ee2017-11-10 14:52:00 -0800101import com.android.launcher3.states.InternalStateHandler;
Sunny Goyal623eddd2018-03-02 12:24:41 -0800102import com.android.launcher3.states.RotationHelper;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800103import com.android.launcher3.touch.ItemClickHandler;
Sunny Goyala5ace712017-11-15 17:12:51 -0800104import com.android.launcher3.uioverrides.UiFactory;
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700105import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530106import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
107import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
Sunny Goyalaeb16432017-10-16 11:46:41 -0700108import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyal2100c782016-08-22 16:00:03 -0700109import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700110import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700111import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700112import com.android.launcher3.util.MultiHashMap;
Sunny Goyal6001ea22018-05-10 16:31:00 -0700113import com.android.launcher3.util.MultiValueAlpha;
114import com.android.launcher3.util.MultiValueAlpha.AlphaProperty;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700115import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800116import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700117import com.android.launcher3.util.PendingRequestArgs;
Sunny Goyal8392c822017-06-20 10:03:56 -0700118import com.android.launcher3.util.SystemUiController;
Mario Bertschlera6936942017-05-31 14:48:19 -0700119import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700120import com.android.launcher3.util.Thunk;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700121import com.android.launcher3.util.TraceHelper;
Sunny Goyal326403e2017-10-02 12:45:10 -0700122import com.android.launcher3.util.UiThreadHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700123import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800124import com.android.launcher3.views.OptionsPopupView;
Sunny Goyal29947f02017-12-18 13:49:44 -0800125import com.android.launcher3.widget.LauncherAppWidgetHostView;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800126import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700127import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal29947f02017-12-18 13:49:44 -0800128import com.android.launcher3.widget.PendingAppWidgetHostView;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800129import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700130import com.android.launcher3.widget.WidgetHostViewLoader;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700131import com.android.launcher3.widget.WidgetListRowEntry;
132import com.android.launcher3.widget.WidgetsFullSheet;
Sunny Goyal952e63d2017-08-16 04:59:08 -0700133import com.android.launcher3.widget.custom.CustomWidgetParser;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700134
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700135import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700136import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700137import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700138import java.util.Collection;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700139import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700140import java.util.List;
Tony Wickham010d2552017-01-20 08:15:28 -0800141import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700142
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143/**
144 * Default launcher application.
145 */
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700146public class Launcher extends BaseDraggingActivity implements LauncherExterns,
147 LauncherModel.Callbacks, LauncherProviderChangeListener, UserEventDelegate{
Vadim Tryshevfedca432015-08-19 17:55:02 -0700148 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700149 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150
Winson Chunga2413752012-04-03 14:22:34 -0700151 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700152
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700154 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700155
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700156 private static final int REQUEST_PICK_APPWIDGET = 9;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157
Michael Jurka8b805b12012-04-18 14:23:14 -0700158 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800159 public static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
160 public static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700161
Jon Mirandac476d6e2017-05-04 16:30:01 -0700162 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700163
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700164 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
165
Mathew Inwood876a8462013-06-14 14:12:41 +0100166 /**
167 * IntentStarter uses request codes starting with this. This must be greater than all activity
168 * request codes used internally.
169 */
170 protected static final int REQUEST_LAST = 100;
171
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 // Type: int
173 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700174 // Type: int
175 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700176 // Type: PendingRequestArgs
177 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
178 // Type: ActivityResultInfo
179 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700180 // Type: SparseArray<Parcelable>
181 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700183 private LauncherStateManager mStateManager;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700184
Adam Cohenad4e15c2013-10-17 16:21:35 -0700185 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186
Winson Chunga2413752012-04-03 14:22:34 -0700187 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700188 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
189 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
190 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700191
Jon Miranda54441f52018-01-24 15:38:25 -0800192 private LauncherAppTransitionManager mAppTransitionManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800193 private Configuration mOldConfig;
Jon Miranda54441f52018-01-24 15:38:25 -0800194
Adam Cohen091440a2015-03-18 14:16:05 -0700195 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700196 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700197 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800198 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700199
Sunny Goyalffe83f12014-08-14 17:39:34 -0700200 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700201 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700202
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700203 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700204
Sunny Goyal316490e2015-06-02 09:38:28 -0700205 @Thunk Hotseat mHotseat;
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700206 @Nullable private View mHotseatSearchBox;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700207
Sunny Goyal47328fd2016-05-25 18:56:41 -0700208 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700209
210 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700211 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700212 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700213
Winson Chung8ae41982017-11-10 11:42:13 -0800214 // UI and state for the overview panel
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800215 private View mOverviewPanel;
Winson Chung8ae41982017-11-10 11:42:13 -0800216
Adam Cohen091440a2015-03-18 14:16:05 -0700217 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400218
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700219 private OnResumeCallback mOnResumeCallback;
220
Sunny Goyal527c7d32015-08-28 15:19:36 -0700221 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700222
Joe Onorato9c1289c2009-08-17 11:03:03 -0400223 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800224 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800225 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700226 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400227
Tony Wickham010d2552017-01-20 08:15:28 -0800228 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700229
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800230 private int mSynchronouslyBoundPage = PagedView.INVALID_PAGE;
Adam Cohen1462de32012-07-24 22:34:36 -0700231
Winson Chung46353de2012-02-16 14:05:10 -0800232 // We only want to get the SharedPreferences once since it does an FS stat each time we get
233 // it from the context.
234 private SharedPreferences mSharedPrefs;
235
Sunny Goyal2100c782016-08-22 16:00:03 -0700236 // Activity result which needs to be processed after workspace has loaded.
237 private ActivityResultInfo mPendingActivityResult;
238 /**
239 * Holds extra information required to handle a result from an external call, like
240 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
241 */
242 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800243
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700244 public ViewGroupFocusHelper mFocusHandler;
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700245
Sunny Goyal623eddd2018-03-02 12:24:41 -0800246 private RotationHelper mRotationHelper;
Sunny Goyal7779d622015-06-11 16:18:39 -0700247
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700248
249 private final Handler mHandler = new Handler();
250 private final Runnable mLogOnDelayedResume = this::logOnDelayedResume;
251
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800252 @Override
253 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700254 if (DEBUG_STRICT_MODE) {
255 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
256 .detectDiskReads()
257 .detectDiskWrites()
258 .detectNetwork() // or .detectAll() for all detectable problems
259 .penaltyLog()
260 .build());
261 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
262 .detectLeakedSqlLiteObjects()
263 .detectLeakedClosableObjects()
264 .penaltyLog()
265 .penaltyDeath()
266 .build());
267 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700268 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700269
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800270 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700271 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400272
Sunny Goyal87f784c2017-01-11 10:48:34 -0800273 LauncherAppState app = LauncherAppState.getInstance(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800274 mOldConfig = new Configuration(getResources().getConfiguration());
Sunny Goyal0e7724c2018-02-22 13:22:21 -0800275 mModel = app.setLauncher(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800276 initDeviceProfile(app.getInvariantDeviceProfile());
Winson Chung6e1c0d32013-10-25 15:24:24 -0700277
Sunny Goyalf7258242015-10-19 16:59:07 -0700278 mSharedPrefs = Utilities.getPrefs(this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800279 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700280 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700281
Joe Onorato41a12d22009-10-31 18:30:00 -0400282 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700283 mAllAppsController = new AllAppsTransitionController(this);
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800284 mStateManager = new LauncherStateManager(this);
Tracy Zhou8b23c6f2018-04-30 17:13:27 -0700285 UiFactory.onCreate(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700286
Sunny Goyalffe83f12014-08-14 17:39:34 -0700287 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700288
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700289 mAppWidgetHost = new LauncherAppWidgetHost(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700290 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700291
Sunny Goyal60820d72017-05-09 12:40:11 -0700292 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700293
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800294 setupViews();
Tony Wickham010d2552017-01-20 08:15:28 -0800295 mPopupDataProvider = new PopupDataProvider(this);
296
Sunny Goyal623eddd2018-03-02 12:24:41 -0800297 mRotationHelper = new RotationHelper(this);
Sunny Goyal90548432018-04-04 17:17:00 -0700298 mAppTransitionManager = LauncherAppTransitionManager.newInstance(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800299
Sunny Goyald3864fa2017-11-14 15:06:36 -0800300 boolean internalStateHandled = InternalStateHandler.handleCreate(this, getIntent());
301 if (internalStateHandled) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800302 if (savedInstanceState != null) {
303 // InternalStateHandler has already set the appropriate state.
304 // We dont need to do anything.
305 savedInstanceState.remove(RUNTIME_STATE);
306 }
307 }
308 restoreState(savedInstanceState);
Winson Chungb63b44c2017-11-10 17:54:44 -0800309
Sunny Goyal2100c782016-08-22 16:00:03 -0700310 // We only load the page synchronously if the user rotates (or triggers a
311 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700312 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
313 if (savedInstanceState != null) {
314 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
315 }
Sunny Goyald3864fa2017-11-14 15:06:36 -0800316
Sunny Goyalfe770c92016-09-27 14:38:58 -0700317 if (!mModel.startLoader(currentScreen)) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800318 if (!internalStateHandled) {
319 // If we are not binding synchronously, show a fade in animation when
320 // the first page bind completes.
Sunny Goyal6001ea22018-05-10 16:31:00 -0700321 mDragLayer.getAlphaProperty(ALPHA_INDEX_LAUNCHER_LOAD).setValue(0);
Sunny Goyald3864fa2017-11-14 15:06:36 -0800322 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700323 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700324 // Pages bound synchronously.
325 mWorkspace.setCurrentPage(currentScreen);
326
Sunny Goyal2100c782016-08-22 16:00:03 -0700327 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800328 }
329
330 // For handling default keys
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800331 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800332
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800333 setContentView(mLauncherView);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800334 getRootView().dispatchInsets();
Mario Bertschlera6936942017-05-31 14:48:19 -0700335
Jon Miranda7fda2852017-07-19 16:07:01 -0700336 // Listen for broadcasts
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700337 registerReceiver(mScreenOffReceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));
Sunny Goyala616d2b2017-06-12 13:54:01 -0700338
Sunny Goyal8392c822017-06-20 10:03:56 -0700339 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
340 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700341
342 if (mLauncherCallbacks != null) {
343 mLauncherCallbacks.onCreate(savedInstanceState);
344 }
Sunny Goyal623eddd2018-03-02 12:24:41 -0800345 mRotationHelper.initialize();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700346
347 TraceHelper.endSection("Launcher-onCreate");
Adam Cohen9211d422014-10-07 18:14:53 -0700348 }
349
Sunny Goyal7779d622015-06-11 16:18:39 -0700350 @Override
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800351 public void onConfigurationChanged(Configuration newConfig) {
352 int diff = newConfig.diff(mOldConfig);
353 if ((diff & (CONFIG_ORIENTATION | CONFIG_SCREEN_SIZE)) != 0) {
354 mUserEventDispatcher = null;
355 initDeviceProfile(mDeviceProfile.inv);
Sunny Goyalfde55052018-02-01 14:46:13 -0800356 dispatchDeviceProfileChanged();
Sunny Goyal59d086c2018-05-07 17:31:40 -0700357 reapplyUi();
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700358 mDragLayer.recreateControllers();
Sunny Goyal7185dd62018-03-14 17:51:49 -0700359
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800360 // TODO: We can probably avoid rebind when only screen size changed.
Sunny Goyal605bcf32018-03-02 15:16:12 -0800361 rebindModel();
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800362 }
363
364 mOldConfig.setTo(newConfig);
Tracy Zhoua706f002018-03-28 13:55:19 -0700365 UiFactory.onLauncherStateOrResumeChanged(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800366 super.onConfigurationChanged(newConfig);
367 }
368
Sunny Goyal605bcf32018-03-02 15:16:12 -0800369 @Override
Sunny Goyal59d086c2018-05-07 17:31:40 -0700370 protected void reapplyUi() {
371 getRootView().dispatchInsets();
372 getStateManager().reapplyState(true /* cancelCurrentAnimation */);
373 }
374
375 @Override
Sunny Goyal605bcf32018-03-02 15:16:12 -0800376 public void rebindModel() {
377 int currentPage = mWorkspace.getNextPage();
378 if (mModel.startLoader(currentPage)) {
379 mWorkspace.setCurrentPage(currentPage);
380 setWorkspaceLoading(true);
381 }
382 }
383
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800384 private void initDeviceProfile(InvariantDeviceProfile idp) {
385 // Load configuration-specific DeviceProfile
Sunny Goyal59d086c2018-05-07 17:31:40 -0700386 mDeviceProfile = idp.getDeviceProfile(this);
387 if (isInMultiWindowModeCompat()) {
388 Display display = getWindowManager().getDefaultDisplay();
389 Point mwSize = new Point();
390 display.getSize(mwSize);
391 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
392 }
393 onDeviceProfileInitiated();
Sunny Goyal605bcf32018-03-02 15:16:12 -0800394 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout(), true);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800395 }
396
Sunny Goyal623eddd2018-03-02 12:24:41 -0800397 public RotationHelper getRotationHelper() {
398 return mRotationHelper;
399 }
400
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700401 public LauncherStateManager getStateManager() {
402 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700403 }
404
Mario Bertschler27288382017-05-24 15:35:09 -0700405 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700406 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800407 return mLauncherView.findViewById(id);
408 }
409
410 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700411 public void onAppWidgetHostReset() {
412 if (mAppWidgetHost != null) {
413 mAppWidgetHost.startListening();
414 }
415 }
416
Adam Cohen9211d422014-10-07 18:14:53 -0700417 private LauncherCallbacks mLauncherCallbacks;
418
Sunny Goyal32554d12015-12-03 15:31:25 -0800419 /**
420 * Call this after onCreate to set or clear overlay.
421 */
422 public void setLauncherOverlay(LauncherOverlay overlay) {
423 if (overlay != null) {
424 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
425 }
426 mWorkspace.setLauncherOverlay(overlay);
427 }
428
Adam Cohen9211d422014-10-07 18:14:53 -0700429 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
430 mLauncherCallbacks = callbacks;
431 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700432 }
433
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700434 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700435 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700436 if (mLauncherCallbacks != null) {
437 mLauncherCallbacks.onLauncherProviderChange();
438 }
439 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700440
Hyunyoung Song3f471442015-04-08 19:01:34 -0700441 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700442 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
443 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700444 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700445 }
446
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000447 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700448 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
449 // This cast is safe as long as the id < 0x00FFFFFF
450 // Since we jail all the dynamically generated views, there should be no clashes
451 // with any other views.
452 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000453 }
454
Tony Wickham010d2552017-01-20 08:15:28 -0800455 public PopupDataProvider getPopupDataProvider() {
456 return mPopupDataProvider;
457 }
458
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700459 @Override
460 public BadgeInfo getBadgeInfoForItem(ItemInfo info) {
461 return mPopupDataProvider.getBadgeInfoForItem(info);
462 }
463
464 @Override
465 public void invalidateParent(ItemInfo info) {
466 FolderIconPreviewVerifier verifier = new FolderIconPreviewVerifier(getDeviceProfile().inv);
467 if (verifier.isItemInPreview(info.rank) && (info.container >= 0)) {
468 View folderIcon = getWorkspace().getHomescreenIconByItemId(info.container);
469 if (folderIcon != null) {
470 folderIcon.invalidate();
471 }
472 }
473 }
474
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000475 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700476 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
477 * a configuration step, this allows the proper animations to run after other transitions.
478 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700479 private long completeAdd(
480 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
481 long screenId = info.screenId;
482 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700483 // When the screen id represents an actual screen (as opposed to a rank) we make sure
484 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700485 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700486 }
Adam Cohendb364c32014-05-20 14:23:40 -0700487
Sunny Goyal2100c782016-08-22 16:00:03 -0700488 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800489 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700490 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700491 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800492 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700493 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700494 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700495 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700496 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700497 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700498 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700499 int widgetId = appWidgetId;
500 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700501 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700502 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700503 // Since the view was just bound, also launch the configure activity if needed
504 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
505 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800506 if (provider != null) {
507 new WidgetAddFlowHandler(provider)
508 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700509 }
510 }
511 break;
512 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800513 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700514
Adam Cohendb364c32014-05-20 14:23:40 -0700515 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800516 }
517
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800518 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700519 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700520 if (isWorkspaceLoading()) {
521 // process the result once the workspace has loaded.
522 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
523 return;
524 }
525 mPendingActivityResult = null;
526
Winson Chunge341d302013-08-16 14:31:00 -0700527 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700528 final PendingRequestArgs requestArgs = mPendingRequestArgs;
529 setWaitingForResult(null);
530 if (requestArgs == null) {
531 return;
532 }
533
534 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700535
Adam Cohenad4e15c2013-10-17 16:21:35 -0700536 Runnable exitSpringLoaded = new Runnable() {
537 @Override
538 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700539 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700540 }
541 };
542
Michael Jurka8b805b12012-04-18 14:23:14 -0700543 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700544 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700545 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700546 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
547 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700548 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700549 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700550 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700551 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700552 addAppWidgetImpl(
553 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800554 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700555 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700556 }
557 return;
558 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200559
Adam Cohened66b2b2012-01-23 17:28:51 -0800560 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700561 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700562
Adam Cohened66b2b2012-01-23 17:28:51 -0800563 // We have special handling for widgets
564 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800565 final int appWidgetId;
566 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
567 : -1;
568 if (widgetId < 0) {
569 appWidgetId = pendingAddWidgetId;
570 } else {
571 appWidgetId = widgetId;
572 }
573
Adam Cohenad4e15c2013-10-17 16:21:35 -0700574 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800575 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700576 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
577 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700578 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700579 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700580 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700581 @Override
582 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700583 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700584 }
585 };
Adam Cohendb364c32014-05-20 14:23:40 -0700586
Sunny Goyal2100c782016-08-22 16:00:03 -0700587 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
588 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
589 } else {
590 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
591 // When the screen id represents an actual screen (as opposed to a rank)
592 // we make sure that the drop page actually exists.
593 requestArgs.screenId =
594 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700595 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700596 final CellLayout dropLayout =
597 mWorkspace.getScreenWithId(requestArgs.screenId);
598
599 dropLayout.setDropPending(true);
600 final Runnable onComplete = new Runnable() {
601 @Override
602 public void run() {
603 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
604 dropLayout.setDropPending(false);
605 }
606 };
607 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
608 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700609 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800610 return;
611 }
612
Sunny Goyald478c832016-04-01 12:04:16 -0700613 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
614 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700615 if (resultCode == RESULT_OK) {
616 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700617 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700618 }
619 // Leave the widget in the pending state if the user canceled the configure.
620 return;
621 }
622
Sunny Goyalaad90582015-07-09 14:22:50 -0700623 if (requestCode == REQUEST_CREATE_SHORTCUT) {
624 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700625 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
626 completeAdd(requestCode, data, -1, requestArgs);
627 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
628 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
629
Sunny Goyalaad90582015-07-09 14:22:50 -0700630 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700631 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
632 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800633 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800634 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800635 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800636 }
637
638 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700639 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800640 final int requestCode, final int resultCode, final Intent data) {
641 handleActivityResult(requestCode, resultCode, data);
642 if (mLauncherCallbacks != null) {
643 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
644 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800645 }
646
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700647 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700648 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600649 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700650 PendingRequestArgs pendingArgs = mPendingRequestArgs;
651 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
652 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
653 setWaitingForResult(null);
654
Sunny Goyal28c6b962015-10-12 11:42:05 -0700655 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700656 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700657 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700658 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700659 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700660 Intent intent = pendingArgs.getPendingIntent();
661
Sunny Goyal28c6b962015-10-12 11:42:05 -0700662 if (grantResults.length > 0
663 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700664 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700665 } else {
666 // TODO: Show a snack bar with link to settings
667 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700668 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700669 }
670 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600671 if (mLauncherCallbacks != null) {
672 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
673 grantResults);
674 }
675 }
676
Adam Cohendb364c32014-05-20 14:23:40 -0700677 /**
678 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
679 *
680 * @param screenId the screen id to check
681 * @return the new screen, or screenId if it exists
682 */
683 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800684 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700685 if (dropLayout == null) {
686 // it's possible that the add screen was removed because it was
687 // empty and a re-bind occurred
688 mWorkspace.addExtraEmptyScreen();
689 return mWorkspace.commitExtraEmptyScreen();
690 } else {
691 return screenId;
692 }
693 }
694
Sunny Goyal2100c782016-08-22 16:00:03 -0700695 @Thunk void completeTwoStageWidgetDrop(
696 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
697 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800698 Runnable onCompleteRunnable = null;
699 int animationType = 0;
700
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700701 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800702 if (resultCode == RESULT_OK) {
703 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
704 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800705 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700706 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800707 onCompleteRunnable = new Runnable() {
708 @Override
709 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700710 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700711 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800712 }
713 };
714 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800715 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800716 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800717 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700718 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700719 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700720 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
721 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700722 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700723 // The animated view may be null in the case of a rotation during widget configuration
724 onCompleteRunnable.run();
725 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800726 }
727
728 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700729 protected void onStop() {
730 super.onStop();
731 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700732
733 if (mLauncherCallbacks != null) {
734 mLauncherCallbacks.onStop();
735 }
Hyunyoung Song1241e612018-05-15 21:46:51 -0700736 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
737 mStateManager.getState().containerType, -1);
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700738
739 mAppWidgetHost.setListenIfResumed(false);
740
Tony Wickham010d2552017-01-20 08:15:28 -0800741 NotificationListener.removeNotificationsChangedListener();
Sunny Goyalaf161fd2018-02-05 16:34:57 -0800742 getStateManager().moveToRestState();
Winson Chungedb0d6b2018-03-21 17:32:55 -0700743
Tracy Zhou62646712018-06-26 14:19:02 -0700744 UiFactory.onLauncherStateOrResumeChanged(this);
745
Winson Chungdea51352018-04-24 13:02:10 -0700746 // Workaround for b/78520668, explicitly trim memory once UI is hidden
Winson Chung473730f2018-04-26 11:54:22 -0700747 onTrimMemory(TRIM_MEMORY_UI_HIDDEN);
Michael Jurkacd496d72013-04-11 11:32:45 -0700748 }
749
750 @Override
751 protected void onStart() {
752 super.onStart();
753 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700754
755 if (mLauncherCallbacks != null) {
756 mLauncherCallbacks.onStart();
757 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800758 mAppWidgetHost.setListenIfResumed(true);
Sunny Goyalcac002d2018-01-04 09:51:45 -0800759 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
Winson Chung62a70be2018-02-23 15:10:05 -0800760 UiFactory.onStart(this);
Michael Jurkacd496d72013-04-11 11:32:45 -0700761 }
762
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700763 private void logOnDelayedResume() {
764 if (hasBeenResumed()) {
765 getUserEventDispatcher().logActionCommand(Action.Command.RESUME,
766 mStateManager.getState().containerType, -1);
767 getUserEventDispatcher().startSession();
768 }
769 }
770
Michael Jurkacd496d72013-04-11 11:32:45 -0700771 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800772 protected void onResume() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700773 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800774 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700775 TraceHelper.partitionSection("ON_RESUME", "superCall");
776
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700777 mHandler.removeCallbacks(mLogOnDelayedResume);
778 Utilities.postAsyncCallback(mHandler, mLogOnDelayedResume);
779
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700780 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700781 // Process any items that were added while Launcher was away.
782 InstallShortcutReceiver.disableAndFlushInstallQueue(
783 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700784
Sunny Goyala474a9b2017-05-04 16:47:11 -0700785 // Refresh shortcuts if the permission changed.
786 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700787
Tracy Zhoue5575f92018-04-19 11:12:53 -0700788 DiscoveryBounce.showForHomeIfNeeded(this);
Adam Cohen9211d422014-10-07 18:14:53 -0700789 if (mLauncherCallbacks != null) {
790 mLauncherCallbacks.onResume();
791 }
Tracy Zhoua706f002018-03-28 13:55:19 -0700792 UiFactory.onLauncherStateOrResumeChanged(this);
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800793
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700794 TraceHelper.endSection("ON_RESUME");
Adam Cohen06dff352012-06-01 17:17:08 -0700795 }
796
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800797 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700798 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700799 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700800 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700801
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700802 super.onPause();
Joe Onorato24b6fd82009-11-12 13:47:09 -0800803 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700804 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700805
Adam Cohen9211d422014-10-07 18:14:53 -0700806 if (mLauncherCallbacks != null) {
807 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700808 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700809 }
810
Winson Chungd7823942018-02-16 03:23:47 +0000811 @Override
Tracy Zhoua706f002018-03-28 13:55:19 -0700812 protected void onUserLeaveHint() {
813 super.onUserLeaveHint();
814 UiFactory.onLauncherStateOrResumeChanged(this);
815 }
816
817 @Override
Winson Chungd7823942018-02-16 03:23:47 +0000818 public void onWindowFocusChanged(boolean hasFocus) {
819 super.onWindowFocusChanged(hasFocus);
820 mStateManager.onWindowFocusChanged();
821 }
822
Adam Cohenc2d6e892014-10-16 09:49:24 -0700823 public interface LauncherOverlay {
824
825 /**
826 * Touch interaction leading to overscroll has begun
827 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700828 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700829
830 /**
831 * Touch interaction related to overscroll has ended
832 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700833 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700834
835 /**
836 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
837 * screen (or in the case of RTL, the rightmost screen).
838 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700839 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700840
841 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800842 * Called when the launcher is ready to use the overlay
843 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700844 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700845 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700846 }
847
848 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700849 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700850 }
851
852 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
853
Sunny Goyalc86df472016-02-25 09:19:38 -0800854 public void onScrollChanged(float progress) {
855 if (mWorkspace != null) {
856 mWorkspace.onOverlayScrollChanged(progress);
857 }
858 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700859 }
860
Sunny Goyal16764582017-11-06 16:00:34 -0800861 public boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -0700862 if (mLauncherCallbacks != null) {
863 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700864 } else {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800865 // On O and above we there is always some setting present settings (add icon to
866 // home screen or icon badging). On earlier APIs we will have the allow rotation
867 // setting, on devices with a locked orientation,
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700868 return Utilities.ATLEAST_OREO || !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -0700869 }
Jorim Jaggid017f882014-01-14 17:08:48 -0800870 }
871
Sunny Goyalf9403d92017-10-18 10:55:56 -0700872 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -0700873 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700874 }
875
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800876 /**
877 * Restores the previous state, if it exists.
878 *
879 * @param savedState The previous state.
880 */
881 private void restoreState(Bundle savedState) {
882 if (savedState == null) {
883 return;
884 }
885
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700886 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -0700887 LauncherState[] stateValues = LauncherState.values();
888 LauncherState state = stateValues[stateOrdinal];
Sunny Goyalbc683e92017-12-06 10:25:07 -0800889 if (!state.disableRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700890 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800891 }
892
Sunny Goyal2100c782016-08-22 16:00:03 -0700893 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
894 if (requestArgs != null) {
895 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800896 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700897
898 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700899
900 SparseArray<Parcelable> widgetsState =
901 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
902 if (widgetsState != null) {
903 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
904 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800905 }
906
907 /**
908 * Finds all the views we need and configure them properly.
909 */
910 private void setupViews() {
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800911 mDragLayer = findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700912 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -0700913 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700914 mWorkspace.initParentViews(mDragLayer);
Sunny Goyal16764582017-11-06 16:00:34 -0800915 mOverviewPanel = findViewById(R.id.overview_panel);
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800916 mHotseat = findViewById(R.id.hotseat);
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700917 mHotseatSearchBox = findViewById(R.id.search_container_hotseat);
Craig Mautner360310b2012-10-26 15:13:08 -0700918
Sunny Goyal784f9c32016-03-23 16:56:54 -0700919 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
920 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
921 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800922
Winson Chung4c98d922011-05-31 16:50:48 -0700923 // Setup the drag layer
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700924 mDragLayer.setup(mDragController, mWorkspace);
925 UiFactory.setOnTouchControllersChangedListener(this, mDragLayer::recreateControllers);
Winson Chung4c98d922011-05-31 16:50:48 -0700926
Hyunyoung Song645764e2016-06-06 14:19:02 -0700927 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -0700928 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
929 // default state, otherwise we will update to the wrong offsets in RTL
930 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700931 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700932 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700933
Tony Wickham34d2c912015-09-11 09:27:58 -0700934 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700935 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700936
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700937 // Setup Apps
938 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -0700939
Winson Chung3d503fb2011-07-13 17:25:49 -0700940 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -0700941 mDragController.setMoveTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700942 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -0400943
Sunny Goyal7185dd62018-03-14 17:51:49 -0700944 mAllAppsController.setupViews(mAppsView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800945 }
946
947 /**
948 * Creates a view representing a shortcut.
949 *
950 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800951 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800952 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700953 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800954 }
955
956 /**
957 * Creates a view representing a shortcut inflated from the specified resource.
958 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800959 * @param parent The group the shortcut belongs to.
960 * @param info The data structure describing the shortcut.
961 *
962 * @return A View inflated from layoutResId.
963 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700964 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -0800965 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
966 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800967 favorite.applyFromShortcutInfo(info);
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800968 favorite.setOnClickListener(ItemClickHandler.INSTANCE);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700969 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800970 return favorite;
971 }
972
973 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -0700974 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800975 *
976 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800977 */
Adam Cohendcd297f2013-06-18 13:13:40 -0700978 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700979 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -0700980 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
981 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700982 return;
983 }
Adam Cohen558baaf2011-08-15 15:22:57 -0700984
Jon Mirandac476d6e2017-05-04 16:30:01 -0700985 int[] cellXY = mTmpAddItemCellCoordinates;
986 CellLayout layout = getCellLayout(container, screenId);
987
Sunny Goyal782f0c92017-01-19 10:27:54 -0800988 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700989 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -0700990 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
991 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -0800992 }
993
994 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -0700995 // Legacy shortcuts are only supported for primary profile.
996 info = Process.myUserHandle().equals(args.user)
997 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800998
Sunny Goyalb57645f2017-03-20 13:57:28 -0700999 if (info == null) {
1000 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1001 return;
1002 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001003 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001004 // The app is trying to add a shortcut without sufficient permissions
1005 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1006 return;
1007 }
1008 }
1009
Jon Mirandac476d6e2017-05-04 16:30:01 -07001010 if (container < 0) {
1011 // Adding a shortcut to the Workspace.
1012 final View view = createShortcut(info);
1013 boolean foundCellSpan = false;
1014 // First we check if we already know the exact location where we want to add this item.
1015 if (cellX >= 0 && cellY >= 0) {
1016 cellXY[0] = cellX;
1017 cellXY[1] = cellY;
1018 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001019
Jon Mirandac476d6e2017-05-04 16:30:01 -07001020 // If appropriate, either create a folder or add to an existing folder
1021 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001022 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001023 return;
1024 }
1025 DragObject dragObject = new DragObject();
1026 dragObject.dragInfo = info;
1027 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1028 true)) {
1029 return;
1030 }
1031 } else {
1032 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1033 }
1034
1035 if (!foundCellSpan) {
1036 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001037 return;
1038 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001039
1040 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1041 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001042 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001043 // Adding a shortcut to a Folder.
Sunny Goyale29897f2017-07-20 10:09:42 -07001044 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001045 if (folderIcon != null) {
1046 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1047 folderInfo.add(info, args.rank, false);
1048 } else {
Sunny Goyale29897f2017-07-20 10:09:42 -07001049 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001050 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001051 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001052 }
1053
Sunny Goyale29897f2017-07-20 10:09:42 -07001054 public FolderIcon findFolderIcon(final long folderIconId) {
1055 return (FolderIcon) mWorkspace.getFirstMatch(new ItemOperator() {
1056 @Override
1057 public boolean evaluate(ItemInfo info, View view) {
1058 return info != null && info.id == folderIconId;
1059 }
1060 });
1061 }
1062
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001063 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001064 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001065 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001066 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001067 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001068 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001069 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1070
Adam Cohened66b2b2012-01-23 17:28:51 -08001071 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001072 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001073 }
Romain Guycbb89e42009-06-08 15:52:54 -07001074
Adam Cohen59400422014-03-05 18:07:04 -08001075 LauncherAppWidgetInfo launcherInfo;
1076 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001077 launcherInfo.spanX = itemInfo.spanX;
1078 launcherInfo.spanY = itemInfo.spanY;
1079 launcherInfo.minSpanX = itemInfo.minSpanX;
1080 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001081 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001082
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001083 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001084 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001085
Sunny Goyal2100c782016-08-22 16:00:03 -07001086 if (hostView == null) {
1087 // Perform actual inflation because we're live
1088 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001089 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001090 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301091 prepareAppWidget(hostView, launcherInfo);
1092 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001093 }
Romain Guycbb89e42009-06-08 15:52:54 -07001094
Sunny Goyald5462aa2016-11-22 16:52:39 +05301095 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001096 hostView.setTag(item);
1097 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001098 hostView.setFocusable(true);
1099 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001100 }
1101
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001102 private final BroadcastReceiver mScreenOffReceiver = new BroadcastReceiver() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001103 @Override
1104 public void onReceive(Context context, Intent intent) {
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001105 // Reset AllApps to its initial state only if we are not in the middle of
1106 // processing a multi-step drop
1107 if (mPendingRequestArgs == null) {
1108 mStateManager.goToState(NORMAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001109 }
1110 }
1111 };
1112
Tony Wickham010d2552017-01-20 08:15:28 -08001113 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
Sunny Goyal29947f02017-12-18 13:49:44 -08001114 mWorkspace.updateIconBadges(updatedBadges);
Sunny Goyal60180b02018-02-07 12:56:30 -08001115 mAppsView.getAppsStore().updateIconBadges(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001116
Sunny Goyal29947f02017-12-18 13:49:44 -08001117 PopupContainerWithArrow popup = PopupContainerWithArrow.getOpen(Launcher.this);
1118 if (popup != null) {
1119 popup.updateNotificationHeader(updatedBadges);
Tony Wickham010d2552017-01-20 08:15:28 -08001120 }
1121 }
1122
Adam Cohended9f8d2010-11-03 13:25:16 -07001123 @Override
1124 public void onAttachedToWindow() {
1125 super.onAttachedToWindow();
1126
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001127 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyalc86df472016-02-25 09:19:38 -08001128 if (mLauncherCallbacks != null) {
1129 mLauncherCallbacks.onAttachedToWindow();
1130 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001131 }
1132
1133 @Override
1134 public void onDetachedFromWindow() {
1135 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001136
1137 if (mLauncherCallbacks != null) {
1138 mLauncherCallbacks.onDetachedFromWindow();
1139 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001140 }
1141
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001142 public AllAppsTransitionController getAllAppsController() {
1143 return mAllAppsController;
1144 }
1145
Sunny Goyal9d69c8d2018-03-19 13:41:31 -07001146 @Override
Winson Chung1a341002018-01-17 10:00:23 -08001147 public LauncherRootView getRootView() {
1148 return (LauncherRootView) mLauncherView;
1149 }
1150
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001151 @Override
Winson Chunga6945242014-01-08 14:04:34 -08001152 public DragLayer getDragLayer() {
1153 return mDragLayer;
1154 }
1155
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001156 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001157 return mAppsView;
1158 }
1159
Winson Chunga6945242014-01-08 14:04:34 -08001160 public Workspace getWorkspace() {
1161 return mWorkspace;
1162 }
1163
1164 public Hotseat getHotseat() {
1165 return mHotseat;
1166 }
1167
Sunny Goyal81b4c7b2018-03-26 12:10:31 -07001168 public View getHotseatSearchBox() {
1169 return mHotseatSearchBox;
1170 }
1171
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001172 public <T extends View> T getOverviewPanel() {
Winson Chung8ae41982017-11-10 11:42:13 -08001173 return (T) mOverviewPanel;
Winson Chunga6945242014-01-08 14:04:34 -08001174 }
1175
Sunny Goyal47328fd2016-05-25 18:56:41 -07001176 public DropTargetBar getDropTargetBar() {
1177 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001178 }
1179
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001180 public LauncherAppWidgetHost getAppWidgetHost() {
1181 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001182 }
Romain Guycbb89e42009-06-08 15:52:54 -07001183
Winson Chunga9abd0e2010-10-27 17:18:37 -07001184 public LauncherModel getModel() {
1185 return mModel;
1186 }
1187
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001188 public ModelWriter getModelWriter() {
1189 return mModelWriter;
1190 }
1191
Adam Cohen79d90c52016-04-22 13:29:20 -07001192 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001193 return mSharedPrefs;
1194 }
1195
Sunny Goyalf8d56fc2018-01-31 15:18:11 -08001196 public int getOrientation() { return mOldConfig.orientation; }
Jon Miranda6bed3502017-09-19 14:43:17 -07001197
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001198 @Override
1199 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001200 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001201 super.onNewIntent(intent);
1202
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001203 boolean alreadyOnHome = hasWindowFocus() && ((intent.getFlags() &
Winson15f8b172015-08-19 11:02:39 -07001204 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1205 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001206
1207 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001208 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001209 && AbstractFloatingView.getTopOpenView(this) == null;
Winson15f8b172015-08-19 11:02:39 -07001210 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001211 boolean internalStateHandled = InternalStateHandler
Sunny Goyal8b264562018-02-03 23:00:13 -08001212 .handleNewIntent(this, intent, isStarted());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001213
Winson15f8b172015-08-19 11:02:39 -07001214 if (isActionMain) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001215 if (!internalStateHandled) {
1216 // Note: There should be at most one log per method call. This is enforced
1217 // implicitly by using if-else statements.
1218 UserEventDispatcher ued = getUserEventDispatcher();
1219 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
1220 if (topOpenView != null) {
1221 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
1222 } else if (alreadyOnHome) {
1223 Target target = newContainerTarget(mStateManager.getState().containerType);
1224 target.pageIndex = mWorkspace.getCurrentPage();
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001225 ued.logActionCommand(Action.Command.HOME_INTENT, target,
1226 newContainerTarget(ContainerType.WORKSPACE));
Sunny Goyald3864fa2017-11-14 15:06:36 -08001227 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001228
Sunny Goyald3864fa2017-11-14 15:06:36 -08001229 // In all these cases, only animate if we're already on home
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001230 AbstractFloatingView.closeAllOpenViews(this, isStarted());
Jon Mirandafeba90f2016-10-06 10:53:29 -07001231
Sunny Goyala2467272018-03-16 14:53:05 -07001232 if (!isInState(NORMAL)) {
1233 // Only change state, if not already the same. This prevents cancelling any
1234 // animations running as part of resume
1235 mStateManager.goToState(NORMAL);
1236 }
Sunny Goyald3864fa2017-11-14 15:06:36 -08001237
1238 // Reset the apps view
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001239 if (!alreadyOnHome) {
Sunny Goyal7185dd62018-03-14 17:51:49 -07001240 mAppsView.reset(isStarted() /* animate */);
Sunny Goyald3864fa2017-11-14 15:06:36 -08001241 }
1242
1243 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()) {
1244 mWorkspace.post(mWorkspace::moveToDefaultScreen);
1245 }
1246 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001247
1248 final View v = getWindow().peekDecorView();
1249 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001250 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001251 }
1252
Adam Cohen9211d422014-10-07 18:14:53 -07001253 if (mLauncherCallbacks != null) {
Sunny Goyal85462132018-04-24 11:39:37 -07001254 mLauncherCallbacks.onHomeIntent(internalStateHandled);
Adam Cohen9211d422014-10-07 18:14:53 -07001255 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001256 }
Winson15f8b172015-08-19 11:02:39 -07001257
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001258 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001259 }
1260
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001261 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001262 public void onRestoreInstanceState(Bundle state) {
1263 super.onRestoreInstanceState(state);
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001264 mWorkspace.restoreInstanceStateForChild(mSynchronouslyBoundPage);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001265 }
1266
1267 @Override
1268 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001269 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001270 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001271
Adam Cohen21cd0022013-10-09 18:57:02 -07001272 }
Sunny Goyalea609262017-10-25 15:47:38 -07001273 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001274
1275
1276 AbstractFloatingView widgets = AbstractFloatingView
1277 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1278 if (widgets != null) {
1279 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1280 widgets.saveHierarchyState(widgetsState);
1281 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1282 } else {
1283 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1284 }
1285
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001286 // We close any open folders and shortcut containers since they will not be re-opened,
1287 // and we need to make sure this state is reflected.
1288 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001289
Sunny Goyal2100c782016-08-22 16:00:03 -07001290 if (mPendingRequestArgs != null) {
1291 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1292 }
1293 if (mPendingActivityResult != null) {
1294 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001295 }
1296
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001297 super.onSaveInstanceState(outState);
1298
Adam Cohen9211d422014-10-07 18:14:53 -07001299 if (mLauncherCallbacks != null) {
1300 mLauncherCallbacks.onSaveInstanceState(outState);
1301 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001302 }
1303
1304 @Override
1305 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001306 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001307
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001308 unregisterReceiver(mScreenOffReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001309 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001310
Tony Wickhama0068302018-04-11 16:16:11 -07001311 UiFactory.setOnTouchControllersChangedListener(this, null);
1312
Winson Chungcd2b0142011-06-08 16:02:26 -07001313 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001314 // It's possible to receive onDestroy after a new Launcher activity has
1315 // been created. In this case, don't interfere with the new Launcher.
1316 if (mModel.isCurrentCallbacks(this)) {
1317 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001318 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001319 }
Sunny Goyal623eddd2018-03-02 12:24:41 -08001320 mRotationHelper.destroy();
Sunny Goyal745bad92016-05-02 10:54:12 -07001321
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001322 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001323 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001324 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001325 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001326 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001327
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001328 TextKeyListener.getInstance().release();
Mario Bertschler27288382017-05-24 15:35:09 -07001329
Michael Jurka2ecf9952012-06-18 12:52:28 -07001330 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001331
Tony2917a8b2017-07-31 23:29:42 -07001332 clearPendingBinds();
1333
Adam Cohen9211d422014-10-07 18:14:53 -07001334 if (mLauncherCallbacks != null) {
1335 mLauncherCallbacks.onDestroy();
1336 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001337 }
1338
Sunny Goyalae502842016-06-17 08:43:56 -07001339 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1340 return mAccessibilityDelegate;
1341 }
1342
Adam Cohena9cf38f2011-05-02 15:36:58 -07001343 public DragController getDragController() {
1344 return mDragController;
1345 }
1346
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001347 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001348 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001349 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001350 }
1351
1352 @Override
1353 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1354 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001355 try {
1356 super.startIntentSenderForResult(intent, requestCode,
1357 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1358 } catch (IntentSender.SendIntentException e) {
1359 throw new ActivityNotFoundException();
1360 }
1361 }
1362
Winson Chung70d72102011-08-12 11:24:35 -07001363 /**
1364 * Indicates that we want global search for this activity by setting the globalSearch
1365 * argument for {@link #startSearch} to true.
1366 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001367 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001368 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001369 Bundle appSearchData, boolean globalSearch) {
1370 if (appSearchData == null) {
1371 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001372 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001373 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001374
Sunny Goyal6f28e712016-09-13 14:19:29 -07001375 if (mLauncherCallbacks == null ||
1376 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1377 // Starting search from the callbacks failed. Start the default global search.
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001378 super.startSearch(initialQuery, selectInitialQuery, appSearchData, true);
Ian Parkinson047036e2014-09-01 15:40:53 +01001379 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001380
1381 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001382 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001383 }
1384
Joe Onorato9c1289c2009-08-17 11:03:03 -04001385 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001386 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001387 }
1388
Adam Cohen517a7f52014-03-01 12:12:59 -08001389 public boolean isWorkspaceLoading() {
1390 return mWorkspaceLoading;
1391 }
1392
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001393 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001394 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001395 }
1396
Sunny Goyal04a324a2017-01-20 21:08:59 -08001397 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001398 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001399 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001400
Sunny Goyal2100c782016-08-22 16:00:03 -07001401 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001402 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001403 if (LOGD) {
1404 Log.d(TAG, "Adding widget from drop");
1405 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001406 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001407 }
1408
Sunny Goyal2100c782016-08-22 16:00:03 -07001409 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001410 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1411 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1412 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001413
Adam Cohenad4e15c2013-10-17 16:21:35 -07001414 Runnable onComplete = new Runnable() {
1415 @Override
1416 public void run() {
1417 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001418 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001419 }
1420 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001421 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001422 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001423 }
1424 }
Romain Guycbb89e42009-06-08 15:52:54 -07001425
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001426 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1427 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001428 info.container = container;
1429 info.screenId = screenId;
1430 if (cell != null) {
1431 info.cellX = cell[0];
1432 info.cellY = cell[1];
1433 }
1434 info.spanX = spanX;
1435 info.spanY = spanY;
1436
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001437 switch (info.itemType) {
1438 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1439 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001440 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001441 break;
1442 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001443 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001444 break;
1445 default:
1446 throw new IllegalStateException("Unknown item type: " + info.itemType);
1447 }
1448 }
1449
Adam Cohenfbba09b2011-07-18 21:43:05 -07001450 /**
1451 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001452 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001453 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001454 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1455 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001456 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1457 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1458 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001459 }
1460
Adam Cohenfbba09b2011-07-18 21:43:05 -07001461 /**
1462 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001463 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001464 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001465 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001466 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001467 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001468 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001469 // In the case where we've prebound the widget, we remove it from the DragLayer
1470 if (LOGD) {
1471 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1472 }
1473 getDragLayer().removeView(hostView);
1474
Adam Cohened66b2b2012-01-23 17:28:51 -08001475 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001476 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001477
1478 // Clear the boundWidget so that it doesn't get destroyed.
1479 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001480 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001481 // In this case, we either need to start an activity to get permission to bind
1482 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001483 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1484 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1485 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1486 this, info.componentName);
1487 } else {
1488 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1489 }
Adam Cohendd70d662012-10-04 16:53:44 -07001490 Bundle options = info.bindOptions;
1491
Sunny Goyalffe83f12014-08-14 17:39:34 -07001492 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1493 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001494 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001495 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001496 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001497 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001498 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001499 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001500 }
1501
Adam Cohendcd297f2013-06-18 13:13:40 -07001502 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001503 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001504 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001505 folderInfo.title = getText(R.string.folder_name);
1506
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001507 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001508 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001509
1510 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001511 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301512 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001513 // Force measure the new folder icon
1514 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1515 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001516 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001517 }
Romain Guycbb89e42009-06-08 15:52:54 -07001518
Winsonc0b52fe2015-09-09 16:38:15 -07001519 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001520 * Unbinds the view for the specified item, and removes the item and all its children.
1521 *
1522 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001523 * @param itemInfo the {@link ItemInfo} for this view.
1524 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001525 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001526 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001527 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001528 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001529 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1530 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001531 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001532 } else {
1533 mWorkspace.removeWorkspaceItem(v);
1534 }
Winsonc0b52fe2015-09-09 16:38:15 -07001535 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001536 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001537 }
1538 } else if (itemInfo instanceof FolderInfo) {
1539 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001540 if (v instanceof FolderIcon) {
1541 ((FolderIcon) v).removeListeners();
1542 }
Winsonc0b52fe2015-09-09 16:38:15 -07001543 mWorkspace.removeWorkspaceItem(v);
1544 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001545 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001546 }
1547 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1548 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001549 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001550 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07001551 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001552 }
1553 } else {
1554 return false;
1555 }
1556 return true;
1557 }
1558
1559 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07001560 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07001561 */
Sunny Goyal56c73602015-09-25 12:55:01 -07001562 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07001563 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07001564 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07001565 // Deleting an app widget ID is a void call but writes to disk before returning
1566 // to the caller...
1567 new AsyncTask<Void, Void, Void>() {
1568 public Void doInBackground(Void ... args) {
1569 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
1570 return null;
1571 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07001572 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07001573 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001574 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001575 }
1576
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001577 @Override
1578 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001579 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001580 }
1581
Joe Onorato88ec0992009-11-19 13:16:06 -08001582 @Override
1583 public void onBackPressed() {
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08001584 if (finishAutoCancelActionMode()) {
1585 return;
1586 }
Adam Cohen9211d422014-10-07 18:14:53 -07001587 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1588 return;
1589 }
1590
Sunny Goyal45478022015-06-08 16:52:41 -07001591 if (mDragController.isDragging()) {
1592 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001593 return;
1594 }
1595
Jon Mirandafeba90f2016-10-06 10:53:29 -07001596 // Note: There should be at most one log per method call. This is enforced implicitly
1597 // by using if-else statements.
1598 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001599 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
Tony Wickham52c1b662018-05-21 13:13:58 -07001600 if (topView != null && topView.onBackPressed()) {
1601 // Handled by the floating view.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001602 } else if (!isInState(NORMAL)) {
Sunny Goyal49bcf342018-01-11 13:59:53 -08001603 LauncherState lastState = mStateManager.getLastState();
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001604 ued.logActionCommand(Action.Command.BACK, mStateManager.getState().containerType,
1605 lastState.containerType);
Sunny Goyal49bcf342018-01-11 13:59:53 -08001606 mStateManager.goToState(lastState);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001607 } else {
1608 // Back button is a no-op here, but give at least some feedback for the button press
1609 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001610 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001611 }
1612
Sunny Goyalfe770c92016-09-27 14:38:58 -07001613 @TargetApi(Build.VERSION_CODES.M)
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001614 @Override
Jon Miranda73c27ec2018-05-16 18:06:23 -07001615 public ActivityOptions getActivityLaunchOptions(View v) {
1616 return mAppTransitionManager.getActivityLaunchOptions(this, v);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001617 }
1618
Tony Wickham3a6746a2018-03-29 09:39:56 -07001619 public LauncherAppTransitionManager getAppTransitionManager() {
1620 return mAppTransitionManager;
1621 }
1622
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001623 @TargetApi(Build.VERSION_CODES.M)
1624 @Override
1625 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
1626 // Due to legacy reasons, direct call shortcuts require Launchers to have the
1627 // corresponding permission. Show the appropriate permission prompt if that
1628 // is the case.
1629 if (intent.getComponent() == null
1630 && Intent.ACTION_CALL.equals(intent.getAction())
1631 && checkSelfPermission(android.Manifest.permission.CALL_PHONE) !=
1632 PackageManager.PERMISSION_GRANTED) {
1633
1634 setWaitingForResult(PendingRequestArgs
1635 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
1636 requestPermissions(new String[]{android.Manifest.permission.CALL_PHONE},
1637 REQUEST_PERMISSION_CALL_PHONE);
1638 return true;
1639 } else {
1640 return false;
1641 }
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001642 }
1643
Hyunyoung Song46d07f72018-05-22 15:41:25 -07001644 @Override
1645 public void modifyUserEvent(LauncherLogProto.LauncherEvent event) {
1646 if (event.srcTarget != null && event.srcTarget.length > 0 &&
1647 event.srcTarget[1].containerType == ContainerType.PREDICTION) {
1648 Target[] targets = new Target[3];
1649 targets[0] = event.srcTarget[0];
1650 targets[1] = event.srcTarget[1];
1651 targets[2] = newTarget(Target.Type.CONTAINER);
1652 event.srcTarget = targets;
1653 LauncherState state = mStateManager.getState();
1654 if (state == LauncherState.ALL_APPS) {
1655 event.srcTarget[2].containerType = ContainerType.ALLAPPS;
1656 } else if (state == LauncherState.OVERVIEW) {
1657 event.srcTarget[2].containerType = ContainerType.TASKSWITCHER;
1658 }
1659 }
1660 }
1661
Sunny Goyala7ce1662016-05-31 15:01:35 -07001662 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Hyunyoung Song1241e612018-05-15 21:46:51 -07001663 boolean success = super.startActivitySafely(v, intent, item);
1664 if (success && v instanceof BubbleTextView) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001665 // This is set to the view that launched the activity that navigated the user away
1666 // from launcher. Since there is no callback for when the activity has finished
1667 // launching, enable the press state and keep this reference to reset the press
1668 // state when we return to launcher.
1669 BubbleTextView btv = (BubbleTextView) v;
1670 btv.setStayPressed(true);
1671 setOnResumeCallback(btv);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001672 }
Hyunyoung Song1241e612018-05-15 21:46:51 -07001673 return success;
Michael Jurka86a720e2012-05-09 11:23:23 -07001674 }
1675
Winson Chung3d503fb2011-07-13 17:25:49 -07001676 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001677 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07001678 return mHotseat != null && layout != null &&
1679 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
1680 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001681
Winson Chung3d503fb2011-07-13 17:25:49 -07001682 /**
1683 * Returns the CellLayout of the specified container at the specified screen.
1684 */
Sunny Goyal92820592015-03-02 11:31:35 -08001685 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001686 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1687 if (mHotseat != null) {
1688 return mHotseat.getLayout();
1689 } else {
1690 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08001691 }
Winson Chung3d503fb2011-07-13 17:25:49 -07001692 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08001693 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08001694 }
1695 }
1696
Michael Jurkae326f182011-11-21 14:05:46 -08001697 @Override
1698 public void onTrimMemory(int level) {
1699 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07001700 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
1701 // The widget preview db can result in holding onto over
1702 // 3MB of memory for caching which isn't necessary.
1703 SQLiteDatabase.releaseMemory();
1704
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08001705 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07001706 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08001707 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01001708 if (mLauncherCallbacks != null) {
1709 mLauncherCallbacks.onTrimMemory(level);
1710 }
Winson Chung62a70be2018-02-23 15:10:05 -08001711 UiFactory.onTrimMemory(this, level);
Michael Jurkae326f182011-11-21 14:05:46 -08001712 }
1713
Michael Jurkad7c28052012-04-27 15:43:36 -07001714 @Override
1715 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07001716 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07001717 final List<CharSequence> text = event.getText();
1718 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07001719 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001720 // TODO: When can workspace be null?
1721 text.add(mWorkspace == null
1722 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07001723 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07001724 return result;
1725 }
1726
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001727 public void setOnResumeCallback(OnResumeCallback callback) {
1728 if (mOnResumeCallback != null) {
1729 mOnResumeCallback.onLauncherResume();
1730 }
1731 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07001732 }
1733
Michael Jurka7607c2f2013-04-03 14:33:19 -07001734 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001735 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001736 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07001737 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001738 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07001739 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001740 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07001741 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07001742 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07001743 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001744 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001745
Joe Onorato9c1289c2009-08-17 11:03:03 -04001746 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07001747 * Clear any pending bind callbacks. This is called when is loader is planning to
1748 * perform a full rebind from scratch.
1749 */
1750 @Override
1751 public void clearPendingBinds() {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001752 if (mPendingExecutor != null) {
1753 mPendingExecutor.markCompleted();
1754 mPendingExecutor = null;
1755 }
1756 }
1757
1758 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001759 * Refreshes the shortcuts shown on the workspace.
1760 *
1761 * Implementation of the method from LauncherModel.Callbacks.
1762 */
1763 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001764 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001765 // Floating panels (except the full widget sheet) are associated with individual icons. If
1766 // we are starting a fresh bind, close all such panels as all the icons are about
1767 // to go away.
1768 AbstractFloatingView.closeOpenViews(this, true,
Sunny Goyal7ede6112017-12-05 15:11:21 -08001769 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_REBIND_SAFE);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08001770
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001771 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08001772
Winson Chungd64d1762013-08-20 14:37:16 -07001773 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07001774 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07001775 mWorkspace.removeAllWorkspaceScreens();
Sunny Goyalc11fac32018-02-27 19:20:15 -08001776 mAppWidgetHost.clearViews();
Winson Chungd64d1762013-08-20 14:37:16 -07001777
Winson Chung3d503fb2011-07-13 17:25:49 -07001778 if (mHotseat != null) {
Sunny Goyal11e96492018-03-23 17:06:00 -07001779 mHotseat.resetLayout(mDeviceProfile.isVerticalBarLayout());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001780 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001781 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001782 }
1783
Adam Cohendcd297f2013-06-18 13:13:40 -07001784 @Override
1785 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001786 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001787 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
1788 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001789 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
1790 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001791 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001792 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
1793 // If there are no screens, we need to have an empty screen
1794 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07001795 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001796 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07001797
Winsonc7d2e832016-07-28 12:24:55 -07001798 // After we have added all the screens, if the wallpaper was locked to the default state,
1799 // then notify to indicate that it can be released and a proper wallpaper offset can be
1800 // computed before the next layout
1801 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07001802 }
1803
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001804 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001805 int count = orderedScreenIds.size();
1806 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001807 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001808 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001809 // No need to bind the first screen, as its always bound.
1810 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
1811 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001812 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001813 }
1814
Sunny Goyalb23980c2017-08-17 07:45:25 -07001815 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08001816 public void bindAppsAdded(ArrayList<Long> newScreens, ArrayList<ItemInfo> addNotAnimated,
1817 ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07001818 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08001819 if (newScreens != null) {
1820 bindAddScreens(newScreens);
1821 }
Winson Chungd64d1762013-08-20 14:37:16 -07001822
1823 // We add the items without animation on non-visible pages, and with
1824 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08001825 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001826 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001827 }
Adam Cohen76a47a12014-02-05 11:47:43 -08001828 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001829 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07001830 }
Winson Chungc58497e2013-09-03 17:48:37 -07001831
Winson Chung87412982013-10-03 18:34:14 -07001832 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07001833 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001834 }
1835
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001836 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001837 * Bind the items start-end from the list.
1838 *
1839 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001840 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07001841 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07001842 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001843 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07001844 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001845 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07001846 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001847 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001848 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07001849 int end = items.size();
1850 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001851 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07001852
1853 // Short circuit if we are loading dock items for a configuration which has no dock
1854 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
1855 mHotseat == null) {
1856 continue;
1857 }
1858
Sunny Goyal639e9062015-08-19 19:17:06 -07001859 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001860 switch (item.itemType) {
1861 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1862 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08001863 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001864 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07001865 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001866 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001867 }
1868 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07001869 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001870 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001871 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001872 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001873 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07001874 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1875 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07001876 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07001877 if (view == null) {
1878 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001879 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08001880 break;
1881 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001882 default:
1883 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001884 }
Sunny Goyal639e9062015-08-19 19:17:06 -07001885
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001886 /*
1887 * Remove colliding items.
1888 */
1889 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1890 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
1891 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
1892 View v = cl.getChildAt(item.cellX, item.cellY);
1893 Object tag = v.getTag();
1894 String desc = "Collision while binding workspace item: " + item
1895 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08001896 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001897 throw (new RuntimeException(desc));
1898 } else {
1899 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001900 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001901 continue;
1902 }
1903 }
1904 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05301905 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07001906 if (animateIcons) {
1907 // Animate all the applications up now
1908 view.setAlpha(0f);
1909 view.setScaleX(0f);
1910 view.setScaleY(0f);
1911 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08001912 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07001913 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001914 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07001915
Winson Chung997a9232013-07-24 15:33:46 -07001916 if (animateIcons) {
1917 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08001918 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07001919 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08001920 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07001921 final Runnable startBounceAnimRunnable = new Runnable() {
1922 public void run() {
1923 anim.playTogether(bounceAnims);
1924 anim.start();
1925 }
1926 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08001927 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07001928 // We post the animation slightly delayed to prevent slowdowns
1929 // when we are loading right after we return to launcher.
1930 mWorkspace.postDelayed(new Runnable() {
1931 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00001932 if (mWorkspace != null) {
Jon Miranda64eb8ea2018-03-29 11:43:58 -07001933 AbstractFloatingView.closeAllOpenViews(Launcher.this, false);
1934
Ian Parkinson94449152014-01-21 13:09:59 +00001935 mWorkspace.snapToPage(newScreenIndex);
1936 mWorkspace.postDelayed(startBounceAnimRunnable,
1937 NEW_APPS_ANIMATION_DELAY);
1938 }
Winson Chung94d67682013-09-25 16:29:40 -07001939 }
1940 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07001941 } else {
1942 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07001943 }
1944 }
Winson Chung64359a52013-07-08 17:17:08 -07001945 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001946 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001947 }
1948
Joe Onorato9c1289c2009-08-17 11:03:03 -04001949 /**
1950 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001951 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07001952 public void bindAppWidget(LauncherAppWidgetInfo item) {
1953 View view = inflateAppWidget(item);
1954 if (view != null) {
1955 mWorkspace.addInScreen(view, item);
1956 mWorkspace.requestLayout();
1957 }
1958 }
1959
1960 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001961 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05301962 PendingAppWidgetHostView view =
1963 new PendingAppWidgetHostView(this, item, mIconCache, true);
1964 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07001965 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001966 }
1967
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001968 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001969
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001970 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08001971
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001972 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
1973 // If the provider is not ready, bind as a pending widget.
1974 appWidgetInfo = null;
1975 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
1976 // The widget id is not valid. Try to find the widget based on the provider info.
1977 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
1978 } else {
1979 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
1980 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07001981
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001982 // If the provider is ready, but the width is not yet restored, try to restore it.
1983 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
1984 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07001985 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001986 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
1987 + " belongs to component " + item.providerName
1988 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001989 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07001990 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07001991 }
Sunny Goyalff572272014-07-23 13:58:07 -07001992
Sunny Goyal84b4adc2015-08-12 15:12:16 -07001993 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001994 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07001995 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
1996 // Id has not been allocated yet. Allocate a new id.
1997 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
1998 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07001999
Sunny Goyald478c832016-04-01 12:04:16 -07002000 // Also try to bind the widget. If the bind fails, the user will be shown
2001 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08002002 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07002003 pendingInfo.spanX = item.spanX;
2004 pendingInfo.spanY = item.spanY;
2005 pendingInfo.minSpanX = item.minSpanX;
2006 pendingInfo.minSpanY = item.minSpanY;
2007 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07002008
2009 boolean isDirectConfig =
2010 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2011 if (isDirectConfig && item.bindOptions != null) {
2012 Bundle newOptions = item.bindOptions.getExtras();
2013 if (options != null) {
2014 newOptions.putAll(options);
2015 }
2016 options = newOptions;
2017 }
Sunny Goyald478c832016-04-01 12:04:16 -07002018 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2019 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002020
Sunny Goyal86df1382016-08-10 15:03:22 -07002021 // We tried to bind once. If we were not able to bind, we would need to
2022 // go through the permission dialog, which means we cannot skip the config
2023 // activity.
2024 item.bindOptions = null;
2025 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2026
Sunny Goyald478c832016-04-01 12:04:16 -07002027 // Bind succeeded
2028 if (success) {
2029 // If the widget has a configure activity, it is still needs to set it up,
2030 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002031 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002032 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2033 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002034 }
Sunny Goyald478c832016-04-01 12:04:16 -07002035
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002036 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002037 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002038 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002039 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002040 // The widget was marked as UI not ready, but there is no configure activity to
2041 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002042 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002043 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002044 }
Sunny Goyalff572272014-07-23 13:58:07 -07002045 }
2046
Sunny Goyald5462aa2016-11-22 16:52:39 +05302047 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002048 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002049 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002050 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002051 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07002052 deleteWidgetInfo(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002053 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002054 }
2055
Sunny Goyal233ee962015-08-03 13:05:01 -07002056 item.minSpanX = appWidgetInfo.minSpanX;
2057 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302058 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002059 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302060 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002061 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302062 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002063
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002064 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002065 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002066 }
2067
Sunny Goyalff572272014-07-23 13:58:07 -07002068 /**
2069 * Restores a pending widget.
2070 *
2071 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002072 */
Sunny Goyald478c832016-04-01 12:04:16 -07002073 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002074 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2075 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2076 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002077 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002078 }
2079
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002080 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002081 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002082 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2083 info.pendingItemInfo = null;
2084 }
Sunny Goyalff572272014-07-23 13:58:07 -07002085
Sunny Goyalba47faa2017-10-04 16:50:57 -07002086 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002087 view.reInflate();
Sunny Goyalba47faa2017-10-04 16:50:57 -07002088 }
2089
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002090 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002091 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002092 }
2093
Adam Cohen1462de32012-07-24 22:34:36 -07002094 public void onPageBoundSynchronously(int page) {
Sunny Goyalf4aceab2017-12-06 16:16:31 -08002095 mSynchronouslyBoundPage = page;
Adam Cohen1462de32012-07-24 22:34:36 -07002096 }
2097
Sunny Goyal527c7d32015-08-28 15:19:36 -07002098 @Override
2099 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2100 if (mPendingExecutor != null) {
2101 mPendingExecutor.markCompleted();
2102 }
2103 mPendingExecutor = executor;
Sunny Goyal60180b02018-02-07 12:56:30 -08002104 if (!isInState(ALL_APPS)) {
2105 mAppsView.getAppsStore().setDeferUpdates(true);
2106 mPendingExecutor.execute(() -> mAppsView.getAppsStore().setDeferUpdates(false));
2107 }
2108
Sunny Goyal527c7d32015-08-28 15:19:36 -07002109 executor.attachTo(this);
2110 }
2111
2112 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2113 if (mPendingExecutor == executor) {
2114 mPendingExecutor = null;
2115 }
2116 }
2117
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002118 @Override
2119 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
Sunny Goyal6001ea22018-05-10 16:31:00 -07002120 AlphaProperty property = mDragLayer.getAlphaProperty(ALPHA_INDEX_LAUNCHER_LOAD);
2121 if (property.getValue() < 1) {
2122 ObjectAnimator anim = ObjectAnimator.ofFloat(property, MultiValueAlpha.VALUE, 1);
2123 if (executor != null) {
2124 anim.addListener(new AnimatorListenerAdapter() {
2125 @Override
2126 public void onAnimationEnd(Animator animation) {
2127 executor.onLoadAnimationCompleted();
2128 }
2129 });
2130 }
2131 anim.start();
Sunny Goyal29947f02017-12-18 13:49:44 -08002132 } else if (executor != null) {
2133 executor.onLoadAnimationCompleted();
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002134 }
2135 }
2136
Joe Onorato9c1289c2009-08-17 11:03:03 -04002137 /**
2138 * Callback saying that there aren't any more items to bind.
2139 *
2140 * Implementation of the method from LauncherModel.Callbacks.
2141 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002142 public void finishBindingItems() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002143 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002144 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002145
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002146 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002147
Sunny Goyal2100c782016-08-22 16:00:03 -07002148 if (mPendingActivityResult != null) {
2149 handleActivityResult(mPendingActivityResult.requestCode,
2150 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2151 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002152 }
2153
Sunny Goyala474a9b2017-05-04 16:47:11 -07002154 InstallShortcutReceiver.disableAndFlushInstallQueue(
2155 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002156
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002157 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002158 }
2159
Winson Chunga2413752012-04-03 14:22:34 -07002160 private boolean canRunNewAppsAnimation() {
2161 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002162 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002163 }
2164
Winson Chungc9168342013-06-26 14:54:55 -07002165 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002166 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07002167 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
2168 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002169 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002170 return bounceAnim;
2171 }
2172
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002173 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002174 * Add the icons for all apps.
2175 *
2176 * Implementation of the method from LauncherModel.Callbacks.
2177 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002178 public void bindAllApplications(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002179 mAppsView.getAppsStore().setApps(apps);
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002180
Adam Cohen9211d422014-10-07 18:14:53 -07002181 if (mLauncherCallbacks != null) {
2182 mLauncherCallbacks.bindAllApplications(apps);
2183 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002184 }
2185
2186 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002187 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
2188 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2189 */
2190 @Override
2191 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002192 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002193 }
2194
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002195 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002196 * A package was updated.
2197 *
2198 * Implementation of the method from LauncherModel.Callbacks.
2199 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002200 @Override
2201 public void bindAppsAddedOrUpdated(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002202 mAppsView.getAppsStore().addOrUpdateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002203 }
2204
Sunny Goyal4390ace2014-10-13 11:33:11 -07002205 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002206 public void bindPromiseAppProgressUpdated(PromiseAppInfo app) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002207 mAppsView.getAppsStore().updatePromiseAppProgress(app);
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002208 }
2209
2210 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002211 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002212 mWorkspace.widgetsRestored(widgets);
2213 }
2214
Joe Onorato9c1289c2009-08-17 11:03:03 -04002215 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002216 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002217 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002218 *
2219 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002220 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002221 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002222 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002223 if (!updated.isEmpty()) {
2224 mWorkspace.updateShortcuts(updated);
2225 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002226 }
2227
2228 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002229 * Update the state of a package, typically related to install state.
2230 *
2231 * Implementation of the method from LauncherModel.Callbacks.
2232 */
Sunny Goyale755d462014-07-22 13:48:29 -07002233 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002234 public void bindRestoreItemsChange(HashSet<ItemInfo> updates) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07002235 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07002236 }
2237
2238 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002239 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07002240 * in addition to specific applications to remove, the reason being that
2241 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002242 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07002243 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002244 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07002245 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002246 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002247 mWorkspace.removeItemsByMatcher(matcher);
2248 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002249 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002250
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002251 @Override
2252 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002253 mAppsView.getAppsStore().removeApps(appInfos);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002254 }
Joe Onoratobe386092009-11-17 17:32:16 -08002255
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002256 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002257 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
2258 mPopupDataProvider.setAllWidgets(allWidgets);
Tony Wickham26b17462017-03-20 17:12:24 -07002259 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
2260 if (topView != null) {
2261 topView.onWidgetsBound();
2262 }
2263 }
2264
Tony Wickham86222d22017-03-29 15:30:43 -07002265 /**
2266 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
2267 * refreshes the widgets and shortcuts associated with the given package/user
2268 */
2269 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07002270 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002271 }
2272
Winson Chung80baf5a2010-08-09 16:03:15 -07002273 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002274 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08002275 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002276 @Override
2277 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
2278 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002279
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002280 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
2281 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07002282 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002283 writer.println(prefix + " Homescreen " + i);
2284
2285 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
2286 for (int j = 0; j < layout.getChildCount(); j++) {
2287 Object tag = layout.getChildAt(j).getTag();
2288 if (tag != null) {
2289 writer.println(prefix + " " + tag.toString());
2290 }
2291 }
2292 }
2293
2294 writer.println(prefix + " Hotseat");
2295 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07002296 for (int j = 0; j < layout.getChildCount(); j++) {
2297 Object tag = layout.getChildAt(j).getTag();
2298 if (tag != null) {
2299 writer.println(prefix + " " + tag.toString());
2300 }
Adam Cohen4caf2982013-08-20 18:54:31 -07002301 }
Sunny Goyala1365452015-10-01 15:46:24 -07002302 }
2303
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002304 writer.println(prefix + "Misc:");
2305 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
2306 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
2307 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
Sunny Goyale43d00d2018-05-14 14:23:18 -07002308 writer.println(" mRotationHelper: " + mRotationHelper);
2309 dumpMisc(writer);
Sunny Goyal2bba1902018-02-27 12:20:50 -08002310
2311 try {
2312 FileLog.flushAll(writer);
2313 } catch (Exception e) {
2314 // Ignore
2315 }
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002316
2317 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002318
Adam Cohen9211d422014-10-07 18:14:53 -07002319 if (mLauncherCallbacks != null) {
2320 mLauncherCallbacks.dump(prefix, fd, writer, args);
2321 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002322 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002323
Sunny Goyal66b24572016-09-21 15:57:55 -07002324 @Override
2325 @TargetApi(Build.VERSION_CODES.N)
2326 public void onProvideKeyboardShortcuts(
2327 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
2328
2329 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002330 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002331 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
2332 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
Vadim Tryshev74c261c2018-06-12 13:44:43 -07002333 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.widget_button_text),
2334 KeyEvent.KEYCODE_W, KeyEvent.META_CTRL_ON));
Sunny Goyal66b24572016-09-21 15:57:55 -07002335 }
Vadim Tryshev3455f8d2018-05-11 20:02:20 -07002336 final View currentFocus = getCurrentFocus();
2337 if (currentFocus != null) {
2338 if (new CustomActionsPopup(this, currentFocus).canShow()) {
2339 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
2340 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
2341 }
2342 if (currentFocus.getTag() instanceof ItemInfo
2343 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
2344 shortcutInfos.add(new KeyboardShortcutInfo(
2345 getString(R.string.shortcuts_menu_with_notifications_description),
2346 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
2347 }
Sunny Goyal66b24572016-09-21 15:57:55 -07002348 }
2349 if (!shortcutInfos.isEmpty()) {
2350 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
2351 }
2352
2353 super.onProvideKeyboardShortcuts(data, menu, deviceId);
2354 }
2355
2356 @Override
2357 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
2358 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
2359 switch (keyCode) {
2360 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002361 if (isInState(NORMAL)) {
2362 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07002363 return true;
2364 }
2365 break;
2366 case KeyEvent.KEYCODE_S: {
2367 View focusedView = getCurrentFocus();
2368 if (focusedView instanceof BubbleTextView
2369 && focusedView.getTag() instanceof ItemInfo
2370 && mAccessibilityDelegate.performAction(focusedView,
2371 (ItemInfo) focusedView.getTag(),
2372 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal00ac9202017-11-09 15:24:06 -08002373 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07002374 return true;
2375 }
2376 break;
2377 }
2378 case KeyEvent.KEYCODE_O:
2379 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
2380 return true;
2381 }
2382 break;
Vadim Tryshev74c261c2018-06-12 13:44:43 -07002383 case KeyEvent.KEYCODE_W:
2384 if (isInState(NORMAL)) {
2385 OptionsPopupView.openWidgets(this);
2386 return true;
2387 }
2388 break;
Sunny Goyal66b24572016-09-21 15:57:55 -07002389 }
2390 }
2391 return super.onKeyShortcut(keyCode, event);
2392 }
2393
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002394 @Override
2395 public boolean onKeyUp(int keyCode, KeyEvent event) {
2396 if (keyCode == KeyEvent.KEYCODE_MENU) {
2397 // KEYCODE_MENU is sent by some tests, for example
2398 // LauncherJankTests#testWidgetsContainerFling. Don't just remove its handling.
2399 if (!mDragController.isDragging() && !mWorkspace.isSwitchingState() &&
2400 isInState(NORMAL)) {
2401 // Close any open floating views.
2402 AbstractFloatingView.closeAllOpenViews(this);
2403
2404 // Setting the touch point to (-1, -1) will show the options popup in the center of
2405 // the screen.
Sunny Goyal2fd7a8b2018-03-30 17:10:13 -07002406 OptionsPopupView.showDefaultOptions(this, -1, -1);
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002407 }
2408 return true;
2409 }
2410 return super.onKeyUp(keyCode, event);
2411 }
2412
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002413 public static Launcher getLauncher(Context context) {
2414 if (context instanceof Launcher) {
2415 return (Launcher) context;
2416 }
2417 return ((Launcher) ((ContextWrapper) context).getBaseContext());
2418 }
2419
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002420 /**
2421 * Callback for listening for onResume
2422 */
2423 public interface OnResumeCallback {
2424
2425 void onLauncherResume();
2426 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002427}