blob: 4fe35033b2f603b2a715d393dbc60331dc304e15 [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
Winson Chung8eb49e02018-07-16 13:33:31 -0700351 public void onEnterAnimationComplete() {
352 super.onEnterAnimationComplete();
353 UiFactory.onEnterAnimationComplete(this);
354 }
355
356 @Override
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800357 public void onConfigurationChanged(Configuration newConfig) {
358 int diff = newConfig.diff(mOldConfig);
359 if ((diff & (CONFIG_ORIENTATION | CONFIG_SCREEN_SIZE)) != 0) {
360 mUserEventDispatcher = null;
361 initDeviceProfile(mDeviceProfile.inv);
Sunny Goyalfde55052018-02-01 14:46:13 -0800362 dispatchDeviceProfileChanged();
Sunny Goyal59d086c2018-05-07 17:31:40 -0700363 reapplyUi();
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700364 mDragLayer.recreateControllers();
Sunny Goyal7185dd62018-03-14 17:51:49 -0700365
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800366 // TODO: We can probably avoid rebind when only screen size changed.
Sunny Goyal605bcf32018-03-02 15:16:12 -0800367 rebindModel();
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800368 }
369
370 mOldConfig.setTo(newConfig);
Tracy Zhoua706f002018-03-28 13:55:19 -0700371 UiFactory.onLauncherStateOrResumeChanged(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800372 super.onConfigurationChanged(newConfig);
373 }
374
Sunny Goyal605bcf32018-03-02 15:16:12 -0800375 @Override
Sunny Goyal59d086c2018-05-07 17:31:40 -0700376 protected void reapplyUi() {
377 getRootView().dispatchInsets();
378 getStateManager().reapplyState(true /* cancelCurrentAnimation */);
379 }
380
381 @Override
Sunny Goyal605bcf32018-03-02 15:16:12 -0800382 public void rebindModel() {
383 int currentPage = mWorkspace.getNextPage();
384 if (mModel.startLoader(currentPage)) {
385 mWorkspace.setCurrentPage(currentPage);
386 setWorkspaceLoading(true);
387 }
388 }
389
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800390 private void initDeviceProfile(InvariantDeviceProfile idp) {
391 // Load configuration-specific DeviceProfile
Sunny Goyal59d086c2018-05-07 17:31:40 -0700392 mDeviceProfile = idp.getDeviceProfile(this);
393 if (isInMultiWindowModeCompat()) {
394 Display display = getWindowManager().getDefaultDisplay();
395 Point mwSize = new Point();
396 display.getSize(mwSize);
397 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
398 }
399 onDeviceProfileInitiated();
Sunny Goyal605bcf32018-03-02 15:16:12 -0800400 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout(), true);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800401 }
402
Sunny Goyal623eddd2018-03-02 12:24:41 -0800403 public RotationHelper getRotationHelper() {
404 return mRotationHelper;
405 }
406
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700407 public LauncherStateManager getStateManager() {
408 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700409 }
410
Mario Bertschler27288382017-05-24 15:35:09 -0700411 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700412 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800413 return mLauncherView.findViewById(id);
414 }
415
416 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700417 public void onAppWidgetHostReset() {
418 if (mAppWidgetHost != null) {
419 mAppWidgetHost.startListening();
420 }
421 }
422
Adam Cohen9211d422014-10-07 18:14:53 -0700423 private LauncherCallbacks mLauncherCallbacks;
424
Sunny Goyal32554d12015-12-03 15:31:25 -0800425 /**
426 * Call this after onCreate to set or clear overlay.
427 */
428 public void setLauncherOverlay(LauncherOverlay overlay) {
429 if (overlay != null) {
430 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
431 }
432 mWorkspace.setLauncherOverlay(overlay);
433 }
434
Adam Cohen9211d422014-10-07 18:14:53 -0700435 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
436 mLauncherCallbacks = callbacks;
437 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700438 }
439
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700440 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700441 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700442 if (mLauncherCallbacks != null) {
443 mLauncherCallbacks.onLauncherProviderChange();
444 }
445 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700446
Hyunyoung Song3f471442015-04-08 19:01:34 -0700447 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700448 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
449 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700450 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700451 }
452
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000453 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700454 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
455 // This cast is safe as long as the id < 0x00FFFFFF
456 // Since we jail all the dynamically generated views, there should be no clashes
457 // with any other views.
458 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000459 }
460
Tony Wickham010d2552017-01-20 08:15:28 -0800461 public PopupDataProvider getPopupDataProvider() {
462 return mPopupDataProvider;
463 }
464
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700465 @Override
466 public BadgeInfo getBadgeInfoForItem(ItemInfo info) {
467 return mPopupDataProvider.getBadgeInfoForItem(info);
468 }
469
470 @Override
471 public void invalidateParent(ItemInfo info) {
472 FolderIconPreviewVerifier verifier = new FolderIconPreviewVerifier(getDeviceProfile().inv);
473 if (verifier.isItemInPreview(info.rank) && (info.container >= 0)) {
474 View folderIcon = getWorkspace().getHomescreenIconByItemId(info.container);
475 if (folderIcon != null) {
476 folderIcon.invalidate();
477 }
478 }
479 }
480
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000481 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700482 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
483 * a configuration step, this allows the proper animations to run after other transitions.
484 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700485 private long completeAdd(
486 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
487 long screenId = info.screenId;
488 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700489 // When the screen id represents an actual screen (as opposed to a rank) we make sure
490 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700491 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700492 }
Adam Cohendb364c32014-05-20 14:23:40 -0700493
Sunny Goyal2100c782016-08-22 16:00:03 -0700494 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800495 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700496 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700497 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800498 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700499 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700500 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700501 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700502 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700503 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700504 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700505 int widgetId = appWidgetId;
506 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700507 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700508 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700509 // Since the view was just bound, also launch the configure activity if needed
510 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
511 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800512 if (provider != null) {
513 new WidgetAddFlowHandler(provider)
514 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700515 }
516 }
517 break;
518 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800519 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700520
Adam Cohendb364c32014-05-20 14:23:40 -0700521 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800522 }
523
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800524 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700525 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700526 if (isWorkspaceLoading()) {
527 // process the result once the workspace has loaded.
528 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
529 return;
530 }
531 mPendingActivityResult = null;
532
Winson Chunge341d302013-08-16 14:31:00 -0700533 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700534 final PendingRequestArgs requestArgs = mPendingRequestArgs;
535 setWaitingForResult(null);
536 if (requestArgs == null) {
537 return;
538 }
539
540 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700541
Adam Cohenad4e15c2013-10-17 16:21:35 -0700542 Runnable exitSpringLoaded = new Runnable() {
543 @Override
544 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700545 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700546 }
547 };
548
Michael Jurka8b805b12012-04-18 14:23:14 -0700549 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700550 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700551 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700552 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
553 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700554 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700555 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700556 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700557 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700558 addAppWidgetImpl(
559 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800560 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700561 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700562 }
563 return;
564 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200565
Adam Cohened66b2b2012-01-23 17:28:51 -0800566 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700567 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700568
Adam Cohened66b2b2012-01-23 17:28:51 -0800569 // We have special handling for widgets
570 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800571 final int appWidgetId;
572 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
573 : -1;
574 if (widgetId < 0) {
575 appWidgetId = pendingAddWidgetId;
576 } else {
577 appWidgetId = widgetId;
578 }
579
Adam Cohenad4e15c2013-10-17 16:21:35 -0700580 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800581 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700582 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
583 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700584 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700585 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700586 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700587 @Override
588 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700589 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700590 }
591 };
Adam Cohendb364c32014-05-20 14:23:40 -0700592
Sunny Goyal2100c782016-08-22 16:00:03 -0700593 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
594 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
595 } else {
596 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
597 // When the screen id represents an actual screen (as opposed to a rank)
598 // we make sure that the drop page actually exists.
599 requestArgs.screenId =
600 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700601 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700602 final CellLayout dropLayout =
603 mWorkspace.getScreenWithId(requestArgs.screenId);
604
605 dropLayout.setDropPending(true);
606 final Runnable onComplete = new Runnable() {
607 @Override
608 public void run() {
609 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
610 dropLayout.setDropPending(false);
611 }
612 };
613 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
614 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700615 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800616 return;
617 }
618
Sunny Goyald478c832016-04-01 12:04:16 -0700619 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
620 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700621 if (resultCode == RESULT_OK) {
622 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700623 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700624 }
625 // Leave the widget in the pending state if the user canceled the configure.
626 return;
627 }
628
Sunny Goyalaad90582015-07-09 14:22:50 -0700629 if (requestCode == REQUEST_CREATE_SHORTCUT) {
630 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700631 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
632 completeAdd(requestCode, data, -1, requestArgs);
633 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
634 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
635
Sunny Goyalaad90582015-07-09 14:22:50 -0700636 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700637 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
638 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800639 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800640 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800641 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800642 }
643
644 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700645 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800646 final int requestCode, final int resultCode, final Intent data) {
647 handleActivityResult(requestCode, resultCode, data);
648 if (mLauncherCallbacks != null) {
649 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
650 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800651 }
652
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700653 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700654 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600655 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700656 PendingRequestArgs pendingArgs = mPendingRequestArgs;
657 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
658 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
659 setWaitingForResult(null);
660
Sunny Goyal28c6b962015-10-12 11:42:05 -0700661 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700662 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700663 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700664 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700665 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700666 Intent intent = pendingArgs.getPendingIntent();
667
Sunny Goyal28c6b962015-10-12 11:42:05 -0700668 if (grantResults.length > 0
669 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700670 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700671 } else {
672 // TODO: Show a snack bar with link to settings
673 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700674 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700675 }
676 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600677 if (mLauncherCallbacks != null) {
678 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
679 grantResults);
680 }
681 }
682
Adam Cohendb364c32014-05-20 14:23:40 -0700683 /**
684 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
685 *
686 * @param screenId the screen id to check
687 * @return the new screen, or screenId if it exists
688 */
689 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800690 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700691 if (dropLayout == null) {
692 // it's possible that the add screen was removed because it was
693 // empty and a re-bind occurred
694 mWorkspace.addExtraEmptyScreen();
695 return mWorkspace.commitExtraEmptyScreen();
696 } else {
697 return screenId;
698 }
699 }
700
Sunny Goyal2100c782016-08-22 16:00:03 -0700701 @Thunk void completeTwoStageWidgetDrop(
702 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
703 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800704 Runnable onCompleteRunnable = null;
705 int animationType = 0;
706
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700707 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800708 if (resultCode == RESULT_OK) {
709 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
710 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800711 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700712 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800713 onCompleteRunnable = new Runnable() {
714 @Override
715 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700716 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700717 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800718 }
719 };
720 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800721 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800722 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800723 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700724 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700725 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700726 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
727 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700728 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700729 // The animated view may be null in the case of a rotation during widget configuration
730 onCompleteRunnable.run();
731 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800732 }
733
734 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700735 protected void onStop() {
736 super.onStop();
737 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700738
739 if (mLauncherCallbacks != null) {
740 mLauncherCallbacks.onStop();
741 }
Hyunyoung Song1241e612018-05-15 21:46:51 -0700742 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
743 mStateManager.getState().containerType, -1);
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700744
745 mAppWidgetHost.setListenIfResumed(false);
746
Tony Wickham010d2552017-01-20 08:15:28 -0800747 NotificationListener.removeNotificationsChangedListener();
Sunny Goyalaf161fd2018-02-05 16:34:57 -0800748 getStateManager().moveToRestState();
Winson Chungedb0d6b2018-03-21 17:32:55 -0700749
Tracy Zhou62646712018-06-26 14:19:02 -0700750 UiFactory.onLauncherStateOrResumeChanged(this);
751
Winson Chungdea51352018-04-24 13:02:10 -0700752 // Workaround for b/78520668, explicitly trim memory once UI is hidden
Winson Chung473730f2018-04-26 11:54:22 -0700753 onTrimMemory(TRIM_MEMORY_UI_HIDDEN);
Michael Jurkacd496d72013-04-11 11:32:45 -0700754 }
755
756 @Override
757 protected void onStart() {
758 super.onStart();
759 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700760
761 if (mLauncherCallbacks != null) {
762 mLauncherCallbacks.onStart();
763 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800764 mAppWidgetHost.setListenIfResumed(true);
Sunny Goyalcac002d2018-01-04 09:51:45 -0800765 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
Winson Chung62a70be2018-02-23 15:10:05 -0800766 UiFactory.onStart(this);
Michael Jurkacd496d72013-04-11 11:32:45 -0700767 }
768
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700769 private void logOnDelayedResume() {
770 if (hasBeenResumed()) {
771 getUserEventDispatcher().logActionCommand(Action.Command.RESUME,
772 mStateManager.getState().containerType, -1);
773 getUserEventDispatcher().startSession();
774 }
775 }
776
Michael Jurkacd496d72013-04-11 11:32:45 -0700777 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800778 protected void onResume() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700779 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800780 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700781 TraceHelper.partitionSection("ON_RESUME", "superCall");
782
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700783 mHandler.removeCallbacks(mLogOnDelayedResume);
784 Utilities.postAsyncCallback(mHandler, mLogOnDelayedResume);
785
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700786 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700787 // Process any items that were added while Launcher was away.
788 InstallShortcutReceiver.disableAndFlushInstallQueue(
789 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700790
Sunny Goyala474a9b2017-05-04 16:47:11 -0700791 // Refresh shortcuts if the permission changed.
792 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700793
Tracy Zhoue5575f92018-04-19 11:12:53 -0700794 DiscoveryBounce.showForHomeIfNeeded(this);
Adam Cohen9211d422014-10-07 18:14:53 -0700795 if (mLauncherCallbacks != null) {
796 mLauncherCallbacks.onResume();
797 }
Tracy Zhoua706f002018-03-28 13:55:19 -0700798 UiFactory.onLauncherStateOrResumeChanged(this);
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800799
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700800 TraceHelper.endSection("ON_RESUME");
Adam Cohen06dff352012-06-01 17:17:08 -0700801 }
802
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800803 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700804 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700805 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700806 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700807
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700808 super.onPause();
Joe Onorato24b6fd82009-11-12 13:47:09 -0800809 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700810 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700811
Adam Cohen9211d422014-10-07 18:14:53 -0700812 if (mLauncherCallbacks != null) {
813 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700814 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700815 }
816
Winson Chungd7823942018-02-16 03:23:47 +0000817 @Override
Tracy Zhoua706f002018-03-28 13:55:19 -0700818 protected void onUserLeaveHint() {
819 super.onUserLeaveHint();
820 UiFactory.onLauncherStateOrResumeChanged(this);
821 }
822
823 @Override
Winson Chungd7823942018-02-16 03:23:47 +0000824 public void onWindowFocusChanged(boolean hasFocus) {
825 super.onWindowFocusChanged(hasFocus);
826 mStateManager.onWindowFocusChanged();
827 }
828
Adam Cohenc2d6e892014-10-16 09:49:24 -0700829 public interface LauncherOverlay {
830
831 /**
832 * Touch interaction leading to overscroll has begun
833 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700834 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700835
836 /**
837 * Touch interaction related to overscroll has ended
838 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700839 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700840
841 /**
842 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
843 * screen (or in the case of RTL, the rightmost screen).
844 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700845 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700846
847 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800848 * Called when the launcher is ready to use the overlay
849 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700850 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700851 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700852 }
853
854 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700855 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700856 }
857
858 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
859
Sunny Goyalc86df472016-02-25 09:19:38 -0800860 public void onScrollChanged(float progress) {
861 if (mWorkspace != null) {
862 mWorkspace.onOverlayScrollChanged(progress);
863 }
864 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700865 }
866
Sunny Goyal16764582017-11-06 16:00:34 -0800867 public boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -0700868 if (mLauncherCallbacks != null) {
869 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700870 } else {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800871 // On O and above we there is always some setting present settings (add icon to
872 // home screen or icon badging). On earlier APIs we will have the allow rotation
873 // setting, on devices with a locked orientation,
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700874 return Utilities.ATLEAST_OREO || !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -0700875 }
Jorim Jaggid017f882014-01-14 17:08:48 -0800876 }
877
Sunny Goyalf9403d92017-10-18 10:55:56 -0700878 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -0700879 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700880 }
881
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800882 /**
883 * Restores the previous state, if it exists.
884 *
885 * @param savedState The previous state.
886 */
887 private void restoreState(Bundle savedState) {
888 if (savedState == null) {
889 return;
890 }
891
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700892 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -0700893 LauncherState[] stateValues = LauncherState.values();
894 LauncherState state = stateValues[stateOrdinal];
Sunny Goyalbc683e92017-12-06 10:25:07 -0800895 if (!state.disableRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700896 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800897 }
898
Sunny Goyal2100c782016-08-22 16:00:03 -0700899 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
900 if (requestArgs != null) {
901 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800902 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700903
904 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700905
906 SparseArray<Parcelable> widgetsState =
907 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
908 if (widgetsState != null) {
909 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
910 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800911 }
912
913 /**
914 * Finds all the views we need and configure them properly.
915 */
916 private void setupViews() {
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800917 mDragLayer = findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700918 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -0700919 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700920 mWorkspace.initParentViews(mDragLayer);
Sunny Goyal16764582017-11-06 16:00:34 -0800921 mOverviewPanel = findViewById(R.id.overview_panel);
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800922 mHotseat = findViewById(R.id.hotseat);
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700923 mHotseatSearchBox = findViewById(R.id.search_container_hotseat);
Craig Mautner360310b2012-10-26 15:13:08 -0700924
Sunny Goyal784f9c32016-03-23 16:56:54 -0700925 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
926 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
927 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800928
Winson Chung4c98d922011-05-31 16:50:48 -0700929 // Setup the drag layer
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700930 mDragLayer.setup(mDragController, mWorkspace);
931 UiFactory.setOnTouchControllersChangedListener(this, mDragLayer::recreateControllers);
Winson Chung4c98d922011-05-31 16:50:48 -0700932
Hyunyoung Song645764e2016-06-06 14:19:02 -0700933 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -0700934 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
935 // default state, otherwise we will update to the wrong offsets in RTL
936 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700937 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700938 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700939
Tony Wickham34d2c912015-09-11 09:27:58 -0700940 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700941 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700942
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700943 // Setup Apps
944 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -0700945
Winson Chung3d503fb2011-07-13 17:25:49 -0700946 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -0700947 mDragController.setMoveTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700948 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -0400949
Sunny Goyal7185dd62018-03-14 17:51:49 -0700950 mAllAppsController.setupViews(mAppsView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800951 }
952
953 /**
954 * Creates a view representing a shortcut.
955 *
956 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800957 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800958 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700959 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800960 }
961
962 /**
963 * Creates a view representing a shortcut inflated from the specified resource.
964 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800965 * @param parent The group the shortcut belongs to.
966 * @param info The data structure describing the shortcut.
967 *
968 * @return A View inflated from layoutResId.
969 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700970 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -0800971 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
972 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800973 favorite.applyFromShortcutInfo(info);
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800974 favorite.setOnClickListener(ItemClickHandler.INSTANCE);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700975 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800976 return favorite;
977 }
978
979 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -0700980 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800981 *
982 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800983 */
Adam Cohendcd297f2013-06-18 13:13:40 -0700984 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700985 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -0700986 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
987 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700988 return;
989 }
Adam Cohen558baaf2011-08-15 15:22:57 -0700990
Jon Mirandac476d6e2017-05-04 16:30:01 -0700991 int[] cellXY = mTmpAddItemCellCoordinates;
992 CellLayout layout = getCellLayout(container, screenId);
993
Sunny Goyal782f0c92017-01-19 10:27:54 -0800994 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700995 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -0700996 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
997 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -0800998 }
999
1000 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001001 // Legacy shortcuts are only supported for primary profile.
1002 info = Process.myUserHandle().equals(args.user)
1003 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001004
Sunny Goyalb57645f2017-03-20 13:57:28 -07001005 if (info == null) {
1006 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1007 return;
1008 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001009 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001010 // The app is trying to add a shortcut without sufficient permissions
1011 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1012 return;
1013 }
1014 }
1015
Jon Mirandac476d6e2017-05-04 16:30:01 -07001016 if (container < 0) {
1017 // Adding a shortcut to the Workspace.
1018 final View view = createShortcut(info);
1019 boolean foundCellSpan = false;
1020 // First we check if we already know the exact location where we want to add this item.
1021 if (cellX >= 0 && cellY >= 0) {
1022 cellXY[0] = cellX;
1023 cellXY[1] = cellY;
1024 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001025
Jon Mirandac476d6e2017-05-04 16:30:01 -07001026 // If appropriate, either create a folder or add to an existing folder
1027 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001028 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001029 return;
1030 }
1031 DragObject dragObject = new DragObject();
1032 dragObject.dragInfo = info;
1033 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1034 true)) {
1035 return;
1036 }
1037 } else {
1038 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1039 }
1040
1041 if (!foundCellSpan) {
1042 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001043 return;
1044 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001045
1046 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1047 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001048 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001049 // Adding a shortcut to a Folder.
Sunny Goyale29897f52017-07-20 10:09:42 -07001050 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001051 if (folderIcon != null) {
1052 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1053 folderInfo.add(info, args.rank, false);
1054 } else {
Sunny Goyale29897f52017-07-20 10:09:42 -07001055 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001056 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001057 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001058 }
1059
Sunny Goyale29897f52017-07-20 10:09:42 -07001060 public FolderIcon findFolderIcon(final long folderIconId) {
1061 return (FolderIcon) mWorkspace.getFirstMatch(new ItemOperator() {
1062 @Override
1063 public boolean evaluate(ItemInfo info, View view) {
1064 return info != null && info.id == folderIconId;
1065 }
1066 });
1067 }
1068
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001069 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001070 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001071 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001072 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001073 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001074 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001075 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1076
Adam Cohened66b2b2012-01-23 17:28:51 -08001077 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001078 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001079 }
Romain Guycbb89e42009-06-08 15:52:54 -07001080
Adam Cohen59400422014-03-05 18:07:04 -08001081 LauncherAppWidgetInfo launcherInfo;
1082 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001083 launcherInfo.spanX = itemInfo.spanX;
1084 launcherInfo.spanY = itemInfo.spanY;
1085 launcherInfo.minSpanX = itemInfo.minSpanX;
1086 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001087 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001088
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001089 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001090 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001091
Sunny Goyal2100c782016-08-22 16:00:03 -07001092 if (hostView == null) {
1093 // Perform actual inflation because we're live
1094 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001095 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001096 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301097 prepareAppWidget(hostView, launcherInfo);
1098 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001099 }
Romain Guycbb89e42009-06-08 15:52:54 -07001100
Sunny Goyald5462aa2016-11-22 16:52:39 +05301101 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001102 hostView.setTag(item);
1103 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001104 hostView.setFocusable(true);
1105 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001106 }
1107
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001108 private final BroadcastReceiver mScreenOffReceiver = new BroadcastReceiver() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001109 @Override
1110 public void onReceive(Context context, Intent intent) {
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001111 // Reset AllApps to its initial state only if we are not in the middle of
1112 // processing a multi-step drop
1113 if (mPendingRequestArgs == null) {
1114 mStateManager.goToState(NORMAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001115 }
1116 }
1117 };
1118
Tony Wickham010d2552017-01-20 08:15:28 -08001119 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
Sunny Goyal29947f02017-12-18 13:49:44 -08001120 mWorkspace.updateIconBadges(updatedBadges);
Sunny Goyal60180b02018-02-07 12:56:30 -08001121 mAppsView.getAppsStore().updateIconBadges(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001122
Sunny Goyal29947f02017-12-18 13:49:44 -08001123 PopupContainerWithArrow popup = PopupContainerWithArrow.getOpen(Launcher.this);
1124 if (popup != null) {
1125 popup.updateNotificationHeader(updatedBadges);
Tony Wickham010d2552017-01-20 08:15:28 -08001126 }
1127 }
1128
Adam Cohended9f8d2010-11-03 13:25:16 -07001129 @Override
1130 public void onAttachedToWindow() {
1131 super.onAttachedToWindow();
1132
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001133 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyalc86df472016-02-25 09:19:38 -08001134 if (mLauncherCallbacks != null) {
1135 mLauncherCallbacks.onAttachedToWindow();
1136 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001137 }
1138
1139 @Override
1140 public void onDetachedFromWindow() {
1141 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001142
1143 if (mLauncherCallbacks != null) {
1144 mLauncherCallbacks.onDetachedFromWindow();
1145 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001146 }
1147
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001148 public AllAppsTransitionController getAllAppsController() {
1149 return mAllAppsController;
1150 }
1151
Sunny Goyal9d69c8d2018-03-19 13:41:31 -07001152 @Override
Winson Chung1a341002018-01-17 10:00:23 -08001153 public LauncherRootView getRootView() {
1154 return (LauncherRootView) mLauncherView;
1155 }
1156
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001157 @Override
Winson Chunga6945242014-01-08 14:04:34 -08001158 public DragLayer getDragLayer() {
1159 return mDragLayer;
1160 }
1161
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001162 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001163 return mAppsView;
1164 }
1165
Winson Chunga6945242014-01-08 14:04:34 -08001166 public Workspace getWorkspace() {
1167 return mWorkspace;
1168 }
1169
1170 public Hotseat getHotseat() {
1171 return mHotseat;
1172 }
1173
Sunny Goyal81b4c7b2018-03-26 12:10:31 -07001174 public View getHotseatSearchBox() {
1175 return mHotseatSearchBox;
1176 }
1177
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001178 public <T extends View> T getOverviewPanel() {
Winson Chung8ae41982017-11-10 11:42:13 -08001179 return (T) mOverviewPanel;
Winson Chunga6945242014-01-08 14:04:34 -08001180 }
1181
Sunny Goyal47328fd2016-05-25 18:56:41 -07001182 public DropTargetBar getDropTargetBar() {
1183 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001184 }
1185
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001186 public LauncherAppWidgetHost getAppWidgetHost() {
1187 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001188 }
Romain Guycbb89e42009-06-08 15:52:54 -07001189
Winson Chunga9abd0e2010-10-27 17:18:37 -07001190 public LauncherModel getModel() {
1191 return mModel;
1192 }
1193
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001194 public ModelWriter getModelWriter() {
1195 return mModelWriter;
1196 }
1197
Adam Cohen79d90c52016-04-22 13:29:20 -07001198 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001199 return mSharedPrefs;
1200 }
1201
Sunny Goyalf8d56fc2018-01-31 15:18:11 -08001202 public int getOrientation() { return mOldConfig.orientation; }
Jon Miranda6bed3502017-09-19 14:43:17 -07001203
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001204 @Override
1205 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001206 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001207 super.onNewIntent(intent);
1208
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001209 boolean alreadyOnHome = hasWindowFocus() && ((intent.getFlags() &
Winson15f8b172015-08-19 11:02:39 -07001210 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1211 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001212
1213 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001214 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001215 && AbstractFloatingView.getTopOpenView(this) == null;
Winson15f8b172015-08-19 11:02:39 -07001216 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001217 boolean internalStateHandled = InternalStateHandler
Sunny Goyal8b264562018-02-03 23:00:13 -08001218 .handleNewIntent(this, intent, isStarted());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001219
Winson15f8b172015-08-19 11:02:39 -07001220 if (isActionMain) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001221 if (!internalStateHandled) {
1222 // Note: There should be at most one log per method call. This is enforced
1223 // implicitly by using if-else statements.
1224 UserEventDispatcher ued = getUserEventDispatcher();
1225 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
1226 if (topOpenView != null) {
1227 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
1228 } else if (alreadyOnHome) {
1229 Target target = newContainerTarget(mStateManager.getState().containerType);
1230 target.pageIndex = mWorkspace.getCurrentPage();
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001231 ued.logActionCommand(Action.Command.HOME_INTENT, target,
1232 newContainerTarget(ContainerType.WORKSPACE));
Sunny Goyald3864fa2017-11-14 15:06:36 -08001233 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001234
Sunny Goyald3864fa2017-11-14 15:06:36 -08001235 // In all these cases, only animate if we're already on home
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001236 AbstractFloatingView.closeAllOpenViews(this, isStarted());
Jon Mirandafeba90f2016-10-06 10:53:29 -07001237
Sunny Goyala2467272018-03-16 14:53:05 -07001238 if (!isInState(NORMAL)) {
1239 // Only change state, if not already the same. This prevents cancelling any
1240 // animations running as part of resume
1241 mStateManager.goToState(NORMAL);
1242 }
Sunny Goyald3864fa2017-11-14 15:06:36 -08001243
1244 // Reset the apps view
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001245 if (!alreadyOnHome) {
Sunny Goyal7185dd62018-03-14 17:51:49 -07001246 mAppsView.reset(isStarted() /* animate */);
Sunny Goyald3864fa2017-11-14 15:06:36 -08001247 }
1248
1249 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()) {
1250 mWorkspace.post(mWorkspace::moveToDefaultScreen);
1251 }
1252 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001253
1254 final View v = getWindow().peekDecorView();
1255 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001256 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001257 }
1258
Adam Cohen9211d422014-10-07 18:14:53 -07001259 if (mLauncherCallbacks != null) {
Sunny Goyal85462132018-04-24 11:39:37 -07001260 mLauncherCallbacks.onHomeIntent(internalStateHandled);
Adam Cohen9211d422014-10-07 18:14:53 -07001261 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001262 }
Winson15f8b172015-08-19 11:02:39 -07001263
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001264 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001265 }
1266
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001267 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001268 public void onRestoreInstanceState(Bundle state) {
1269 super.onRestoreInstanceState(state);
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001270 mWorkspace.restoreInstanceStateForChild(mSynchronouslyBoundPage);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001271 }
1272
1273 @Override
1274 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001275 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001276 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001277
Adam Cohen21cd0022013-10-09 18:57:02 -07001278 }
Sunny Goyalea609262017-10-25 15:47:38 -07001279 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001280
1281
1282 AbstractFloatingView widgets = AbstractFloatingView
1283 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1284 if (widgets != null) {
1285 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1286 widgets.saveHierarchyState(widgetsState);
1287 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1288 } else {
1289 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1290 }
1291
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001292 // We close any open folders and shortcut containers since they will not be re-opened,
1293 // and we need to make sure this state is reflected.
1294 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001295
Sunny Goyal2100c782016-08-22 16:00:03 -07001296 if (mPendingRequestArgs != null) {
1297 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1298 }
1299 if (mPendingActivityResult != null) {
1300 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001301 }
1302
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001303 super.onSaveInstanceState(outState);
1304
Adam Cohen9211d422014-10-07 18:14:53 -07001305 if (mLauncherCallbacks != null) {
1306 mLauncherCallbacks.onSaveInstanceState(outState);
1307 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001308 }
1309
1310 @Override
1311 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001312 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001313
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001314 unregisterReceiver(mScreenOffReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001315 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001316
Tony Wickhama0068302018-04-11 16:16:11 -07001317 UiFactory.setOnTouchControllersChangedListener(this, null);
1318
Winson Chungcd2b0142011-06-08 16:02:26 -07001319 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001320 // It's possible to receive onDestroy after a new Launcher activity has
1321 // been created. In this case, don't interfere with the new Launcher.
1322 if (mModel.isCurrentCallbacks(this)) {
1323 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001324 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001325 }
Sunny Goyal623eddd2018-03-02 12:24:41 -08001326 mRotationHelper.destroy();
Sunny Goyal745bad92016-05-02 10:54:12 -07001327
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001328 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001329 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001330 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001331 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001332 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001333
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001334 TextKeyListener.getInstance().release();
Mario Bertschler27288382017-05-24 15:35:09 -07001335
Michael Jurka2ecf9952012-06-18 12:52:28 -07001336 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001337
Tony2917a8b2017-07-31 23:29:42 -07001338 clearPendingBinds();
1339
Adam Cohen9211d422014-10-07 18:14:53 -07001340 if (mLauncherCallbacks != null) {
1341 mLauncherCallbacks.onDestroy();
1342 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001343 }
1344
Sunny Goyalae502842016-06-17 08:43:56 -07001345 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1346 return mAccessibilityDelegate;
1347 }
1348
Adam Cohena9cf38f2011-05-02 15:36:58 -07001349 public DragController getDragController() {
1350 return mDragController;
1351 }
1352
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001353 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001354 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001355 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001356 }
1357
1358 @Override
1359 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1360 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001361 try {
1362 super.startIntentSenderForResult(intent, requestCode,
1363 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1364 } catch (IntentSender.SendIntentException e) {
1365 throw new ActivityNotFoundException();
1366 }
1367 }
1368
Winson Chung70d72102011-08-12 11:24:35 -07001369 /**
1370 * Indicates that we want global search for this activity by setting the globalSearch
1371 * argument for {@link #startSearch} to true.
1372 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001373 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001374 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001375 Bundle appSearchData, boolean globalSearch) {
1376 if (appSearchData == null) {
1377 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001378 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001379 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001380
Sunny Goyal6f28e712016-09-13 14:19:29 -07001381 if (mLauncherCallbacks == null ||
1382 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1383 // Starting search from the callbacks failed. Start the default global search.
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001384 super.startSearch(initialQuery, selectInitialQuery, appSearchData, true);
Ian Parkinson047036e2014-09-01 15:40:53 +01001385 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001386
1387 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001388 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001389 }
1390
Joe Onorato9c1289c2009-08-17 11:03:03 -04001391 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001392 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001393 }
1394
Adam Cohen517a7f52014-03-01 12:12:59 -08001395 public boolean isWorkspaceLoading() {
1396 return mWorkspaceLoading;
1397 }
1398
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001399 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001400 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001401 }
1402
Sunny Goyal04a324a2017-01-20 21:08:59 -08001403 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001404 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001405 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001406
Sunny Goyal2100c782016-08-22 16:00:03 -07001407 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001408 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001409 if (LOGD) {
1410 Log.d(TAG, "Adding widget from drop");
1411 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001412 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001413 }
1414
Sunny Goyal2100c782016-08-22 16:00:03 -07001415 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001416 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1417 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1418 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001419
Adam Cohenad4e15c2013-10-17 16:21:35 -07001420 Runnable onComplete = new Runnable() {
1421 @Override
1422 public void run() {
1423 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001424 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001425 }
1426 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001427 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001428 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001429 }
1430 }
Romain Guycbb89e42009-06-08 15:52:54 -07001431
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001432 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1433 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001434 info.container = container;
1435 info.screenId = screenId;
1436 if (cell != null) {
1437 info.cellX = cell[0];
1438 info.cellY = cell[1];
1439 }
1440 info.spanX = spanX;
1441 info.spanY = spanY;
1442
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001443 switch (info.itemType) {
1444 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1445 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001446 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001447 break;
1448 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001449 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001450 break;
1451 default:
1452 throw new IllegalStateException("Unknown item type: " + info.itemType);
1453 }
1454 }
1455
Adam Cohenfbba09b2011-07-18 21:43:05 -07001456 /**
1457 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001458 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001459 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001460 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1461 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001462 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1463 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1464 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001465 }
1466
Adam Cohenfbba09b2011-07-18 21:43:05 -07001467 /**
1468 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001469 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001470 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001471 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001472 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001473 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001474 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001475 // In the case where we've prebound the widget, we remove it from the DragLayer
1476 if (LOGD) {
1477 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1478 }
1479 getDragLayer().removeView(hostView);
1480
Adam Cohened66b2b2012-01-23 17:28:51 -08001481 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001482 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001483
1484 // Clear the boundWidget so that it doesn't get destroyed.
1485 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001486 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001487 // In this case, we either need to start an activity to get permission to bind
1488 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001489 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1490 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1491 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1492 this, info.componentName);
1493 } else {
1494 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1495 }
Adam Cohendd70d662012-10-04 16:53:44 -07001496 Bundle options = info.bindOptions;
1497
Sunny Goyalffe83f12014-08-14 17:39:34 -07001498 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1499 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001500 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001501 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001502 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001503 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001504 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001505 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001506 }
1507
Adam Cohendcd297f2013-06-18 13:13:40 -07001508 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001509 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001510 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511 folderInfo.title = getText(R.string.folder_name);
1512
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001513 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001514 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001515
1516 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001517 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301518 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001519 // Force measure the new folder icon
1520 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1521 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001522 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001523 }
Romain Guycbb89e42009-06-08 15:52:54 -07001524
Winsonc0b52fe2015-09-09 16:38:15 -07001525 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001526 * Unbinds the view for the specified item, and removes the item and all its children.
1527 *
1528 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001529 * @param itemInfo the {@link ItemInfo} for this view.
1530 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001531 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001532 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001533 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001534 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001535 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1536 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001537 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001538 } else {
1539 mWorkspace.removeWorkspaceItem(v);
1540 }
Winsonc0b52fe2015-09-09 16:38:15 -07001541 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001542 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001543 }
1544 } else if (itemInfo instanceof FolderInfo) {
1545 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001546 if (v instanceof FolderIcon) {
1547 ((FolderIcon) v).removeListeners();
1548 }
Winsonc0b52fe2015-09-09 16:38:15 -07001549 mWorkspace.removeWorkspaceItem(v);
1550 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001551 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001552 }
1553 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1554 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001555 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001556 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07001557 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001558 }
1559 } else {
1560 return false;
1561 }
1562 return true;
1563 }
1564
1565 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07001566 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07001567 */
Sunny Goyal56c73602015-09-25 12:55:01 -07001568 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07001569 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07001570 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07001571 // Deleting an app widget ID is a void call but writes to disk before returning
1572 // to the caller...
1573 new AsyncTask<Void, Void, Void>() {
1574 public Void doInBackground(Void ... args) {
1575 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
1576 return null;
1577 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07001578 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07001579 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001580 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001581 }
1582
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001583 @Override
1584 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001585 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001586 }
1587
Joe Onorato88ec0992009-11-19 13:16:06 -08001588 @Override
1589 public void onBackPressed() {
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08001590 if (finishAutoCancelActionMode()) {
1591 return;
1592 }
Adam Cohen9211d422014-10-07 18:14:53 -07001593 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1594 return;
1595 }
1596
Sunny Goyal45478022015-06-08 16:52:41 -07001597 if (mDragController.isDragging()) {
1598 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001599 return;
1600 }
1601
Jon Mirandafeba90f2016-10-06 10:53:29 -07001602 // Note: There should be at most one log per method call. This is enforced implicitly
1603 // by using if-else statements.
1604 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001605 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
Tony Wickham52c1b662018-05-21 13:13:58 -07001606 if (topView != null && topView.onBackPressed()) {
1607 // Handled by the floating view.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001608 } else if (!isInState(NORMAL)) {
Sunny Goyal49bcf342018-01-11 13:59:53 -08001609 LauncherState lastState = mStateManager.getLastState();
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001610 ued.logActionCommand(Action.Command.BACK, mStateManager.getState().containerType,
1611 lastState.containerType);
Sunny Goyal49bcf342018-01-11 13:59:53 -08001612 mStateManager.goToState(lastState);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001613 } else {
1614 // Back button is a no-op here, but give at least some feedback for the button press
1615 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001616 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001617 }
1618
Sunny Goyalfe770c92016-09-27 14:38:58 -07001619 @TargetApi(Build.VERSION_CODES.M)
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001620 @Override
Jon Miranda73c27ec2018-05-16 18:06:23 -07001621 public ActivityOptions getActivityLaunchOptions(View v) {
1622 return mAppTransitionManager.getActivityLaunchOptions(this, v);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001623 }
1624
Tony Wickham3a6746a2018-03-29 09:39:56 -07001625 public LauncherAppTransitionManager getAppTransitionManager() {
1626 return mAppTransitionManager;
1627 }
1628
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001629 @TargetApi(Build.VERSION_CODES.M)
1630 @Override
1631 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
1632 // Due to legacy reasons, direct call shortcuts require Launchers to have the
1633 // corresponding permission. Show the appropriate permission prompt if that
1634 // is the case.
1635 if (intent.getComponent() == null
1636 && Intent.ACTION_CALL.equals(intent.getAction())
1637 && checkSelfPermission(android.Manifest.permission.CALL_PHONE) !=
1638 PackageManager.PERMISSION_GRANTED) {
1639
1640 setWaitingForResult(PendingRequestArgs
1641 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
1642 requestPermissions(new String[]{android.Manifest.permission.CALL_PHONE},
1643 REQUEST_PERMISSION_CALL_PHONE);
1644 return true;
1645 } else {
1646 return false;
1647 }
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001648 }
1649
Hyunyoung Song46d07f72018-05-22 15:41:25 -07001650 @Override
1651 public void modifyUserEvent(LauncherLogProto.LauncherEvent event) {
1652 if (event.srcTarget != null && event.srcTarget.length > 0 &&
1653 event.srcTarget[1].containerType == ContainerType.PREDICTION) {
1654 Target[] targets = new Target[3];
1655 targets[0] = event.srcTarget[0];
1656 targets[1] = event.srcTarget[1];
1657 targets[2] = newTarget(Target.Type.CONTAINER);
1658 event.srcTarget = targets;
1659 LauncherState state = mStateManager.getState();
1660 if (state == LauncherState.ALL_APPS) {
1661 event.srcTarget[2].containerType = ContainerType.ALLAPPS;
1662 } else if (state == LauncherState.OVERVIEW) {
1663 event.srcTarget[2].containerType = ContainerType.TASKSWITCHER;
1664 }
1665 }
1666 }
1667
Sunny Goyala7ce1662016-05-31 15:01:35 -07001668 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Hyunyoung Song1241e612018-05-15 21:46:51 -07001669 boolean success = super.startActivitySafely(v, intent, item);
1670 if (success && v instanceof BubbleTextView) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001671 // This is set to the view that launched the activity that navigated the user away
1672 // from launcher. Since there is no callback for when the activity has finished
1673 // launching, enable the press state and keep this reference to reset the press
1674 // state when we return to launcher.
1675 BubbleTextView btv = (BubbleTextView) v;
1676 btv.setStayPressed(true);
1677 setOnResumeCallback(btv);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001678 }
Hyunyoung Song1241e612018-05-15 21:46:51 -07001679 return success;
Michael Jurka86a720e2012-05-09 11:23:23 -07001680 }
1681
Winson Chung3d503fb2011-07-13 17:25:49 -07001682 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001683 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07001684 return mHotseat != null && layout != null &&
1685 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
1686 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001687
Winson Chung3d503fb2011-07-13 17:25:49 -07001688 /**
1689 * Returns the CellLayout of the specified container at the specified screen.
1690 */
Sunny Goyal92820592015-03-02 11:31:35 -08001691 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001692 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1693 if (mHotseat != null) {
1694 return mHotseat.getLayout();
1695 } else {
1696 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08001697 }
Winson Chung3d503fb2011-07-13 17:25:49 -07001698 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08001699 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08001700 }
1701 }
1702
Michael Jurkae326f182011-11-21 14:05:46 -08001703 @Override
1704 public void onTrimMemory(int level) {
1705 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07001706 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
1707 // The widget preview db can result in holding onto over
1708 // 3MB of memory for caching which isn't necessary.
1709 SQLiteDatabase.releaseMemory();
1710
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08001711 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07001712 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08001713 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01001714 if (mLauncherCallbacks != null) {
1715 mLauncherCallbacks.onTrimMemory(level);
1716 }
Winson Chung62a70be2018-02-23 15:10:05 -08001717 UiFactory.onTrimMemory(this, level);
Michael Jurkae326f182011-11-21 14:05:46 -08001718 }
1719
Michael Jurkad7c28052012-04-27 15:43:36 -07001720 @Override
1721 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07001722 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07001723 final List<CharSequence> text = event.getText();
1724 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07001725 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001726 // TODO: When can workspace be null?
1727 text.add(mWorkspace == null
1728 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07001729 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07001730 return result;
1731 }
1732
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001733 public void setOnResumeCallback(OnResumeCallback callback) {
1734 if (mOnResumeCallback != null) {
1735 mOnResumeCallback.onLauncherResume();
1736 }
1737 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07001738 }
1739
Michael Jurka7607c2f2013-04-03 14:33:19 -07001740 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001741 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001742 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07001743 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001744 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07001745 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001746 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07001747 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07001748 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07001749 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001750 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001751
Joe Onorato9c1289c2009-08-17 11:03:03 -04001752 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07001753 * Clear any pending bind callbacks. This is called when is loader is planning to
1754 * perform a full rebind from scratch.
1755 */
1756 @Override
1757 public void clearPendingBinds() {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001758 if (mPendingExecutor != null) {
1759 mPendingExecutor.markCompleted();
1760 mPendingExecutor = null;
1761 }
1762 }
1763
1764 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001765 * Refreshes the shortcuts shown on the workspace.
1766 *
1767 * Implementation of the method from LauncherModel.Callbacks.
1768 */
1769 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001770 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001771 // Floating panels (except the full widget sheet) are associated with individual icons. If
1772 // we are starting a fresh bind, close all such panels as all the icons are about
1773 // to go away.
1774 AbstractFloatingView.closeOpenViews(this, true,
Sunny Goyal7ede6112017-12-05 15:11:21 -08001775 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_REBIND_SAFE);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08001776
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001777 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08001778
Winson Chungd64d1762013-08-20 14:37:16 -07001779 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07001780 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07001781 mWorkspace.removeAllWorkspaceScreens();
Sunny Goyalc11fac32018-02-27 19:20:15 -08001782 mAppWidgetHost.clearViews();
Winson Chungd64d1762013-08-20 14:37:16 -07001783
Winson Chung3d503fb2011-07-13 17:25:49 -07001784 if (mHotseat != null) {
Sunny Goyal11e96492018-03-23 17:06:00 -07001785 mHotseat.resetLayout(mDeviceProfile.isVerticalBarLayout());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001786 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001787 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001788 }
1789
Adam Cohendcd297f2013-06-18 13:13:40 -07001790 @Override
1791 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001792 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001793 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
1794 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001795 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
1796 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001797 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001798 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
1799 // If there are no screens, we need to have an empty screen
1800 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07001801 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001802 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07001803
Winsonc7d2e832016-07-28 12:24:55 -07001804 // After we have added all the screens, if the wallpaper was locked to the default state,
1805 // then notify to indicate that it can be released and a proper wallpaper offset can be
1806 // computed before the next layout
1807 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07001808 }
1809
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001810 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001811 int count = orderedScreenIds.size();
1812 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001813 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001814 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001815 // No need to bind the first screen, as its always bound.
1816 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
1817 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001818 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001819 }
1820
Sunny Goyalb23980c2017-08-17 07:45:25 -07001821 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08001822 public void bindAppsAdded(ArrayList<Long> newScreens, ArrayList<ItemInfo> addNotAnimated,
1823 ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07001824 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08001825 if (newScreens != null) {
1826 bindAddScreens(newScreens);
1827 }
Winson Chungd64d1762013-08-20 14:37:16 -07001828
1829 // We add the items without animation on non-visible pages, and with
1830 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08001831 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001832 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001833 }
Adam Cohen76a47a12014-02-05 11:47:43 -08001834 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001835 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07001836 }
Winson Chungc58497e2013-09-03 17:48:37 -07001837
Winson Chung87412982013-10-03 18:34:14 -07001838 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07001839 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001840 }
1841
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001842 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001843 * Bind the items start-end from the list.
1844 *
1845 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001846 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07001847 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07001848 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001849 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07001850 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001851 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07001852 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001853 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001854 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07001855 int end = items.size();
1856 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001857 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07001858
1859 // Short circuit if we are loading dock items for a configuration which has no dock
1860 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
1861 mHotseat == null) {
1862 continue;
1863 }
1864
Sunny Goyal639e9062015-08-19 19:17:06 -07001865 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001866 switch (item.itemType) {
1867 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1868 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08001869 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001870 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07001871 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001872 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001873 }
1874 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07001875 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001876 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001877 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001878 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001879 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07001880 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1881 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07001882 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07001883 if (view == null) {
1884 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001885 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08001886 break;
1887 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001888 default:
1889 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001890 }
Sunny Goyal639e9062015-08-19 19:17:06 -07001891
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001892 /*
1893 * Remove colliding items.
1894 */
1895 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1896 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
1897 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
1898 View v = cl.getChildAt(item.cellX, item.cellY);
1899 Object tag = v.getTag();
1900 String desc = "Collision while binding workspace item: " + item
1901 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08001902 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001903 throw (new RuntimeException(desc));
1904 } else {
1905 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001906 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001907 continue;
1908 }
1909 }
1910 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05301911 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07001912 if (animateIcons) {
1913 // Animate all the applications up now
1914 view.setAlpha(0f);
1915 view.setScaleX(0f);
1916 view.setScaleY(0f);
1917 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08001918 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07001919 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001920 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07001921
Winson Chung997a9232013-07-24 15:33:46 -07001922 if (animateIcons) {
1923 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08001924 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07001925 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08001926 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07001927 final Runnable startBounceAnimRunnable = new Runnable() {
1928 public void run() {
1929 anim.playTogether(bounceAnims);
1930 anim.start();
1931 }
1932 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08001933 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07001934 // We post the animation slightly delayed to prevent slowdowns
1935 // when we are loading right after we return to launcher.
1936 mWorkspace.postDelayed(new Runnable() {
1937 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00001938 if (mWorkspace != null) {
Jon Miranda64eb8ea2018-03-29 11:43:58 -07001939 AbstractFloatingView.closeAllOpenViews(Launcher.this, false);
1940
Ian Parkinson94449152014-01-21 13:09:59 +00001941 mWorkspace.snapToPage(newScreenIndex);
1942 mWorkspace.postDelayed(startBounceAnimRunnable,
1943 NEW_APPS_ANIMATION_DELAY);
1944 }
Winson Chung94d67682013-09-25 16:29:40 -07001945 }
1946 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07001947 } else {
1948 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07001949 }
1950 }
Winson Chung64359a52013-07-08 17:17:08 -07001951 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001952 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001953 }
1954
Joe Onorato9c1289c2009-08-17 11:03:03 -04001955 /**
1956 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001957 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07001958 public void bindAppWidget(LauncherAppWidgetInfo item) {
1959 View view = inflateAppWidget(item);
1960 if (view != null) {
1961 mWorkspace.addInScreen(view, item);
1962 mWorkspace.requestLayout();
1963 }
1964 }
1965
1966 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001967 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05301968 PendingAppWidgetHostView view =
1969 new PendingAppWidgetHostView(this, item, mIconCache, true);
1970 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07001971 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001972 }
1973
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001974 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001975
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001976 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08001977
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001978 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
1979 // If the provider is not ready, bind as a pending widget.
1980 appWidgetInfo = null;
1981 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
1982 // The widget id is not valid. Try to find the widget based on the provider info.
1983 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
1984 } else {
1985 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
1986 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07001987
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001988 // If the provider is ready, but the width is not yet restored, try to restore it.
1989 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
1990 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07001991 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001992 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
1993 + " belongs to component " + item.providerName
1994 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001995 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07001996 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07001997 }
Sunny Goyalff572272014-07-23 13:58:07 -07001998
Sunny Goyal84b4adc2015-08-12 15:12:16 -07001999 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002000 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07002001 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2002 // Id has not been allocated yet. Allocate a new id.
2003 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
2004 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07002005
Sunny Goyald478c832016-04-01 12:04:16 -07002006 // Also try to bind the widget. If the bind fails, the user will be shown
2007 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08002008 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07002009 pendingInfo.spanX = item.spanX;
2010 pendingInfo.spanY = item.spanY;
2011 pendingInfo.minSpanX = item.minSpanX;
2012 pendingInfo.minSpanY = item.minSpanY;
2013 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07002014
2015 boolean isDirectConfig =
2016 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2017 if (isDirectConfig && item.bindOptions != null) {
2018 Bundle newOptions = item.bindOptions.getExtras();
2019 if (options != null) {
2020 newOptions.putAll(options);
2021 }
2022 options = newOptions;
2023 }
Sunny Goyald478c832016-04-01 12:04:16 -07002024 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2025 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002026
Sunny Goyal86df1382016-08-10 15:03:22 -07002027 // We tried to bind once. If we were not able to bind, we would need to
2028 // go through the permission dialog, which means we cannot skip the config
2029 // activity.
2030 item.bindOptions = null;
2031 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2032
Sunny Goyald478c832016-04-01 12:04:16 -07002033 // Bind succeeded
2034 if (success) {
2035 // If the widget has a configure activity, it is still needs to set it up,
2036 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002037 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002038 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2039 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002040 }
Sunny Goyald478c832016-04-01 12:04:16 -07002041
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002042 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002043 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002044 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002045 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002046 // The widget was marked as UI not ready, but there is no configure activity to
2047 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002048 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002049 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002050 }
Sunny Goyalff572272014-07-23 13:58:07 -07002051 }
2052
Sunny Goyald5462aa2016-11-22 16:52:39 +05302053 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002054 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002055 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002056 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002057 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07002058 deleteWidgetInfo(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002059 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002060 }
2061
Sunny Goyal233ee962015-08-03 13:05:01 -07002062 item.minSpanX = appWidgetInfo.minSpanX;
2063 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302064 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002065 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302066 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002067 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302068 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002069
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002070 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002071 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002072 }
2073
Sunny Goyalff572272014-07-23 13:58:07 -07002074 /**
2075 * Restores a pending widget.
2076 *
2077 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002078 */
Sunny Goyald478c832016-04-01 12:04:16 -07002079 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002080 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2081 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2082 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002083 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002084 }
2085
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002086 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002087 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002088 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2089 info.pendingItemInfo = null;
2090 }
Sunny Goyalff572272014-07-23 13:58:07 -07002091
Sunny Goyalba47faa2017-10-04 16:50:57 -07002092 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002093 view.reInflate();
Sunny Goyalba47faa2017-10-04 16:50:57 -07002094 }
2095
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002096 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002097 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002098 }
2099
Adam Cohen1462de32012-07-24 22:34:36 -07002100 public void onPageBoundSynchronously(int page) {
Sunny Goyalf4aceab2017-12-06 16:16:31 -08002101 mSynchronouslyBoundPage = page;
Adam Cohen1462de32012-07-24 22:34:36 -07002102 }
2103
Sunny Goyal527c7d32015-08-28 15:19:36 -07002104 @Override
2105 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2106 if (mPendingExecutor != null) {
2107 mPendingExecutor.markCompleted();
2108 }
2109 mPendingExecutor = executor;
Sunny Goyal60180b02018-02-07 12:56:30 -08002110 if (!isInState(ALL_APPS)) {
2111 mAppsView.getAppsStore().setDeferUpdates(true);
2112 mPendingExecutor.execute(() -> mAppsView.getAppsStore().setDeferUpdates(false));
2113 }
2114
Sunny Goyal527c7d32015-08-28 15:19:36 -07002115 executor.attachTo(this);
2116 }
2117
2118 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2119 if (mPendingExecutor == executor) {
2120 mPendingExecutor = null;
2121 }
2122 }
2123
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002124 @Override
2125 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
Sunny Goyal6001ea22018-05-10 16:31:00 -07002126 AlphaProperty property = mDragLayer.getAlphaProperty(ALPHA_INDEX_LAUNCHER_LOAD);
2127 if (property.getValue() < 1) {
2128 ObjectAnimator anim = ObjectAnimator.ofFloat(property, MultiValueAlpha.VALUE, 1);
2129 if (executor != null) {
2130 anim.addListener(new AnimatorListenerAdapter() {
2131 @Override
2132 public void onAnimationEnd(Animator animation) {
2133 executor.onLoadAnimationCompleted();
2134 }
2135 });
2136 }
2137 anim.start();
Sunny Goyal29947f02017-12-18 13:49:44 -08002138 } else if (executor != null) {
2139 executor.onLoadAnimationCompleted();
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002140 }
2141 }
2142
Joe Onorato9c1289c2009-08-17 11:03:03 -04002143 /**
2144 * Callback saying that there aren't any more items to bind.
2145 *
2146 * Implementation of the method from LauncherModel.Callbacks.
2147 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002148 public void finishBindingItems() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002149 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002150 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002151
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002152 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002153
Sunny Goyal2100c782016-08-22 16:00:03 -07002154 if (mPendingActivityResult != null) {
2155 handleActivityResult(mPendingActivityResult.requestCode,
2156 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2157 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002158 }
2159
Sunny Goyala474a9b2017-05-04 16:47:11 -07002160 InstallShortcutReceiver.disableAndFlushInstallQueue(
2161 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002162
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002163 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002164 }
2165
Winson Chunga2413752012-04-03 14:22:34 -07002166 private boolean canRunNewAppsAnimation() {
2167 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002168 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002169 }
2170
Winson Chungc9168342013-06-26 14:54:55 -07002171 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002172 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07002173 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
2174 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002175 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002176 return bounceAnim;
2177 }
2178
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002179 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002180 * Add the icons for all apps.
2181 *
2182 * Implementation of the method from LauncherModel.Callbacks.
2183 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002184 public void bindAllApplications(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002185 mAppsView.getAppsStore().setApps(apps);
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002186
Adam Cohen9211d422014-10-07 18:14:53 -07002187 if (mLauncherCallbacks != null) {
2188 mLauncherCallbacks.bindAllApplications(apps);
2189 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002190 }
2191
2192 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002193 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
2194 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2195 */
2196 @Override
2197 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002198 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002199 }
2200
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002201 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002202 * A package was updated.
2203 *
2204 * Implementation of the method from LauncherModel.Callbacks.
2205 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002206 @Override
2207 public void bindAppsAddedOrUpdated(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002208 mAppsView.getAppsStore().addOrUpdateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002209 }
2210
Sunny Goyal4390ace2014-10-13 11:33:11 -07002211 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002212 public void bindPromiseAppProgressUpdated(PromiseAppInfo app) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002213 mAppsView.getAppsStore().updatePromiseAppProgress(app);
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002214 }
2215
2216 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002217 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002218 mWorkspace.widgetsRestored(widgets);
2219 }
2220
Joe Onorato9c1289c2009-08-17 11:03:03 -04002221 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002222 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002223 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002224 *
2225 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002226 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002227 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002228 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002229 if (!updated.isEmpty()) {
2230 mWorkspace.updateShortcuts(updated);
2231 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002232 }
2233
2234 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002235 * Update the state of a package, typically related to install state.
2236 *
2237 * Implementation of the method from LauncherModel.Callbacks.
2238 */
Sunny Goyale755d462014-07-22 13:48:29 -07002239 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002240 public void bindRestoreItemsChange(HashSet<ItemInfo> updates) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07002241 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07002242 }
2243
2244 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002245 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07002246 * in addition to specific applications to remove, the reason being that
2247 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002248 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07002249 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002250 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07002251 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002252 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002253 mWorkspace.removeItemsByMatcher(matcher);
2254 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002255 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002256
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002257 @Override
2258 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002259 mAppsView.getAppsStore().removeApps(appInfos);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002260 }
Joe Onoratobe386092009-11-17 17:32:16 -08002261
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002262 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002263 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
2264 mPopupDataProvider.setAllWidgets(allWidgets);
Tony Wickham26b17462017-03-20 17:12:24 -07002265 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
2266 if (topView != null) {
2267 topView.onWidgetsBound();
2268 }
2269 }
2270
Tony Wickham86222d22017-03-29 15:30:43 -07002271 /**
2272 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
2273 * refreshes the widgets and shortcuts associated with the given package/user
2274 */
2275 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07002276 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002277 }
2278
Winson Chung80baf5a2010-08-09 16:03:15 -07002279 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002280 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08002281 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002282 @Override
2283 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
2284 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002285
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002286 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
2287 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07002288 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002289 writer.println(prefix + " Homescreen " + i);
2290
2291 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
2292 for (int j = 0; j < layout.getChildCount(); j++) {
2293 Object tag = layout.getChildAt(j).getTag();
2294 if (tag != null) {
2295 writer.println(prefix + " " + tag.toString());
2296 }
2297 }
2298 }
2299
2300 writer.println(prefix + " Hotseat");
2301 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07002302 for (int j = 0; j < layout.getChildCount(); j++) {
2303 Object tag = layout.getChildAt(j).getTag();
2304 if (tag != null) {
2305 writer.println(prefix + " " + tag.toString());
2306 }
Adam Cohen4caf2982013-08-20 18:54:31 -07002307 }
Sunny Goyala1365452015-10-01 15:46:24 -07002308 }
2309
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002310 writer.println(prefix + "Misc:");
2311 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
2312 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
2313 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
Sunny Goyale43d00d2018-05-14 14:23:18 -07002314 writer.println(" mRotationHelper: " + mRotationHelper);
2315 dumpMisc(writer);
Sunny Goyal2bba1902018-02-27 12:20:50 -08002316
2317 try {
2318 FileLog.flushAll(writer);
2319 } catch (Exception e) {
2320 // Ignore
2321 }
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002322
2323 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002324
Adam Cohen9211d422014-10-07 18:14:53 -07002325 if (mLauncherCallbacks != null) {
2326 mLauncherCallbacks.dump(prefix, fd, writer, args);
2327 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002328 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002329
Sunny Goyal66b24572016-09-21 15:57:55 -07002330 @Override
2331 @TargetApi(Build.VERSION_CODES.N)
2332 public void onProvideKeyboardShortcuts(
2333 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
2334
2335 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002336 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002337 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
2338 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
Vadim Tryshev74c261c2018-06-12 13:44:43 -07002339 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.widget_button_text),
2340 KeyEvent.KEYCODE_W, KeyEvent.META_CTRL_ON));
Sunny Goyal66b24572016-09-21 15:57:55 -07002341 }
Vadim Tryshev3455f8d2018-05-11 20:02:20 -07002342 final View currentFocus = getCurrentFocus();
2343 if (currentFocus != null) {
2344 if (new CustomActionsPopup(this, currentFocus).canShow()) {
2345 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
2346 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
2347 }
2348 if (currentFocus.getTag() instanceof ItemInfo
2349 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
2350 shortcutInfos.add(new KeyboardShortcutInfo(
2351 getString(R.string.shortcuts_menu_with_notifications_description),
2352 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
2353 }
Sunny Goyal66b24572016-09-21 15:57:55 -07002354 }
2355 if (!shortcutInfos.isEmpty()) {
2356 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
2357 }
2358
2359 super.onProvideKeyboardShortcuts(data, menu, deviceId);
2360 }
2361
2362 @Override
2363 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
2364 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
2365 switch (keyCode) {
2366 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002367 if (isInState(NORMAL)) {
2368 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07002369 return true;
2370 }
2371 break;
2372 case KeyEvent.KEYCODE_S: {
2373 View focusedView = getCurrentFocus();
2374 if (focusedView instanceof BubbleTextView
2375 && focusedView.getTag() instanceof ItemInfo
2376 && mAccessibilityDelegate.performAction(focusedView,
2377 (ItemInfo) focusedView.getTag(),
2378 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal00ac9202017-11-09 15:24:06 -08002379 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07002380 return true;
2381 }
2382 break;
2383 }
2384 case KeyEvent.KEYCODE_O:
2385 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
2386 return true;
2387 }
2388 break;
Vadim Tryshev74c261c2018-06-12 13:44:43 -07002389 case KeyEvent.KEYCODE_W:
2390 if (isInState(NORMAL)) {
2391 OptionsPopupView.openWidgets(this);
2392 return true;
2393 }
2394 break;
Sunny Goyal66b24572016-09-21 15:57:55 -07002395 }
2396 }
2397 return super.onKeyShortcut(keyCode, event);
2398 }
2399
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002400 @Override
2401 public boolean onKeyUp(int keyCode, KeyEvent event) {
2402 if (keyCode == KeyEvent.KEYCODE_MENU) {
2403 // KEYCODE_MENU is sent by some tests, for example
2404 // LauncherJankTests#testWidgetsContainerFling. Don't just remove its handling.
2405 if (!mDragController.isDragging() && !mWorkspace.isSwitchingState() &&
2406 isInState(NORMAL)) {
2407 // Close any open floating views.
2408 AbstractFloatingView.closeAllOpenViews(this);
2409
2410 // Setting the touch point to (-1, -1) will show the options popup in the center of
2411 // the screen.
Sunny Goyal2fd7a8b2018-03-30 17:10:13 -07002412 OptionsPopupView.showDefaultOptions(this, -1, -1);
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002413 }
2414 return true;
2415 }
2416 return super.onKeyUp(keyCode, event);
2417 }
2418
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002419 public static Launcher getLauncher(Context context) {
2420 if (context instanceof Launcher) {
2421 return (Launcher) context;
2422 }
2423 return ((Launcher) ((ContextWrapper) context).getBaseContext());
2424 }
2425
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002426 /**
2427 * Callback for listening for onResume
2428 */
2429 public interface OnResumeCallback {
2430
2431 void onLauncherResume();
2432 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002433}