blob: 0eeec70f0f0fa896fbb2c113da78dedbdda139bd [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080019import static android.content.pm.ActivityInfo.CONFIG_ORIENTATION;
20import static android.content.pm.ActivityInfo.CONFIG_SCREEN_SIZE;
Vadim Tryshev6d2321c2018-04-11 17:19:08 -070021
Sunny Goyal4c7f2152017-10-17 17:17:16 -070022import static com.android.launcher3.LauncherAnimUtils.SPRING_LOADED_EXIT_DELAY;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -070023import static com.android.launcher3.LauncherState.ALL_APPS;
24import static com.android.launcher3.LauncherState.NORMAL;
Sunny Goyalaeb16432017-10-16 11:46:41 -070025import static com.android.launcher3.logging.LoggerUtils.newContainerTarget;
Sunny Goyal326403e2017-10-02 12:45:10 -070026
Gilles Debunnedd6c9922010-10-25 11:23:41 -070027import android.animation.Animator;
Jon Miranda2d89ea82017-05-04 11:47:53 -070028import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070029import android.animation.AnimatorSet;
Jon Miranda2d89ea82017-05-04 11:47:53 -070030import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070031import android.animation.ValueAnimator;
Adam Cohen0b395352014-06-09 22:54:36 +000032import android.annotation.TargetApi;
Tony Wickham005df0b2018-02-13 11:28:12 -080033import android.app.ActivityOptions;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080034import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070035import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080037import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080038import android.content.ComponentCallbacks2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.Context;
Andrew Sappersteinabef55a2016-06-19 12:49:00 -070040import android.content.ContextWrapper;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070042import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070043import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070044import android.content.SharedPreferences;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.content.pm.PackageManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080046import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070047import android.database.sqlite.SQLiteDatabase;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070048import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020049import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.os.Bundle;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070051import android.os.Parcelable;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080052import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070053import android.os.StrictMode;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070054import android.os.UserHandle;
Tony Wickham86222d22017-03-29 15:30:43 -070055import android.support.annotation.Nullable;
Michael Jurkaa33411c2012-06-14 16:18:21 -070056import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070058import android.util.Log;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070059import android.util.SparseArray;
Adam Cohen96d30a12013-07-16 18:13:21 -070060import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070061import android.view.KeyboardShortcutGroup;
62import android.view.KeyboardShortcutInfo;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080063import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -070064import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070065import android.view.View;
Adam Cohen96d30a12013-07-16 18:13:21 -070066import android.view.ViewGroup;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080067import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070068import android.view.animation.OvershootInterpolator;
Adam Cohen96d30a12013-07-16 18:13:21 -070069import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070070
Sunny Goyal651077b2014-06-30 14:15:31 -070071import com.android.launcher3.DropTarget.DragObject;
Jon Mirandac476d6e2017-05-04 16:30:01 -070072import com.android.launcher3.Workspace.ItemOperator;
Sunny Goyalae502842016-06-17 08:43:56 -070073import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070074import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070075import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal7ede6112017-12-05 15:11:21 -080076import com.android.launcher3.allapps.DiscoveryBounce;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070077import com.android.launcher3.badge.BadgeInfo;
Sunny Goyalffe83f12014-08-14 17:39:34 -070078import com.android.launcher3.compat.AppWidgetManagerCompat;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -070079import com.android.launcher3.compat.LauncherAppsCompatVO;
Tony Wickhame0c33232016-02-08 11:37:04 -080080import com.android.launcher3.config.FeatureFlags;
Vadim Tryshevfedca432015-08-19 17:55:02 -070081import com.android.launcher3.dragndrop.DragController;
82import com.android.launcher3.dragndrop.DragLayer;
83import com.android.launcher3.dragndrop.DragView;
Sunny Goyal26119432016-02-18 22:09:23 +000084import com.android.launcher3.folder.FolderIcon;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070085import com.android.launcher3.folder.FolderIconPreviewVerifier;
Sunny Goyal66b24572016-09-21 15:57:55 -070086import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -070087import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -070088import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -070089import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyala535ae42017-02-27 10:07:13 -080090import com.android.launcher3.model.ModelWriter;
Sunny Goyala535ae42017-02-27 10:07:13 -080091import com.android.launcher3.notification.NotificationListener;
Tony Wickham540913e2017-01-23 11:47:51 -080092import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -080093import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -070094import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyalf8088ee2017-11-10 14:52:00 -080095import com.android.launcher3.states.InternalStateHandler;
Sunny Goyal623eddd2018-03-02 12:24:41 -080096import com.android.launcher3.states.RotationHelper;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -080097import com.android.launcher3.touch.ItemClickHandler;
Sunny Goyala5ace712017-11-15 17:12:51 -080098import com.android.launcher3.uioverrides.UiFactory;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +053099import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
100import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
Sunny Goyalaeb16432017-10-16 11:46:41 -0700101import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyal2100c782016-08-22 16:00:03 -0700102import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700103import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700104import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700105import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700106import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800107import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700108import com.android.launcher3.util.PendingRequestArgs;
Sunny Goyal8392c822017-06-20 10:03:56 -0700109import com.android.launcher3.util.SystemUiController;
Mario Bertschlera6936942017-05-31 14:48:19 -0700110import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700111import com.android.launcher3.util.Thunk;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700112import com.android.launcher3.util.TraceHelper;
Sunny Goyal326403e2017-10-02 12:45:10 -0700113import com.android.launcher3.util.UiThreadHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700114import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800115import com.android.launcher3.views.OptionsPopupView;
Sunny Goyal29947f02017-12-18 13:49:44 -0800116import com.android.launcher3.widget.LauncherAppWidgetHostView;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800117import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700118import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal29947f02017-12-18 13:49:44 -0800119import com.android.launcher3.widget.PendingAppWidgetHostView;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800120import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700121import com.android.launcher3.widget.WidgetHostViewLoader;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700122import com.android.launcher3.widget.WidgetListRowEntry;
123import com.android.launcher3.widget.WidgetsFullSheet;
Sunny Goyal952e63d2017-08-16 04:59:08 -0700124import com.android.launcher3.widget.custom.CustomWidgetParser;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700125
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700126import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700127import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700128import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700129import java.util.Collection;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700130import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700131import java.util.List;
Tony Wickham010d2552017-01-20 08:15:28 -0800132import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700133
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134/**
135 * Default launcher application.
136 */
Sunny Goyalab837732018-03-27 17:35:54 -0700137public class Launcher extends BaseDraggingActivity
138 implements LauncherExterns, LauncherModel.Callbacks, LauncherProviderChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700139 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700140 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141
Winson Chunga2413752012-04-03 14:22:34 -0700142 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700143
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700145 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700146
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700147 private static final int REQUEST_PICK_APPWIDGET = 9;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148
Michael Jurka8b805b12012-04-18 14:23:14 -0700149 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800150 public static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
151 public static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700152
Jon Mirandac476d6e2017-05-04 16:30:01 -0700153 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700154
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700155 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
156
Mathew Inwood876a8462013-06-14 14:12:41 +0100157 /**
158 * IntentStarter uses request codes starting with this. This must be greater than all activity
159 * request codes used internally.
160 */
161 protected static final int REQUEST_LAST = 100;
162
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 // Type: int
164 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700165 // Type: int
166 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700167 // Type: PendingRequestArgs
168 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
169 // Type: ActivityResultInfo
170 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700171 // Type: SparseArray<Parcelable>
172 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700174 private LauncherStateManager mStateManager;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700175
Adam Cohenad4e15c2013-10-17 16:21:35 -0700176 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177
Winson Chunga2413752012-04-03 14:22:34 -0700178 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700179 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
180 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
181 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700182
Jon Miranda54441f52018-01-24 15:38:25 -0800183 private LauncherAppTransitionManager mAppTransitionManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800184 private Configuration mOldConfig;
Jon Miranda54441f52018-01-24 15:38:25 -0800185
Adam Cohen091440a2015-03-18 14:16:05 -0700186 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700187 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700188 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800189 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700190
Sunny Goyalffe83f12014-08-14 17:39:34 -0700191 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700192 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700193
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700194 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700195
Sunny Goyal316490e2015-06-02 09:38:28 -0700196 @Thunk Hotseat mHotseat;
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700197 private View mDragHandleIndicator;
198 @Nullable private View mHotseatSearchBox;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700199
Sunny Goyal47328fd2016-05-25 18:56:41 -0700200 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700201
202 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700203 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700204 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700205
Winson Chung8ae41982017-11-10 11:42:13 -0800206 // UI and state for the overview panel
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800207 private View mOverviewPanel;
Winson Chung8ae41982017-11-10 11:42:13 -0800208
Vadim Tryshev6d2321c2018-04-11 17:19:08 -0700209 private View mOverviewPanelContainer;
210
Adam Cohen091440a2015-03-18 14:16:05 -0700211 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400212
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700213 private OnResumeCallback mOnResumeCallback;
214
Sunny Goyal527c7d32015-08-28 15:19:36 -0700215 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700216
Joe Onorato9c1289c2009-08-17 11:03:03 -0400217 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800218 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800219 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700220 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400221
Jon Miranda2d89ea82017-05-04 11:47:53 -0700222 private ObjectAnimator mScrimAnimator;
Jon Miranda7fda2852017-07-19 16:07:01 -0700223 private boolean mShouldFadeInScrim;
Jon Miranda2d89ea82017-05-04 11:47:53 -0700224
Tony Wickham010d2552017-01-20 08:15:28 -0800225 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700226
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800227 private int mSynchronouslyBoundPage = PagedView.INVALID_PAGE;
Adam Cohen1462de32012-07-24 22:34:36 -0700228
Winson Chung46353de2012-02-16 14:05:10 -0800229 // We only want to get the SharedPreferences once since it does an FS stat each time we get
230 // it from the context.
231 private SharedPreferences mSharedPrefs;
232
Sunny Goyal2100c782016-08-22 16:00:03 -0700233 // Activity result which needs to be processed after workspace has loaded.
234 private ActivityResultInfo mPendingActivityResult;
235 /**
236 * Holds extra information required to handle a result from an external call, like
237 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
238 */
239 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800240
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700241 public ViewGroupFocusHelper mFocusHandler;
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700242 private boolean mAppLaunchSuccess;
243
Sunny Goyal623eddd2018-03-02 12:24:41 -0800244 private RotationHelper mRotationHelper;
Sunny Goyal7779d622015-06-11 16:18:39 -0700245
Winson Chungedb0d6b2018-03-21 17:32:55 -0700246 // Used to keep track of the swipe up state
247 private SharedPreferences.OnSharedPreferenceChangeListener mSharedPrefsListener =
248 (sharedPreferences, s) -> {
249 mDragLayer.setup(mDragController);
250 };
251
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800252 @Override
253 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700254 if (DEBUG_STRICT_MODE) {
255 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
256 .detectDiskReads()
257 .detectDiskWrites()
258 .detectNetwork() // or .detectAll() for all detectable problems
259 .penaltyLog()
260 .build());
261 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
262 .detectLeakedSqlLiteObjects()
263 .detectLeakedClosableObjects()
264 .penaltyLog()
265 .penaltyDeath()
266 .build());
267 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700268 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700269
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800270 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700271 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400272
Sunny Goyal87f784c2017-01-11 10:48:34 -0800273 LauncherAppState app = LauncherAppState.getInstance(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800274 mOldConfig = new Configuration(getResources().getConfiguration());
Sunny Goyal0e7724c2018-02-22 13:22:21 -0800275 mModel = app.setLauncher(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800276 initDeviceProfile(app.getInvariantDeviceProfile());
Winson Chung6e1c0d32013-10-25 15:24:24 -0700277
Sunny Goyalf7258242015-10-19 16:59:07 -0700278 mSharedPrefs = Utilities.getPrefs(this);
Winson Chungedb0d6b2018-03-21 17:32:55 -0700279 mSharedPrefs.registerOnSharedPreferenceChangeListener(mSharedPrefsListener);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800280 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700281 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700282
Joe Onorato41a12d22009-10-31 18:30:00 -0400283 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700284 mAllAppsController = new AllAppsTransitionController(this);
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800285 mStateManager = new LauncherStateManager(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700286
Sunny Goyalffe83f12014-08-14 17:39:34 -0700287 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700288
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700289 mAppWidgetHost = new LauncherAppWidgetHost(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700290 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700291
Sunny Goyal60820d72017-05-09 12:40:11 -0700292 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700293
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800294 setupViews();
Tony Wickham010d2552017-01-20 08:15:28 -0800295 mPopupDataProvider = new PopupDataProvider(this);
296
Sunny Goyal623eddd2018-03-02 12:24:41 -0800297 mRotationHelper = new RotationHelper(this);
Sunny Goyal90548432018-04-04 17:17:00 -0700298 mAppTransitionManager = LauncherAppTransitionManager.newInstance(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800299
Sunny Goyald3864fa2017-11-14 15:06:36 -0800300 boolean internalStateHandled = InternalStateHandler.handleCreate(this, getIntent());
301 if (internalStateHandled) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800302 if (savedInstanceState != null) {
303 // InternalStateHandler has already set the appropriate state.
304 // We dont need to do anything.
305 savedInstanceState.remove(RUNTIME_STATE);
306 }
307 }
308 restoreState(savedInstanceState);
Winson Chungb63b44c2017-11-10 17:54:44 -0800309
Sunny Goyal2100c782016-08-22 16:00:03 -0700310 // We only load the page synchronously if the user rotates (or triggers a
311 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700312 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
313 if (savedInstanceState != null) {
314 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
315 }
Sunny Goyald3864fa2017-11-14 15:06:36 -0800316
Sunny Goyalfe770c92016-09-27 14:38:58 -0700317 if (!mModel.startLoader(currentScreen)) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800318 if (!internalStateHandled) {
319 // If we are not binding synchronously, show a fade in animation when
320 // the first page bind completes.
321 mDragLayer.setAlpha(0);
322 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700323 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700324 // Pages bound synchronously.
325 mWorkspace.setCurrentPage(currentScreen);
326
Sunny Goyal2100c782016-08-22 16:00:03 -0700327 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800328 }
329
330 // For handling default keys
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800331 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800332
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800333 setContentView(mLauncherView);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800334 getRootView().dispatchInsets();
Mario Bertschlera6936942017-05-31 14:48:19 -0700335
Jon Miranda7fda2852017-07-19 16:07:01 -0700336 // Listen for broadcasts
337 IntentFilter filter = new IntentFilter();
338 filter.addAction(Intent.ACTION_SCREEN_OFF);
339 filter.addAction(Intent.ACTION_USER_PRESENT); // When the device wakes up + keyguard is gone
340 registerReceiver(mReceiver, filter);
341 mShouldFadeInScrim = true;
Sunny Goyala616d2b2017-06-12 13:54:01 -0700342
Sunny Goyal8392c822017-06-20 10:03:56 -0700343 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
344 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700345
346 if (mLauncherCallbacks != null) {
347 mLauncherCallbacks.onCreate(savedInstanceState);
348 }
Sunny Goyal623eddd2018-03-02 12:24:41 -0800349 mRotationHelper.initialize();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700350
351 TraceHelper.endSection("Launcher-onCreate");
Adam Cohen9211d422014-10-07 18:14:53 -0700352 }
353
Sunny Goyal7779d622015-06-11 16:18:39 -0700354 @Override
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800355 public void onConfigurationChanged(Configuration newConfig) {
356 int diff = newConfig.diff(mOldConfig);
357 if ((diff & (CONFIG_ORIENTATION | CONFIG_SCREEN_SIZE)) != 0) {
358 mUserEventDispatcher = null;
359 initDeviceProfile(mDeviceProfile.inv);
Sunny Goyalfde55052018-02-01 14:46:13 -0800360 dispatchDeviceProfileChanged();
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800361
362 getRootView().dispatchInsets();
363 getStateManager().reapplyState();
364
Sunny Goyal7185dd62018-03-14 17:51:49 -0700365 // Recreate touch controllers
366 mDragLayer.setup(mDragController);
367
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800368 // TODO: We can probably avoid rebind when only screen size changed.
Sunny Goyal605bcf32018-03-02 15:16:12 -0800369 rebindModel();
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800370 }
371
372 mOldConfig.setTo(newConfig);
Tracy Zhoua706f002018-03-28 13:55:19 -0700373 UiFactory.onLauncherStateOrResumeChanged(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800374 super.onConfigurationChanged(newConfig);
375 }
376
Sunny Goyal605bcf32018-03-02 15:16:12 -0800377 @Override
378 public void rebindModel() {
379 int currentPage = mWorkspace.getNextPage();
380 if (mModel.startLoader(currentPage)) {
381 mWorkspace.setCurrentPage(currentPage);
382 setWorkspaceLoading(true);
383 }
384 }
385
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800386 private void initDeviceProfile(InvariantDeviceProfile idp) {
387 // Load configuration-specific DeviceProfile
Sunny Goyalf633ef52018-03-13 09:57:05 -0700388 setDeviceProfile(idp.getDeviceProfile(this));
Sunny Goyal605bcf32018-03-02 15:16:12 -0800389 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout(), true);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800390 }
391
Sunny Goyal623eddd2018-03-02 12:24:41 -0800392 public RotationHelper getRotationHelper() {
393 return mRotationHelper;
394 }
395
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700396 public LauncherStateManager getStateManager() {
397 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700398 }
399
Mario Bertschler27288382017-05-24 15:35:09 -0700400 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700401 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800402 return mLauncherView.findViewById(id);
403 }
404
405 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700406 public void onAppWidgetHostReset() {
407 if (mAppWidgetHost != null) {
408 mAppWidgetHost.startListening();
409 }
410 }
411
Adam Cohen9211d422014-10-07 18:14:53 -0700412 private LauncherCallbacks mLauncherCallbacks;
413
Sunny Goyal32554d12015-12-03 15:31:25 -0800414 /**
415 * Call this after onCreate to set or clear overlay.
416 */
417 public void setLauncherOverlay(LauncherOverlay overlay) {
418 if (overlay != null) {
419 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
420 }
421 mWorkspace.setLauncherOverlay(overlay);
422 }
423
Adam Cohen9211d422014-10-07 18:14:53 -0700424 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
425 mLauncherCallbacks = callbacks;
426 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700427 }
428
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700429 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700430 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700431 if (mLauncherCallbacks != null) {
432 mLauncherCallbacks.onLauncherProviderChange();
433 }
434 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700435
Hyunyoung Song3f471442015-04-08 19:01:34 -0700436 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700437 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
438 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700439 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700440 }
441
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000442 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700443 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
444 // This cast is safe as long as the id < 0x00FFFFFF
445 // Since we jail all the dynamically generated views, there should be no clashes
446 // with any other views.
447 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000448 }
449
Tony Wickham010d2552017-01-20 08:15:28 -0800450 public PopupDataProvider getPopupDataProvider() {
451 return mPopupDataProvider;
452 }
453
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700454 @Override
455 public BadgeInfo getBadgeInfoForItem(ItemInfo info) {
456 return mPopupDataProvider.getBadgeInfoForItem(info);
457 }
458
459 @Override
460 public void invalidateParent(ItemInfo info) {
461 FolderIconPreviewVerifier verifier = new FolderIconPreviewVerifier(getDeviceProfile().inv);
462 if (verifier.isItemInPreview(info.rank) && (info.container >= 0)) {
463 View folderIcon = getWorkspace().getHomescreenIconByItemId(info.container);
464 if (folderIcon != null) {
465 folderIcon.invalidate();
466 }
467 }
468 }
469
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000470 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700471 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
472 * a configuration step, this allows the proper animations to run after other transitions.
473 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700474 private long completeAdd(
475 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
476 long screenId = info.screenId;
477 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700478 // When the screen id represents an actual screen (as opposed to a rank) we make sure
479 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700480 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700481 }
Adam Cohendb364c32014-05-20 14:23:40 -0700482
Sunny Goyal2100c782016-08-22 16:00:03 -0700483 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800484 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700485 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700486 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800487 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700488 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700489 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700490 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700491 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700492 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700493 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700494 int widgetId = appWidgetId;
495 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700496 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700497 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700498 // Since the view was just bound, also launch the configure activity if needed
499 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
500 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800501 if (provider != null) {
502 new WidgetAddFlowHandler(provider)
503 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700504 }
505 }
506 break;
507 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800508 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700509
Adam Cohendb364c32014-05-20 14:23:40 -0700510 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800511 }
512
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800513 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700514 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700515 if (isWorkspaceLoading()) {
516 // process the result once the workspace has loaded.
517 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
518 return;
519 }
520 mPendingActivityResult = null;
521
Winson Chunge341d302013-08-16 14:31:00 -0700522 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700523 final PendingRequestArgs requestArgs = mPendingRequestArgs;
524 setWaitingForResult(null);
525 if (requestArgs == null) {
526 return;
527 }
528
529 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700530
Adam Cohenad4e15c2013-10-17 16:21:35 -0700531 Runnable exitSpringLoaded = new Runnable() {
532 @Override
533 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700534 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700535 }
536 };
537
Michael Jurka8b805b12012-04-18 14:23:14 -0700538 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700539 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700540 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700541 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
542 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700543 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700544 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700545 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700546 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700547 addAppWidgetImpl(
548 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800549 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700550 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700551 }
552 return;
553 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200554
Adam Cohened66b2b2012-01-23 17:28:51 -0800555 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700556 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700557
Adam Cohened66b2b2012-01-23 17:28:51 -0800558 // We have special handling for widgets
559 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800560 final int appWidgetId;
561 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
562 : -1;
563 if (widgetId < 0) {
564 appWidgetId = pendingAddWidgetId;
565 } else {
566 appWidgetId = widgetId;
567 }
568
Adam Cohenad4e15c2013-10-17 16:21:35 -0700569 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800570 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700571 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
572 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700573 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700574 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700575 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700576 @Override
577 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700578 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700579 }
580 };
Adam Cohendb364c32014-05-20 14:23:40 -0700581
Sunny Goyal2100c782016-08-22 16:00:03 -0700582 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
583 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
584 } else {
585 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
586 // When the screen id represents an actual screen (as opposed to a rank)
587 // we make sure that the drop page actually exists.
588 requestArgs.screenId =
589 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700590 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700591 final CellLayout dropLayout =
592 mWorkspace.getScreenWithId(requestArgs.screenId);
593
594 dropLayout.setDropPending(true);
595 final Runnable onComplete = new Runnable() {
596 @Override
597 public void run() {
598 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
599 dropLayout.setDropPending(false);
600 }
601 };
602 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
603 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700604 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800605 return;
606 }
607
Sunny Goyald478c832016-04-01 12:04:16 -0700608 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
609 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700610 if (resultCode == RESULT_OK) {
611 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700612 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700613 }
614 // Leave the widget in the pending state if the user canceled the configure.
615 return;
616 }
617
Sunny Goyalaad90582015-07-09 14:22:50 -0700618 if (requestCode == REQUEST_CREATE_SHORTCUT) {
619 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700620 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
621 completeAdd(requestCode, data, -1, requestArgs);
622 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
623 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
624
Sunny Goyalaad90582015-07-09 14:22:50 -0700625 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700626 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
627 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800628 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800629 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800630 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800631 }
632
633 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700634 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800635 final int requestCode, final int resultCode, final Intent data) {
636 handleActivityResult(requestCode, resultCode, data);
637 if (mLauncherCallbacks != null) {
638 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
639 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800640 }
641
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700642 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700643 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600644 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700645 PendingRequestArgs pendingArgs = mPendingRequestArgs;
646 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
647 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
648 setWaitingForResult(null);
649
Sunny Goyal28c6b962015-10-12 11:42:05 -0700650 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700651 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700652 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700653 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700654 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700655 Intent intent = pendingArgs.getPendingIntent();
656
Sunny Goyal28c6b962015-10-12 11:42:05 -0700657 if (grantResults.length > 0
658 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700659 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700660 } else {
661 // TODO: Show a snack bar with link to settings
662 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700663 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700664 }
665 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600666 if (mLauncherCallbacks != null) {
667 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
668 grantResults);
669 }
670 }
671
Adam Cohendb364c32014-05-20 14:23:40 -0700672 /**
673 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
674 *
675 * @param screenId the screen id to check
676 * @return the new screen, or screenId if it exists
677 */
678 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800679 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700680 if (dropLayout == null) {
681 // it's possible that the add screen was removed because it was
682 // empty and a re-bind occurred
683 mWorkspace.addExtraEmptyScreen();
684 return mWorkspace.commitExtraEmptyScreen();
685 } else {
686 return screenId;
687 }
688 }
689
Sunny Goyal2100c782016-08-22 16:00:03 -0700690 @Thunk void completeTwoStageWidgetDrop(
691 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
692 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800693 Runnable onCompleteRunnable = null;
694 int animationType = 0;
695
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700696 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800697 if (resultCode == RESULT_OK) {
698 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
699 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800700 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700701 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800702 onCompleteRunnable = new Runnable() {
703 @Override
704 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700705 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700706 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800707 }
708 };
709 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800710 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800711 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800712 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700713 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700714 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700715 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
716 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700717 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700718 // The animated view may be null in the case of a rotation during widget configuration
719 onCompleteRunnable.run();
720 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800721 }
722
723 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700724 protected void onStop() {
725 super.onStop();
726 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700727
728 if (mLauncherCallbacks != null) {
729 mLauncherCallbacks.onStop();
730 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800731 mAppWidgetHost.setListenIfResumed(false);
Tony Wickham010d2552017-01-20 08:15:28 -0800732
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700733 if (!mAppLaunchSuccess) {
734 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -0800735 mStateManager.getState().containerType, -1);
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700736 }
Tony Wickham010d2552017-01-20 08:15:28 -0800737 NotificationListener.removeNotificationsChangedListener();
Sunny Goyalaf161fd2018-02-05 16:34:57 -0800738 getStateManager().moveToRestState();
Winson Chungedb0d6b2018-03-21 17:32:55 -0700739
Michael Jurkacd496d72013-04-11 11:32:45 -0700740 }
741
742 @Override
743 protected void onStart() {
744 super.onStart();
745 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700746
747 if (mLauncherCallbacks != null) {
748 mLauncherCallbacks.onStart();
749 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800750 mAppWidgetHost.setListenIfResumed(true);
Sunny Goyalcac002d2018-01-04 09:51:45 -0800751 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
Jon Miranda2d89ea82017-05-04 11:47:53 -0700752
Jon Mirandade43a712018-01-18 11:35:10 -0800753 if (mShouldFadeInScrim && mLauncherView.getBackground() != null) {
Jon Miranda2d89ea82017-05-04 11:47:53 -0700754 if (mScrimAnimator != null) {
755 mScrimAnimator.cancel();
756 }
Jon Mirandade43a712018-01-18 11:35:10 -0800757 mLauncherView.getBackground().setAlpha(0);
758 mScrimAnimator = ObjectAnimator.ofInt(mLauncherView.getBackground(),
Jon Miranda2d89ea82017-05-04 11:47:53 -0700759 LauncherAnimUtils.DRAWABLE_ALPHA, 0, 255);
760 mScrimAnimator.addListener(new AnimatorListenerAdapter() {
761 @Override
762 public void onAnimationEnd(Animator animation) {
763 mScrimAnimator = null;
764 }
765 });
766 mScrimAnimator.setDuration(600);
767 mScrimAnimator.setStartDelay(getWindow().getTransitionBackgroundFadeDuration());
768 mScrimAnimator.start();
769 }
Jon Miranda7fda2852017-07-19 16:07:01 -0700770 mShouldFadeInScrim = false;
Winson Chung62a70be2018-02-23 15:10:05 -0800771 UiFactory.onStart(this);
Michael Jurkacd496d72013-04-11 11:32:45 -0700772 }
773
774 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800775 protected void onResume() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700776 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800777 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700778 TraceHelper.partitionSection("ON_RESUME", "superCall");
779
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700780 mAppLaunchSuccess = false;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700781 getUserEventDispatcher().resetElapsedSessionMillis();
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700782 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700783 // Process any items that were added while Launcher was away.
784 InstallShortcutReceiver.disableAndFlushInstallQueue(
785 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700786
Sunny Goyala474a9b2017-05-04 16:47:11 -0700787 // Refresh shortcuts if the permission changed.
788 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700789
Tracy Zhoue5575f92018-04-19 11:12:53 -0700790 DiscoveryBounce.showForHomeIfNeeded(this);
Adam Cohen9211d422014-10-07 18:14:53 -0700791 if (mLauncherCallbacks != null) {
792 mLauncherCallbacks.onResume();
793 }
Tracy Zhoua706f002018-03-28 13:55:19 -0700794 UiFactory.onLauncherStateOrResumeChanged(this);
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800795
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700796 TraceHelper.endSection("ON_RESUME");
Adam Cohen06dff352012-06-01 17:17:08 -0700797 }
798
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800799 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700800 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700801 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700802 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700803
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700804 super.onPause();
Joe Onorato24b6fd82009-11-12 13:47:09 -0800805 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700806 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700807
Adam Cohen9211d422014-10-07 18:14:53 -0700808 if (mLauncherCallbacks != null) {
809 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700810 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700811 }
812
Winson Chungd7823942018-02-16 03:23:47 +0000813 @Override
Tracy Zhoua706f002018-03-28 13:55:19 -0700814 protected void onUserLeaveHint() {
815 super.onUserLeaveHint();
816 UiFactory.onLauncherStateOrResumeChanged(this);
817 }
818
819 @Override
Winson Chungd7823942018-02-16 03:23:47 +0000820 public void onWindowFocusChanged(boolean hasFocus) {
821 super.onWindowFocusChanged(hasFocus);
822 mStateManager.onWindowFocusChanged();
823 }
824
Adam Cohenc2d6e892014-10-16 09:49:24 -0700825 public interface LauncherOverlay {
826
827 /**
828 * Touch interaction leading to overscroll has begun
829 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700830 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700831
832 /**
833 * Touch interaction related to overscroll has ended
834 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700835 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700836
837 /**
838 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
839 * screen (or in the case of RTL, the rightmost screen).
840 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700841 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700842
843 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800844 * Called when the launcher is ready to use the overlay
845 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700846 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700847 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700848 }
849
850 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700851 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700852 }
853
854 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
855
Sunny Goyalc86df472016-02-25 09:19:38 -0800856 public void onScrollChanged(float progress) {
857 if (mWorkspace != null) {
858 mWorkspace.onOverlayScrollChanged(progress);
859 }
860 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700861 }
862
Sunny Goyal16764582017-11-06 16:00:34 -0800863 public boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -0700864 if (mLauncherCallbacks != null) {
865 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700866 } else {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800867 // On O and above we there is always some setting present settings (add icon to
868 // home screen or icon badging). On earlier APIs we will have the allow rotation
869 // setting, on devices with a locked orientation,
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700870 return Utilities.ATLEAST_OREO || !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -0700871 }
Jorim Jaggid017f882014-01-14 17:08:48 -0800872 }
873
Sunny Goyalf9403d92017-10-18 10:55:56 -0700874 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -0700875 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700876 }
877
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800878 /**
879 * Restores the previous state, if it exists.
880 *
881 * @param savedState The previous state.
882 */
883 private void restoreState(Bundle savedState) {
884 if (savedState == null) {
885 return;
886 }
887
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700888 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -0700889 LauncherState[] stateValues = LauncherState.values();
890 LauncherState state = stateValues[stateOrdinal];
Sunny Goyalbc683e92017-12-06 10:25:07 -0800891 if (!state.disableRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700892 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800893 }
894
Sunny Goyal2100c782016-08-22 16:00:03 -0700895 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
896 if (requestArgs != null) {
897 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800898 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700899
900 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700901
902 SparseArray<Parcelable> widgetsState =
903 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
904 if (widgetsState != null) {
905 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
906 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800907 }
908
909 /**
910 * Finds all the views we need and configure them properly.
911 */
912 private void setupViews() {
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800913 mDragLayer = findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700914 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -0700915 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700916 mWorkspace.initParentViews(mDragLayer);
Sunny Goyal16764582017-11-06 16:00:34 -0800917 mOverviewPanel = findViewById(R.id.overview_panel);
Vadim Tryshev6d2321c2018-04-11 17:19:08 -0700918 mOverviewPanelContainer = findViewById(R.id.overview_panel_container);
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800919 mHotseat = findViewById(R.id.hotseat);
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700920 mDragHandleIndicator = findViewById(R.id.drag_indicator);
921 mHotseatSearchBox = findViewById(R.id.search_container_hotseat);
Craig Mautner360310b2012-10-26 15:13:08 -0700922
Sunny Goyal784f9c32016-03-23 16:56:54 -0700923 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
924 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
925 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800926
Winson Chung4c98d922011-05-31 16:50:48 -0700927 // Setup the drag layer
Tony Wickhama0068302018-04-11 16:16:11 -0700928 Runnable setupDragLayer = () -> mDragLayer.setup(mDragController);
929 UiFactory.setOnTouchControllersChangedListener(this, setupDragLayer);
930 setupDragLayer.run();
Winson Chung4c98d922011-05-31 16:50:48 -0700931
Hyunyoung Song645764e2016-06-06 14:19:02 -0700932 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -0700933 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
934 // default state, otherwise we will update to the wrong offsets in RTL
935 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700936 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700937 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700938
Tony Wickham34d2c912015-09-11 09:27:58 -0700939 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700940 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700941
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700942 // Setup Apps
943 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -0700944
Winson Chung3d503fb2011-07-13 17:25:49 -0700945 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -0700946 mDragController.setMoveTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700947 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -0400948
Sunny Goyal7185dd62018-03-14 17:51:49 -0700949 mAllAppsController.setupViews(mAppsView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800950 }
951
952 /**
953 * Creates a view representing a shortcut.
954 *
955 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800956 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800957 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700958 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800959 }
960
961 /**
962 * Creates a view representing a shortcut inflated from the specified resource.
963 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800964 * @param parent The group the shortcut belongs to.
965 * @param info The data structure describing the shortcut.
966 *
967 * @return A View inflated from layoutResId.
968 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700969 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -0800970 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
971 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800972 favorite.applyFromShortcutInfo(info);
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800973 favorite.setOnClickListener(ItemClickHandler.INSTANCE);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700974 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800975 return favorite;
976 }
977
978 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -0700979 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800980 *
981 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800982 */
Adam Cohendcd297f2013-06-18 13:13:40 -0700983 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700984 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -0700985 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
986 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700987 return;
988 }
Adam Cohen558baaf2011-08-15 15:22:57 -0700989
Jon Mirandac476d6e2017-05-04 16:30:01 -0700990 int[] cellXY = mTmpAddItemCellCoordinates;
991 CellLayout layout = getCellLayout(container, screenId);
992
Sunny Goyal782f0c92017-01-19 10:27:54 -0800993 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700994 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -0700995 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
996 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -0800997 }
998
999 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001000 // Legacy shortcuts are only supported for primary profile.
1001 info = Process.myUserHandle().equals(args.user)
1002 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001003
Sunny Goyalb57645f2017-03-20 13:57:28 -07001004 if (info == null) {
1005 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1006 return;
1007 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001008 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001009 // The app is trying to add a shortcut without sufficient permissions
1010 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1011 return;
1012 }
1013 }
1014
Jon Mirandac476d6e2017-05-04 16:30:01 -07001015 if (container < 0) {
1016 // Adding a shortcut to the Workspace.
1017 final View view = createShortcut(info);
1018 boolean foundCellSpan = false;
1019 // First we check if we already know the exact location where we want to add this item.
1020 if (cellX >= 0 && cellY >= 0) {
1021 cellXY[0] = cellX;
1022 cellXY[1] = cellY;
1023 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001024
Jon Mirandac476d6e2017-05-04 16:30:01 -07001025 // If appropriate, either create a folder or add to an existing folder
1026 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001027 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001028 return;
1029 }
1030 DragObject dragObject = new DragObject();
1031 dragObject.dragInfo = info;
1032 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1033 true)) {
1034 return;
1035 }
1036 } else {
1037 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1038 }
1039
1040 if (!foundCellSpan) {
1041 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001042 return;
1043 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001044
1045 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1046 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001047 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001048 // Adding a shortcut to a Folder.
Sunny Goyale29897f2017-07-20 10:09:42 -07001049 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001050 if (folderIcon != null) {
1051 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1052 folderInfo.add(info, args.rank, false);
1053 } else {
Sunny Goyale29897f2017-07-20 10:09:42 -07001054 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001055 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001056 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001057 }
1058
Sunny Goyale29897f2017-07-20 10:09:42 -07001059 public FolderIcon findFolderIcon(final long folderIconId) {
1060 return (FolderIcon) mWorkspace.getFirstMatch(new ItemOperator() {
1061 @Override
1062 public boolean evaluate(ItemInfo info, View view) {
1063 return info != null && info.id == folderIconId;
1064 }
1065 });
1066 }
1067
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001068 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001069 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001070 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001071 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001072 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001073 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001074 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1075
Adam Cohened66b2b2012-01-23 17:28:51 -08001076 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001077 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001078 }
Romain Guycbb89e42009-06-08 15:52:54 -07001079
Adam Cohen59400422014-03-05 18:07:04 -08001080 LauncherAppWidgetInfo launcherInfo;
1081 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001082 launcherInfo.spanX = itemInfo.spanX;
1083 launcherInfo.spanY = itemInfo.spanY;
1084 launcherInfo.minSpanX = itemInfo.minSpanX;
1085 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001086 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001087
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001088 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001089 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090
Sunny Goyal2100c782016-08-22 16:00:03 -07001091 if (hostView == null) {
1092 // Perform actual inflation because we're live
1093 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001094 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001095 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301096 prepareAppWidget(hostView, launcherInfo);
1097 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001098 }
Romain Guycbb89e42009-06-08 15:52:54 -07001099
Sunny Goyald5462aa2016-11-22 16:52:39 +05301100 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001101 hostView.setTag(item);
1102 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001103 hostView.setFocusable(true);
1104 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001105 }
1106
Adam Cohended9f8d2010-11-03 13:25:16 -07001107 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1108 @Override
1109 public void onReceive(Context context, Intent intent) {
1110 final String action = intent.getAction();
1111 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001112 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001113 // processing a multi-step drop
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001114 if (mAppsView != null && mPendingRequestArgs == null) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001115 mStateManager.goToState(NORMAL);
Winson Chung785d2eb2011-04-14 16:08:02 -07001116 }
Jon Miranda7fda2852017-07-19 16:07:01 -07001117 mShouldFadeInScrim = true;
1118 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1119 // ACTION_USER_PRESENT is sent after onStart/onResume. This covers the case where
1120 // the user unlocked and the Launcher is not in the foreground.
1121 mShouldFadeInScrim = false;
Adam Cohended9f8d2010-11-03 13:25:16 -07001122 }
1123 }
1124 };
1125
Tony Wickham010d2552017-01-20 08:15:28 -08001126 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
Sunny Goyal29947f02017-12-18 13:49:44 -08001127 mWorkspace.updateIconBadges(updatedBadges);
Sunny Goyal60180b02018-02-07 12:56:30 -08001128 mAppsView.getAppsStore().updateIconBadges(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001129
Sunny Goyal29947f02017-12-18 13:49:44 -08001130 PopupContainerWithArrow popup = PopupContainerWithArrow.getOpen(Launcher.this);
1131 if (popup != null) {
1132 popup.updateNotificationHeader(updatedBadges);
Tony Wickham010d2552017-01-20 08:15:28 -08001133 }
1134 }
1135
Adam Cohended9f8d2010-11-03 13:25:16 -07001136 @Override
1137 public void onAttachedToWindow() {
1138 super.onAttachedToWindow();
1139
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001140 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyalc86df472016-02-25 09:19:38 -08001141 if (mLauncherCallbacks != null) {
1142 mLauncherCallbacks.onAttachedToWindow();
1143 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001144 }
1145
1146 @Override
1147 public void onDetachedFromWindow() {
1148 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001149
1150 if (mLauncherCallbacks != null) {
1151 mLauncherCallbacks.onDetachedFromWindow();
1152 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001153 }
1154
Sunny Goyal3bac2c22018-02-07 14:44:05 -08001155 public void onQuickstepGestureStarted(boolean isVisible) {
1156 if (mLauncherCallbacks != null) {
1157 mLauncherCallbacks.onQuickstepGestureStarted(isVisible);
1158 }
1159 }
1160
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001161 public AllAppsTransitionController getAllAppsController() {
1162 return mAllAppsController;
1163 }
1164
Sunny Goyal9d69c8d2018-03-19 13:41:31 -07001165 @Override
Winson Chung1a341002018-01-17 10:00:23 -08001166 public LauncherRootView getRootView() {
1167 return (LauncherRootView) mLauncherView;
1168 }
1169
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001170 @Override
Winson Chunga6945242014-01-08 14:04:34 -08001171 public DragLayer getDragLayer() {
1172 return mDragLayer;
1173 }
1174
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001175 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001176 return mAppsView;
1177 }
1178
Winson Chunga6945242014-01-08 14:04:34 -08001179 public Workspace getWorkspace() {
1180 return mWorkspace;
1181 }
1182
1183 public Hotseat getHotseat() {
1184 return mHotseat;
1185 }
1186
Sunny Goyal81b4c7b2018-03-26 12:10:31 -07001187 public View getDragHandleIndicator() {
1188 return mDragHandleIndicator;
1189 }
1190
1191 public View getHotseatSearchBox() {
1192 return mHotseatSearchBox;
1193 }
1194
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001195 public <T extends View> T getOverviewPanel() {
Winson Chung8ae41982017-11-10 11:42:13 -08001196 return (T) mOverviewPanel;
Winson Chunga6945242014-01-08 14:04:34 -08001197 }
1198
Vadim Tryshev6d2321c2018-04-11 17:19:08 -07001199 public <T extends View> T getOverviewPanelContainer() {
1200 return (T) mOverviewPanelContainer;
1201 }
1202
Sunny Goyal47328fd2016-05-25 18:56:41 -07001203 public DropTargetBar getDropTargetBar() {
1204 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001205 }
1206
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001207 public LauncherAppWidgetHost getAppWidgetHost() {
1208 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001209 }
Romain Guycbb89e42009-06-08 15:52:54 -07001210
Winson Chunga9abd0e2010-10-27 17:18:37 -07001211 public LauncherModel getModel() {
1212 return mModel;
1213 }
1214
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001215 public ModelWriter getModelWriter() {
1216 return mModelWriter;
1217 }
1218
Adam Cohen79d90c52016-04-22 13:29:20 -07001219 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001220 return mSharedPrefs;
1221 }
1222
Sunny Goyalf8d56fc2018-01-31 15:18:11 -08001223 public int getOrientation() { return mOldConfig.orientation; }
Jon Miranda6bed3502017-09-19 14:43:17 -07001224
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001225 @Override
1226 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001227 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001228 super.onNewIntent(intent);
1229
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001230 boolean alreadyOnHome = hasWindowFocus() && ((intent.getFlags() &
Winson15f8b172015-08-19 11:02:39 -07001231 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1232 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001233
1234 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001235 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001236 && AbstractFloatingView.getTopOpenView(this) == null;
Winson15f8b172015-08-19 11:02:39 -07001237 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001238 boolean internalStateHandled = InternalStateHandler
Sunny Goyal8b264562018-02-03 23:00:13 -08001239 .handleNewIntent(this, intent, isStarted());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001240
Winson15f8b172015-08-19 11:02:39 -07001241 if (isActionMain) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001242 if (!internalStateHandled) {
1243 // Note: There should be at most one log per method call. This is enforced
1244 // implicitly by using if-else statements.
1245 UserEventDispatcher ued = getUserEventDispatcher();
1246 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
1247 if (topOpenView != null) {
1248 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
1249 } else if (alreadyOnHome) {
1250 Target target = newContainerTarget(mStateManager.getState().containerType);
1251 target.pageIndex = mWorkspace.getCurrentPage();
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001252 ued.logActionCommand(Action.Command.HOME_INTENT, target,
1253 newContainerTarget(ContainerType.WORKSPACE));
Sunny Goyald3864fa2017-11-14 15:06:36 -08001254 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001255
Sunny Goyald3864fa2017-11-14 15:06:36 -08001256 // In all these cases, only animate if we're already on home
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001257 AbstractFloatingView.closeAllOpenViews(this, isStarted());
Jon Mirandafeba90f2016-10-06 10:53:29 -07001258
Sunny Goyala2467272018-03-16 14:53:05 -07001259 if (!isInState(NORMAL)) {
1260 // Only change state, if not already the same. This prevents cancelling any
1261 // animations running as part of resume
1262 mStateManager.goToState(NORMAL);
1263 }
Sunny Goyald3864fa2017-11-14 15:06:36 -08001264
1265 // Reset the apps view
1266 if (!alreadyOnHome && mAppsView != null) {
Sunny Goyal7185dd62018-03-14 17:51:49 -07001267 mAppsView.reset(isStarted() /* animate */);
Sunny Goyald3864fa2017-11-14 15:06:36 -08001268 }
1269
1270 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()) {
1271 mWorkspace.post(mWorkspace::moveToDefaultScreen);
1272 }
1273 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001274
1275 final View v = getWindow().peekDecorView();
1276 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001277 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001278 }
1279
Adam Cohen9211d422014-10-07 18:14:53 -07001280 if (mLauncherCallbacks != null) {
1281 mLauncherCallbacks.onHomeIntent();
1282 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001283 }
Winson15f8b172015-08-19 11:02:39 -07001284
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001285 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001286 }
1287
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001288 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001289 public void onRestoreInstanceState(Bundle state) {
1290 super.onRestoreInstanceState(state);
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001291 mWorkspace.restoreInstanceStateForChild(mSynchronouslyBoundPage);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001292 }
1293
1294 @Override
1295 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001296 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001297 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001298
Adam Cohen21cd0022013-10-09 18:57:02 -07001299 }
Sunny Goyalea609262017-10-25 15:47:38 -07001300 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001301
1302
1303 AbstractFloatingView widgets = AbstractFloatingView
1304 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1305 if (widgets != null) {
1306 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1307 widgets.saveHierarchyState(widgetsState);
1308 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1309 } else {
1310 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1311 }
1312
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001313 // We close any open folders and shortcut containers since they will not be re-opened,
1314 // and we need to make sure this state is reflected.
1315 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001316
Sunny Goyal2100c782016-08-22 16:00:03 -07001317 if (mPendingRequestArgs != null) {
1318 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1319 }
1320 if (mPendingActivityResult != null) {
1321 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001322 }
1323
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001324 super.onSaveInstanceState(outState);
1325
Adam Cohen9211d422014-10-07 18:14:53 -07001326 if (mLauncherCallbacks != null) {
1327 mLauncherCallbacks.onSaveInstanceState(outState);
1328 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001329 }
1330
1331 @Override
1332 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001333 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001334
Sunny Goyal5cc7bbd2017-06-12 09:50:39 -07001335 unregisterReceiver(mReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001336 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001337
Tony Wickhama0068302018-04-11 16:16:11 -07001338 UiFactory.setOnTouchControllersChangedListener(this, null);
1339
Winson Chungcd2b0142011-06-08 16:02:26 -07001340 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001341 // It's possible to receive onDestroy after a new Launcher activity has
1342 // been created. In this case, don't interfere with the new Launcher.
1343 if (mModel.isCurrentCallbacks(this)) {
1344 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001345 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001346 }
Sunny Goyal623eddd2018-03-02 12:24:41 -08001347 mRotationHelper.destroy();
Winson Chungedb0d6b2018-03-21 17:32:55 -07001348 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mSharedPrefsListener);
Sunny Goyal745bad92016-05-02 10:54:12 -07001349
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001350 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001351 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001352 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001353 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001354 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001355
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001356 TextKeyListener.getInstance().release();
Mario Bertschler27288382017-05-24 15:35:09 -07001357
Michael Jurka2ecf9952012-06-18 12:52:28 -07001358 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001359
Tony2917a8b2017-07-31 23:29:42 -07001360 clearPendingBinds();
1361
Adam Cohen9211d422014-10-07 18:14:53 -07001362 if (mLauncherCallbacks != null) {
1363 mLauncherCallbacks.onDestroy();
1364 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001365 }
1366
Sunny Goyalae502842016-06-17 08:43:56 -07001367 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1368 return mAccessibilityDelegate;
1369 }
1370
Adam Cohena9cf38f2011-05-02 15:36:58 -07001371 public DragController getDragController() {
1372 return mDragController;
1373 }
1374
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001375 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001376 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001377 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001378 }
1379
1380 @Override
1381 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1382 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001383 try {
1384 super.startIntentSenderForResult(intent, requestCode,
1385 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1386 } catch (IntentSender.SendIntentException e) {
1387 throw new ActivityNotFoundException();
1388 }
1389 }
1390
Winson Chung70d72102011-08-12 11:24:35 -07001391 /**
1392 * Indicates that we want global search for this activity by setting the globalSearch
1393 * argument for {@link #startSearch} to true.
1394 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001395 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001396 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001397 Bundle appSearchData, boolean globalSearch) {
1398 if (appSearchData == null) {
1399 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001400 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001401 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001402
Sunny Goyal6f28e712016-09-13 14:19:29 -07001403 if (mLauncherCallbacks == null ||
1404 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1405 // Starting search from the callbacks failed. Start the default global search.
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001406 super.startSearch(initialQuery, selectInitialQuery, appSearchData, true);
Ian Parkinson047036e2014-09-01 15:40:53 +01001407 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001408
1409 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001410 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001411 }
1412
Joe Onorato9c1289c2009-08-17 11:03:03 -04001413 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001414 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001415 }
1416
Adam Cohen517a7f52014-03-01 12:12:59 -08001417 public boolean isWorkspaceLoading() {
1418 return mWorkspaceLoading;
1419 }
1420
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001421 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001422 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001423 }
1424
Sunny Goyal04a324a2017-01-20 21:08:59 -08001425 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001426 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001427 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001428
Sunny Goyal2100c782016-08-22 16:00:03 -07001429 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001430 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001431 if (LOGD) {
1432 Log.d(TAG, "Adding widget from drop");
1433 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001434 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001435 }
1436
Sunny Goyal2100c782016-08-22 16:00:03 -07001437 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001438 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1439 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1440 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001441
Adam Cohenad4e15c2013-10-17 16:21:35 -07001442 Runnable onComplete = new Runnable() {
1443 @Override
1444 public void run() {
1445 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001446 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001447 }
1448 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001449 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001450 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001451 }
1452 }
Romain Guycbb89e42009-06-08 15:52:54 -07001453
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001454 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1455 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001456 info.container = container;
1457 info.screenId = screenId;
1458 if (cell != null) {
1459 info.cellX = cell[0];
1460 info.cellY = cell[1];
1461 }
1462 info.spanX = spanX;
1463 info.spanY = spanY;
1464
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001465 switch (info.itemType) {
1466 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1467 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001468 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001469 break;
1470 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001471 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001472 break;
1473 default:
1474 throw new IllegalStateException("Unknown item type: " + info.itemType);
1475 }
1476 }
1477
Adam Cohenfbba09b2011-07-18 21:43:05 -07001478 /**
1479 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001480 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001481 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001482 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1483 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001484 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1485 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1486 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001487 }
1488
Adam Cohenfbba09b2011-07-18 21:43:05 -07001489 /**
1490 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001491 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001492 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001493 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001494 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001495 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001496 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001497 // In the case where we've prebound the widget, we remove it from the DragLayer
1498 if (LOGD) {
1499 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1500 }
1501 getDragLayer().removeView(hostView);
1502
Adam Cohened66b2b2012-01-23 17:28:51 -08001503 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001504 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001505
1506 // Clear the boundWidget so that it doesn't get destroyed.
1507 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001508 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001509 // In this case, we either need to start an activity to get permission to bind
1510 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001511 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1512 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1513 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1514 this, info.componentName);
1515 } else {
1516 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1517 }
Adam Cohendd70d662012-10-04 16:53:44 -07001518 Bundle options = info.bindOptions;
1519
Sunny Goyalffe83f12014-08-14 17:39:34 -07001520 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1521 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001522 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001523 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001524 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001525 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001526 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001527 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001528 }
1529
Adam Cohendcd297f2013-06-18 13:13:40 -07001530 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001531 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001532 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001533 folderInfo.title = getText(R.string.folder_name);
1534
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001535 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001536 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001537
1538 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001539 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301540 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001541 // Force measure the new folder icon
1542 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1543 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001544 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001545 }
Romain Guycbb89e42009-06-08 15:52:54 -07001546
Winsonc0b52fe2015-09-09 16:38:15 -07001547 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001548 * Unbinds the view for the specified item, and removes the item and all its children.
1549 *
1550 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001551 * @param itemInfo the {@link ItemInfo} for this view.
1552 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001553 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001554 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001555 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001556 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001557 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1558 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001559 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001560 } else {
1561 mWorkspace.removeWorkspaceItem(v);
1562 }
Winsonc0b52fe2015-09-09 16:38:15 -07001563 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001564 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001565 }
1566 } else if (itemInfo instanceof FolderInfo) {
1567 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001568 if (v instanceof FolderIcon) {
1569 ((FolderIcon) v).removeListeners();
1570 }
Winsonc0b52fe2015-09-09 16:38:15 -07001571 mWorkspace.removeWorkspaceItem(v);
1572 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001573 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001574 }
1575 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1576 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001577 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001578 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07001579 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001580 }
1581 } else {
1582 return false;
1583 }
1584 return true;
1585 }
1586
1587 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07001588 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07001589 */
Sunny Goyal56c73602015-09-25 12:55:01 -07001590 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07001591 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07001592 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07001593 // Deleting an app widget ID is a void call but writes to disk before returning
1594 // to the caller...
1595 new AsyncTask<Void, Void, Void>() {
1596 public Void doInBackground(Void ... args) {
1597 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
1598 return null;
1599 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07001600 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07001601 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001602 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001603 }
1604
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001605 @Override
1606 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001607 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001608 }
1609
Joe Onorato88ec0992009-11-19 13:16:06 -08001610 @Override
1611 public void onBackPressed() {
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08001612 if (finishAutoCancelActionMode()) {
1613 return;
1614 }
Adam Cohen9211d422014-10-07 18:14:53 -07001615 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1616 return;
1617 }
1618
Sunny Goyal45478022015-06-08 16:52:41 -07001619 if (mDragController.isDragging()) {
1620 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001621 return;
1622 }
1623
Jon Mirandafeba90f2016-10-06 10:53:29 -07001624 // Note: There should be at most one log per method call. This is enforced implicitly
1625 // by using if-else statements.
1626 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001627 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
1628 if (topView != null) {
Sunny Goyal37920962017-09-28 13:43:24 -07001629 topView.onBackPressed();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001630 } else if (!isInState(NORMAL)) {
Sunny Goyal49bcf342018-01-11 13:59:53 -08001631 LauncherState lastState = mStateManager.getLastState();
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001632 ued.logActionCommand(Action.Command.BACK, mStateManager.getState().containerType,
1633 lastState.containerType);
Sunny Goyal49bcf342018-01-11 13:59:53 -08001634 mStateManager.goToState(lastState);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001635 } else {
1636 // Back button is a no-op here, but give at least some feedback for the button press
1637 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001638 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001639 }
1640
Sunny Goyalfe770c92016-09-27 14:38:58 -07001641 @TargetApi(Build.VERSION_CODES.M)
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001642 @Override
Tony Wickham005df0b2018-02-13 11:28:12 -08001643 public ActivityOptions getActivityLaunchOptions(View v, boolean useDefaultLaunchOptions) {
Jon Miranda17940f02018-01-18 14:46:34 -08001644 return useDefaultLaunchOptions
Jon Miranda54441f52018-01-24 15:38:25 -08001645 ? mAppTransitionManager.getDefaultActivityLaunchOptions(this, v)
1646 : mAppTransitionManager.getActivityLaunchOptions(this, v);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001647 }
1648
Tony Wickham3a6746a2018-03-29 09:39:56 -07001649 public LauncherAppTransitionManager getAppTransitionManager() {
1650 return mAppTransitionManager;
1651 }
1652
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001653 @TargetApi(Build.VERSION_CODES.M)
1654 @Override
1655 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
1656 // Due to legacy reasons, direct call shortcuts require Launchers to have the
1657 // corresponding permission. Show the appropriate permission prompt if that
1658 // is the case.
1659 if (intent.getComponent() == null
1660 && Intent.ACTION_CALL.equals(intent.getAction())
1661 && checkSelfPermission(android.Manifest.permission.CALL_PHONE) !=
1662 PackageManager.PERMISSION_GRANTED) {
1663
1664 setWaitingForResult(PendingRequestArgs
1665 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
1666 requestPermissions(new String[]{android.Manifest.permission.CALL_PHONE},
1667 REQUEST_PERMISSION_CALL_PHONE);
1668 return true;
1669 } else {
1670 return false;
1671 }
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001672 }
1673
Sunny Goyala7ce1662016-05-31 15:01:35 -07001674 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001675 mAppLaunchSuccess = super.startActivitySafely(v, intent, item);
1676 if (mAppLaunchSuccess && v instanceof BubbleTextView) {
1677 // This is set to the view that launched the activity that navigated the user away
1678 // from launcher. Since there is no callback for when the activity has finished
1679 // launching, enable the press state and keep this reference to reset the press
1680 // state when we return to launcher.
1681 BubbleTextView btv = (BubbleTextView) v;
1682 btv.setStayPressed(true);
1683 setOnResumeCallback(btv);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001684 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07001685 return mAppLaunchSuccess;
Michael Jurka86a720e2012-05-09 11:23:23 -07001686 }
1687
Winson Chung3d503fb2011-07-13 17:25:49 -07001688 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001689 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07001690 return mHotseat != null && layout != null &&
1691 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
1692 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001693
Winson Chung3d503fb2011-07-13 17:25:49 -07001694 /**
1695 * Returns the CellLayout of the specified container at the specified screen.
1696 */
Sunny Goyal92820592015-03-02 11:31:35 -08001697 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001698 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1699 if (mHotseat != null) {
1700 return mHotseat.getLayout();
1701 } else {
1702 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08001703 }
Winson Chung3d503fb2011-07-13 17:25:49 -07001704 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08001705 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08001706 }
1707 }
1708
Michael Jurkae326f182011-11-21 14:05:46 -08001709 @Override
1710 public void onTrimMemory(int level) {
1711 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07001712 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
1713 // The widget preview db can result in holding onto over
1714 // 3MB of memory for caching which isn't necessary.
1715 SQLiteDatabase.releaseMemory();
1716
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08001717 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07001718 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08001719 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01001720 if (mLauncherCallbacks != null) {
1721 mLauncherCallbacks.onTrimMemory(level);
1722 }
Winson Chung62a70be2018-02-23 15:10:05 -08001723 UiFactory.onTrimMemory(this, level);
Michael Jurkae326f182011-11-21 14:05:46 -08001724 }
1725
Michael Jurkad7c28052012-04-27 15:43:36 -07001726 @Override
1727 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07001728 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07001729 final List<CharSequence> text = event.getText();
1730 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07001731 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001732 // TODO: When can workspace be null?
1733 text.add(mWorkspace == null
1734 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07001735 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07001736 return result;
1737 }
1738
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001739 public void setOnResumeCallback(OnResumeCallback callback) {
1740 if (mOnResumeCallback != null) {
1741 mOnResumeCallback.onLauncherResume();
1742 }
1743 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07001744 }
1745
Michael Jurka7607c2f2013-04-03 14:33:19 -07001746 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001747 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001748 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07001749 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001750 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07001751 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001752 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07001753 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07001754 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07001755 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001756 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001757
Joe Onorato9c1289c2009-08-17 11:03:03 -04001758 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07001759 * Clear any pending bind callbacks. This is called when is loader is planning to
1760 * perform a full rebind from scratch.
1761 */
1762 @Override
1763 public void clearPendingBinds() {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001764 if (mPendingExecutor != null) {
1765 mPendingExecutor.markCompleted();
1766 mPendingExecutor = null;
1767 }
1768 }
1769
1770 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001771 * Refreshes the shortcuts shown on the workspace.
1772 *
1773 * Implementation of the method from LauncherModel.Callbacks.
1774 */
1775 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001776 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001777 // Floating panels (except the full widget sheet) are associated with individual icons. If
1778 // we are starting a fresh bind, close all such panels as all the icons are about
1779 // to go away.
1780 AbstractFloatingView.closeOpenViews(this, true,
Sunny Goyal7ede6112017-12-05 15:11:21 -08001781 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_REBIND_SAFE);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08001782
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001783 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08001784
Winson Chungd64d1762013-08-20 14:37:16 -07001785 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07001786 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07001787 mWorkspace.removeAllWorkspaceScreens();
Sunny Goyalc11fac32018-02-27 19:20:15 -08001788 mAppWidgetHost.clearViews();
Winson Chungd64d1762013-08-20 14:37:16 -07001789
Winson Chung3d503fb2011-07-13 17:25:49 -07001790 if (mHotseat != null) {
Sunny Goyal11e96492018-03-23 17:06:00 -07001791 mHotseat.resetLayout(mDeviceProfile.isVerticalBarLayout());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001792 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001793 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001794 }
1795
Adam Cohendcd297f2013-06-18 13:13:40 -07001796 @Override
1797 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001798 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001799 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
1800 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001801 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
1802 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001803 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001804 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
1805 // If there are no screens, we need to have an empty screen
1806 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07001807 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001808 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07001809
Winsonc7d2e832016-07-28 12:24:55 -07001810 // After we have added all the screens, if the wallpaper was locked to the default state,
1811 // then notify to indicate that it can be released and a proper wallpaper offset can be
1812 // computed before the next layout
1813 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07001814 }
1815
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001816 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001817 int count = orderedScreenIds.size();
1818 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001819 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001820 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001821 // No need to bind the first screen, as its always bound.
1822 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
1823 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001824 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001825 }
1826
Sunny Goyalb23980c2017-08-17 07:45:25 -07001827 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08001828 public void bindAppsAdded(ArrayList<Long> newScreens, ArrayList<ItemInfo> addNotAnimated,
1829 ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07001830 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08001831 if (newScreens != null) {
1832 bindAddScreens(newScreens);
1833 }
Winson Chungd64d1762013-08-20 14:37:16 -07001834
1835 // We add the items without animation on non-visible pages, and with
1836 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08001837 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001838 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001839 }
Adam Cohen76a47a12014-02-05 11:47:43 -08001840 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001841 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07001842 }
Winson Chungc58497e2013-09-03 17:48:37 -07001843
Winson Chung87412982013-10-03 18:34:14 -07001844 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07001845 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001846 }
1847
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001848 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001849 * Bind the items start-end from the list.
1850 *
1851 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001852 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07001853 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07001854 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001855 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07001856 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001857 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07001858 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001859 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001860 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07001861 int end = items.size();
1862 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001863 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07001864
1865 // Short circuit if we are loading dock items for a configuration which has no dock
1866 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
1867 mHotseat == null) {
1868 continue;
1869 }
1870
Sunny Goyal639e9062015-08-19 19:17:06 -07001871 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001872 switch (item.itemType) {
1873 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1874 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08001875 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001876 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07001877 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001878 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001879 }
1880 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07001881 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001882 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001883 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001884 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001885 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07001886 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1887 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07001888 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07001889 if (view == null) {
1890 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001891 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08001892 break;
1893 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001894 default:
1895 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001896 }
Sunny Goyal639e9062015-08-19 19:17:06 -07001897
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001898 /*
1899 * Remove colliding items.
1900 */
1901 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1902 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
1903 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
1904 View v = cl.getChildAt(item.cellX, item.cellY);
1905 Object tag = v.getTag();
1906 String desc = "Collision while binding workspace item: " + item
1907 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08001908 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001909 throw (new RuntimeException(desc));
1910 } else {
1911 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001912 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001913 continue;
1914 }
1915 }
1916 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05301917 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07001918 if (animateIcons) {
1919 // Animate all the applications up now
1920 view.setAlpha(0f);
1921 view.setScaleX(0f);
1922 view.setScaleY(0f);
1923 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08001924 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07001925 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001926 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07001927
Winson Chung997a9232013-07-24 15:33:46 -07001928 if (animateIcons) {
1929 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08001930 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07001931 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08001932 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07001933 final Runnable startBounceAnimRunnable = new Runnable() {
1934 public void run() {
1935 anim.playTogether(bounceAnims);
1936 anim.start();
1937 }
1938 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08001939 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07001940 // We post the animation slightly delayed to prevent slowdowns
1941 // when we are loading right after we return to launcher.
1942 mWorkspace.postDelayed(new Runnable() {
1943 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00001944 if (mWorkspace != null) {
Jon Miranda64eb8ea2018-03-29 11:43:58 -07001945 AbstractFloatingView.closeAllOpenViews(Launcher.this, false);
1946
Ian Parkinson94449152014-01-21 13:09:59 +00001947 mWorkspace.snapToPage(newScreenIndex);
1948 mWorkspace.postDelayed(startBounceAnimRunnable,
1949 NEW_APPS_ANIMATION_DELAY);
1950 }
Winson Chung94d67682013-09-25 16:29:40 -07001951 }
1952 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07001953 } else {
1954 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07001955 }
1956 }
Winson Chung64359a52013-07-08 17:17:08 -07001957 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001958 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001959 }
1960
Joe Onorato9c1289c2009-08-17 11:03:03 -04001961 /**
1962 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001963 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07001964 public void bindAppWidget(LauncherAppWidgetInfo item) {
1965 View view = inflateAppWidget(item);
1966 if (view != null) {
1967 mWorkspace.addInScreen(view, item);
1968 mWorkspace.requestLayout();
1969 }
1970 }
1971
1972 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001973 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05301974 PendingAppWidgetHostView view =
1975 new PendingAppWidgetHostView(this, item, mIconCache, true);
1976 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07001977 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001978 }
1979
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001980 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001981
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001982 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08001983
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001984 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
1985 // If the provider is not ready, bind as a pending widget.
1986 appWidgetInfo = null;
1987 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
1988 // The widget id is not valid. Try to find the widget based on the provider info.
1989 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
1990 } else {
1991 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
1992 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07001993
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001994 // If the provider is ready, but the width is not yet restored, try to restore it.
1995 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
1996 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07001997 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001998 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
1999 + " belongs to component " + item.providerName
2000 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002001 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002002 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002003 }
Sunny Goyalff572272014-07-23 13:58:07 -07002004
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002005 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002006 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07002007 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2008 // Id has not been allocated yet. Allocate a new id.
2009 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
2010 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07002011
Sunny Goyald478c832016-04-01 12:04:16 -07002012 // Also try to bind the widget. If the bind fails, the user will be shown
2013 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08002014 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07002015 pendingInfo.spanX = item.spanX;
2016 pendingInfo.spanY = item.spanY;
2017 pendingInfo.minSpanX = item.minSpanX;
2018 pendingInfo.minSpanY = item.minSpanY;
2019 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07002020
2021 boolean isDirectConfig =
2022 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2023 if (isDirectConfig && item.bindOptions != null) {
2024 Bundle newOptions = item.bindOptions.getExtras();
2025 if (options != null) {
2026 newOptions.putAll(options);
2027 }
2028 options = newOptions;
2029 }
Sunny Goyald478c832016-04-01 12:04:16 -07002030 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2031 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002032
Sunny Goyal86df1382016-08-10 15:03:22 -07002033 // We tried to bind once. If we were not able to bind, we would need to
2034 // go through the permission dialog, which means we cannot skip the config
2035 // activity.
2036 item.bindOptions = null;
2037 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2038
Sunny Goyald478c832016-04-01 12:04:16 -07002039 // Bind succeeded
2040 if (success) {
2041 // If the widget has a configure activity, it is still needs to set it up,
2042 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002043 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002044 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2045 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002046 }
Sunny Goyald478c832016-04-01 12:04:16 -07002047
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002048 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002049 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002050 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002051 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002052 // The widget was marked as UI not ready, but there is no configure activity to
2053 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002054 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002055 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002056 }
Sunny Goyalff572272014-07-23 13:58:07 -07002057 }
2058
Sunny Goyald5462aa2016-11-22 16:52:39 +05302059 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002060 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002061 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002062 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002063 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07002064 deleteWidgetInfo(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002065 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002066 }
2067
Sunny Goyal233ee962015-08-03 13:05:01 -07002068 item.minSpanX = appWidgetInfo.minSpanX;
2069 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302070 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002071 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302072 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002073 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302074 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002075
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002076 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002077 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002078 }
2079
Sunny Goyalff572272014-07-23 13:58:07 -07002080 /**
2081 * Restores a pending widget.
2082 *
2083 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002084 */
Sunny Goyald478c832016-04-01 12:04:16 -07002085 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002086 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2087 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2088 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002089 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002090 }
2091
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002092 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002093 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002094 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2095 info.pendingItemInfo = null;
2096 }
Sunny Goyalff572272014-07-23 13:58:07 -07002097
Sunny Goyalba47faa2017-10-04 16:50:57 -07002098 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002099 view.reInflate();
Sunny Goyalba47faa2017-10-04 16:50:57 -07002100 }
2101
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002102 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002103 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002104 }
2105
Adam Cohen1462de32012-07-24 22:34:36 -07002106 public void onPageBoundSynchronously(int page) {
Sunny Goyalf4aceab2017-12-06 16:16:31 -08002107 mSynchronouslyBoundPage = page;
Adam Cohen1462de32012-07-24 22:34:36 -07002108 }
2109
Sunny Goyal527c7d32015-08-28 15:19:36 -07002110 @Override
2111 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2112 if (mPendingExecutor != null) {
2113 mPendingExecutor.markCompleted();
2114 }
2115 mPendingExecutor = executor;
Sunny Goyal60180b02018-02-07 12:56:30 -08002116 if (!isInState(ALL_APPS)) {
2117 mAppsView.getAppsStore().setDeferUpdates(true);
2118 mPendingExecutor.execute(() -> mAppsView.getAppsStore().setDeferUpdates(false));
2119 }
2120
Sunny Goyal527c7d32015-08-28 15:19:36 -07002121 executor.attachTo(this);
2122 }
2123
2124 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2125 if (mPendingExecutor == executor) {
2126 mPendingExecutor = null;
2127 }
2128 }
2129
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002130 @Override
2131 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002132 if (mDragLayer.getAlpha() < 1) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002133 mDragLayer.animate().alpha(1).withEndAction(
2134 executor == null ? null : executor::onLoadAnimationCompleted).start();
2135 } else if (executor != null) {
2136 executor.onLoadAnimationCompleted();
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002137 }
2138 }
2139
Joe Onorato9c1289c2009-08-17 11:03:03 -04002140 /**
2141 * Callback saying that there aren't any more items to bind.
2142 *
2143 * Implementation of the method from LauncherModel.Callbacks.
2144 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002145 public void finishBindingItems() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002146 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002147 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002148
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002149 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002150
Sunny Goyal2100c782016-08-22 16:00:03 -07002151 if (mPendingActivityResult != null) {
2152 handleActivityResult(mPendingActivityResult.requestCode,
2153 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2154 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002155 }
2156
Sunny Goyala474a9b2017-05-04 16:47:11 -07002157 InstallShortcutReceiver.disableAndFlushInstallQueue(
2158 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002159
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002160 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002161 }
2162
Winson Chunga2413752012-04-03 14:22:34 -07002163 private boolean canRunNewAppsAnimation() {
2164 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002165 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002166 }
2167
Winson Chungc9168342013-06-26 14:54:55 -07002168 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002169 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07002170 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
2171 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002172 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002173 return bounceAnim;
2174 }
2175
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002176 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002177 * Add the icons for all apps.
2178 *
2179 * Implementation of the method from LauncherModel.Callbacks.
2180 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002181 public void bindAllApplications(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002182 mAppsView.getAppsStore().setApps(apps);
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002183
Adam Cohen9211d422014-10-07 18:14:53 -07002184 if (mLauncherCallbacks != null) {
2185 mLauncherCallbacks.bindAllApplications(apps);
2186 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002187 }
2188
2189 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002190 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
2191 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2192 */
2193 @Override
2194 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002195 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002196 }
2197
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002198 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002199 * A package was updated.
2200 *
2201 * Implementation of the method from LauncherModel.Callbacks.
2202 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002203 @Override
2204 public void bindAppsAddedOrUpdated(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002205 mAppsView.getAppsStore().addOrUpdateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002206 }
2207
Sunny Goyal4390ace2014-10-13 11:33:11 -07002208 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002209 public void bindPromiseAppProgressUpdated(PromiseAppInfo app) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002210 mAppsView.getAppsStore().updatePromiseAppProgress(app);
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002211 }
2212
2213 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002214 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002215 mWorkspace.widgetsRestored(widgets);
2216 }
2217
Joe Onorato9c1289c2009-08-17 11:03:03 -04002218 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002219 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002220 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002221 *
2222 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002223 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002224 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002225 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002226 if (!updated.isEmpty()) {
2227 mWorkspace.updateShortcuts(updated);
2228 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002229 }
2230
2231 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002232 * Update the state of a package, typically related to install state.
2233 *
2234 * Implementation of the method from LauncherModel.Callbacks.
2235 */
Sunny Goyale755d462014-07-22 13:48:29 -07002236 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002237 public void bindRestoreItemsChange(HashSet<ItemInfo> updates) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07002238 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07002239 }
2240
2241 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002242 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07002243 * in addition to specific applications to remove, the reason being that
2244 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002245 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07002246 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002247 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07002248 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002249 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002250 mWorkspace.removeItemsByMatcher(matcher);
2251 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002252 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002253
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002254 @Override
2255 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002256 mAppsView.getAppsStore().removeApps(appInfos);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002257 }
Joe Onoratobe386092009-11-17 17:32:16 -08002258
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002259 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002260 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
2261 mPopupDataProvider.setAllWidgets(allWidgets);
Tony Wickham26b17462017-03-20 17:12:24 -07002262 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
2263 if (topView != null) {
2264 topView.onWidgetsBound();
2265 }
2266 }
2267
Tony Wickham86222d22017-03-29 15:30:43 -07002268 /**
2269 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
2270 * refreshes the widgets and shortcuts associated with the given package/user
2271 */
2272 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07002273 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002274 }
2275
Winson Chung80baf5a2010-08-09 16:03:15 -07002276 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002277 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08002278 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002279 @Override
2280 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
2281 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002282
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002283 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
2284 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07002285 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002286 writer.println(prefix + " Homescreen " + i);
2287
2288 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
2289 for (int j = 0; j < layout.getChildCount(); j++) {
2290 Object tag = layout.getChildAt(j).getTag();
2291 if (tag != null) {
2292 writer.println(prefix + " " + tag.toString());
2293 }
2294 }
2295 }
2296
2297 writer.println(prefix + " Hotseat");
2298 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07002299 for (int j = 0; j < layout.getChildCount(); j++) {
2300 Object tag = layout.getChildAt(j).getTag();
2301 if (tag != null) {
2302 writer.println(prefix + " " + tag.toString());
2303 }
Adam Cohen4caf2982013-08-20 18:54:31 -07002304 }
Sunny Goyala1365452015-10-01 15:46:24 -07002305 }
2306
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002307 writer.println(prefix + "Misc:");
2308 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
2309 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
2310 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
Sunny Goyal2bba1902018-02-27 12:20:50 -08002311 writer.println(" deviceProfile isTransposed=" + getDeviceProfile().isVerticalBarLayout());
2312 writer.println(" orientation=" + getResources().getConfiguration().orientation);
2313
2314 try {
2315 FileLog.flushAll(writer);
2316 } catch (Exception e) {
2317 // Ignore
2318 }
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002319
2320 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002321
Adam Cohen9211d422014-10-07 18:14:53 -07002322 if (mLauncherCallbacks != null) {
2323 mLauncherCallbacks.dump(prefix, fd, writer, args);
2324 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002325 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002326
Sunny Goyal66b24572016-09-21 15:57:55 -07002327 @Override
2328 @TargetApi(Build.VERSION_CODES.N)
2329 public void onProvideKeyboardShortcuts(
2330 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
2331
2332 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002333 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002334 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
2335 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
2336 }
2337 View currentFocus = getCurrentFocus();
2338 if (new CustomActionsPopup(this, currentFocus).canShow()) {
2339 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
2340 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
2341 }
Tony18c4aa42017-05-10 21:23:57 -05002342 if (currentFocus.getTag() instanceof ItemInfo
2343 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002344 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
2345 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
2346 }
2347 if (!shortcutInfos.isEmpty()) {
2348 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
2349 }
2350
2351 super.onProvideKeyboardShortcuts(data, menu, deviceId);
2352 }
2353
2354 @Override
2355 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
2356 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
2357 switch (keyCode) {
2358 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002359 if (isInState(NORMAL)) {
2360 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07002361 return true;
2362 }
2363 break;
2364 case KeyEvent.KEYCODE_S: {
2365 View focusedView = getCurrentFocus();
2366 if (focusedView instanceof BubbleTextView
2367 && focusedView.getTag() instanceof ItemInfo
2368 && mAccessibilityDelegate.performAction(focusedView,
2369 (ItemInfo) focusedView.getTag(),
2370 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal00ac9202017-11-09 15:24:06 -08002371 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07002372 return true;
2373 }
2374 break;
2375 }
2376 case KeyEvent.KEYCODE_O:
2377 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
2378 return true;
2379 }
2380 break;
2381 }
2382 }
2383 return super.onKeyShortcut(keyCode, event);
2384 }
2385
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002386 @Override
2387 public boolean onKeyUp(int keyCode, KeyEvent event) {
2388 if (keyCode == KeyEvent.KEYCODE_MENU) {
2389 // KEYCODE_MENU is sent by some tests, for example
2390 // LauncherJankTests#testWidgetsContainerFling. Don't just remove its handling.
2391 if (!mDragController.isDragging() && !mWorkspace.isSwitchingState() &&
2392 isInState(NORMAL)) {
2393 // Close any open floating views.
2394 AbstractFloatingView.closeAllOpenViews(this);
2395
2396 // Setting the touch point to (-1, -1) will show the options popup in the center of
2397 // the screen.
Sunny Goyal2fd7a8b2018-03-30 17:10:13 -07002398 OptionsPopupView.showDefaultOptions(this, -1, -1);
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002399 }
2400 return true;
2401 }
2402 return super.onKeyUp(keyCode, event);
2403 }
2404
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002405 public static Launcher getLauncher(Context context) {
2406 if (context instanceof Launcher) {
2407 return (Launcher) context;
2408 }
2409 return ((Launcher) ((ContextWrapper) context).getBaseContext());
2410 }
2411
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002412 /**
2413 * Callback for listening for onResume
2414 */
2415 public interface OnResumeCallback {
2416
2417 void onLauncherResume();
2418 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002419}