blob: 88408606f5500d1d43c10103a0d2b37f2fe8d126 [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;
Sunny Goyal4c7f2152017-10-17 17:17:16 -070021import static com.android.launcher3.LauncherAnimUtils.SPRING_LOADED_EXIT_DELAY;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -070022import static com.android.launcher3.LauncherState.ALL_APPS;
23import static com.android.launcher3.LauncherState.NORMAL;
Sunny Goyalaeb16432017-10-16 11:46:41 -070024import static com.android.launcher3.logging.LoggerUtils.newContainerTarget;
Sunny Goyal326403e2017-10-02 12:45:10 -070025
Gilles Debunnedd6c9922010-10-25 11:23:41 -070026import android.animation.Animator;
Jon Miranda2d89ea82017-05-04 11:47:53 -070027import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070028import android.animation.AnimatorSet;
Jon Miranda2d89ea82017-05-04 11:47:53 -070029import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070030import android.animation.ValueAnimator;
Adam Cohen0b395352014-06-09 22:54:36 +000031import android.annotation.TargetApi;
Tony Wickham005df0b2018-02-13 11:28:12 -080032import android.app.ActivityOptions;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080033import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070034import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080036import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080037import android.content.ComponentCallbacks2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.content.Context;
Andrew Sappersteinabef55a2016-06-19 12:49:00 -070039import android.content.ContextWrapper;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070041import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070042import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070043import android.content.SharedPreferences;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044import android.content.pm.PackageManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080045import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070046import android.database.sqlite.SQLiteDatabase;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070047import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020048import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.os.Bundle;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070050import android.os.Parcelable;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080051import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070052import android.os.StrictMode;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070053import android.os.UserHandle;
Tony Wickham86222d22017-03-29 15:30:43 -070054import android.support.annotation.Nullable;
Michael Jurkaa33411c2012-06-14 16:18:21 -070055import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070057import android.util.Log;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070058import android.util.SparseArray;
Adam Cohen96d30a12013-07-16 18:13:21 -070059import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070060import android.view.KeyboardShortcutGroup;
61import android.view.KeyboardShortcutInfo;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080062import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -070063import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070064import android.view.View;
Adam Cohen96d30a12013-07-16 18:13:21 -070065import android.view.ViewGroup;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080066import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070067import android.view.animation.OvershootInterpolator;
Adam Cohen96d30a12013-07-16 18:13:21 -070068import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070069
Sunny Goyal651077b2014-06-30 14:15:31 -070070import com.android.launcher3.DropTarget.DragObject;
Jon Mirandac476d6e2017-05-04 16:30:01 -070071import com.android.launcher3.Workspace.ItemOperator;
Sunny Goyalae502842016-06-17 08:43:56 -070072import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070073import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070074import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal7ede6112017-12-05 15:11:21 -080075import com.android.launcher3.allapps.DiscoveryBounce;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070076import com.android.launcher3.badge.BadgeInfo;
Sunny Goyalffe83f12014-08-14 17:39:34 -070077import com.android.launcher3.compat.AppWidgetManagerCompat;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -070078import com.android.launcher3.compat.LauncherAppsCompatVO;
Tony Wickhame0c33232016-02-08 11:37:04 -080079import com.android.launcher3.config.FeatureFlags;
Vadim Tryshevfedca432015-08-19 17:55:02 -070080import com.android.launcher3.dragndrop.DragController;
81import com.android.launcher3.dragndrop.DragLayer;
82import com.android.launcher3.dragndrop.DragView;
Sunny Goyal26119432016-02-18 22:09:23 +000083import com.android.launcher3.folder.FolderIcon;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070084import com.android.launcher3.folder.FolderIconPreviewVerifier;
Sunny Goyal66b24572016-09-21 15:57:55 -070085import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -070086import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -070087import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -070088import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyala535ae42017-02-27 10:07:13 -080089import com.android.launcher3.model.ModelWriter;
Sunny Goyala535ae42017-02-27 10:07:13 -080090import com.android.launcher3.notification.NotificationListener;
Tony Wickham540913e2017-01-23 11:47:51 -080091import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -080092import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -070093import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyalf8088ee2017-11-10 14:52:00 -080094import com.android.launcher3.states.InternalStateHandler;
Sunny Goyal623eddd2018-03-02 12:24:41 -080095import com.android.launcher3.states.RotationHelper;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -080096import com.android.launcher3.touch.ItemClickHandler;
Sunny Goyala5ace712017-11-15 17:12:51 -080097import com.android.launcher3.uioverrides.UiFactory;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +053098import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
99import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
Sunny Goyalaeb16432017-10-16 11:46:41 -0700100import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyal2100c782016-08-22 16:00:03 -0700101import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700102import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700103import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700104import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700105import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800106import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700107import com.android.launcher3.util.PendingRequestArgs;
Sunny Goyal8392c822017-06-20 10:03:56 -0700108import com.android.launcher3.util.SystemUiController;
Mario Bertschlera6936942017-05-31 14:48:19 -0700109import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700110import com.android.launcher3.util.Thunk;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700111import com.android.launcher3.util.TraceHelper;
Sunny Goyal326403e2017-10-02 12:45:10 -0700112import com.android.launcher3.util.UiThreadHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700113import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800114import com.android.launcher3.views.OptionsPopupView;
Sunny Goyal29947f02017-12-18 13:49:44 -0800115import com.android.launcher3.widget.LauncherAppWidgetHostView;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800116import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700117import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal29947f02017-12-18 13:49:44 -0800118import com.android.launcher3.widget.PendingAppWidgetHostView;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800119import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700120import com.android.launcher3.widget.WidgetHostViewLoader;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700121import com.android.launcher3.widget.WidgetListRowEntry;
122import com.android.launcher3.widget.WidgetsFullSheet;
Sunny Goyal952e63d2017-08-16 04:59:08 -0700123import com.android.launcher3.widget.custom.CustomWidgetParser;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700124
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700125import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700126import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700127import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700128import java.util.Collection;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700129import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700130import java.util.List;
Tony Wickham010d2552017-01-20 08:15:28 -0800131import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700132
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133/**
134 * Default launcher application.
135 */
Sunny Goyalab837732018-03-27 17:35:54 -0700136public class Launcher extends BaseDraggingActivity
137 implements LauncherExterns, LauncherModel.Callbacks, LauncherProviderChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700138 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700139 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140
Winson Chunga2413752012-04-03 14:22:34 -0700141 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700142
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700144 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700145
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700146 private static final int REQUEST_PICK_APPWIDGET = 9;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147
Michael Jurka8b805b12012-04-18 14:23:14 -0700148 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800149 public static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
150 public static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700151
Jon Mirandac476d6e2017-05-04 16:30:01 -0700152 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700153
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700154 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
155
Mathew Inwood876a8462013-06-14 14:12:41 +0100156 /**
157 * IntentStarter uses request codes starting with this. This must be greater than all activity
158 * request codes used internally.
159 */
160 protected static final int REQUEST_LAST = 100;
161
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 // Type: int
163 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700164 // Type: int
165 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700166 // Type: PendingRequestArgs
167 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
168 // Type: ActivityResultInfo
169 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700170 // Type: SparseArray<Parcelable>
171 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700173 private LauncherStateManager mStateManager;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700174
Adam Cohenad4e15c2013-10-17 16:21:35 -0700175 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176
Winson Chunga2413752012-04-03 14:22:34 -0700177 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700178 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
179 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
180 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700181
Jon Miranda54441f52018-01-24 15:38:25 -0800182 private LauncherAppTransitionManager mAppTransitionManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800183 private Configuration mOldConfig;
Jon Miranda54441f52018-01-24 15:38:25 -0800184
Adam Cohen091440a2015-03-18 14:16:05 -0700185 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700186 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700187 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800188 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700189
Sunny Goyalffe83f12014-08-14 17:39:34 -0700190 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700191 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700192
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700193 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700194
Sunny Goyal316490e2015-06-02 09:38:28 -0700195 @Thunk Hotseat mHotseat;
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700196 private View mDragHandleIndicator;
197 @Nullable private View mHotseatSearchBox;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700198
Sunny Goyal47328fd2016-05-25 18:56:41 -0700199 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700200
201 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700202 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700203 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700204
Winson Chung8ae41982017-11-10 11:42:13 -0800205 // UI and state for the overview panel
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800206 private View mOverviewPanel;
Winson Chung8ae41982017-11-10 11:42:13 -0800207
Adam Cohen091440a2015-03-18 14:16:05 -0700208 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400209
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700210 private OnResumeCallback mOnResumeCallback;
211
Sunny Goyal527c7d32015-08-28 15:19:36 -0700212 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700213
Joe Onorato9c1289c2009-08-17 11:03:03 -0400214 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800215 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800216 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700217 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400218
Jon Miranda2d89ea82017-05-04 11:47:53 -0700219 private ObjectAnimator mScrimAnimator;
Jon Miranda7fda2852017-07-19 16:07:01 -0700220 private boolean mShouldFadeInScrim;
Jon Miranda2d89ea82017-05-04 11:47:53 -0700221
Tony Wickham010d2552017-01-20 08:15:28 -0800222 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700223
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800224 private int mSynchronouslyBoundPage = PagedView.INVALID_PAGE;
Adam Cohen1462de32012-07-24 22:34:36 -0700225
Winson Chung46353de2012-02-16 14:05:10 -0800226 // We only want to get the SharedPreferences once since it does an FS stat each time we get
227 // it from the context.
228 private SharedPreferences mSharedPrefs;
229
Sunny Goyal2100c782016-08-22 16:00:03 -0700230 // Activity result which needs to be processed after workspace has loaded.
231 private ActivityResultInfo mPendingActivityResult;
232 /**
233 * Holds extra information required to handle a result from an external call, like
234 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
235 */
236 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800237
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700238 public ViewGroupFocusHelper mFocusHandler;
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700239 private boolean mAppLaunchSuccess;
240
Sunny Goyal623eddd2018-03-02 12:24:41 -0800241 private RotationHelper mRotationHelper;
Sunny Goyal7779d622015-06-11 16:18:39 -0700242
Winson Chungedb0d6b2018-03-21 17:32:55 -0700243 // Used to keep track of the swipe up state
244 private SharedPreferences.OnSharedPreferenceChangeListener mSharedPrefsListener =
245 (sharedPreferences, s) -> {
246 mDragLayer.setup(mDragController);
247 };
248
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800249 @Override
250 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700251 if (DEBUG_STRICT_MODE) {
252 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
253 .detectDiskReads()
254 .detectDiskWrites()
255 .detectNetwork() // or .detectAll() for all detectable problems
256 .penaltyLog()
257 .build());
258 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
259 .detectLeakedSqlLiteObjects()
260 .detectLeakedClosableObjects()
261 .penaltyLog()
262 .penaltyDeath()
263 .build());
264 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700265 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700266
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800267 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700268 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400269
Sunny Goyal87f784c2017-01-11 10:48:34 -0800270 LauncherAppState app = LauncherAppState.getInstance(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800271 mOldConfig = new Configuration(getResources().getConfiguration());
Sunny Goyal0e7724c2018-02-22 13:22:21 -0800272 mModel = app.setLauncher(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800273 initDeviceProfile(app.getInvariantDeviceProfile());
Winson Chung6e1c0d32013-10-25 15:24:24 -0700274
Sunny Goyalf7258242015-10-19 16:59:07 -0700275 mSharedPrefs = Utilities.getPrefs(this);
Winson Chungedb0d6b2018-03-21 17:32:55 -0700276 mSharedPrefs.registerOnSharedPreferenceChangeListener(mSharedPrefsListener);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800277 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700278 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700279
Joe Onorato41a12d22009-10-31 18:30:00 -0400280 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700281 mAllAppsController = new AllAppsTransitionController(this);
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800282 mStateManager = new LauncherStateManager(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700283
Sunny Goyalffe83f12014-08-14 17:39:34 -0700284 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700285
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700286 mAppWidgetHost = new LauncherAppWidgetHost(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700287 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700288
Sunny Goyal60820d72017-05-09 12:40:11 -0700289 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700290
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800291 setupViews();
Tony Wickham010d2552017-01-20 08:15:28 -0800292 mPopupDataProvider = new PopupDataProvider(this);
293
Sunny Goyal623eddd2018-03-02 12:24:41 -0800294 mRotationHelper = new RotationHelper(this);
Sunny Goyal90548432018-04-04 17:17:00 -0700295 mAppTransitionManager = LauncherAppTransitionManager.newInstance(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800296
Sunny Goyald3864fa2017-11-14 15:06:36 -0800297 boolean internalStateHandled = InternalStateHandler.handleCreate(this, getIntent());
298 if (internalStateHandled) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800299 if (savedInstanceState != null) {
300 // InternalStateHandler has already set the appropriate state.
301 // We dont need to do anything.
302 savedInstanceState.remove(RUNTIME_STATE);
303 }
304 }
305 restoreState(savedInstanceState);
Winson Chungb63b44c2017-11-10 17:54:44 -0800306
Sunny Goyal2100c782016-08-22 16:00:03 -0700307 // We only load the page synchronously if the user rotates (or triggers a
308 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700309 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
310 if (savedInstanceState != null) {
311 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
312 }
Sunny Goyald3864fa2017-11-14 15:06:36 -0800313
Sunny Goyalfe770c92016-09-27 14:38:58 -0700314 if (!mModel.startLoader(currentScreen)) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800315 if (!internalStateHandled) {
316 // If we are not binding synchronously, show a fade in animation when
317 // the first page bind completes.
318 mDragLayer.setAlpha(0);
319 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700320 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700321 // Pages bound synchronously.
322 mWorkspace.setCurrentPage(currentScreen);
323
Sunny Goyal2100c782016-08-22 16:00:03 -0700324 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800325 }
326
327 // For handling default keys
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800328 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800329
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800330 setContentView(mLauncherView);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800331 getRootView().dispatchInsets();
Mario Bertschlera6936942017-05-31 14:48:19 -0700332
Jon Miranda7fda2852017-07-19 16:07:01 -0700333 // Listen for broadcasts
334 IntentFilter filter = new IntentFilter();
335 filter.addAction(Intent.ACTION_SCREEN_OFF);
336 filter.addAction(Intent.ACTION_USER_PRESENT); // When the device wakes up + keyguard is gone
337 registerReceiver(mReceiver, filter);
338 mShouldFadeInScrim = true;
Sunny Goyala616d2b2017-06-12 13:54:01 -0700339
Sunny Goyal8392c822017-06-20 10:03:56 -0700340 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
341 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700342
343 if (mLauncherCallbacks != null) {
344 mLauncherCallbacks.onCreate(savedInstanceState);
345 }
Sunny Goyal623eddd2018-03-02 12:24:41 -0800346 mRotationHelper.initialize();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700347
348 TraceHelper.endSection("Launcher-onCreate");
Adam Cohen9211d422014-10-07 18:14:53 -0700349 }
350
Sunny Goyal7779d622015-06-11 16:18:39 -0700351 @Override
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800352 public void onConfigurationChanged(Configuration newConfig) {
353 int diff = newConfig.diff(mOldConfig);
354 if ((diff & (CONFIG_ORIENTATION | CONFIG_SCREEN_SIZE)) != 0) {
355 mUserEventDispatcher = null;
356 initDeviceProfile(mDeviceProfile.inv);
Sunny Goyalfde55052018-02-01 14:46:13 -0800357 dispatchDeviceProfileChanged();
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800358
359 getRootView().dispatchInsets();
360 getStateManager().reapplyState();
361
Sunny Goyal7185dd62018-03-14 17:51:49 -0700362 // Recreate touch controllers
363 mDragLayer.setup(mDragController);
364
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800365 // TODO: We can probably avoid rebind when only screen size changed.
Sunny Goyal605bcf32018-03-02 15:16:12 -0800366 rebindModel();
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800367 }
368
369 mOldConfig.setTo(newConfig);
Tracy Zhoua706f002018-03-28 13:55:19 -0700370 UiFactory.onLauncherStateOrResumeChanged(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800371 super.onConfigurationChanged(newConfig);
372 }
373
Sunny Goyal605bcf32018-03-02 15:16:12 -0800374 @Override
375 public void rebindModel() {
376 int currentPage = mWorkspace.getNextPage();
377 if (mModel.startLoader(currentPage)) {
378 mWorkspace.setCurrentPage(currentPage);
379 setWorkspaceLoading(true);
380 }
381 }
382
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800383 private void initDeviceProfile(InvariantDeviceProfile idp) {
384 // Load configuration-specific DeviceProfile
Sunny Goyalf633ef52018-03-13 09:57:05 -0700385 setDeviceProfile(idp.getDeviceProfile(this));
Sunny Goyal605bcf32018-03-02 15:16:12 -0800386 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout(), true);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800387 }
388
Sunny Goyal623eddd2018-03-02 12:24:41 -0800389 public RotationHelper getRotationHelper() {
390 return mRotationHelper;
391 }
392
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700393 public LauncherStateManager getStateManager() {
394 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700395 }
396
Mario Bertschler27288382017-05-24 15:35:09 -0700397 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700398 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800399 return mLauncherView.findViewById(id);
400 }
401
402 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700403 public void onAppWidgetHostReset() {
404 if (mAppWidgetHost != null) {
405 mAppWidgetHost.startListening();
406 }
407 }
408
Adam Cohen9211d422014-10-07 18:14:53 -0700409 private LauncherCallbacks mLauncherCallbacks;
410
Sunny Goyal32554d12015-12-03 15:31:25 -0800411 /**
412 * Call this after onCreate to set or clear overlay.
413 */
414 public void setLauncherOverlay(LauncherOverlay overlay) {
415 if (overlay != null) {
416 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
417 }
418 mWorkspace.setLauncherOverlay(overlay);
419 }
420
Adam Cohen9211d422014-10-07 18:14:53 -0700421 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
422 mLauncherCallbacks = callbacks;
423 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700424 }
425
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700426 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700427 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700428 if (mLauncherCallbacks != null) {
429 mLauncherCallbacks.onLauncherProviderChange();
430 }
431 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700432
Hyunyoung Song3f471442015-04-08 19:01:34 -0700433 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700434 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
435 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700436 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700437 }
438
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000439 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700440 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
441 // This cast is safe as long as the id < 0x00FFFFFF
442 // Since we jail all the dynamically generated views, there should be no clashes
443 // with any other views.
444 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000445 }
446
Tony Wickham010d2552017-01-20 08:15:28 -0800447 public PopupDataProvider getPopupDataProvider() {
448 return mPopupDataProvider;
449 }
450
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700451 @Override
452 public BadgeInfo getBadgeInfoForItem(ItemInfo info) {
453 return mPopupDataProvider.getBadgeInfoForItem(info);
454 }
455
456 @Override
457 public void invalidateParent(ItemInfo info) {
458 FolderIconPreviewVerifier verifier = new FolderIconPreviewVerifier(getDeviceProfile().inv);
459 if (verifier.isItemInPreview(info.rank) && (info.container >= 0)) {
460 View folderIcon = getWorkspace().getHomescreenIconByItemId(info.container);
461 if (folderIcon != null) {
462 folderIcon.invalidate();
463 }
464 }
465 }
466
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000467 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700468 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
469 * a configuration step, this allows the proper animations to run after other transitions.
470 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700471 private long completeAdd(
472 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
473 long screenId = info.screenId;
474 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700475 // When the screen id represents an actual screen (as opposed to a rank) we make sure
476 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700477 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700478 }
Adam Cohendb364c32014-05-20 14:23:40 -0700479
Sunny Goyal2100c782016-08-22 16:00:03 -0700480 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800481 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700482 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700483 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800484 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700485 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700486 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700487 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700488 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700489 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700490 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700491 int widgetId = appWidgetId;
492 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700493 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700494 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700495 // Since the view was just bound, also launch the configure activity if needed
496 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
497 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800498 if (provider != null) {
499 new WidgetAddFlowHandler(provider)
500 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700501 }
502 }
503 break;
504 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800505 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700506
Adam Cohendb364c32014-05-20 14:23:40 -0700507 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800508 }
509
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800510 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700511 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700512 if (isWorkspaceLoading()) {
513 // process the result once the workspace has loaded.
514 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
515 return;
516 }
517 mPendingActivityResult = null;
518
Winson Chunge341d302013-08-16 14:31:00 -0700519 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700520 final PendingRequestArgs requestArgs = mPendingRequestArgs;
521 setWaitingForResult(null);
522 if (requestArgs == null) {
523 return;
524 }
525
526 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700527
Adam Cohenad4e15c2013-10-17 16:21:35 -0700528 Runnable exitSpringLoaded = new Runnable() {
529 @Override
530 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700531 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700532 }
533 };
534
Michael Jurka8b805b12012-04-18 14:23:14 -0700535 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700536 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700537 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700538 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
539 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700540 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700541 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700542 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700543 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700544 addAppWidgetImpl(
545 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800546 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700547 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700548 }
549 return;
550 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200551
Adam Cohened66b2b2012-01-23 17:28:51 -0800552 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700553 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700554
Adam Cohened66b2b2012-01-23 17:28:51 -0800555 // We have special handling for widgets
556 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800557 final int appWidgetId;
558 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
559 : -1;
560 if (widgetId < 0) {
561 appWidgetId = pendingAddWidgetId;
562 } else {
563 appWidgetId = widgetId;
564 }
565
Adam Cohenad4e15c2013-10-17 16:21:35 -0700566 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800567 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700568 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
569 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700570 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700571 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700572 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700573 @Override
574 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700575 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700576 }
577 };
Adam Cohendb364c32014-05-20 14:23:40 -0700578
Sunny Goyal2100c782016-08-22 16:00:03 -0700579 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
580 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
581 } else {
582 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
583 // When the screen id represents an actual screen (as opposed to a rank)
584 // we make sure that the drop page actually exists.
585 requestArgs.screenId =
586 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700587 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700588 final CellLayout dropLayout =
589 mWorkspace.getScreenWithId(requestArgs.screenId);
590
591 dropLayout.setDropPending(true);
592 final Runnable onComplete = new Runnable() {
593 @Override
594 public void run() {
595 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
596 dropLayout.setDropPending(false);
597 }
598 };
599 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
600 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700601 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800602 return;
603 }
604
Sunny Goyald478c832016-04-01 12:04:16 -0700605 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
606 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700607 if (resultCode == RESULT_OK) {
608 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700609 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700610 }
611 // Leave the widget in the pending state if the user canceled the configure.
612 return;
613 }
614
Sunny Goyalaad90582015-07-09 14:22:50 -0700615 if (requestCode == REQUEST_CREATE_SHORTCUT) {
616 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700617 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
618 completeAdd(requestCode, data, -1, requestArgs);
619 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
620 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
621
Sunny Goyalaad90582015-07-09 14:22:50 -0700622 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700623 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
624 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800625 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800626 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800627 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800628 }
629
630 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700631 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800632 final int requestCode, final int resultCode, final Intent data) {
633 handleActivityResult(requestCode, resultCode, data);
634 if (mLauncherCallbacks != null) {
635 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
636 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800637 }
638
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700639 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700640 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600641 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700642 PendingRequestArgs pendingArgs = mPendingRequestArgs;
643 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
644 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
645 setWaitingForResult(null);
646
Sunny Goyal28c6b962015-10-12 11:42:05 -0700647 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700648 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700649 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700650 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700651 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700652 Intent intent = pendingArgs.getPendingIntent();
653
Sunny Goyal28c6b962015-10-12 11:42:05 -0700654 if (grantResults.length > 0
655 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700656 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700657 } else {
658 // TODO: Show a snack bar with link to settings
659 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700660 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700661 }
662 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600663 if (mLauncherCallbacks != null) {
664 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
665 grantResults);
666 }
667 }
668
Adam Cohendb364c32014-05-20 14:23:40 -0700669 /**
670 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
671 *
672 * @param screenId the screen id to check
673 * @return the new screen, or screenId if it exists
674 */
675 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800676 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700677 if (dropLayout == null) {
678 // it's possible that the add screen was removed because it was
679 // empty and a re-bind occurred
680 mWorkspace.addExtraEmptyScreen();
681 return mWorkspace.commitExtraEmptyScreen();
682 } else {
683 return screenId;
684 }
685 }
686
Sunny Goyal2100c782016-08-22 16:00:03 -0700687 @Thunk void completeTwoStageWidgetDrop(
688 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
689 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800690 Runnable onCompleteRunnable = null;
691 int animationType = 0;
692
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700693 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800694 if (resultCode == RESULT_OK) {
695 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
696 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800697 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700698 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800699 onCompleteRunnable = new Runnable() {
700 @Override
701 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700702 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700703 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800704 }
705 };
706 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800707 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800708 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800709 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700710 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700711 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700712 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
713 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700714 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700715 // The animated view may be null in the case of a rotation during widget configuration
716 onCompleteRunnable.run();
717 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800718 }
719
720 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700721 protected void onStop() {
722 super.onStop();
723 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700724
725 if (mLauncherCallbacks != null) {
726 mLauncherCallbacks.onStop();
727 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800728 mAppWidgetHost.setListenIfResumed(false);
Tony Wickham010d2552017-01-20 08:15:28 -0800729
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700730 if (!mAppLaunchSuccess) {
731 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -0800732 mStateManager.getState().containerType, -1);
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700733 }
Tony Wickham010d2552017-01-20 08:15:28 -0800734 NotificationListener.removeNotificationsChangedListener();
Sunny Goyalaf161fd2018-02-05 16:34:57 -0800735 getStateManager().moveToRestState();
Winson Chungedb0d6b2018-03-21 17:32:55 -0700736
Michael Jurkacd496d72013-04-11 11:32:45 -0700737 }
738
739 @Override
740 protected void onStart() {
741 super.onStart();
742 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700743
744 if (mLauncherCallbacks != null) {
745 mLauncherCallbacks.onStart();
746 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800747 mAppWidgetHost.setListenIfResumed(true);
Sunny Goyalcac002d2018-01-04 09:51:45 -0800748 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
Jon Miranda2d89ea82017-05-04 11:47:53 -0700749
Jon Mirandade43a712018-01-18 11:35:10 -0800750 if (mShouldFadeInScrim && mLauncherView.getBackground() != null) {
Jon Miranda2d89ea82017-05-04 11:47:53 -0700751 if (mScrimAnimator != null) {
752 mScrimAnimator.cancel();
753 }
Jon Mirandade43a712018-01-18 11:35:10 -0800754 mLauncherView.getBackground().setAlpha(0);
755 mScrimAnimator = ObjectAnimator.ofInt(mLauncherView.getBackground(),
Jon Miranda2d89ea82017-05-04 11:47:53 -0700756 LauncherAnimUtils.DRAWABLE_ALPHA, 0, 255);
757 mScrimAnimator.addListener(new AnimatorListenerAdapter() {
758 @Override
759 public void onAnimationEnd(Animator animation) {
760 mScrimAnimator = null;
761 }
762 });
763 mScrimAnimator.setDuration(600);
764 mScrimAnimator.setStartDelay(getWindow().getTransitionBackgroundFadeDuration());
765 mScrimAnimator.start();
766 }
Jon Miranda7fda2852017-07-19 16:07:01 -0700767 mShouldFadeInScrim = false;
Winson Chung62a70be2018-02-23 15:10:05 -0800768 UiFactory.onStart(this);
Michael Jurkacd496d72013-04-11 11:32:45 -0700769 }
770
771 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800772 protected void onResume() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700773 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800774 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700775 TraceHelper.partitionSection("ON_RESUME", "superCall");
776
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700777 mAppLaunchSuccess = false;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700778 getUserEventDispatcher().resetElapsedSessionMillis();
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700779 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700780 // Process any items that were added while Launcher was away.
781 InstallShortcutReceiver.disableAndFlushInstallQueue(
782 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700783
Sunny Goyala474a9b2017-05-04 16:47:11 -0700784 // Refresh shortcuts if the permission changed.
785 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700786
Sunny Goyal7ede6112017-12-05 15:11:21 -0800787 DiscoveryBounce.showIfNeeded(this);
Adam Cohen9211d422014-10-07 18:14:53 -0700788 if (mLauncherCallbacks != null) {
789 mLauncherCallbacks.onResume();
790 }
Tracy Zhoua706f002018-03-28 13:55:19 -0700791 UiFactory.onLauncherStateOrResumeChanged(this);
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800792
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700793 TraceHelper.endSection("ON_RESUME");
Adam Cohen06dff352012-06-01 17:17:08 -0700794 }
795
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800796 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700797 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700798 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700799 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700800
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700801 super.onPause();
Joe Onorato24b6fd82009-11-12 13:47:09 -0800802 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700803 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700804
Adam Cohen9211d422014-10-07 18:14:53 -0700805 if (mLauncherCallbacks != null) {
806 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700807 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700808 }
809
Winson Chungd7823942018-02-16 03:23:47 +0000810 @Override
Tracy Zhoua706f002018-03-28 13:55:19 -0700811 protected void onUserLeaveHint() {
812 super.onUserLeaveHint();
813 UiFactory.onLauncherStateOrResumeChanged(this);
814 }
815
816 @Override
Winson Chungd7823942018-02-16 03:23:47 +0000817 public void onWindowFocusChanged(boolean hasFocus) {
818 super.onWindowFocusChanged(hasFocus);
819 mStateManager.onWindowFocusChanged();
820 }
821
Adam Cohenc2d6e892014-10-16 09:49:24 -0700822 public interface LauncherOverlay {
823
824 /**
825 * Touch interaction leading to overscroll has begun
826 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700827 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700828
829 /**
830 * Touch interaction related to overscroll has ended
831 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700832 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700833
834 /**
835 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
836 * screen (or in the case of RTL, the rightmost screen).
837 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700838 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700839
840 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800841 * Called when the launcher is ready to use the overlay
842 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700843 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700844 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700845 }
846
847 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700848 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700849 }
850
851 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
852
Sunny Goyalc86df472016-02-25 09:19:38 -0800853 public void onScrollChanged(float progress) {
854 if (mWorkspace != null) {
855 mWorkspace.onOverlayScrollChanged(progress);
856 }
857 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700858 }
859
Sunny Goyal16764582017-11-06 16:00:34 -0800860 public boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -0700861 if (mLauncherCallbacks != null) {
862 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700863 } else {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800864 // On O and above we there is always some setting present settings (add icon to
865 // home screen or icon badging). On earlier APIs we will have the allow rotation
866 // setting, on devices with a locked orientation,
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700867 return Utilities.ATLEAST_OREO || !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -0700868 }
Jorim Jaggid017f882014-01-14 17:08:48 -0800869 }
870
Sunny Goyalf9403d92017-10-18 10:55:56 -0700871 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -0700872 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700873 }
874
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800875 /**
876 * Restores the previous state, if it exists.
877 *
878 * @param savedState The previous state.
879 */
880 private void restoreState(Bundle savedState) {
881 if (savedState == null) {
882 return;
883 }
884
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700885 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -0700886 LauncherState[] stateValues = LauncherState.values();
887 LauncherState state = stateValues[stateOrdinal];
Sunny Goyalbc683e92017-12-06 10:25:07 -0800888 if (!state.disableRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700889 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800890 }
891
Sunny Goyal2100c782016-08-22 16:00:03 -0700892 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
893 if (requestArgs != null) {
894 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800895 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700896
897 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700898
899 SparseArray<Parcelable> widgetsState =
900 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
901 if (widgetsState != null) {
902 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
903 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800904 }
905
906 /**
907 * Finds all the views we need and configure them properly.
908 */
909 private void setupViews() {
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800910 mDragLayer = findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700911 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -0700912 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700913 mWorkspace.initParentViews(mDragLayer);
Sunny Goyal16764582017-11-06 16:00:34 -0800914 mOverviewPanel = findViewById(R.id.overview_panel);
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800915 mHotseat = findViewById(R.id.hotseat);
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700916 mDragHandleIndicator = findViewById(R.id.drag_indicator);
917 mHotseatSearchBox = findViewById(R.id.search_container_hotseat);
Craig Mautner360310b2012-10-26 15:13:08 -0700918
Sunny Goyal784f9c32016-03-23 16:56:54 -0700919 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
920 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
921 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800922
Winson Chung4c98d922011-05-31 16:50:48 -0700923 // Setup the drag layer
Tony Wickhama0068302018-04-11 16:16:11 -0700924 Runnable setupDragLayer = () -> mDragLayer.setup(mDragController);
925 UiFactory.setOnTouchControllersChangedListener(this, setupDragLayer);
926 setupDragLayer.run();
Winson Chung4c98d922011-05-31 16:50:48 -0700927
Hyunyoung Song645764e2016-06-06 14:19:02 -0700928 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -0700929 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
930 // default state, otherwise we will update to the wrong offsets in RTL
931 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700932 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700933 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700934
Tony Wickham34d2c912015-09-11 09:27:58 -0700935 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700936 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700937
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700938 // Setup Apps
939 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -0700940
Winson Chung3d503fb2011-07-13 17:25:49 -0700941 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -0700942 mDragController.setMoveTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700943 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -0400944
Sunny Goyal7185dd62018-03-14 17:51:49 -0700945 mAllAppsController.setupViews(mAppsView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800946 }
947
948 /**
949 * Creates a view representing a shortcut.
950 *
951 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800952 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800953 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700954 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800955 }
956
957 /**
958 * Creates a view representing a shortcut inflated from the specified resource.
959 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800960 * @param parent The group the shortcut belongs to.
961 * @param info The data structure describing the shortcut.
962 *
963 * @return A View inflated from layoutResId.
964 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700965 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -0800966 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
967 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800968 favorite.applyFromShortcutInfo(info);
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800969 favorite.setOnClickListener(ItemClickHandler.INSTANCE);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700970 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800971 return favorite;
972 }
973
974 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -0700975 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800976 *
977 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800978 */
Adam Cohendcd297f2013-06-18 13:13:40 -0700979 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700980 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -0700981 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
982 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700983 return;
984 }
Adam Cohen558baaf2011-08-15 15:22:57 -0700985
Jon Mirandac476d6e2017-05-04 16:30:01 -0700986 int[] cellXY = mTmpAddItemCellCoordinates;
987 CellLayout layout = getCellLayout(container, screenId);
988
Sunny Goyal782f0c92017-01-19 10:27:54 -0800989 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700990 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -0700991 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
992 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -0800993 }
994
995 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -0700996 // Legacy shortcuts are only supported for primary profile.
997 info = Process.myUserHandle().equals(args.user)
998 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800999
Sunny Goyalb57645f2017-03-20 13:57:28 -07001000 if (info == null) {
1001 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1002 return;
1003 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001004 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001005 // The app is trying to add a shortcut without sufficient permissions
1006 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1007 return;
1008 }
1009 }
1010
Jon Mirandac476d6e2017-05-04 16:30:01 -07001011 if (container < 0) {
1012 // Adding a shortcut to the Workspace.
1013 final View view = createShortcut(info);
1014 boolean foundCellSpan = false;
1015 // First we check if we already know the exact location where we want to add this item.
1016 if (cellX >= 0 && cellY >= 0) {
1017 cellXY[0] = cellX;
1018 cellXY[1] = cellY;
1019 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001020
Jon Mirandac476d6e2017-05-04 16:30:01 -07001021 // If appropriate, either create a folder or add to an existing folder
1022 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001023 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001024 return;
1025 }
1026 DragObject dragObject = new DragObject();
1027 dragObject.dragInfo = info;
1028 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1029 true)) {
1030 return;
1031 }
1032 } else {
1033 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1034 }
1035
1036 if (!foundCellSpan) {
1037 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001038 return;
1039 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001040
1041 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1042 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001043 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001044 // Adding a shortcut to a Folder.
Sunny Goyale29897f2017-07-20 10:09:42 -07001045 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001046 if (folderIcon != null) {
1047 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1048 folderInfo.add(info, args.rank, false);
1049 } else {
Sunny Goyale29897f2017-07-20 10:09:42 -07001050 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001051 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001052 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001053 }
1054
Sunny Goyale29897f2017-07-20 10:09:42 -07001055 public FolderIcon findFolderIcon(final long folderIconId) {
1056 return (FolderIcon) mWorkspace.getFirstMatch(new ItemOperator() {
1057 @Override
1058 public boolean evaluate(ItemInfo info, View view) {
1059 return info != null && info.id == folderIconId;
1060 }
1061 });
1062 }
1063
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001064 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001065 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001066 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001067 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001068 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001069 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001070 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1071
Adam Cohened66b2b2012-01-23 17:28:51 -08001072 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001073 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001074 }
Romain Guycbb89e42009-06-08 15:52:54 -07001075
Adam Cohen59400422014-03-05 18:07:04 -08001076 LauncherAppWidgetInfo launcherInfo;
1077 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001078 launcherInfo.spanX = itemInfo.spanX;
1079 launcherInfo.spanY = itemInfo.spanY;
1080 launcherInfo.minSpanX = itemInfo.minSpanX;
1081 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001082 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001083
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001084 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001085 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001086
Sunny Goyal2100c782016-08-22 16:00:03 -07001087 if (hostView == null) {
1088 // Perform actual inflation because we're live
1089 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001091 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301092 prepareAppWidget(hostView, launcherInfo);
1093 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001094 }
Romain Guycbb89e42009-06-08 15:52:54 -07001095
Sunny Goyald5462aa2016-11-22 16:52:39 +05301096 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001097 hostView.setTag(item);
1098 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001099 hostView.setFocusable(true);
1100 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001101 }
1102
Adam Cohended9f8d2010-11-03 13:25:16 -07001103 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1104 @Override
1105 public void onReceive(Context context, Intent intent) {
1106 final String action = intent.getAction();
1107 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001108 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001109 // processing a multi-step drop
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001110 if (mAppsView != null && mPendingRequestArgs == null) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001111 mStateManager.goToState(NORMAL);
Winson Chung785d2eb2011-04-14 16:08:02 -07001112 }
Jon Miranda7fda2852017-07-19 16:07:01 -07001113 mShouldFadeInScrim = true;
1114 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1115 // ACTION_USER_PRESENT is sent after onStart/onResume. This covers the case where
1116 // the user unlocked and the Launcher is not in the foreground.
1117 mShouldFadeInScrim = false;
Adam Cohended9f8d2010-11-03 13:25:16 -07001118 }
1119 }
1120 };
1121
Tony Wickham010d2552017-01-20 08:15:28 -08001122 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
Sunny Goyal29947f02017-12-18 13:49:44 -08001123 mWorkspace.updateIconBadges(updatedBadges);
Sunny Goyal60180b02018-02-07 12:56:30 -08001124 mAppsView.getAppsStore().updateIconBadges(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001125
Sunny Goyal29947f02017-12-18 13:49:44 -08001126 PopupContainerWithArrow popup = PopupContainerWithArrow.getOpen(Launcher.this);
1127 if (popup != null) {
1128 popup.updateNotificationHeader(updatedBadges);
Tony Wickham010d2552017-01-20 08:15:28 -08001129 }
1130 }
1131
Adam Cohended9f8d2010-11-03 13:25:16 -07001132 @Override
1133 public void onAttachedToWindow() {
1134 super.onAttachedToWindow();
1135
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001136 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyalc86df472016-02-25 09:19:38 -08001137 if (mLauncherCallbacks != null) {
1138 mLauncherCallbacks.onAttachedToWindow();
1139 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001140 }
1141
1142 @Override
1143 public void onDetachedFromWindow() {
1144 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001145
1146 if (mLauncherCallbacks != null) {
1147 mLauncherCallbacks.onDetachedFromWindow();
1148 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001149 }
1150
Sunny Goyal3bac2c22018-02-07 14:44:05 -08001151 public void onQuickstepGestureStarted(boolean isVisible) {
1152 if (mLauncherCallbacks != null) {
1153 mLauncherCallbacks.onQuickstepGestureStarted(isVisible);
1154 }
1155 }
1156
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001157 public AllAppsTransitionController getAllAppsController() {
1158 return mAllAppsController;
1159 }
1160
Sunny Goyal9d69c8d2018-03-19 13:41:31 -07001161 @Override
Winson Chung1a341002018-01-17 10:00:23 -08001162 public LauncherRootView getRootView() {
1163 return (LauncherRootView) mLauncherView;
1164 }
1165
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001166 @Override
Winson Chunga6945242014-01-08 14:04:34 -08001167 public DragLayer getDragLayer() {
1168 return mDragLayer;
1169 }
1170
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001171 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001172 return mAppsView;
1173 }
1174
Winson Chunga6945242014-01-08 14:04:34 -08001175 public Workspace getWorkspace() {
1176 return mWorkspace;
1177 }
1178
1179 public Hotseat getHotseat() {
1180 return mHotseat;
1181 }
1182
Sunny Goyal81b4c7b2018-03-26 12:10:31 -07001183 public View getDragHandleIndicator() {
1184 return mDragHandleIndicator;
1185 }
1186
1187 public View getHotseatSearchBox() {
1188 return mHotseatSearchBox;
1189 }
1190
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001191 public <T extends View> T getOverviewPanel() {
Winson Chung8ae41982017-11-10 11:42:13 -08001192 return (T) mOverviewPanel;
Winson Chunga6945242014-01-08 14:04:34 -08001193 }
1194
Sunny Goyal47328fd2016-05-25 18:56:41 -07001195 public DropTargetBar getDropTargetBar() {
1196 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001197 }
1198
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001199 public LauncherAppWidgetHost getAppWidgetHost() {
1200 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001201 }
Romain Guycbb89e42009-06-08 15:52:54 -07001202
Winson Chunga9abd0e2010-10-27 17:18:37 -07001203 public LauncherModel getModel() {
1204 return mModel;
1205 }
1206
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001207 public ModelWriter getModelWriter() {
1208 return mModelWriter;
1209 }
1210
Adam Cohen79d90c52016-04-22 13:29:20 -07001211 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001212 return mSharedPrefs;
1213 }
1214
Sunny Goyalf8d56fc2018-01-31 15:18:11 -08001215 public int getOrientation() { return mOldConfig.orientation; }
Jon Miranda6bed3502017-09-19 14:43:17 -07001216
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001217 @Override
1218 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001219 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001220 super.onNewIntent(intent);
1221
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001222 boolean alreadyOnHome = hasWindowFocus() && ((intent.getFlags() &
Winson15f8b172015-08-19 11:02:39 -07001223 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1224 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001225
1226 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001227 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001228 && AbstractFloatingView.getTopOpenView(this) == null;
Winson15f8b172015-08-19 11:02:39 -07001229 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001230 boolean internalStateHandled = InternalStateHandler
Sunny Goyal8b264562018-02-03 23:00:13 -08001231 .handleNewIntent(this, intent, isStarted());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001232
Winson15f8b172015-08-19 11:02:39 -07001233 if (isActionMain) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001234 if (!internalStateHandled) {
1235 // Note: There should be at most one log per method call. This is enforced
1236 // implicitly by using if-else statements.
1237 UserEventDispatcher ued = getUserEventDispatcher();
1238 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
1239 if (topOpenView != null) {
1240 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
1241 } else if (alreadyOnHome) {
1242 Target target = newContainerTarget(mStateManager.getState().containerType);
1243 target.pageIndex = mWorkspace.getCurrentPage();
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001244 ued.logActionCommand(Action.Command.HOME_INTENT, target,
1245 newContainerTarget(ContainerType.WORKSPACE));
Sunny Goyald3864fa2017-11-14 15:06:36 -08001246 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001247
Sunny Goyald3864fa2017-11-14 15:06:36 -08001248 // In all these cases, only animate if we're already on home
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001249 AbstractFloatingView.closeAllOpenViews(this, isStarted());
Jon Mirandafeba90f2016-10-06 10:53:29 -07001250
Sunny Goyala2467272018-03-16 14:53:05 -07001251 if (!isInState(NORMAL)) {
1252 // Only change state, if not already the same. This prevents cancelling any
1253 // animations running as part of resume
1254 mStateManager.goToState(NORMAL);
1255 }
Sunny Goyald3864fa2017-11-14 15:06:36 -08001256
1257 // Reset the apps view
1258 if (!alreadyOnHome && mAppsView != null) {
Sunny Goyal7185dd62018-03-14 17:51:49 -07001259 mAppsView.reset(isStarted() /* animate */);
Sunny Goyald3864fa2017-11-14 15:06:36 -08001260 }
1261
1262 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()) {
1263 mWorkspace.post(mWorkspace::moveToDefaultScreen);
1264 }
1265 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001266
1267 final View v = getWindow().peekDecorView();
1268 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001269 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001270 }
1271
Adam Cohen9211d422014-10-07 18:14:53 -07001272 if (mLauncherCallbacks != null) {
1273 mLauncherCallbacks.onHomeIntent();
1274 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001275 }
Winson15f8b172015-08-19 11:02:39 -07001276
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001277 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001278 }
1279
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001280 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001281 public void onRestoreInstanceState(Bundle state) {
1282 super.onRestoreInstanceState(state);
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001283 mWorkspace.restoreInstanceStateForChild(mSynchronouslyBoundPage);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001284 }
1285
1286 @Override
1287 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001288 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001289 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001290
Adam Cohen21cd0022013-10-09 18:57:02 -07001291 }
Sunny Goyalea609262017-10-25 15:47:38 -07001292 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001293
1294
1295 AbstractFloatingView widgets = AbstractFloatingView
1296 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1297 if (widgets != null) {
1298 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1299 widgets.saveHierarchyState(widgetsState);
1300 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1301 } else {
1302 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1303 }
1304
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001305 // We close any open folders and shortcut containers since they will not be re-opened,
1306 // and we need to make sure this state is reflected.
1307 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001308
Sunny Goyal2100c782016-08-22 16:00:03 -07001309 if (mPendingRequestArgs != null) {
1310 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1311 }
1312 if (mPendingActivityResult != null) {
1313 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001314 }
1315
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001316 super.onSaveInstanceState(outState);
1317
Adam Cohen9211d422014-10-07 18:14:53 -07001318 if (mLauncherCallbacks != null) {
1319 mLauncherCallbacks.onSaveInstanceState(outState);
1320 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001321 }
1322
1323 @Override
1324 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001325 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001326
Sunny Goyal5cc7bbd2017-06-12 09:50:39 -07001327 unregisterReceiver(mReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001328 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001329
Tony Wickhama0068302018-04-11 16:16:11 -07001330 UiFactory.setOnTouchControllersChangedListener(this, null);
1331
Winson Chungcd2b0142011-06-08 16:02:26 -07001332 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001333 // It's possible to receive onDestroy after a new Launcher activity has
1334 // been created. In this case, don't interfere with the new Launcher.
1335 if (mModel.isCurrentCallbacks(this)) {
1336 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001337 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001338 }
Sunny Goyal623eddd2018-03-02 12:24:41 -08001339 mRotationHelper.destroy();
Winson Chungedb0d6b2018-03-21 17:32:55 -07001340 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mSharedPrefsListener);
Sunny Goyal745bad92016-05-02 10:54:12 -07001341
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001342 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001343 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001344 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001345 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001346 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001347
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001348 TextKeyListener.getInstance().release();
Mario Bertschler27288382017-05-24 15:35:09 -07001349
Michael Jurka2ecf9952012-06-18 12:52:28 -07001350 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001351
Tony2917a8b2017-07-31 23:29:42 -07001352 clearPendingBinds();
1353
Adam Cohen9211d422014-10-07 18:14:53 -07001354 if (mLauncherCallbacks != null) {
1355 mLauncherCallbacks.onDestroy();
1356 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001357 }
1358
Sunny Goyalae502842016-06-17 08:43:56 -07001359 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1360 return mAccessibilityDelegate;
1361 }
1362
Adam Cohena9cf38f2011-05-02 15:36:58 -07001363 public DragController getDragController() {
1364 return mDragController;
1365 }
1366
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001367 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001368 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001369 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001370 }
1371
1372 @Override
1373 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1374 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001375 try {
1376 super.startIntentSenderForResult(intent, requestCode,
1377 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1378 } catch (IntentSender.SendIntentException e) {
1379 throw new ActivityNotFoundException();
1380 }
1381 }
1382
Winson Chung70d72102011-08-12 11:24:35 -07001383 /**
1384 * Indicates that we want global search for this activity by setting the globalSearch
1385 * argument for {@link #startSearch} to true.
1386 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001387 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001388 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001389 Bundle appSearchData, boolean globalSearch) {
1390 if (appSearchData == null) {
1391 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001392 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001393 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001394
Sunny Goyal6f28e712016-09-13 14:19:29 -07001395 if (mLauncherCallbacks == null ||
1396 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1397 // Starting search from the callbacks failed. Start the default global search.
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001398 super.startSearch(initialQuery, selectInitialQuery, appSearchData, true);
Ian Parkinson047036e2014-09-01 15:40:53 +01001399 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001400
1401 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001402 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001403 }
1404
Joe Onorato9c1289c2009-08-17 11:03:03 -04001405 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001406 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001407 }
1408
Adam Cohen517a7f52014-03-01 12:12:59 -08001409 public boolean isWorkspaceLoading() {
1410 return mWorkspaceLoading;
1411 }
1412
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001413 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001414 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001415 }
1416
Sunny Goyal04a324a2017-01-20 21:08:59 -08001417 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001418 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001419 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001420
Sunny Goyal2100c782016-08-22 16:00:03 -07001421 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001422 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001423 if (LOGD) {
1424 Log.d(TAG, "Adding widget from drop");
1425 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001426 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001427 }
1428
Sunny Goyal2100c782016-08-22 16:00:03 -07001429 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001430 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1431 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1432 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001433
Adam Cohenad4e15c2013-10-17 16:21:35 -07001434 Runnable onComplete = new Runnable() {
1435 @Override
1436 public void run() {
1437 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001438 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001439 }
1440 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001441 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001442 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001443 }
1444 }
Romain Guycbb89e42009-06-08 15:52:54 -07001445
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001446 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1447 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001448 info.container = container;
1449 info.screenId = screenId;
1450 if (cell != null) {
1451 info.cellX = cell[0];
1452 info.cellY = cell[1];
1453 }
1454 info.spanX = spanX;
1455 info.spanY = spanY;
1456
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001457 switch (info.itemType) {
1458 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1459 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001460 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001461 break;
1462 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001463 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001464 break;
1465 default:
1466 throw new IllegalStateException("Unknown item type: " + info.itemType);
1467 }
1468 }
1469
Adam Cohenfbba09b2011-07-18 21:43:05 -07001470 /**
1471 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001472 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001473 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001474 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1475 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001476 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1477 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1478 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001479 }
1480
Adam Cohenfbba09b2011-07-18 21:43:05 -07001481 /**
1482 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001483 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001484 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001485 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001486 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001487 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001488 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001489 // In the case where we've prebound the widget, we remove it from the DragLayer
1490 if (LOGD) {
1491 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1492 }
1493 getDragLayer().removeView(hostView);
1494
Adam Cohened66b2b2012-01-23 17:28:51 -08001495 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001496 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001497
1498 // Clear the boundWidget so that it doesn't get destroyed.
1499 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001500 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001501 // In this case, we either need to start an activity to get permission to bind
1502 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001503 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1504 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1505 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1506 this, info.componentName);
1507 } else {
1508 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1509 }
Adam Cohendd70d662012-10-04 16:53:44 -07001510 Bundle options = info.bindOptions;
1511
Sunny Goyalffe83f12014-08-14 17:39:34 -07001512 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1513 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001514 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001515 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001516 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001517 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001518 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001519 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001520 }
1521
Adam Cohendcd297f2013-06-18 13:13:40 -07001522 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001523 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001524 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001525 folderInfo.title = getText(R.string.folder_name);
1526
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001527 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001528 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001529
1530 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001531 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301532 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001533 // Force measure the new folder icon
1534 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1535 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001536 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001537 }
Romain Guycbb89e42009-06-08 15:52:54 -07001538
Winsonc0b52fe2015-09-09 16:38:15 -07001539 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001540 * Unbinds the view for the specified item, and removes the item and all its children.
1541 *
1542 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001543 * @param itemInfo the {@link ItemInfo} for this view.
1544 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001545 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001546 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001547 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001548 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001549 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1550 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001551 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001552 } else {
1553 mWorkspace.removeWorkspaceItem(v);
1554 }
Winsonc0b52fe2015-09-09 16:38:15 -07001555 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001556 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001557 }
1558 } else if (itemInfo instanceof FolderInfo) {
1559 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001560 if (v instanceof FolderIcon) {
1561 ((FolderIcon) v).removeListeners();
1562 }
Winsonc0b52fe2015-09-09 16:38:15 -07001563 mWorkspace.removeWorkspaceItem(v);
1564 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001565 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001566 }
1567 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1568 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001569 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001570 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07001571 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001572 }
1573 } else {
1574 return false;
1575 }
1576 return true;
1577 }
1578
1579 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07001580 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07001581 */
Sunny Goyal56c73602015-09-25 12:55:01 -07001582 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07001583 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07001584 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07001585 // Deleting an app widget ID is a void call but writes to disk before returning
1586 // to the caller...
1587 new AsyncTask<Void, Void, Void>() {
1588 public Void doInBackground(Void ... args) {
1589 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
1590 return null;
1591 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07001592 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07001593 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001594 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001595 }
1596
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001597 @Override
1598 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001599 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001600 }
1601
Joe Onorato88ec0992009-11-19 13:16:06 -08001602 @Override
1603 public void onBackPressed() {
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08001604 if (finishAutoCancelActionMode()) {
1605 return;
1606 }
Adam Cohen9211d422014-10-07 18:14:53 -07001607 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1608 return;
1609 }
1610
Sunny Goyal45478022015-06-08 16:52:41 -07001611 if (mDragController.isDragging()) {
1612 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001613 return;
1614 }
1615
Jon Mirandafeba90f2016-10-06 10:53:29 -07001616 // Note: There should be at most one log per method call. This is enforced implicitly
1617 // by using if-else statements.
1618 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001619 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
1620 if (topView != null) {
Sunny Goyal37920962017-09-28 13:43:24 -07001621 topView.onBackPressed();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001622 } else if (!isInState(NORMAL)) {
Sunny Goyal49bcf342018-01-11 13:59:53 -08001623 LauncherState lastState = mStateManager.getLastState();
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001624 ued.logActionCommand(Action.Command.BACK, mStateManager.getState().containerType,
1625 lastState.containerType);
Sunny Goyal49bcf342018-01-11 13:59:53 -08001626 mStateManager.goToState(lastState);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001627 } else {
1628 // Back button is a no-op here, but give at least some feedback for the button press
1629 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001630 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001631 }
1632
Sunny Goyalfe770c92016-09-27 14:38:58 -07001633 @TargetApi(Build.VERSION_CODES.M)
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001634 @Override
Tony Wickham005df0b2018-02-13 11:28:12 -08001635 public ActivityOptions getActivityLaunchOptions(View v, boolean useDefaultLaunchOptions) {
Jon Miranda17940f02018-01-18 14:46:34 -08001636 return useDefaultLaunchOptions
Jon Miranda54441f52018-01-24 15:38:25 -08001637 ? mAppTransitionManager.getDefaultActivityLaunchOptions(this, v)
1638 : mAppTransitionManager.getActivityLaunchOptions(this, v);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001639 }
1640
Tony Wickham3a6746a2018-03-29 09:39:56 -07001641 public LauncherAppTransitionManager getAppTransitionManager() {
1642 return mAppTransitionManager;
1643 }
1644
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001645 @TargetApi(Build.VERSION_CODES.M)
1646 @Override
1647 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
1648 // Due to legacy reasons, direct call shortcuts require Launchers to have the
1649 // corresponding permission. Show the appropriate permission prompt if that
1650 // is the case.
1651 if (intent.getComponent() == null
1652 && Intent.ACTION_CALL.equals(intent.getAction())
1653 && checkSelfPermission(android.Manifest.permission.CALL_PHONE) !=
1654 PackageManager.PERMISSION_GRANTED) {
1655
1656 setWaitingForResult(PendingRequestArgs
1657 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
1658 requestPermissions(new String[]{android.Manifest.permission.CALL_PHONE},
1659 REQUEST_PERMISSION_CALL_PHONE);
1660 return true;
1661 } else {
1662 return false;
1663 }
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001664 }
1665
Sunny Goyala7ce1662016-05-31 15:01:35 -07001666 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001667 mAppLaunchSuccess = super.startActivitySafely(v, intent, item);
1668 if (mAppLaunchSuccess && v instanceof BubbleTextView) {
1669 // This is set to the view that launched the activity that navigated the user away
1670 // from launcher. Since there is no callback for when the activity has finished
1671 // launching, enable the press state and keep this reference to reset the press
1672 // state when we return to launcher.
1673 BubbleTextView btv = (BubbleTextView) v;
1674 btv.setStayPressed(true);
1675 setOnResumeCallback(btv);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001676 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07001677 return mAppLaunchSuccess;
Michael Jurka86a720e2012-05-09 11:23:23 -07001678 }
1679
Winson Chung3d503fb2011-07-13 17:25:49 -07001680 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001681 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07001682 return mHotseat != null && layout != null &&
1683 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
1684 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001685
Winson Chung3d503fb2011-07-13 17:25:49 -07001686 /**
1687 * Returns the CellLayout of the specified container at the specified screen.
1688 */
Sunny Goyal92820592015-03-02 11:31:35 -08001689 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001690 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1691 if (mHotseat != null) {
1692 return mHotseat.getLayout();
1693 } else {
1694 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08001695 }
Winson Chung3d503fb2011-07-13 17:25:49 -07001696 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08001697 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08001698 }
1699 }
1700
Michael Jurkae326f182011-11-21 14:05:46 -08001701 @Override
1702 public void onTrimMemory(int level) {
1703 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07001704 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
1705 // The widget preview db can result in holding onto over
1706 // 3MB of memory for caching which isn't necessary.
1707 SQLiteDatabase.releaseMemory();
1708
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08001709 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07001710 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08001711 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01001712 if (mLauncherCallbacks != null) {
1713 mLauncherCallbacks.onTrimMemory(level);
1714 }
Winson Chung62a70be2018-02-23 15:10:05 -08001715 UiFactory.onTrimMemory(this, level);
Michael Jurkae326f182011-11-21 14:05:46 -08001716 }
1717
Michael Jurkad7c28052012-04-27 15:43:36 -07001718 @Override
1719 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07001720 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07001721 final List<CharSequence> text = event.getText();
1722 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07001723 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001724 // TODO: When can workspace be null?
1725 text.add(mWorkspace == null
1726 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07001727 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07001728 return result;
1729 }
1730
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001731 public void setOnResumeCallback(OnResumeCallback callback) {
1732 if (mOnResumeCallback != null) {
1733 mOnResumeCallback.onLauncherResume();
1734 }
1735 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07001736 }
1737
Michael Jurka7607c2f2013-04-03 14:33:19 -07001738 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001739 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001740 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07001741 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001742 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07001743 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001744 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07001745 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07001746 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07001747 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001748 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001749
Joe Onorato9c1289c2009-08-17 11:03:03 -04001750 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07001751 * Clear any pending bind callbacks. This is called when is loader is planning to
1752 * perform a full rebind from scratch.
1753 */
1754 @Override
1755 public void clearPendingBinds() {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001756 if (mPendingExecutor != null) {
1757 mPendingExecutor.markCompleted();
1758 mPendingExecutor = null;
1759 }
1760 }
1761
1762 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001763 * Refreshes the shortcuts shown on the workspace.
1764 *
1765 * Implementation of the method from LauncherModel.Callbacks.
1766 */
1767 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001768 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001769 // Floating panels (except the full widget sheet) are associated with individual icons. If
1770 // we are starting a fresh bind, close all such panels as all the icons are about
1771 // to go away.
1772 AbstractFloatingView.closeOpenViews(this, true,
Sunny Goyal7ede6112017-12-05 15:11:21 -08001773 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_REBIND_SAFE);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08001774
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001775 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08001776
Winson Chungd64d1762013-08-20 14:37:16 -07001777 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07001778 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07001779 mWorkspace.removeAllWorkspaceScreens();
Sunny Goyalc11fac32018-02-27 19:20:15 -08001780 mAppWidgetHost.clearViews();
Winson Chungd64d1762013-08-20 14:37:16 -07001781
Winson Chung3d503fb2011-07-13 17:25:49 -07001782 if (mHotseat != null) {
Sunny Goyal11e96492018-03-23 17:06:00 -07001783 mHotseat.resetLayout(mDeviceProfile.isVerticalBarLayout());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001784 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001785 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001786 }
1787
Adam Cohendcd297f2013-06-18 13:13:40 -07001788 @Override
1789 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001790 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001791 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
1792 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001793 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
1794 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001795 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001796 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
1797 // If there are no screens, we need to have an empty screen
1798 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07001799 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001800 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07001801
Winsonc7d2e832016-07-28 12:24:55 -07001802 // After we have added all the screens, if the wallpaper was locked to the default state,
1803 // then notify to indicate that it can be released and a proper wallpaper offset can be
1804 // computed before the next layout
1805 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07001806 }
1807
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001808 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001809 int count = orderedScreenIds.size();
1810 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001811 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001812 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001813 // No need to bind the first screen, as its always bound.
1814 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
1815 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001816 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001817 }
1818
Sunny Goyalb23980c2017-08-17 07:45:25 -07001819 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08001820 public void bindAppsAdded(ArrayList<Long> newScreens, ArrayList<ItemInfo> addNotAnimated,
1821 ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07001822 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08001823 if (newScreens != null) {
1824 bindAddScreens(newScreens);
1825 }
Winson Chungd64d1762013-08-20 14:37:16 -07001826
1827 // We add the items without animation on non-visible pages, and with
1828 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08001829 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001830 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001831 }
Adam Cohen76a47a12014-02-05 11:47:43 -08001832 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001833 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07001834 }
Winson Chungc58497e2013-09-03 17:48:37 -07001835
Winson Chung87412982013-10-03 18:34:14 -07001836 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07001837 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001838 }
1839
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001840 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001841 * Bind the items start-end from the list.
1842 *
1843 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001844 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07001845 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07001846 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001847 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07001848 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001849 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07001850 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001851 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001852 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07001853 int end = items.size();
1854 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001855 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07001856
1857 // Short circuit if we are loading dock items for a configuration which has no dock
1858 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
1859 mHotseat == null) {
1860 continue;
1861 }
1862
Sunny Goyal639e9062015-08-19 19:17:06 -07001863 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001864 switch (item.itemType) {
1865 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1866 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08001867 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001868 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07001869 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001870 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001871 }
1872 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07001873 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001874 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001875 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001876 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001877 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07001878 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1879 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07001880 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07001881 if (view == null) {
1882 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001883 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08001884 break;
1885 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001886 default:
1887 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001888 }
Sunny Goyal639e9062015-08-19 19:17:06 -07001889
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001890 /*
1891 * Remove colliding items.
1892 */
1893 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1894 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
1895 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
1896 View v = cl.getChildAt(item.cellX, item.cellY);
1897 Object tag = v.getTag();
1898 String desc = "Collision while binding workspace item: " + item
1899 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08001900 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001901 throw (new RuntimeException(desc));
1902 } else {
1903 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001904 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001905 continue;
1906 }
1907 }
1908 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05301909 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07001910 if (animateIcons) {
1911 // Animate all the applications up now
1912 view.setAlpha(0f);
1913 view.setScaleX(0f);
1914 view.setScaleY(0f);
1915 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08001916 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07001917 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001918 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07001919
Winson Chung997a9232013-07-24 15:33:46 -07001920 if (animateIcons) {
1921 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08001922 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07001923 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08001924 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07001925 final Runnable startBounceAnimRunnable = new Runnable() {
1926 public void run() {
1927 anim.playTogether(bounceAnims);
1928 anim.start();
1929 }
1930 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08001931 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07001932 // We post the animation slightly delayed to prevent slowdowns
1933 // when we are loading right after we return to launcher.
1934 mWorkspace.postDelayed(new Runnable() {
1935 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00001936 if (mWorkspace != null) {
Jon Miranda64eb8ea2018-03-29 11:43:58 -07001937 AbstractFloatingView.closeAllOpenViews(Launcher.this, false);
1938
Ian Parkinson94449152014-01-21 13:09:59 +00001939 mWorkspace.snapToPage(newScreenIndex);
1940 mWorkspace.postDelayed(startBounceAnimRunnable,
1941 NEW_APPS_ANIMATION_DELAY);
1942 }
Winson Chung94d67682013-09-25 16:29:40 -07001943 }
1944 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07001945 } else {
1946 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07001947 }
1948 }
Winson Chung64359a52013-07-08 17:17:08 -07001949 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001950 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001951 }
1952
Joe Onorato9c1289c2009-08-17 11:03:03 -04001953 /**
1954 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001955 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07001956 public void bindAppWidget(LauncherAppWidgetInfo item) {
1957 View view = inflateAppWidget(item);
1958 if (view != null) {
1959 mWorkspace.addInScreen(view, item);
1960 mWorkspace.requestLayout();
1961 }
1962 }
1963
1964 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001965 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05301966 PendingAppWidgetHostView view =
1967 new PendingAppWidgetHostView(this, item, mIconCache, true);
1968 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07001969 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001970 }
1971
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001972 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001973
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001974 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08001975
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001976 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
1977 // If the provider is not ready, bind as a pending widget.
1978 appWidgetInfo = null;
1979 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
1980 // The widget id is not valid. Try to find the widget based on the provider info.
1981 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
1982 } else {
1983 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
1984 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07001985
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001986 // If the provider is ready, but the width is not yet restored, try to restore it.
1987 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
1988 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07001989 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001990 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
1991 + " belongs to component " + item.providerName
1992 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001993 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07001994 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07001995 }
Sunny Goyalff572272014-07-23 13:58:07 -07001996
Sunny Goyal84b4adc2015-08-12 15:12:16 -07001997 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001998 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07001999 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2000 // Id has not been allocated yet. Allocate a new id.
2001 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
2002 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07002003
Sunny Goyald478c832016-04-01 12:04:16 -07002004 // Also try to bind the widget. If the bind fails, the user will be shown
2005 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08002006 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07002007 pendingInfo.spanX = item.spanX;
2008 pendingInfo.spanY = item.spanY;
2009 pendingInfo.minSpanX = item.minSpanX;
2010 pendingInfo.minSpanY = item.minSpanY;
2011 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07002012
2013 boolean isDirectConfig =
2014 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2015 if (isDirectConfig && item.bindOptions != null) {
2016 Bundle newOptions = item.bindOptions.getExtras();
2017 if (options != null) {
2018 newOptions.putAll(options);
2019 }
2020 options = newOptions;
2021 }
Sunny Goyald478c832016-04-01 12:04:16 -07002022 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2023 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002024
Sunny Goyal86df1382016-08-10 15:03:22 -07002025 // We tried to bind once. If we were not able to bind, we would need to
2026 // go through the permission dialog, which means we cannot skip the config
2027 // activity.
2028 item.bindOptions = null;
2029 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2030
Sunny Goyald478c832016-04-01 12:04:16 -07002031 // Bind succeeded
2032 if (success) {
2033 // If the widget has a configure activity, it is still needs to set it up,
2034 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002035 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002036 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2037 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002038 }
Sunny Goyald478c832016-04-01 12:04:16 -07002039
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002040 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002041 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002042 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002043 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002044 // The widget was marked as UI not ready, but there is no configure activity to
2045 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002046 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002047 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002048 }
Sunny Goyalff572272014-07-23 13:58:07 -07002049 }
2050
Sunny Goyald5462aa2016-11-22 16:52:39 +05302051 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002052 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002053 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002054 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002055 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07002056 deleteWidgetInfo(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002057 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002058 }
2059
Sunny Goyal233ee962015-08-03 13:05:01 -07002060 item.minSpanX = appWidgetInfo.minSpanX;
2061 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302062 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002063 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302064 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002065 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302066 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002067
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002068 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002069 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002070 }
2071
Sunny Goyalff572272014-07-23 13:58:07 -07002072 /**
2073 * Restores a pending widget.
2074 *
2075 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002076 */
Sunny Goyald478c832016-04-01 12:04:16 -07002077 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002078 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2079 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2080 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002081 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002082 }
2083
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002084 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002085 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002086 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2087 info.pendingItemInfo = null;
2088 }
Sunny Goyalff572272014-07-23 13:58:07 -07002089
Sunny Goyalba47faa2017-10-04 16:50:57 -07002090 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002091 view.reInflate();
Sunny Goyalba47faa2017-10-04 16:50:57 -07002092 }
2093
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002094 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002095 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002096 }
2097
Adam Cohen1462de32012-07-24 22:34:36 -07002098 public void onPageBoundSynchronously(int page) {
Sunny Goyalf4aceab2017-12-06 16:16:31 -08002099 mSynchronouslyBoundPage = page;
Adam Cohen1462de32012-07-24 22:34:36 -07002100 }
2101
Sunny Goyal527c7d32015-08-28 15:19:36 -07002102 @Override
2103 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2104 if (mPendingExecutor != null) {
2105 mPendingExecutor.markCompleted();
2106 }
2107 mPendingExecutor = executor;
Sunny Goyal60180b02018-02-07 12:56:30 -08002108 if (!isInState(ALL_APPS)) {
2109 mAppsView.getAppsStore().setDeferUpdates(true);
2110 mPendingExecutor.execute(() -> mAppsView.getAppsStore().setDeferUpdates(false));
2111 }
2112
Sunny Goyal527c7d32015-08-28 15:19:36 -07002113 executor.attachTo(this);
2114 }
2115
2116 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2117 if (mPendingExecutor == executor) {
2118 mPendingExecutor = null;
2119 }
2120 }
2121
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002122 @Override
2123 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002124 if (mDragLayer.getAlpha() < 1) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002125 mDragLayer.animate().alpha(1).withEndAction(
2126 executor == null ? null : executor::onLoadAnimationCompleted).start();
2127 } else if (executor != null) {
2128 executor.onLoadAnimationCompleted();
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002129 }
2130 }
2131
Joe Onorato9c1289c2009-08-17 11:03:03 -04002132 /**
2133 * Callback saying that there aren't any more items to bind.
2134 *
2135 * Implementation of the method from LauncherModel.Callbacks.
2136 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002137 public void finishBindingItems() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002138 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002139 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002140
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002141 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002142
Sunny Goyal2100c782016-08-22 16:00:03 -07002143 if (mPendingActivityResult != null) {
2144 handleActivityResult(mPendingActivityResult.requestCode,
2145 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2146 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002147 }
2148
Sunny Goyala474a9b2017-05-04 16:47:11 -07002149 InstallShortcutReceiver.disableAndFlushInstallQueue(
2150 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002151
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002152 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002153 }
2154
Winson Chunga2413752012-04-03 14:22:34 -07002155 private boolean canRunNewAppsAnimation() {
2156 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002157 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002158 }
2159
Winson Chungc9168342013-06-26 14:54:55 -07002160 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002161 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07002162 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
2163 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002164 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002165 return bounceAnim;
2166 }
2167
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002168 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002169 * Add the icons for all apps.
2170 *
2171 * Implementation of the method from LauncherModel.Callbacks.
2172 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002173 public void bindAllApplications(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002174 mAppsView.getAppsStore().setApps(apps);
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002175
Adam Cohen9211d422014-10-07 18:14:53 -07002176 if (mLauncherCallbacks != null) {
2177 mLauncherCallbacks.bindAllApplications(apps);
2178 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002179 }
2180
2181 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002182 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
2183 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2184 */
2185 @Override
2186 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002187 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002188 }
2189
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002190 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002191 * A package was updated.
2192 *
2193 * Implementation of the method from LauncherModel.Callbacks.
2194 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002195 @Override
2196 public void bindAppsAddedOrUpdated(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002197 mAppsView.getAppsStore().addOrUpdateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002198 }
2199
Sunny Goyal4390ace2014-10-13 11:33:11 -07002200 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002201 public void bindPromiseAppProgressUpdated(PromiseAppInfo app) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002202 mAppsView.getAppsStore().updatePromiseAppProgress(app);
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002203 }
2204
2205 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002206 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002207 mWorkspace.widgetsRestored(widgets);
2208 }
2209
Joe Onorato9c1289c2009-08-17 11:03:03 -04002210 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002211 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002212 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002213 *
2214 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002215 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002216 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002217 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002218 if (!updated.isEmpty()) {
2219 mWorkspace.updateShortcuts(updated);
2220 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002221 }
2222
2223 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002224 * Update the state of a package, typically related to install state.
2225 *
2226 * Implementation of the method from LauncherModel.Callbacks.
2227 */
Sunny Goyale755d462014-07-22 13:48:29 -07002228 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002229 public void bindRestoreItemsChange(HashSet<ItemInfo> updates) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07002230 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07002231 }
2232
2233 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002234 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07002235 * in addition to specific applications to remove, the reason being that
2236 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002237 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07002238 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002239 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07002240 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002241 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002242 mWorkspace.removeItemsByMatcher(matcher);
2243 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002244 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002245
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002246 @Override
2247 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002248 mAppsView.getAppsStore().removeApps(appInfos);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002249 }
Joe Onoratobe386092009-11-17 17:32:16 -08002250
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002251 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002252 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
2253 mPopupDataProvider.setAllWidgets(allWidgets);
Tony Wickham26b17462017-03-20 17:12:24 -07002254 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
2255 if (topView != null) {
2256 topView.onWidgetsBound();
2257 }
2258 }
2259
Tony Wickham86222d22017-03-29 15:30:43 -07002260 /**
2261 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
2262 * refreshes the widgets and shortcuts associated with the given package/user
2263 */
2264 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07002265 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002266 }
2267
Winson Chung80baf5a2010-08-09 16:03:15 -07002268 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002269 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08002270 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002271 @Override
2272 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
2273 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002274
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002275 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
2276 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07002277 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002278 writer.println(prefix + " Homescreen " + i);
2279
2280 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
2281 for (int j = 0; j < layout.getChildCount(); j++) {
2282 Object tag = layout.getChildAt(j).getTag();
2283 if (tag != null) {
2284 writer.println(prefix + " " + tag.toString());
2285 }
2286 }
2287 }
2288
2289 writer.println(prefix + " Hotseat");
2290 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07002291 for (int j = 0; j < layout.getChildCount(); j++) {
2292 Object tag = layout.getChildAt(j).getTag();
2293 if (tag != null) {
2294 writer.println(prefix + " " + tag.toString());
2295 }
Adam Cohen4caf2982013-08-20 18:54:31 -07002296 }
Sunny Goyala1365452015-10-01 15:46:24 -07002297 }
2298
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002299 writer.println(prefix + "Misc:");
2300 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
2301 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
2302 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
Sunny Goyal2bba1902018-02-27 12:20:50 -08002303 writer.println(" deviceProfile isTransposed=" + getDeviceProfile().isVerticalBarLayout());
2304 writer.println(" orientation=" + getResources().getConfiguration().orientation);
2305
2306 try {
2307 FileLog.flushAll(writer);
2308 } catch (Exception e) {
2309 // Ignore
2310 }
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002311
2312 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002313
Adam Cohen9211d422014-10-07 18:14:53 -07002314 if (mLauncherCallbacks != null) {
2315 mLauncherCallbacks.dump(prefix, fd, writer, args);
2316 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002317 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002318
Sunny Goyal66b24572016-09-21 15:57:55 -07002319 @Override
2320 @TargetApi(Build.VERSION_CODES.N)
2321 public void onProvideKeyboardShortcuts(
2322 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
2323
2324 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002325 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002326 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
2327 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
2328 }
2329 View currentFocus = getCurrentFocus();
2330 if (new CustomActionsPopup(this, currentFocus).canShow()) {
2331 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
2332 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
2333 }
Tony18c4aa42017-05-10 21:23:57 -05002334 if (currentFocus.getTag() instanceof ItemInfo
2335 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002336 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
2337 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
2338 }
2339 if (!shortcutInfos.isEmpty()) {
2340 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
2341 }
2342
2343 super.onProvideKeyboardShortcuts(data, menu, deviceId);
2344 }
2345
2346 @Override
2347 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
2348 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
2349 switch (keyCode) {
2350 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002351 if (isInState(NORMAL)) {
2352 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07002353 return true;
2354 }
2355 break;
2356 case KeyEvent.KEYCODE_S: {
2357 View focusedView = getCurrentFocus();
2358 if (focusedView instanceof BubbleTextView
2359 && focusedView.getTag() instanceof ItemInfo
2360 && mAccessibilityDelegate.performAction(focusedView,
2361 (ItemInfo) focusedView.getTag(),
2362 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal00ac9202017-11-09 15:24:06 -08002363 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07002364 return true;
2365 }
2366 break;
2367 }
2368 case KeyEvent.KEYCODE_O:
2369 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
2370 return true;
2371 }
2372 break;
2373 }
2374 }
2375 return super.onKeyShortcut(keyCode, event);
2376 }
2377
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002378 @Override
2379 public boolean onKeyUp(int keyCode, KeyEvent event) {
2380 if (keyCode == KeyEvent.KEYCODE_MENU) {
2381 // KEYCODE_MENU is sent by some tests, for example
2382 // LauncherJankTests#testWidgetsContainerFling. Don't just remove its handling.
2383 if (!mDragController.isDragging() && !mWorkspace.isSwitchingState() &&
2384 isInState(NORMAL)) {
2385 // Close any open floating views.
2386 AbstractFloatingView.closeAllOpenViews(this);
2387
2388 // Setting the touch point to (-1, -1) will show the options popup in the center of
2389 // the screen.
Sunny Goyal2fd7a8b2018-03-30 17:10:13 -07002390 OptionsPopupView.showDefaultOptions(this, -1, -1);
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002391 }
2392 return true;
2393 }
2394 return super.onKeyUp(keyCode, event);
2395 }
2396
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002397 public static Launcher getLauncher(Context context) {
2398 if (context instanceof Launcher) {
2399 return (Launcher) context;
2400 }
2401 return ((Launcher) ((ContextWrapper) context).getBaseContext());
2402 }
2403
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002404 /**
2405 * Callback for listening for onResume
2406 */
2407 public interface OnResumeCallback {
2408
2409 void onLauncherResume();
2410 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002411}