blob: 002dd1a9b66a2619e07b119af0d7aec359c058f3 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080019import static android.content.pm.ActivityInfo.CONFIG_ORIENTATION;
20import static android.content.pm.ActivityInfo.CONFIG_SCREEN_SIZE;
Sunny Goyal623eddd2018-03-02 12:24:41 -080021
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;
Mario Bertschler27288382017-05-24 15:35:09 -070084import com.android.launcher3.dynamicui.WallpaperColorInfo;
Sunny Goyal26119432016-02-18 22:09:23 +000085import com.android.launcher3.folder.FolderIcon;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070086import com.android.launcher3.folder.FolderIconPreviewVerifier;
Sunny Goyal66b24572016-09-21 15:57:55 -070087import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -070088import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -070089import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -070090import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyala535ae42017-02-27 10:07:13 -080091import com.android.launcher3.model.ModelWriter;
Sunny Goyala535ae42017-02-27 10:07:13 -080092import com.android.launcher3.notification.NotificationListener;
Tony Wickham540913e2017-01-23 11:47:51 -080093import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -080094import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -070095import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyalf8088ee2017-11-10 14:52:00 -080096import com.android.launcher3.states.InternalStateHandler;
Sunny Goyal623eddd2018-03-02 12:24:41 -080097import com.android.launcher3.states.RotationHelper;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -080098import com.android.launcher3.touch.ItemClickHandler;
Sunny Goyala5ace712017-11-15 17:12:51 -080099import com.android.launcher3.uioverrides.UiFactory;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530100import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
101import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
Sunny Goyalaeb16432017-10-16 11:46:41 -0700102import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyal2100c782016-08-22 16:00:03 -0700103import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700104import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700105import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700106import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700107import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800108import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700109import com.android.launcher3.util.PendingRequestArgs;
Sunny Goyal8392c822017-06-20 10:03:56 -0700110import com.android.launcher3.util.SystemUiController;
Mario Bertschlera6936942017-05-31 14:48:19 -0700111import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700112import com.android.launcher3.util.Thunk;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700113import com.android.launcher3.util.TraceHelper;
Sunny Goyal326403e2017-10-02 12:45:10 -0700114import com.android.launcher3.util.UiThreadHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700115import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800116import com.android.launcher3.views.OptionsPopupView;
Sunny Goyal29947f02017-12-18 13:49:44 -0800117import com.android.launcher3.widget.LauncherAppWidgetHostView;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800118import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700119import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal29947f02017-12-18 13:49:44 -0800120import com.android.launcher3.widget.PendingAppWidgetHostView;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800121import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700122import com.android.launcher3.widget.WidgetHostViewLoader;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700123import com.android.launcher3.widget.WidgetListRowEntry;
124import com.android.launcher3.widget.WidgetsFullSheet;
Sunny Goyal952e63d2017-08-16 04:59:08 -0700125import com.android.launcher3.widget.custom.CustomWidgetParser;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700126
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700127import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700128import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700129import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700130import java.util.Collection;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700131import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700132import java.util.List;
Tony Wickham010d2552017-01-20 08:15:28 -0800133import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700134
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135/**
136 * Default launcher application.
137 */
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700138public class Launcher extends BaseDraggingActivity implements LauncherExterns, LauncherModel.Callbacks,
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800139 LauncherProviderChangeListener, WallpaperColorInfo.OnThemeChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700140 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700141 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142
Winson Chunga2413752012-04-03 14:22:34 -0700143 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700144
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700146 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700147
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700148 private static final int REQUEST_PICK_APPWIDGET = 9;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149
Michael Jurka8b805b12012-04-18 14:23:14 -0700150 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800151 public static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
152 public static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700153
Jon Mirandac476d6e2017-05-04 16:30:01 -0700154 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700155
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700156 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
157
Mathew Inwood876a8462013-06-14 14:12:41 +0100158 /**
159 * IntentStarter uses request codes starting with this. This must be greater than all activity
160 * request codes used internally.
161 */
162 protected static final int REQUEST_LAST = 100;
163
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164 // Type: int
165 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700166 // Type: int
167 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700168 // Type: PendingRequestArgs
169 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
170 // Type: ActivityResultInfo
171 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700172 // Type: SparseArray<Parcelable>
173 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700175 private LauncherStateManager mStateManager;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700176
Adam Cohenad4e15c2013-10-17 16:21:35 -0700177 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178
Winson Chunga2413752012-04-03 14:22:34 -0700179 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700180 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
181 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
182 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700183
Jon Miranda54441f52018-01-24 15:38:25 -0800184 private LauncherAppTransitionManager mAppTransitionManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800185 private Configuration mOldConfig;
Jon Miranda54441f52018-01-24 15:38:25 -0800186
Adam Cohen091440a2015-03-18 14:16:05 -0700187 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700188 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700189 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800190 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700191
Sunny Goyalffe83f12014-08-14 17:39:34 -0700192 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700193 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700194
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700195 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700196
Sunny Goyal316490e2015-06-02 09:38:28 -0700197 @Thunk Hotseat mHotseat;
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700198 private View mDragHandleIndicator;
199 @Nullable private View mHotseatSearchBox;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700200
Sunny Goyal47328fd2016-05-25 18:56:41 -0700201 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700202
203 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700204 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700205 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700206
Winson Chung8ae41982017-11-10 11:42:13 -0800207 // UI and state for the overview panel
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800208 private View mOverviewPanel;
Winson Chung8ae41982017-11-10 11:42:13 -0800209
Adam Cohen091440a2015-03-18 14:16:05 -0700210 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400211
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700212 private OnResumeCallback mOnResumeCallback;
213
Sunny Goyal527c7d32015-08-28 15:19:36 -0700214 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700215
Joe Onorato9c1289c2009-08-17 11:03:03 -0400216 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800217 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800218 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700219 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400220
Jon Miranda2d89ea82017-05-04 11:47:53 -0700221 private ObjectAnimator mScrimAnimator;
Jon Miranda7fda2852017-07-19 16:07:01 -0700222 private boolean mShouldFadeInScrim;
Jon Miranda2d89ea82017-05-04 11:47:53 -0700223
Tony Wickham010d2552017-01-20 08:15:28 -0800224 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700225
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800226 private int mSynchronouslyBoundPage = PagedView.INVALID_PAGE;
Adam Cohen1462de32012-07-24 22:34:36 -0700227
Winson Chung46353de2012-02-16 14:05:10 -0800228 // We only want to get the SharedPreferences once since it does an FS stat each time we get
229 // it from the context.
230 private SharedPreferences mSharedPrefs;
231
Sunny Goyal2100c782016-08-22 16:00:03 -0700232 // Activity result which needs to be processed after workspace has loaded.
233 private ActivityResultInfo mPendingActivityResult;
234 /**
235 * Holds extra information required to handle a result from an external call, like
236 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
237 */
238 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800239
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700240 public ViewGroupFocusHelper mFocusHandler;
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700241 private boolean mAppLaunchSuccess;
242
Sunny Goyal623eddd2018-03-02 12:24:41 -0800243 private RotationHelper mRotationHelper;
Sunny Goyal7779d622015-06-11 16:18:39 -0700244
Winson Chungedb0d6b2018-03-21 17:32:55 -0700245 // Used to keep track of the swipe up state
246 private SharedPreferences.OnSharedPreferenceChangeListener mSharedPrefsListener =
247 (sharedPreferences, s) -> {
248 mDragLayer.setup(mDragController);
249 };
250
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800251 @Override
252 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700253 if (DEBUG_STRICT_MODE) {
254 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
255 .detectDiskReads()
256 .detectDiskWrites()
257 .detectNetwork() // or .detectAll() for all detectable problems
258 .penaltyLog()
259 .build());
260 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
261 .detectLeakedSqlLiteObjects()
262 .detectLeakedClosableObjects()
263 .penaltyLog()
264 .penaltyDeath()
265 .build());
266 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700267 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700268
Mario Bertschler27288382017-05-24 15:35:09 -0700269 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
270 wallpaperColorInfo.setOnThemeChangeListener(this);
Mario Bertschlera6936942017-05-31 14:48:19 -0700271 overrideTheme(wallpaperColorInfo.isDark(), wallpaperColorInfo.supportsDarkText());
Mario Bertschler27288382017-05-24 15:35:09 -0700272
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800273 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700274 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400275
Sunny Goyal87f784c2017-01-11 10:48:34 -0800276 LauncherAppState app = LauncherAppState.getInstance(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800277 mOldConfig = new Configuration(getResources().getConfiguration());
Sunny Goyal0e7724c2018-02-22 13:22:21 -0800278 mModel = app.setLauncher(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800279 initDeviceProfile(app.getInvariantDeviceProfile());
Winson Chung6e1c0d32013-10-25 15:24:24 -0700280
Sunny Goyalf7258242015-10-19 16:59:07 -0700281 mSharedPrefs = Utilities.getPrefs(this);
Winson Chungedb0d6b2018-03-21 17:32:55 -0700282 mSharedPrefs.registerOnSharedPreferenceChangeListener(mSharedPrefsListener);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800283 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700284 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700285
Joe Onorato41a12d22009-10-31 18:30:00 -0400286 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700287 mAllAppsController = new AllAppsTransitionController(this);
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800288 mStateManager = new LauncherStateManager(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700289
Sunny Goyalffe83f12014-08-14 17:39:34 -0700290 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700291
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700292 mAppWidgetHost = new LauncherAppWidgetHost(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700293 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700294
Sunny Goyal60820d72017-05-09 12:40:11 -0700295 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700296
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800297 setupViews();
Tony Wickham010d2552017-01-20 08:15:28 -0800298 mPopupDataProvider = new PopupDataProvider(this);
299
Sunny Goyal623eddd2018-03-02 12:24:41 -0800300 mRotationHelper = new RotationHelper(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800301
Sunny Goyald3864fa2017-11-14 15:06:36 -0800302 boolean internalStateHandled = InternalStateHandler.handleCreate(this, getIntent());
303 if (internalStateHandled) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800304 if (savedInstanceState != null) {
305 // InternalStateHandler has already set the appropriate state.
306 // We dont need to do anything.
307 savedInstanceState.remove(RUNTIME_STATE);
308 }
309 }
310 restoreState(savedInstanceState);
Winson Chungb63b44c2017-11-10 17:54:44 -0800311
Sunny Goyal2100c782016-08-22 16:00:03 -0700312 // We only load the page synchronously if the user rotates (or triggers a
313 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700314 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
315 if (savedInstanceState != null) {
316 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
317 }
Sunny Goyald3864fa2017-11-14 15:06:36 -0800318
Sunny Goyalfe770c92016-09-27 14:38:58 -0700319 if (!mModel.startLoader(currentScreen)) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800320 if (!internalStateHandled) {
321 // If we are not binding synchronously, show a fade in animation when
322 // the first page bind completes.
323 mDragLayer.setAlpha(0);
324 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700325 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700326 // Pages bound synchronously.
327 mWorkspace.setCurrentPage(currentScreen);
328
Sunny Goyal2100c782016-08-22 16:00:03 -0700329 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800330 }
331
332 // For handling default keys
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800333 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800334
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800335 setContentView(mLauncherView);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800336 getRootView().dispatchInsets();
Mario Bertschlera6936942017-05-31 14:48:19 -0700337
Jon Miranda7fda2852017-07-19 16:07:01 -0700338 // Listen for broadcasts
339 IntentFilter filter = new IntentFilter();
340 filter.addAction(Intent.ACTION_SCREEN_OFF);
341 filter.addAction(Intent.ACTION_USER_PRESENT); // When the device wakes up + keyguard is gone
342 registerReceiver(mReceiver, filter);
343 mShouldFadeInScrim = true;
Sunny Goyala616d2b2017-06-12 13:54:01 -0700344
Sunny Goyal8392c822017-06-20 10:03:56 -0700345 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
346 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700347
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800348 mAppTransitionManager = LauncherAppTransitionManager.newInstance(this);
Jon Miranda7853bdb2018-01-22 17:32:30 -0800349
Sunny Goyal14b32402017-07-31 10:03:28 -0700350 if (mLauncherCallbacks != null) {
351 mLauncherCallbacks.onCreate(savedInstanceState);
352 }
Sunny Goyal623eddd2018-03-02 12:24:41 -0800353 mRotationHelper.initialize();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700354
355 TraceHelper.endSection("Launcher-onCreate");
Adam Cohen9211d422014-10-07 18:14:53 -0700356 }
357
Sunny Goyal7779d622015-06-11 16:18:39 -0700358 @Override
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800359 public void onConfigurationChanged(Configuration newConfig) {
360 int diff = newConfig.diff(mOldConfig);
361 if ((diff & (CONFIG_ORIENTATION | CONFIG_SCREEN_SIZE)) != 0) {
362 mUserEventDispatcher = null;
363 initDeviceProfile(mDeviceProfile.inv);
Sunny Goyal2bba1902018-02-27 12:20:50 -0800364 FileLog.d(TAG, "Config changed, my orientation=" +
365 getResources().getConfiguration().orientation +
366 ", new orientation=" + newConfig.orientation +
367 ", old orientation=" + mOldConfig.orientation +
368 ", isTransposed=" + mDeviceProfile.isVerticalBarLayout());
Sunny Goyalfde55052018-02-01 14:46:13 -0800369 dispatchDeviceProfileChanged();
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800370
371 getRootView().dispatchInsets();
372 getStateManager().reapplyState();
373
Sunny Goyal7185dd62018-03-14 17:51:49 -0700374 // Recreate touch controllers
375 mDragLayer.setup(mDragController);
376
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800377 // TODO: We can probably avoid rebind when only screen size changed.
Sunny Goyal605bcf32018-03-02 15:16:12 -0800378 rebindModel();
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800379 }
380
381 mOldConfig.setTo(newConfig);
Tracy Zhoua706f002018-03-28 13:55:19 -0700382 UiFactory.onLauncherStateOrResumeChanged(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800383 super.onConfigurationChanged(newConfig);
384 }
385
Sunny Goyal605bcf32018-03-02 15:16:12 -0800386 @Override
387 public void rebindModel() {
388 int currentPage = mWorkspace.getNextPage();
389 if (mModel.startLoader(currentPage)) {
390 mWorkspace.setCurrentPage(currentPage);
391 setWorkspaceLoading(true);
392 }
393 }
394
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800395 private void initDeviceProfile(InvariantDeviceProfile idp) {
396 // Load configuration-specific DeviceProfile
Sunny Goyalf633ef52018-03-13 09:57:05 -0700397 setDeviceProfile(idp.getDeviceProfile(this));
Sunny Goyal605bcf32018-03-02 15:16:12 -0800398 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout(), true);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800399 }
400
Sunny Goyal623eddd2018-03-02 12:24:41 -0800401 public RotationHelper getRotationHelper() {
402 return mRotationHelper;
403 }
404
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800405 @Override
Mario Bertschler27288382017-05-24 15:35:09 -0700406 public void onThemeChanged() {
407 recreate();
408 }
409
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700410 public LauncherStateManager getStateManager() {
411 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700412 }
413
Mario Bertschlera6936942017-05-31 14:48:19 -0700414 protected void overrideTheme(boolean isDark, boolean supportsDarkText) {
Mario Bertschler27288382017-05-24 15:35:09 -0700415 if (isDark) {
416 setTheme(R.style.LauncherThemeDark);
Mario Bertschlera6936942017-05-31 14:48:19 -0700417 } else if (supportsDarkText) {
418 setTheme(R.style.LauncherThemeDarkText);
Mario Bertschler27288382017-05-24 15:35:09 -0700419 }
420 }
421
422 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700423 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800424 return mLauncherView.findViewById(id);
425 }
426
427 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700428 public void onAppWidgetHostReset() {
429 if (mAppWidgetHost != null) {
430 mAppWidgetHost.startListening();
431 }
432 }
433
Adam Cohen9211d422014-10-07 18:14:53 -0700434 private LauncherCallbacks mLauncherCallbacks;
435
Sunny Goyal32554d12015-12-03 15:31:25 -0800436 /**
437 * Call this after onCreate to set or clear overlay.
438 */
439 public void setLauncherOverlay(LauncherOverlay overlay) {
440 if (overlay != null) {
441 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
442 }
443 mWorkspace.setLauncherOverlay(overlay);
444 }
445
Adam Cohen9211d422014-10-07 18:14:53 -0700446 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
447 mLauncherCallbacks = callbacks;
448 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700449 }
450
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700451 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700452 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700453 if (mLauncherCallbacks != null) {
454 mLauncherCallbacks.onLauncherProviderChange();
455 }
456 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700457
Hyunyoung Song3f471442015-04-08 19:01:34 -0700458 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700459 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
460 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700461 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700462 }
463
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000464 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700465 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
466 // This cast is safe as long as the id < 0x00FFFFFF
467 // Since we jail all the dynamically generated views, there should be no clashes
468 // with any other views.
469 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000470 }
471
Tony Wickham010d2552017-01-20 08:15:28 -0800472 public PopupDataProvider getPopupDataProvider() {
473 return mPopupDataProvider;
474 }
475
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700476 @Override
477 public BadgeInfo getBadgeInfoForItem(ItemInfo info) {
478 return mPopupDataProvider.getBadgeInfoForItem(info);
479 }
480
481 @Override
482 public void invalidateParent(ItemInfo info) {
483 FolderIconPreviewVerifier verifier = new FolderIconPreviewVerifier(getDeviceProfile().inv);
484 if (verifier.isItemInPreview(info.rank) && (info.container >= 0)) {
485 View folderIcon = getWorkspace().getHomescreenIconByItemId(info.container);
486 if (folderIcon != null) {
487 folderIcon.invalidate();
488 }
489 }
490 }
491
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000492 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700493 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
494 * a configuration step, this allows the proper animations to run after other transitions.
495 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700496 private long completeAdd(
497 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
498 long screenId = info.screenId;
499 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700500 // When the screen id represents an actual screen (as opposed to a rank) we make sure
501 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700502 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700503 }
Adam Cohendb364c32014-05-20 14:23:40 -0700504
Sunny Goyal2100c782016-08-22 16:00:03 -0700505 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800506 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700507 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700508 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800509 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700510 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700511 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700512 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700513 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700514 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700515 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700516 int widgetId = appWidgetId;
517 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700518 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700519 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700520 // Since the view was just bound, also launch the configure activity if needed
521 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
522 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800523 if (provider != null) {
524 new WidgetAddFlowHandler(provider)
525 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700526 }
527 }
528 break;
529 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800530 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700531
Adam Cohendb364c32014-05-20 14:23:40 -0700532 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800533 }
534
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800535 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700536 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700537 if (isWorkspaceLoading()) {
538 // process the result once the workspace has loaded.
539 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
540 return;
541 }
542 mPendingActivityResult = null;
543
Winson Chunge341d302013-08-16 14:31:00 -0700544 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700545 final PendingRequestArgs requestArgs = mPendingRequestArgs;
546 setWaitingForResult(null);
547 if (requestArgs == null) {
548 return;
549 }
550
551 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700552
Adam Cohenad4e15c2013-10-17 16:21:35 -0700553 Runnable exitSpringLoaded = new Runnable() {
554 @Override
555 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700556 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700557 }
558 };
559
Michael Jurka8b805b12012-04-18 14:23:14 -0700560 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700561 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700562 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700563 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
564 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700565 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700566 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700567 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700568 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700569 addAppWidgetImpl(
570 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800571 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700572 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700573 }
574 return;
575 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200576
Adam Cohened66b2b2012-01-23 17:28:51 -0800577 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700578 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700579
Adam Cohened66b2b2012-01-23 17:28:51 -0800580 // We have special handling for widgets
581 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800582 final int appWidgetId;
583 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
584 : -1;
585 if (widgetId < 0) {
586 appWidgetId = pendingAddWidgetId;
587 } else {
588 appWidgetId = widgetId;
589 }
590
Adam Cohenad4e15c2013-10-17 16:21:35 -0700591 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800592 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700593 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
594 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700595 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700596 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700597 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700598 @Override
599 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700600 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700601 }
602 };
Adam Cohendb364c32014-05-20 14:23:40 -0700603
Sunny Goyal2100c782016-08-22 16:00:03 -0700604 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
605 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
606 } else {
607 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
608 // When the screen id represents an actual screen (as opposed to a rank)
609 // we make sure that the drop page actually exists.
610 requestArgs.screenId =
611 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700612 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700613 final CellLayout dropLayout =
614 mWorkspace.getScreenWithId(requestArgs.screenId);
615
616 dropLayout.setDropPending(true);
617 final Runnable onComplete = new Runnable() {
618 @Override
619 public void run() {
620 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
621 dropLayout.setDropPending(false);
622 }
623 };
624 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
625 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700626 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800627 return;
628 }
629
Sunny Goyald478c832016-04-01 12:04:16 -0700630 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
631 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700632 if (resultCode == RESULT_OK) {
633 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700634 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700635 }
636 // Leave the widget in the pending state if the user canceled the configure.
637 return;
638 }
639
Sunny Goyalaad90582015-07-09 14:22:50 -0700640 if (requestCode == REQUEST_CREATE_SHORTCUT) {
641 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700642 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
643 completeAdd(requestCode, data, -1, requestArgs);
644 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
645 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
646
Sunny Goyalaad90582015-07-09 14:22:50 -0700647 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700648 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
649 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800650 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800651 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800652 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800653 }
654
655 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700656 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800657 final int requestCode, final int resultCode, final Intent data) {
658 handleActivityResult(requestCode, resultCode, data);
659 if (mLauncherCallbacks != null) {
660 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
661 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800662 }
663
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700664 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700665 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600666 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700667 PendingRequestArgs pendingArgs = mPendingRequestArgs;
668 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
669 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
670 setWaitingForResult(null);
671
Sunny Goyal28c6b962015-10-12 11:42:05 -0700672 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700673 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700674 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700675 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700676 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700677 Intent intent = pendingArgs.getPendingIntent();
678
Sunny Goyal28c6b962015-10-12 11:42:05 -0700679 if (grantResults.length > 0
680 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700681 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700682 } else {
683 // TODO: Show a snack bar with link to settings
684 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700685 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700686 }
687 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600688 if (mLauncherCallbacks != null) {
689 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
690 grantResults);
691 }
692 }
693
Adam Cohendb364c32014-05-20 14:23:40 -0700694 /**
695 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
696 *
697 * @param screenId the screen id to check
698 * @return the new screen, or screenId if it exists
699 */
700 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800701 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700702 if (dropLayout == null) {
703 // it's possible that the add screen was removed because it was
704 // empty and a re-bind occurred
705 mWorkspace.addExtraEmptyScreen();
706 return mWorkspace.commitExtraEmptyScreen();
707 } else {
708 return screenId;
709 }
710 }
711
Sunny Goyal2100c782016-08-22 16:00:03 -0700712 @Thunk void completeTwoStageWidgetDrop(
713 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
714 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800715 Runnable onCompleteRunnable = null;
716 int animationType = 0;
717
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700718 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800719 if (resultCode == RESULT_OK) {
720 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
721 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800722 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700723 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800724 onCompleteRunnable = new Runnable() {
725 @Override
726 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700727 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700728 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800729 }
730 };
731 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800732 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800733 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800734 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700735 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700736 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700737 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
738 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700739 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700740 // The animated view may be null in the case of a rotation during widget configuration
741 onCompleteRunnable.run();
742 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800743 }
744
745 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700746 protected void onStop() {
747 super.onStop();
748 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700749
750 if (mLauncherCallbacks != null) {
751 mLauncherCallbacks.onStop();
752 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800753 mAppWidgetHost.setListenIfResumed(false);
Tony Wickham010d2552017-01-20 08:15:28 -0800754
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700755 if (!mAppLaunchSuccess) {
756 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -0800757 mStateManager.getState().containerType, -1);
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700758 }
Tony Wickham010d2552017-01-20 08:15:28 -0800759 NotificationListener.removeNotificationsChangedListener();
Sunny Goyalaf161fd2018-02-05 16:34:57 -0800760 getStateManager().moveToRestState();
Winson Chungedb0d6b2018-03-21 17:32:55 -0700761
Michael Jurkacd496d72013-04-11 11:32:45 -0700762 }
763
764 @Override
765 protected void onStart() {
766 super.onStart();
767 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700768
769 if (mLauncherCallbacks != null) {
770 mLauncherCallbacks.onStart();
771 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800772 mAppWidgetHost.setListenIfResumed(true);
Sunny Goyalcac002d2018-01-04 09:51:45 -0800773 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
Jon Miranda2d89ea82017-05-04 11:47:53 -0700774
Jon Mirandade43a712018-01-18 11:35:10 -0800775 if (mShouldFadeInScrim && mLauncherView.getBackground() != null) {
Jon Miranda2d89ea82017-05-04 11:47:53 -0700776 if (mScrimAnimator != null) {
777 mScrimAnimator.cancel();
778 }
Jon Mirandade43a712018-01-18 11:35:10 -0800779 mLauncherView.getBackground().setAlpha(0);
780 mScrimAnimator = ObjectAnimator.ofInt(mLauncherView.getBackground(),
Jon Miranda2d89ea82017-05-04 11:47:53 -0700781 LauncherAnimUtils.DRAWABLE_ALPHA, 0, 255);
782 mScrimAnimator.addListener(new AnimatorListenerAdapter() {
783 @Override
784 public void onAnimationEnd(Animator animation) {
785 mScrimAnimator = null;
786 }
787 });
788 mScrimAnimator.setDuration(600);
789 mScrimAnimator.setStartDelay(getWindow().getTransitionBackgroundFadeDuration());
790 mScrimAnimator.start();
791 }
Jon Miranda7fda2852017-07-19 16:07:01 -0700792 mShouldFadeInScrim = false;
Winson Chung62a70be2018-02-23 15:10:05 -0800793 UiFactory.onStart(this);
Michael Jurkacd496d72013-04-11 11:32:45 -0700794 }
795
796 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800797 protected void onResume() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700798 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800799 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700800 TraceHelper.partitionSection("ON_RESUME", "superCall");
801
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700802 mAppLaunchSuccess = false;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700803 getUserEventDispatcher().resetElapsedSessionMillis();
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700804 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700805 // Process any items that were added while Launcher was away.
806 InstallShortcutReceiver.disableAndFlushInstallQueue(
807 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700808
Sunny Goyala474a9b2017-05-04 16:47:11 -0700809 // Refresh shortcuts if the permission changed.
810 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700811
Sunny Goyal7ede6112017-12-05 15:11:21 -0800812 DiscoveryBounce.showIfNeeded(this);
Adam Cohen9211d422014-10-07 18:14:53 -0700813 if (mLauncherCallbacks != null) {
814 mLauncherCallbacks.onResume();
815 }
Tracy Zhoua706f002018-03-28 13:55:19 -0700816 UiFactory.onLauncherStateOrResumeChanged(this);
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800817
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700818 TraceHelper.endSection("ON_RESUME");
Adam Cohen06dff352012-06-01 17:17:08 -0700819 }
820
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800821 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700822 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700823 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700824 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700825
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700826 super.onPause();
Joe Onorato24b6fd82009-11-12 13:47:09 -0800827 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700828 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700829
Adam Cohen9211d422014-10-07 18:14:53 -0700830 if (mLauncherCallbacks != null) {
831 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700832 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700833 }
834
Winson Chungd7823942018-02-16 03:23:47 +0000835 @Override
Tracy Zhoua706f002018-03-28 13:55:19 -0700836 protected void onUserLeaveHint() {
837 super.onUserLeaveHint();
838 UiFactory.onLauncherStateOrResumeChanged(this);
839 }
840
841 @Override
Winson Chungd7823942018-02-16 03:23:47 +0000842 public void onWindowFocusChanged(boolean hasFocus) {
843 super.onWindowFocusChanged(hasFocus);
844 mStateManager.onWindowFocusChanged();
845 }
846
Adam Cohenc2d6e892014-10-16 09:49:24 -0700847 public interface LauncherOverlay {
848
849 /**
850 * Touch interaction leading to overscroll has begun
851 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700852 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700853
854 /**
855 * Touch interaction related to overscroll has ended
856 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700857 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700858
859 /**
860 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
861 * screen (or in the case of RTL, the rightmost screen).
862 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700863 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700864
865 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800866 * Called when the launcher is ready to use the overlay
867 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700868 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700869 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700870 }
871
872 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700873 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700874 }
875
876 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
877
Sunny Goyalc86df472016-02-25 09:19:38 -0800878 public void onScrollChanged(float progress) {
879 if (mWorkspace != null) {
880 mWorkspace.onOverlayScrollChanged(progress);
881 }
882 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700883 }
884
Sunny Goyal16764582017-11-06 16:00:34 -0800885 public boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -0700886 if (mLauncherCallbacks != null) {
887 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700888 } else {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800889 // On O and above we there is always some setting present settings (add icon to
890 // home screen or icon badging). On earlier APIs we will have the allow rotation
891 // setting, on devices with a locked orientation,
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700892 return Utilities.ATLEAST_OREO || !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -0700893 }
Jorim Jaggid017f882014-01-14 17:08:48 -0800894 }
895
Sunny Goyalf9403d92017-10-18 10:55:56 -0700896 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -0700897 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700898 }
899
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800900 /**
901 * Restores the previous state, if it exists.
902 *
903 * @param savedState The previous state.
904 */
905 private void restoreState(Bundle savedState) {
906 if (savedState == null) {
907 return;
908 }
909
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700910 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -0700911 LauncherState[] stateValues = LauncherState.values();
912 LauncherState state = stateValues[stateOrdinal];
Sunny Goyalbc683e92017-12-06 10:25:07 -0800913 if (!state.disableRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700914 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800915 }
916
Sunny Goyal2100c782016-08-22 16:00:03 -0700917 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
918 if (requestArgs != null) {
919 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800920 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700921
922 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700923
924 SparseArray<Parcelable> widgetsState =
925 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
926 if (widgetsState != null) {
927 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
928 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800929 }
930
931 /**
932 * Finds all the views we need and configure them properly.
933 */
934 private void setupViews() {
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800935 mDragLayer = findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700936 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -0700937 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700938 mWorkspace.initParentViews(mDragLayer);
Sunny Goyal16764582017-11-06 16:00:34 -0800939 mOverviewPanel = findViewById(R.id.overview_panel);
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800940 mHotseat = findViewById(R.id.hotseat);
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700941 mDragHandleIndicator = findViewById(R.id.drag_indicator);
942 mHotseatSearchBox = findViewById(R.id.search_container_hotseat);
Craig Mautner360310b2012-10-26 15:13:08 -0700943
Sunny Goyal784f9c32016-03-23 16:56:54 -0700944 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
945 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
946 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800947
Winson Chung4c98d922011-05-31 16:50:48 -0700948 // Setup the drag layer
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700949 mDragLayer.setup(mDragController);
Winson Chung4c98d922011-05-31 16:50:48 -0700950
Hyunyoung Song645764e2016-06-06 14:19:02 -0700951 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -0700952 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
953 // default state, otherwise we will update to the wrong offsets in RTL
954 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700955 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700956 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700957
Tony Wickham34d2c912015-09-11 09:27:58 -0700958 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700959 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700960
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700961 // Setup Apps
962 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -0700963
Winson Chung3d503fb2011-07-13 17:25:49 -0700964 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -0700965 mDragController.setMoveTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700966 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -0400967
Sunny Goyal7185dd62018-03-14 17:51:49 -0700968 mAllAppsController.setupViews(mAppsView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800969 }
970
971 /**
972 * Creates a view representing a shortcut.
973 *
974 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800975 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800976 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700977 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800978 }
979
980 /**
981 * Creates a view representing a shortcut inflated from the specified resource.
982 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800983 * @param parent The group the shortcut belongs to.
984 * @param info The data structure describing the shortcut.
985 *
986 * @return A View inflated from layoutResId.
987 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700988 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -0800989 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
990 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800991 favorite.applyFromShortcutInfo(info);
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800992 favorite.setOnClickListener(ItemClickHandler.INSTANCE);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700993 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800994 return favorite;
995 }
996
997 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -0700998 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800999 *
1000 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001001 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001002 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001003 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001004 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
1005 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001006 return;
1007 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001008
Jon Mirandac476d6e2017-05-04 16:30:01 -07001009 int[] cellXY = mTmpAddItemCellCoordinates;
1010 CellLayout layout = getCellLayout(container, screenId);
1011
Sunny Goyal782f0c92017-01-19 10:27:54 -08001012 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001013 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001014 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
1015 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001016 }
1017
1018 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001019 // Legacy shortcuts are only supported for primary profile.
1020 info = Process.myUserHandle().equals(args.user)
1021 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001022
Sunny Goyalb57645f2017-03-20 13:57:28 -07001023 if (info == null) {
1024 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1025 return;
1026 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001027 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001028 // The app is trying to add a shortcut without sufficient permissions
1029 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1030 return;
1031 }
1032 }
1033
Jon Mirandac476d6e2017-05-04 16:30:01 -07001034 if (container < 0) {
1035 // Adding a shortcut to the Workspace.
1036 final View view = createShortcut(info);
1037 boolean foundCellSpan = false;
1038 // First we check if we already know the exact location where we want to add this item.
1039 if (cellX >= 0 && cellY >= 0) {
1040 cellXY[0] = cellX;
1041 cellXY[1] = cellY;
1042 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001043
Jon Mirandac476d6e2017-05-04 16:30:01 -07001044 // If appropriate, either create a folder or add to an existing folder
1045 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001046 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001047 return;
1048 }
1049 DragObject dragObject = new DragObject();
1050 dragObject.dragInfo = info;
1051 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1052 true)) {
1053 return;
1054 }
1055 } else {
1056 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1057 }
1058
1059 if (!foundCellSpan) {
1060 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001061 return;
1062 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001063
1064 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1065 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001066 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001067 // Adding a shortcut to a Folder.
Sunny Goyale29897f52017-07-20 10:09:42 -07001068 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001069 if (folderIcon != null) {
1070 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1071 folderInfo.add(info, args.rank, false);
1072 } else {
Sunny Goyale29897f52017-07-20 10:09:42 -07001073 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001074 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001075 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001076 }
1077
Sunny Goyale29897f52017-07-20 10:09:42 -07001078 public FolderIcon findFolderIcon(final long folderIconId) {
1079 return (FolderIcon) mWorkspace.getFirstMatch(new ItemOperator() {
1080 @Override
1081 public boolean evaluate(ItemInfo info, View view) {
1082 return info != null && info.id == folderIconId;
1083 }
1084 });
1085 }
1086
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001087 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001088 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001089 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001090 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001091 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001092 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001093 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1094
Adam Cohened66b2b2012-01-23 17:28:51 -08001095 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001096 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001097 }
Romain Guycbb89e42009-06-08 15:52:54 -07001098
Adam Cohen59400422014-03-05 18:07:04 -08001099 LauncherAppWidgetInfo launcherInfo;
1100 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001101 launcherInfo.spanX = itemInfo.spanX;
1102 launcherInfo.spanY = itemInfo.spanY;
1103 launcherInfo.minSpanX = itemInfo.minSpanX;
1104 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001105 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001106
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001107 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001108 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001109
Sunny Goyal2100c782016-08-22 16:00:03 -07001110 if (hostView == null) {
1111 // Perform actual inflation because we're live
1112 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001113 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001114 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301115 prepareAppWidget(hostView, launcherInfo);
1116 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001117 }
Romain Guycbb89e42009-06-08 15:52:54 -07001118
Sunny Goyald5462aa2016-11-22 16:52:39 +05301119 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001120 hostView.setTag(item);
1121 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001122 hostView.setFocusable(true);
1123 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001124 }
1125
Adam Cohended9f8d2010-11-03 13:25:16 -07001126 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1127 @Override
1128 public void onReceive(Context context, Intent intent) {
1129 final String action = intent.getAction();
1130 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001131 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001132 // processing a multi-step drop
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001133 if (mAppsView != null && mPendingRequestArgs == null) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001134 mStateManager.goToState(NORMAL);
Winson Chung785d2eb2011-04-14 16:08:02 -07001135 }
Jon Miranda7fda2852017-07-19 16:07:01 -07001136 mShouldFadeInScrim = true;
1137 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1138 // ACTION_USER_PRESENT is sent after onStart/onResume. This covers the case where
1139 // the user unlocked and the Launcher is not in the foreground.
1140 mShouldFadeInScrim = false;
Adam Cohended9f8d2010-11-03 13:25:16 -07001141 }
1142 }
1143 };
1144
Tony Wickham010d2552017-01-20 08:15:28 -08001145 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
Sunny Goyal29947f02017-12-18 13:49:44 -08001146 mWorkspace.updateIconBadges(updatedBadges);
Sunny Goyal60180b02018-02-07 12:56:30 -08001147 mAppsView.getAppsStore().updateIconBadges(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001148
Sunny Goyal29947f02017-12-18 13:49:44 -08001149 PopupContainerWithArrow popup = PopupContainerWithArrow.getOpen(Launcher.this);
1150 if (popup != null) {
1151 popup.updateNotificationHeader(updatedBadges);
Tony Wickham010d2552017-01-20 08:15:28 -08001152 }
1153 }
1154
Adam Cohended9f8d2010-11-03 13:25:16 -07001155 @Override
1156 public void onAttachedToWindow() {
1157 super.onAttachedToWindow();
1158
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001159 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyalc86df472016-02-25 09:19:38 -08001160 if (mLauncherCallbacks != null) {
1161 mLauncherCallbacks.onAttachedToWindow();
1162 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001163 }
1164
1165 @Override
1166 public void onDetachedFromWindow() {
1167 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001168
1169 if (mLauncherCallbacks != null) {
1170 mLauncherCallbacks.onDetachedFromWindow();
1171 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001172 }
1173
Sunny Goyal3bac2c22018-02-07 14:44:05 -08001174 public void onQuickstepGestureStarted(boolean isVisible) {
1175 if (mLauncherCallbacks != null) {
1176 mLauncherCallbacks.onQuickstepGestureStarted(isVisible);
1177 }
1178 }
1179
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001180 public AllAppsTransitionController getAllAppsController() {
1181 return mAllAppsController;
1182 }
1183
Sunny Goyal9d69c8d2018-03-19 13:41:31 -07001184 @Override
Winson Chung1a341002018-01-17 10:00:23 -08001185 public LauncherRootView getRootView() {
1186 return (LauncherRootView) mLauncherView;
1187 }
1188
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001189 @Override
Winson Chunga6945242014-01-08 14:04:34 -08001190 public DragLayer getDragLayer() {
1191 return mDragLayer;
1192 }
1193
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001194 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001195 return mAppsView;
1196 }
1197
Winson Chunga6945242014-01-08 14:04:34 -08001198 public Workspace getWorkspace() {
1199 return mWorkspace;
1200 }
1201
1202 public Hotseat getHotseat() {
1203 return mHotseat;
1204 }
1205
Sunny Goyal81b4c7b2018-03-26 12:10:31 -07001206 public View getDragHandleIndicator() {
1207 return mDragHandleIndicator;
1208 }
1209
1210 public View getHotseatSearchBox() {
1211 return mHotseatSearchBox;
1212 }
1213
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001214 public <T extends View> T getOverviewPanel() {
Winson Chung8ae41982017-11-10 11:42:13 -08001215 return (T) mOverviewPanel;
Winson Chunga6945242014-01-08 14:04:34 -08001216 }
1217
Sunny Goyal47328fd2016-05-25 18:56:41 -07001218 public DropTargetBar getDropTargetBar() {
1219 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001220 }
1221
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001222 public LauncherAppWidgetHost getAppWidgetHost() {
1223 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001224 }
Romain Guycbb89e42009-06-08 15:52:54 -07001225
Winson Chunga9abd0e2010-10-27 17:18:37 -07001226 public LauncherModel getModel() {
1227 return mModel;
1228 }
1229
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001230 public ModelWriter getModelWriter() {
1231 return mModelWriter;
1232 }
1233
Adam Cohen79d90c52016-04-22 13:29:20 -07001234 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001235 return mSharedPrefs;
1236 }
1237
Sunny Goyalf8d56fc2018-01-31 15:18:11 -08001238 public int getOrientation() { return mOldConfig.orientation; }
Jon Miranda6bed3502017-09-19 14:43:17 -07001239
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001240 @Override
1241 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001242 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001243 super.onNewIntent(intent);
1244
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001245 boolean alreadyOnHome = hasWindowFocus() && ((intent.getFlags() &
Winson15f8b172015-08-19 11:02:39 -07001246 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1247 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001248
1249 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001250 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001251 && AbstractFloatingView.getTopOpenView(this) == null;
Winson15f8b172015-08-19 11:02:39 -07001252 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001253 boolean internalStateHandled = InternalStateHandler
Sunny Goyal8b264562018-02-03 23:00:13 -08001254 .handleNewIntent(this, intent, isStarted());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001255
Winson15f8b172015-08-19 11:02:39 -07001256 if (isActionMain) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001257 if (!internalStateHandled) {
1258 // Note: There should be at most one log per method call. This is enforced
1259 // implicitly by using if-else statements.
1260 UserEventDispatcher ued = getUserEventDispatcher();
1261 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
1262 if (topOpenView != null) {
1263 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
1264 } else if (alreadyOnHome) {
1265 Target target = newContainerTarget(mStateManager.getState().containerType);
1266 target.pageIndex = mWorkspace.getCurrentPage();
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001267 ued.logActionCommand(Action.Command.HOME_INTENT, target,
1268 newContainerTarget(ContainerType.WORKSPACE));
Sunny Goyald3864fa2017-11-14 15:06:36 -08001269 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001270
Sunny Goyald3864fa2017-11-14 15:06:36 -08001271 // In all these cases, only animate if we're already on home
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001272 AbstractFloatingView.closeAllOpenViews(this, isStarted());
Jon Mirandafeba90f2016-10-06 10:53:29 -07001273
Sunny Goyala2467272018-03-16 14:53:05 -07001274 if (!isInState(NORMAL)) {
1275 // Only change state, if not already the same. This prevents cancelling any
1276 // animations running as part of resume
1277 mStateManager.goToState(NORMAL);
1278 }
Sunny Goyald3864fa2017-11-14 15:06:36 -08001279
1280 // Reset the apps view
1281 if (!alreadyOnHome && mAppsView != null) {
Sunny Goyal7185dd62018-03-14 17:51:49 -07001282 mAppsView.reset(isStarted() /* animate */);
Sunny Goyald3864fa2017-11-14 15:06:36 -08001283 }
1284
1285 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()) {
1286 mWorkspace.post(mWorkspace::moveToDefaultScreen);
1287 }
1288 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001289
1290 final View v = getWindow().peekDecorView();
1291 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001292 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001293 }
1294
Adam Cohen9211d422014-10-07 18:14:53 -07001295 if (mLauncherCallbacks != null) {
1296 mLauncherCallbacks.onHomeIntent();
1297 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001298 }
Winson15f8b172015-08-19 11:02:39 -07001299
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001300 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001301 }
1302
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001303 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001304 public void onRestoreInstanceState(Bundle state) {
1305 super.onRestoreInstanceState(state);
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001306 mWorkspace.restoreInstanceStateForChild(mSynchronouslyBoundPage);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001307 }
1308
1309 @Override
1310 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001311 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001312 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001313
Adam Cohen21cd0022013-10-09 18:57:02 -07001314 }
Sunny Goyalea609262017-10-25 15:47:38 -07001315 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001316
1317
1318 AbstractFloatingView widgets = AbstractFloatingView
1319 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1320 if (widgets != null) {
1321 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1322 widgets.saveHierarchyState(widgetsState);
1323 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1324 } else {
1325 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1326 }
1327
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001328 // We close any open folders and shortcut containers since they will not be re-opened,
1329 // and we need to make sure this state is reflected.
1330 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001331
Sunny Goyal2100c782016-08-22 16:00:03 -07001332 if (mPendingRequestArgs != null) {
1333 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1334 }
1335 if (mPendingActivityResult != null) {
1336 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001337 }
1338
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001339 super.onSaveInstanceState(outState);
1340
Adam Cohen9211d422014-10-07 18:14:53 -07001341 if (mLauncherCallbacks != null) {
1342 mLauncherCallbacks.onSaveInstanceState(outState);
1343 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001344 }
1345
1346 @Override
1347 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001348 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001349
Sunny Goyal5cc7bbd2017-06-12 09:50:39 -07001350 unregisterReceiver(mReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001351 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001352
Winson Chungcd2b0142011-06-08 16:02:26 -07001353 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001354 // It's possible to receive onDestroy after a new Launcher activity has
1355 // been created. In this case, don't interfere with the new Launcher.
1356 if (mModel.isCurrentCallbacks(this)) {
1357 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001358 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001359 }
Sunny Goyal623eddd2018-03-02 12:24:41 -08001360 mRotationHelper.destroy();
Winson Chungedb0d6b2018-03-21 17:32:55 -07001361 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mSharedPrefsListener);
Sunny Goyal745bad92016-05-02 10:54:12 -07001362
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001363 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001364 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001365 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001366 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001367 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001368
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001369 TextKeyListener.getInstance().release();
Mario Bertschler27288382017-05-24 15:35:09 -07001370 WallpaperColorInfo.getInstance(this).setOnThemeChangeListener(null);
1371
Michael Jurka2ecf9952012-06-18 12:52:28 -07001372 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001373
Tony2917a8b2017-07-31 23:29:42 -07001374 clearPendingBinds();
1375
Adam Cohen9211d422014-10-07 18:14:53 -07001376 if (mLauncherCallbacks != null) {
1377 mLauncherCallbacks.onDestroy();
1378 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001379 }
1380
Sunny Goyalae502842016-06-17 08:43:56 -07001381 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1382 return mAccessibilityDelegate;
1383 }
1384
Adam Cohena9cf38f2011-05-02 15:36:58 -07001385 public DragController getDragController() {
1386 return mDragController;
1387 }
1388
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001389 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001390 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001391 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001392 }
1393
1394 @Override
1395 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1396 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001397 try {
1398 super.startIntentSenderForResult(intent, requestCode,
1399 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1400 } catch (IntentSender.SendIntentException e) {
1401 throw new ActivityNotFoundException();
1402 }
1403 }
1404
Winson Chung70d72102011-08-12 11:24:35 -07001405 /**
1406 * Indicates that we want global search for this activity by setting the globalSearch
1407 * argument for {@link #startSearch} to true.
1408 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001409 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001410 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001411 Bundle appSearchData, boolean globalSearch) {
1412 if (appSearchData == null) {
1413 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001414 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001415 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001416
Sunny Goyal6f28e712016-09-13 14:19:29 -07001417 if (mLauncherCallbacks == null ||
1418 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1419 // Starting search from the callbacks failed. Start the default global search.
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001420 super.startSearch(initialQuery, selectInitialQuery, appSearchData, true);
Ian Parkinson047036e2014-09-01 15:40:53 +01001421 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001422
1423 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001424 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001425 }
1426
Joe Onorato9c1289c2009-08-17 11:03:03 -04001427 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001428 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001429 }
1430
Adam Cohen517a7f52014-03-01 12:12:59 -08001431 public boolean isWorkspaceLoading() {
1432 return mWorkspaceLoading;
1433 }
1434
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001435 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001436 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001437 }
1438
Sunny Goyal04a324a2017-01-20 21:08:59 -08001439 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001440 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001441 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001442
Sunny Goyal2100c782016-08-22 16:00:03 -07001443 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001444 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001445 if (LOGD) {
1446 Log.d(TAG, "Adding widget from drop");
1447 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001448 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001449 }
1450
Sunny Goyal2100c782016-08-22 16:00:03 -07001451 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001452 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1453 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1454 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001455
Adam Cohenad4e15c2013-10-17 16:21:35 -07001456 Runnable onComplete = new Runnable() {
1457 @Override
1458 public void run() {
1459 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001460 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001461 }
1462 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001463 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001464 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001465 }
1466 }
Romain Guycbb89e42009-06-08 15:52:54 -07001467
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001468 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1469 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001470 info.container = container;
1471 info.screenId = screenId;
1472 if (cell != null) {
1473 info.cellX = cell[0];
1474 info.cellY = cell[1];
1475 }
1476 info.spanX = spanX;
1477 info.spanY = spanY;
1478
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001479 switch (info.itemType) {
1480 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1481 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001482 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001483 break;
1484 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001485 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001486 break;
1487 default:
1488 throw new IllegalStateException("Unknown item type: " + info.itemType);
1489 }
1490 }
1491
Adam Cohenfbba09b2011-07-18 21:43:05 -07001492 /**
1493 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001494 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001495 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001496 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1497 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001498 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1499 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1500 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001501 }
1502
Adam Cohenfbba09b2011-07-18 21:43:05 -07001503 /**
1504 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001505 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001506 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001507 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001508 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001509 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001510 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001511 // In the case where we've prebound the widget, we remove it from the DragLayer
1512 if (LOGD) {
1513 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1514 }
1515 getDragLayer().removeView(hostView);
1516
Adam Cohened66b2b2012-01-23 17:28:51 -08001517 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001518 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001519
1520 // Clear the boundWidget so that it doesn't get destroyed.
1521 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001522 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001523 // In this case, we either need to start an activity to get permission to bind
1524 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001525 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1526 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1527 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1528 this, info.componentName);
1529 } else {
1530 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1531 }
Adam Cohendd70d662012-10-04 16:53:44 -07001532 Bundle options = info.bindOptions;
1533
Sunny Goyalffe83f12014-08-14 17:39:34 -07001534 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1535 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001536 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001537 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001538 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001539 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001540 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001541 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001542 }
1543
Adam Cohendcd297f2013-06-18 13:13:40 -07001544 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001545 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001546 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001547 folderInfo.title = getText(R.string.folder_name);
1548
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001550 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001551
1552 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001553 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301554 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001555 // Force measure the new folder icon
1556 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1557 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001558 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001559 }
Romain Guycbb89e42009-06-08 15:52:54 -07001560
Winsonc0b52fe2015-09-09 16:38:15 -07001561 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001562 * Unbinds the view for the specified item, and removes the item and all its children.
1563 *
1564 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001565 * @param itemInfo the {@link ItemInfo} for this view.
1566 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001567 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001568 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001569 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001570 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001571 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1572 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001573 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001574 } else {
1575 mWorkspace.removeWorkspaceItem(v);
1576 }
Winsonc0b52fe2015-09-09 16:38:15 -07001577 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001578 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001579 }
1580 } else if (itemInfo instanceof FolderInfo) {
1581 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001582 if (v instanceof FolderIcon) {
1583 ((FolderIcon) v).removeListeners();
1584 }
Winsonc0b52fe2015-09-09 16:38:15 -07001585 mWorkspace.removeWorkspaceItem(v);
1586 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001587 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001588 }
1589 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1590 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001591 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001592 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07001593 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001594 }
1595 } else {
1596 return false;
1597 }
1598 return true;
1599 }
1600
1601 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07001602 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07001603 */
Sunny Goyal56c73602015-09-25 12:55:01 -07001604 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07001605 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07001606 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07001607 // Deleting an app widget ID is a void call but writes to disk before returning
1608 // to the caller...
1609 new AsyncTask<Void, Void, Void>() {
1610 public Void doInBackground(Void ... args) {
1611 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
1612 return null;
1613 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07001614 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07001615 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001616 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001617 }
1618
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001619 @Override
1620 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001621 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001622 }
1623
Joe Onorato88ec0992009-11-19 13:16:06 -08001624 @Override
1625 public void onBackPressed() {
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08001626 if (finishAutoCancelActionMode()) {
1627 return;
1628 }
Adam Cohen9211d422014-10-07 18:14:53 -07001629 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1630 return;
1631 }
1632
Sunny Goyal45478022015-06-08 16:52:41 -07001633 if (mDragController.isDragging()) {
1634 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001635 return;
1636 }
1637
Jon Mirandafeba90f2016-10-06 10:53:29 -07001638 // Note: There should be at most one log per method call. This is enforced implicitly
1639 // by using if-else statements.
1640 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001641 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
1642 if (topView != null) {
Sunny Goyal37920962017-09-28 13:43:24 -07001643 topView.onBackPressed();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001644 } else if (!isInState(NORMAL)) {
Sunny Goyal49bcf342018-01-11 13:59:53 -08001645 LauncherState lastState = mStateManager.getLastState();
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001646 ued.logActionCommand(Action.Command.BACK, mStateManager.getState().containerType,
1647 lastState.containerType);
Sunny Goyal49bcf342018-01-11 13:59:53 -08001648 mStateManager.goToState(lastState);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001649 } else {
1650 // Back button is a no-op here, but give at least some feedback for the button press
1651 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001652 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001653 }
1654
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001655 /**
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001656 * Event handler for the wallpaper picker button that appears after a long press
1657 * on the home screen.
1658 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001659 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07001660 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07001661 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
1662 return;
1663 }
Tony Wickham785f7a52015-08-31 17:28:32 -07001664 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
1665 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001666 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001667 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001668
1669 String pickerPackage = getString(R.string.wallpaper_picker_package);
Sunny Goyal112c9272018-03-28 14:41:02 -07001670 if (!TextUtils.isEmpty(pickerPackage)) {
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001671 intent.setPackage(pickerPackage);
Sunny Goyala5ace712017-11-15 17:12:51 -08001672 } else {
Sunny Goyal112c9272018-03-28 14:41:02 -07001673 // If there is no target package, use the default intent chooser animation
1674 intent.putExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION, true);
Sunny Goyala5ace712017-11-15 17:12:51 -08001675 }
Sunny Goyal112c9272018-03-28 14:41:02 -07001676 startActivitySafely(v, intent, null);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001677 }
1678
Sunny Goyalfe770c92016-09-27 14:38:58 -07001679 @TargetApi(Build.VERSION_CODES.M)
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001680 @Override
Tony Wickham005df0b2018-02-13 11:28:12 -08001681 public ActivityOptions getActivityLaunchOptions(View v, boolean useDefaultLaunchOptions) {
Jon Miranda17940f02018-01-18 14:46:34 -08001682 return useDefaultLaunchOptions
Jon Miranda54441f52018-01-24 15:38:25 -08001683 ? mAppTransitionManager.getDefaultActivityLaunchOptions(this, v)
1684 : mAppTransitionManager.getActivityLaunchOptions(this, v);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001685 }
1686
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001687 @TargetApi(Build.VERSION_CODES.M)
1688 @Override
1689 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
1690 // Due to legacy reasons, direct call shortcuts require Launchers to have the
1691 // corresponding permission. Show the appropriate permission prompt if that
1692 // is the case.
1693 if (intent.getComponent() == null
1694 && Intent.ACTION_CALL.equals(intent.getAction())
1695 && checkSelfPermission(android.Manifest.permission.CALL_PHONE) !=
1696 PackageManager.PERMISSION_GRANTED) {
1697
1698 setWaitingForResult(PendingRequestArgs
1699 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
1700 requestPermissions(new String[]{android.Manifest.permission.CALL_PHONE},
1701 REQUEST_PERMISSION_CALL_PHONE);
1702 return true;
1703 } else {
1704 return false;
1705 }
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001706 }
1707
Sunny Goyala7ce1662016-05-31 15:01:35 -07001708 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001709 mAppLaunchSuccess = super.startActivitySafely(v, intent, item);
1710 if (mAppLaunchSuccess && v instanceof BubbleTextView) {
1711 // This is set to the view that launched the activity that navigated the user away
1712 // from launcher. Since there is no callback for when the activity has finished
1713 // launching, enable the press state and keep this reference to reset the press
1714 // state when we return to launcher.
1715 BubbleTextView btv = (BubbleTextView) v;
1716 btv.setStayPressed(true);
1717 setOnResumeCallback(btv);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001718 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07001719 return mAppLaunchSuccess;
Michael Jurka86a720e2012-05-09 11:23:23 -07001720 }
1721
Winson Chung3d503fb2011-07-13 17:25:49 -07001722 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001723 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07001724 return mHotseat != null && layout != null &&
1725 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
1726 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001727
Winson Chung3d503fb2011-07-13 17:25:49 -07001728 /**
1729 * Returns the CellLayout of the specified container at the specified screen.
1730 */
Sunny Goyal92820592015-03-02 11:31:35 -08001731 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001732 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1733 if (mHotseat != null) {
1734 return mHotseat.getLayout();
1735 } else {
1736 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08001737 }
Winson Chung3d503fb2011-07-13 17:25:49 -07001738 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08001739 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08001740 }
1741 }
1742
Michael Jurkae326f182011-11-21 14:05:46 -08001743 @Override
1744 public void onTrimMemory(int level) {
1745 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07001746 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
1747 // The widget preview db can result in holding onto over
1748 // 3MB of memory for caching which isn't necessary.
1749 SQLiteDatabase.releaseMemory();
1750
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08001751 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07001752 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08001753 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01001754 if (mLauncherCallbacks != null) {
1755 mLauncherCallbacks.onTrimMemory(level);
1756 }
Winson Chung62a70be2018-02-23 15:10:05 -08001757 UiFactory.onTrimMemory(this, level);
Michael Jurkae326f182011-11-21 14:05:46 -08001758 }
1759
Michael Jurkad7c28052012-04-27 15:43:36 -07001760 @Override
1761 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07001762 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07001763 final List<CharSequence> text = event.getText();
1764 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07001765 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001766 // TODO: When can workspace be null?
1767 text.add(mWorkspace == null
1768 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07001769 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07001770 return result;
1771 }
1772
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001773 public void setOnResumeCallback(OnResumeCallback callback) {
1774 if (mOnResumeCallback != null) {
1775 mOnResumeCallback.onLauncherResume();
1776 }
1777 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07001778 }
1779
Michael Jurka7607c2f2013-04-03 14:33:19 -07001780 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001781 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001782 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07001783 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001784 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07001785 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001786 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07001787 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07001788 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07001789 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001790 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001791
Joe Onorato9c1289c2009-08-17 11:03:03 -04001792 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07001793 * Clear any pending bind callbacks. This is called when is loader is planning to
1794 * perform a full rebind from scratch.
1795 */
1796 @Override
1797 public void clearPendingBinds() {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001798 if (mPendingExecutor != null) {
1799 mPendingExecutor.markCompleted();
1800 mPendingExecutor = null;
1801 }
1802 }
1803
1804 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001805 * Refreshes the shortcuts shown on the workspace.
1806 *
1807 * Implementation of the method from LauncherModel.Callbacks.
1808 */
1809 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001810 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001811 // Floating panels (except the full widget sheet) are associated with individual icons. If
1812 // we are starting a fresh bind, close all such panels as all the icons are about
1813 // to go away.
1814 AbstractFloatingView.closeOpenViews(this, true,
Sunny Goyal7ede6112017-12-05 15:11:21 -08001815 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_REBIND_SAFE);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08001816
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001817 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08001818
Winson Chungd64d1762013-08-20 14:37:16 -07001819 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07001820 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07001821 mWorkspace.removeAllWorkspaceScreens();
Sunny Goyalc11fac32018-02-27 19:20:15 -08001822 mAppWidgetHost.clearViews();
Winson Chungd64d1762013-08-20 14:37:16 -07001823
Winson Chung3d503fb2011-07-13 17:25:49 -07001824 if (mHotseat != null) {
Sunny Goyal11e96492018-03-23 17:06:00 -07001825 mHotseat.resetLayout(mDeviceProfile.isVerticalBarLayout());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001826 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001827 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001828 }
1829
Adam Cohendcd297f2013-06-18 13:13:40 -07001830 @Override
1831 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001832 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001833 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
1834 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001835 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
1836 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001837 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001838 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
1839 // If there are no screens, we need to have an empty screen
1840 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07001841 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001842 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07001843
Winsonc7d2e832016-07-28 12:24:55 -07001844 // After we have added all the screens, if the wallpaper was locked to the default state,
1845 // then notify to indicate that it can be released and a proper wallpaper offset can be
1846 // computed before the next layout
1847 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07001848 }
1849
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001850 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001851 int count = orderedScreenIds.size();
1852 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001853 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001854 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001855 // No need to bind the first screen, as its always bound.
1856 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
1857 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001858 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001859 }
1860
Sunny Goyalb23980c2017-08-17 07:45:25 -07001861 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08001862 public void bindAppsAdded(ArrayList<Long> newScreens, ArrayList<ItemInfo> addNotAnimated,
1863 ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07001864 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08001865 if (newScreens != null) {
1866 bindAddScreens(newScreens);
1867 }
Winson Chungd64d1762013-08-20 14:37:16 -07001868
1869 // We add the items without animation on non-visible pages, and with
1870 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08001871 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001872 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001873 }
Adam Cohen76a47a12014-02-05 11:47:43 -08001874 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001875 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07001876 }
Winson Chungc58497e2013-09-03 17:48:37 -07001877
Winson Chung87412982013-10-03 18:34:14 -07001878 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07001879 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001880 }
1881
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001882 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001883 * Bind the items start-end from the list.
1884 *
1885 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001886 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07001887 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07001888 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001889 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07001890 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001891 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07001892 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001893 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001894 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07001895 int end = items.size();
1896 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001897 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07001898
1899 // Short circuit if we are loading dock items for a configuration which has no dock
1900 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
1901 mHotseat == null) {
1902 continue;
1903 }
1904
Sunny Goyal639e9062015-08-19 19:17:06 -07001905 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001906 switch (item.itemType) {
1907 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1908 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08001909 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001910 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07001911 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001912 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001913 }
1914 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07001915 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001916 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001917 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001918 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001919 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07001920 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1921 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07001922 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07001923 if (view == null) {
1924 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001925 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08001926 break;
1927 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001928 default:
1929 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001930 }
Sunny Goyal639e9062015-08-19 19:17:06 -07001931
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001932 /*
1933 * Remove colliding items.
1934 */
1935 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1936 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
1937 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
1938 View v = cl.getChildAt(item.cellX, item.cellY);
1939 Object tag = v.getTag();
1940 String desc = "Collision while binding workspace item: " + item
1941 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08001942 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001943 throw (new RuntimeException(desc));
1944 } else {
1945 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001946 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001947 continue;
1948 }
1949 }
1950 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05301951 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07001952 if (animateIcons) {
1953 // Animate all the applications up now
1954 view.setAlpha(0f);
1955 view.setScaleX(0f);
1956 view.setScaleY(0f);
1957 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08001958 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07001959 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001960 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07001961
Winson Chung997a9232013-07-24 15:33:46 -07001962 if (animateIcons) {
1963 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08001964 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07001965 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08001966 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07001967 final Runnable startBounceAnimRunnable = new Runnable() {
1968 public void run() {
1969 anim.playTogether(bounceAnims);
1970 anim.start();
1971 }
1972 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08001973 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07001974 // We post the animation slightly delayed to prevent slowdowns
1975 // when we are loading right after we return to launcher.
1976 mWorkspace.postDelayed(new Runnable() {
1977 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00001978 if (mWorkspace != null) {
Jon Miranda64eb8ea2018-03-29 11:43:58 -07001979 AbstractFloatingView.closeAllOpenViews(Launcher.this, false);
1980
Ian Parkinson94449152014-01-21 13:09:59 +00001981 mWorkspace.snapToPage(newScreenIndex);
1982 mWorkspace.postDelayed(startBounceAnimRunnable,
1983 NEW_APPS_ANIMATION_DELAY);
1984 }
Winson Chung94d67682013-09-25 16:29:40 -07001985 }
1986 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07001987 } else {
1988 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07001989 }
1990 }
Winson Chung64359a52013-07-08 17:17:08 -07001991 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001992 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001993 }
1994
Joe Onorato9c1289c2009-08-17 11:03:03 -04001995 /**
1996 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001997 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07001998 public void bindAppWidget(LauncherAppWidgetInfo item) {
1999 View view = inflateAppWidget(item);
2000 if (view != null) {
2001 mWorkspace.addInScreen(view, item);
2002 mWorkspace.requestLayout();
2003 }
2004 }
2005
2006 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002007 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302008 PendingAppWidgetHostView view =
2009 new PendingAppWidgetHostView(this, item, mIconCache, true);
2010 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002011 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002012 }
2013
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002014 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002015
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002016 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08002017
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002018 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2019 // If the provider is not ready, bind as a pending widget.
2020 appWidgetInfo = null;
2021 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2022 // The widget id is not valid. Try to find the widget based on the provider info.
2023 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
2024 } else {
2025 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
2026 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002027
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002028 // If the provider is ready, but the width is not yet restored, try to restore it.
2029 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
2030 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07002031 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002032 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
2033 + " belongs to component " + item.providerName
2034 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002035 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002036 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002037 }
Sunny Goyalff572272014-07-23 13:58:07 -07002038
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002039 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002040 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07002041 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2042 // Id has not been allocated yet. Allocate a new id.
2043 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
2044 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07002045
Sunny Goyald478c832016-04-01 12:04:16 -07002046 // Also try to bind the widget. If the bind fails, the user will be shown
2047 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08002048 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07002049 pendingInfo.spanX = item.spanX;
2050 pendingInfo.spanY = item.spanY;
2051 pendingInfo.minSpanX = item.minSpanX;
2052 pendingInfo.minSpanY = item.minSpanY;
2053 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07002054
2055 boolean isDirectConfig =
2056 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2057 if (isDirectConfig && item.bindOptions != null) {
2058 Bundle newOptions = item.bindOptions.getExtras();
2059 if (options != null) {
2060 newOptions.putAll(options);
2061 }
2062 options = newOptions;
2063 }
Sunny Goyald478c832016-04-01 12:04:16 -07002064 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2065 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002066
Sunny Goyal86df1382016-08-10 15:03:22 -07002067 // We tried to bind once. If we were not able to bind, we would need to
2068 // go through the permission dialog, which means we cannot skip the config
2069 // activity.
2070 item.bindOptions = null;
2071 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2072
Sunny Goyald478c832016-04-01 12:04:16 -07002073 // Bind succeeded
2074 if (success) {
2075 // If the widget has a configure activity, it is still needs to set it up,
2076 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002077 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002078 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2079 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002080 }
Sunny Goyald478c832016-04-01 12:04:16 -07002081
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002082 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002083 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002084 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002085 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002086 // The widget was marked as UI not ready, but there is no configure activity to
2087 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002088 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002089 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002090 }
Sunny Goyalff572272014-07-23 13:58:07 -07002091 }
2092
Sunny Goyald5462aa2016-11-22 16:52:39 +05302093 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002094 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002095 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002096 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002097 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07002098 deleteWidgetInfo(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002099 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002100 }
2101
Sunny Goyal233ee962015-08-03 13:05:01 -07002102 item.minSpanX = appWidgetInfo.minSpanX;
2103 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302104 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002105 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302106 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002107 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302108 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002109
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002110 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002111 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002112 }
2113
Sunny Goyalff572272014-07-23 13:58:07 -07002114 /**
2115 * Restores a pending widget.
2116 *
2117 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002118 */
Sunny Goyald478c832016-04-01 12:04:16 -07002119 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002120 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2121 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2122 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002123 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002124 }
2125
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002126 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002127 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002128 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2129 info.pendingItemInfo = null;
2130 }
Sunny Goyalff572272014-07-23 13:58:07 -07002131
Sunny Goyalba47faa2017-10-04 16:50:57 -07002132 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002133 view.reInflate();
Sunny Goyalba47faa2017-10-04 16:50:57 -07002134 }
2135
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002136 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002137 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002138 }
2139
Adam Cohen1462de32012-07-24 22:34:36 -07002140 public void onPageBoundSynchronously(int page) {
Sunny Goyalf4aceab2017-12-06 16:16:31 -08002141 mSynchronouslyBoundPage = page;
Adam Cohen1462de32012-07-24 22:34:36 -07002142 }
2143
Sunny Goyal527c7d32015-08-28 15:19:36 -07002144 @Override
2145 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2146 if (mPendingExecutor != null) {
2147 mPendingExecutor.markCompleted();
2148 }
2149 mPendingExecutor = executor;
Sunny Goyal60180b02018-02-07 12:56:30 -08002150 if (!isInState(ALL_APPS)) {
2151 mAppsView.getAppsStore().setDeferUpdates(true);
2152 mPendingExecutor.execute(() -> mAppsView.getAppsStore().setDeferUpdates(false));
2153 }
2154
Sunny Goyal527c7d32015-08-28 15:19:36 -07002155 executor.attachTo(this);
2156 }
2157
2158 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2159 if (mPendingExecutor == executor) {
2160 mPendingExecutor = null;
2161 }
2162 }
2163
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002164 @Override
2165 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002166 if (mDragLayer.getAlpha() < 1) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002167 mDragLayer.animate().alpha(1).withEndAction(
2168 executor == null ? null : executor::onLoadAnimationCompleted).start();
2169 } else if (executor != null) {
2170 executor.onLoadAnimationCompleted();
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002171 }
2172 }
2173
Joe Onorato9c1289c2009-08-17 11:03:03 -04002174 /**
2175 * Callback saying that there aren't any more items to bind.
2176 *
2177 * Implementation of the method from LauncherModel.Callbacks.
2178 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002179 public void finishBindingItems() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002180 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002181 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002182
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002183 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002184
Sunny Goyal2100c782016-08-22 16:00:03 -07002185 if (mPendingActivityResult != null) {
2186 handleActivityResult(mPendingActivityResult.requestCode,
2187 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2188 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002189 }
2190
Sunny Goyala474a9b2017-05-04 16:47:11 -07002191 InstallShortcutReceiver.disableAndFlushInstallQueue(
2192 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002193
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002194 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002195 }
2196
Winson Chunga2413752012-04-03 14:22:34 -07002197 private boolean canRunNewAppsAnimation() {
2198 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002199 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002200 }
2201
Winson Chungc9168342013-06-26 14:54:55 -07002202 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002203 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07002204 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
2205 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002206 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002207 return bounceAnim;
2208 }
2209
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002210 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002211 * Add the icons for all apps.
2212 *
2213 * Implementation of the method from LauncherModel.Callbacks.
2214 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002215 public void bindAllApplications(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002216 mAppsView.getAppsStore().setApps(apps);
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002217
Adam Cohen9211d422014-10-07 18:14:53 -07002218 if (mLauncherCallbacks != null) {
2219 mLauncherCallbacks.bindAllApplications(apps);
2220 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002221 }
2222
2223 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002224 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
2225 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2226 */
2227 @Override
2228 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002229 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002230 }
2231
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002232 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002233 * A package was updated.
2234 *
2235 * Implementation of the method from LauncherModel.Callbacks.
2236 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002237 @Override
2238 public void bindAppsAddedOrUpdated(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002239 mAppsView.getAppsStore().addOrUpdateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002240 }
2241
Sunny Goyal4390ace2014-10-13 11:33:11 -07002242 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002243 public void bindPromiseAppProgressUpdated(PromiseAppInfo app) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002244 mAppsView.getAppsStore().updatePromiseAppProgress(app);
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002245 }
2246
2247 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002248 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002249 mWorkspace.widgetsRestored(widgets);
2250 }
2251
Joe Onorato9c1289c2009-08-17 11:03:03 -04002252 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002253 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002254 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002255 *
2256 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002257 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002258 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002259 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002260 if (!updated.isEmpty()) {
2261 mWorkspace.updateShortcuts(updated);
2262 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002263 }
2264
2265 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002266 * Update the state of a package, typically related to install state.
2267 *
2268 * Implementation of the method from LauncherModel.Callbacks.
2269 */
Sunny Goyale755d462014-07-22 13:48:29 -07002270 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002271 public void bindRestoreItemsChange(HashSet<ItemInfo> updates) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07002272 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07002273 }
2274
2275 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002276 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07002277 * in addition to specific applications to remove, the reason being that
2278 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002279 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07002280 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002281 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07002282 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002283 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002284 mWorkspace.removeItemsByMatcher(matcher);
2285 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002286 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002287
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002288 @Override
2289 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002290 mAppsView.getAppsStore().removeApps(appInfos);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002291 }
Joe Onoratobe386092009-11-17 17:32:16 -08002292
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002293 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002294 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
2295 mPopupDataProvider.setAllWidgets(allWidgets);
Tony Wickham26b17462017-03-20 17:12:24 -07002296 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
2297 if (topView != null) {
2298 topView.onWidgetsBound();
2299 }
2300 }
2301
Tony Wickham86222d22017-03-29 15:30:43 -07002302 /**
2303 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
2304 * refreshes the widgets and shortcuts associated with the given package/user
2305 */
2306 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07002307 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002308 }
2309
Winson Chung80baf5a2010-08-09 16:03:15 -07002310 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002311 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08002312 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002313 @Override
2314 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
2315 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002316
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002317 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
2318 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07002319 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002320 writer.println(prefix + " Homescreen " + i);
2321
2322 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
2323 for (int j = 0; j < layout.getChildCount(); j++) {
2324 Object tag = layout.getChildAt(j).getTag();
2325 if (tag != null) {
2326 writer.println(prefix + " " + tag.toString());
2327 }
2328 }
2329 }
2330
2331 writer.println(prefix + " Hotseat");
2332 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07002333 for (int j = 0; j < layout.getChildCount(); j++) {
2334 Object tag = layout.getChildAt(j).getTag();
2335 if (tag != null) {
2336 writer.println(prefix + " " + tag.toString());
2337 }
Adam Cohen4caf2982013-08-20 18:54:31 -07002338 }
Sunny Goyala1365452015-10-01 15:46:24 -07002339 }
2340
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002341 writer.println(prefix + "Misc:");
2342 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
2343 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
2344 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
Sunny Goyal2bba1902018-02-27 12:20:50 -08002345 writer.println(" deviceProfile isTransposed=" + getDeviceProfile().isVerticalBarLayout());
2346 writer.println(" orientation=" + getResources().getConfiguration().orientation);
2347
2348 try {
2349 FileLog.flushAll(writer);
2350 } catch (Exception e) {
2351 // Ignore
2352 }
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002353
2354 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002355
Adam Cohen9211d422014-10-07 18:14:53 -07002356 if (mLauncherCallbacks != null) {
2357 mLauncherCallbacks.dump(prefix, fd, writer, args);
2358 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002359 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002360
Sunny Goyal66b24572016-09-21 15:57:55 -07002361 @Override
2362 @TargetApi(Build.VERSION_CODES.N)
2363 public void onProvideKeyboardShortcuts(
2364 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
2365
2366 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002367 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002368 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
2369 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
2370 }
2371 View currentFocus = getCurrentFocus();
2372 if (new CustomActionsPopup(this, currentFocus).canShow()) {
2373 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
2374 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
2375 }
Tony18c4aa42017-05-10 21:23:57 -05002376 if (currentFocus.getTag() instanceof ItemInfo
2377 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002378 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
2379 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
2380 }
2381 if (!shortcutInfos.isEmpty()) {
2382 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
2383 }
2384
2385 super.onProvideKeyboardShortcuts(data, menu, deviceId);
2386 }
2387
2388 @Override
2389 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
2390 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
2391 switch (keyCode) {
2392 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002393 if (isInState(NORMAL)) {
2394 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07002395 return true;
2396 }
2397 break;
2398 case KeyEvent.KEYCODE_S: {
2399 View focusedView = getCurrentFocus();
2400 if (focusedView instanceof BubbleTextView
2401 && focusedView.getTag() instanceof ItemInfo
2402 && mAccessibilityDelegate.performAction(focusedView,
2403 (ItemInfo) focusedView.getTag(),
2404 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal00ac9202017-11-09 15:24:06 -08002405 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07002406 return true;
2407 }
2408 break;
2409 }
2410 case KeyEvent.KEYCODE_O:
2411 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
2412 return true;
2413 }
2414 break;
2415 }
2416 }
2417 return super.onKeyShortcut(keyCode, event);
2418 }
2419
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002420 @Override
2421 public boolean onKeyUp(int keyCode, KeyEvent event) {
2422 if (keyCode == KeyEvent.KEYCODE_MENU) {
2423 // KEYCODE_MENU is sent by some tests, for example
2424 // LauncherJankTests#testWidgetsContainerFling. Don't just remove its handling.
2425 if (!mDragController.isDragging() && !mWorkspace.isSwitchingState() &&
2426 isInState(NORMAL)) {
2427 // Close any open floating views.
2428 AbstractFloatingView.closeAllOpenViews(this);
2429
2430 // Setting the touch point to (-1, -1) will show the options popup in the center of
2431 // the screen.
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08002432 OptionsPopupView.show(this, -1, -1);
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002433 }
2434 return true;
2435 }
2436 return super.onKeyUp(keyCode, event);
2437 }
2438
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002439 public static Launcher getLauncher(Context context) {
2440 if (context instanceof Launcher) {
2441 return (Launcher) context;
2442 }
2443 return ((Launcher) ((ContextWrapper) context).getBaseContext());
2444 }
2445
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002446 /**
2447 * Callback for listening for onResume
2448 */
2449 public interface OnResumeCallback {
2450
2451 void onLauncherResume();
2452 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002453}