blob: 521ad48a38a26caac4bf23938af7e2693e8626fd [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080019import static android.content.pm.ActivityInfo.CONFIG_ORIENTATION;
20import static android.content.pm.ActivityInfo.CONFIG_SCREEN_SIZE;
Vadim Tryshev6d2321c2018-04-11 17:19:08 -070021
Sunny Goyal4c7f2152017-10-17 17:17:16 -070022import static com.android.launcher3.LauncherAnimUtils.SPRING_LOADED_EXIT_DELAY;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -070023import static com.android.launcher3.LauncherState.ALL_APPS;
24import static com.android.launcher3.LauncherState.NORMAL;
Sunny Goyalaeb16432017-10-16 11:46:41 -070025import static com.android.launcher3.logging.LoggerUtils.newContainerTarget;
Sunny Goyal326403e2017-10-02 12:45:10 -070026
Gilles Debunnedd6c9922010-10-25 11:23:41 -070027import android.animation.Animator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070028import android.animation.AnimatorSet;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070029import android.animation.ValueAnimator;
Adam Cohen0b395352014-06-09 22:54:36 +000030import android.annotation.TargetApi;
Tony Wickham005df0b2018-02-13 11:28:12 -080031import android.app.ActivityOptions;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080032import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070033import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080035import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080036import android.content.ComponentCallbacks2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037import android.content.Context;
Andrew Sappersteinabef55a2016-06-19 12:49:00 -070038import android.content.ContextWrapper;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070040import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070041import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070042import android.content.SharedPreferences;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.content.pm.PackageManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080044import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070045import android.database.sqlite.SQLiteDatabase;
Sunny Goyal59d086c2018-05-07 17:31:40 -070046import android.graphics.Point;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070047import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020048import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.os.Bundle;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070050import android.os.Parcelable;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080051import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070052import android.os.StrictMode;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070053import android.os.UserHandle;
Tony Wickham86222d22017-03-29 15:30:43 -070054import android.support.annotation.Nullable;
Michael Jurkaa33411c2012-06-14 16:18:21 -070055import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070057import android.util.Log;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070058import android.util.SparseArray;
Sunny Goyal59d086c2018-05-07 17:31:40 -070059import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070060import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070061import android.view.KeyboardShortcutGroup;
62import android.view.KeyboardShortcutInfo;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080063import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -070064import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070065import android.view.View;
Adam Cohen96d30a12013-07-16 18:13:21 -070066import android.view.ViewGroup;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080067import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070068import android.view.animation.OvershootInterpolator;
Adam Cohen96d30a12013-07-16 18:13:21 -070069import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070070
Sunny Goyal651077b2014-06-30 14:15:31 -070071import com.android.launcher3.DropTarget.DragObject;
Jon Mirandac476d6e2017-05-04 16:30:01 -070072import com.android.launcher3.Workspace.ItemOperator;
Sunny Goyalae502842016-06-17 08:43:56 -070073import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070074import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070075import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal7ede6112017-12-05 15:11:21 -080076import com.android.launcher3.allapps.DiscoveryBounce;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070077import com.android.launcher3.badge.BadgeInfo;
Sunny Goyalffe83f12014-08-14 17:39:34 -070078import com.android.launcher3.compat.AppWidgetManagerCompat;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -070079import com.android.launcher3.compat.LauncherAppsCompatVO;
Tony Wickhame0c33232016-02-08 11:37:04 -080080import com.android.launcher3.config.FeatureFlags;
Vadim Tryshevfedca432015-08-19 17:55:02 -070081import com.android.launcher3.dragndrop.DragController;
82import com.android.launcher3.dragndrop.DragLayer;
83import com.android.launcher3.dragndrop.DragView;
Sunny Goyal26119432016-02-18 22:09:23 +000084import com.android.launcher3.folder.FolderIcon;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070085import com.android.launcher3.folder.FolderIconPreviewVerifier;
Sunny Goyal66b24572016-09-21 15:57:55 -070086import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -070087import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -070088import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -070089import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyala535ae42017-02-27 10:07:13 -080090import com.android.launcher3.model.ModelWriter;
Sunny Goyala535ae42017-02-27 10:07:13 -080091import com.android.launcher3.notification.NotificationListener;
Tony Wickham540913e2017-01-23 11:47:51 -080092import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -080093import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -070094import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyalf8088ee2017-11-10 14:52:00 -080095import com.android.launcher3.states.InternalStateHandler;
Sunny Goyal623eddd2018-03-02 12:24:41 -080096import com.android.launcher3.states.RotationHelper;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -080097import com.android.launcher3.touch.ItemClickHandler;
Sunny Goyala5ace712017-11-15 17:12:51 -080098import com.android.launcher3.uioverrides.UiFactory;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +053099import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
100import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
Sunny Goyalaeb16432017-10-16 11:46:41 -0700101import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyal2100c782016-08-22 16:00:03 -0700102import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700103import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700104import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700105import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700106import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800107import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700108import com.android.launcher3.util.PendingRequestArgs;
Sunny Goyal8392c822017-06-20 10:03:56 -0700109import com.android.launcher3.util.SystemUiController;
Mario Bertschlera6936942017-05-31 14:48:19 -0700110import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700111import com.android.launcher3.util.Thunk;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700112import com.android.launcher3.util.TraceHelper;
Sunny Goyal326403e2017-10-02 12:45:10 -0700113import com.android.launcher3.util.UiThreadHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700114import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800115import com.android.launcher3.views.OptionsPopupView;
Sunny Goyal29947f02017-12-18 13:49:44 -0800116import com.android.launcher3.widget.LauncherAppWidgetHostView;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800117import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700118import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal29947f02017-12-18 13:49:44 -0800119import com.android.launcher3.widget.PendingAppWidgetHostView;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800120import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700121import com.android.launcher3.widget.WidgetHostViewLoader;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700122import com.android.launcher3.widget.WidgetListRowEntry;
123import com.android.launcher3.widget.WidgetsFullSheet;
Sunny Goyal952e63d2017-08-16 04:59:08 -0700124import com.android.launcher3.widget.custom.CustomWidgetParser;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700125
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700126import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700127import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700128import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700129import java.util.Collection;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700130import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700131import java.util.List;
Tony Wickham010d2552017-01-20 08:15:28 -0800132import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700133
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134/**
135 * Default launcher application.
136 */
Sunny Goyalab837732018-03-27 17:35:54 -0700137public class Launcher extends BaseDraggingActivity
138 implements LauncherExterns, LauncherModel.Callbacks, LauncherProviderChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700139 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700140 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141
Winson Chunga2413752012-04-03 14:22:34 -0700142 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700143
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700145 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700146
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700147 private static final int REQUEST_PICK_APPWIDGET = 9;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148
Michael Jurka8b805b12012-04-18 14:23:14 -0700149 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800150 public static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
151 public static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700152
Jon Mirandac476d6e2017-05-04 16:30:01 -0700153 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700154
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700155 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
156
Mathew Inwood876a8462013-06-14 14:12:41 +0100157 /**
158 * IntentStarter uses request codes starting with this. This must be greater than all activity
159 * request codes used internally.
160 */
161 protected static final int REQUEST_LAST = 100;
162
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 // Type: int
164 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700165 // Type: int
166 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700167 // Type: PendingRequestArgs
168 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
169 // Type: ActivityResultInfo
170 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700171 // Type: SparseArray<Parcelable>
172 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700174 private LauncherStateManager mStateManager;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700175
Adam Cohenad4e15c2013-10-17 16:21:35 -0700176 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177
Winson Chunga2413752012-04-03 14:22:34 -0700178 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700179 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
180 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
181 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700182
Jon Miranda54441f52018-01-24 15:38:25 -0800183 private LauncherAppTransitionManager mAppTransitionManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800184 private Configuration mOldConfig;
Jon Miranda54441f52018-01-24 15:38:25 -0800185
Adam Cohen091440a2015-03-18 14:16:05 -0700186 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700187 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700188 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800189 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700190
Sunny Goyalffe83f12014-08-14 17:39:34 -0700191 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700192 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700193
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700194 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700195
Sunny Goyal316490e2015-06-02 09:38:28 -0700196 @Thunk Hotseat mHotseat;
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700197 @Nullable private View mHotseatSearchBox;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700198
Sunny Goyal47328fd2016-05-25 18:56:41 -0700199 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700200
201 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700202 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700203 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700204
Winson Chung8ae41982017-11-10 11:42:13 -0800205 // UI and state for the overview panel
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800206 private View mOverviewPanel;
Winson Chung8ae41982017-11-10 11:42:13 -0800207
Vadim Tryshev6d2321c2018-04-11 17:19:08 -0700208 private View mOverviewPanelContainer;
209
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
Tony Wickham010d2552017-01-20 08:15:28 -0800221 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700222
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800223 private int mSynchronouslyBoundPage = PagedView.INVALID_PAGE;
Adam Cohen1462de32012-07-24 22:34:36 -0700224
Winson Chung46353de2012-02-16 14:05:10 -0800225 // We only want to get the SharedPreferences once since it does an FS stat each time we get
226 // it from the context.
227 private SharedPreferences mSharedPrefs;
228
Sunny Goyal2100c782016-08-22 16:00:03 -0700229 // Activity result which needs to be processed after workspace has loaded.
230 private ActivityResultInfo mPendingActivityResult;
231 /**
232 * Holds extra information required to handle a result from an external call, like
233 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
234 */
235 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800236
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700237 public ViewGroupFocusHelper mFocusHandler;
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700238 private boolean mAppLaunchSuccess;
239
Sunny Goyal623eddd2018-03-02 12:24:41 -0800240 private RotationHelper mRotationHelper;
Sunny Goyal7779d622015-06-11 16:18:39 -0700241
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800242 @Override
243 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700244 if (DEBUG_STRICT_MODE) {
245 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
246 .detectDiskReads()
247 .detectDiskWrites()
248 .detectNetwork() // or .detectAll() for all detectable problems
249 .penaltyLog()
250 .build());
251 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
252 .detectLeakedSqlLiteObjects()
253 .detectLeakedClosableObjects()
254 .penaltyLog()
255 .penaltyDeath()
256 .build());
257 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700258 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700259
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700261 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400262
Sunny Goyal87f784c2017-01-11 10:48:34 -0800263 LauncherAppState app = LauncherAppState.getInstance(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800264 mOldConfig = new Configuration(getResources().getConfiguration());
Sunny Goyal0e7724c2018-02-22 13:22:21 -0800265 mModel = app.setLauncher(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800266 initDeviceProfile(app.getInvariantDeviceProfile());
Winson Chung6e1c0d32013-10-25 15:24:24 -0700267
Sunny Goyalf7258242015-10-19 16:59:07 -0700268 mSharedPrefs = Utilities.getPrefs(this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800269 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700270 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700271
Joe Onorato41a12d22009-10-31 18:30:00 -0400272 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700273 mAllAppsController = new AllAppsTransitionController(this);
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800274 mStateManager = new LauncherStateManager(this);
Tracy Zhou8b23c6f2018-04-30 17:13:27 -0700275 UiFactory.onCreate(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700276
Sunny Goyalffe83f12014-08-14 17:39:34 -0700277 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700278
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700279 mAppWidgetHost = new LauncherAppWidgetHost(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700280 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700281
Sunny Goyal60820d72017-05-09 12:40:11 -0700282 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700283
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800284 setupViews();
Tony Wickham010d2552017-01-20 08:15:28 -0800285 mPopupDataProvider = new PopupDataProvider(this);
286
Sunny Goyal623eddd2018-03-02 12:24:41 -0800287 mRotationHelper = new RotationHelper(this);
Sunny Goyal90548432018-04-04 17:17:00 -0700288 mAppTransitionManager = LauncherAppTransitionManager.newInstance(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800289
Sunny Goyald3864fa2017-11-14 15:06:36 -0800290 boolean internalStateHandled = InternalStateHandler.handleCreate(this, getIntent());
291 if (internalStateHandled) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800292 if (savedInstanceState != null) {
293 // InternalStateHandler has already set the appropriate state.
294 // We dont need to do anything.
295 savedInstanceState.remove(RUNTIME_STATE);
296 }
297 }
298 restoreState(savedInstanceState);
Winson Chungb63b44c2017-11-10 17:54:44 -0800299
Sunny Goyal2100c782016-08-22 16:00:03 -0700300 // We only load the page synchronously if the user rotates (or triggers a
301 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700302 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
303 if (savedInstanceState != null) {
304 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
305 }
Sunny Goyald3864fa2017-11-14 15:06:36 -0800306
Sunny Goyalfe770c92016-09-27 14:38:58 -0700307 if (!mModel.startLoader(currentScreen)) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800308 if (!internalStateHandled) {
309 // If we are not binding synchronously, show a fade in animation when
310 // the first page bind completes.
Jon Mirandadbd92862018-05-01 13:07:53 -0700311 mLauncherView.setAlpha(0);
Sunny Goyald3864fa2017-11-14 15:06:36 -0800312 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700313 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700314 // Pages bound synchronously.
315 mWorkspace.setCurrentPage(currentScreen);
316
Sunny Goyal2100c782016-08-22 16:00:03 -0700317 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800318 }
319
320 // For handling default keys
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800321 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800322
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800323 setContentView(mLauncherView);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800324 getRootView().dispatchInsets();
Mario Bertschlera6936942017-05-31 14:48:19 -0700325
Jon Miranda7fda2852017-07-19 16:07:01 -0700326 // Listen for broadcasts
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700327 registerReceiver(mScreenOffReceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));
Sunny Goyala616d2b2017-06-12 13:54:01 -0700328
Sunny Goyal8392c822017-06-20 10:03:56 -0700329 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
330 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700331
332 if (mLauncherCallbacks != null) {
333 mLauncherCallbacks.onCreate(savedInstanceState);
334 }
Sunny Goyal623eddd2018-03-02 12:24:41 -0800335 mRotationHelper.initialize();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700336
337 TraceHelper.endSection("Launcher-onCreate");
Adam Cohen9211d422014-10-07 18:14:53 -0700338 }
339
Sunny Goyal7779d622015-06-11 16:18:39 -0700340 @Override
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800341 public void onConfigurationChanged(Configuration newConfig) {
342 int diff = newConfig.diff(mOldConfig);
343 if ((diff & (CONFIG_ORIENTATION | CONFIG_SCREEN_SIZE)) != 0) {
344 mUserEventDispatcher = null;
345 initDeviceProfile(mDeviceProfile.inv);
Sunny Goyalfde55052018-02-01 14:46:13 -0800346 dispatchDeviceProfileChanged();
Sunny Goyal59d086c2018-05-07 17:31:40 -0700347 reapplyUi();
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700348 mDragLayer.recreateControllers();
Sunny Goyal7185dd62018-03-14 17:51:49 -0700349
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800350 // TODO: We can probably avoid rebind when only screen size changed.
Sunny Goyal605bcf32018-03-02 15:16:12 -0800351 rebindModel();
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800352 }
353
354 mOldConfig.setTo(newConfig);
Tracy Zhoua706f002018-03-28 13:55:19 -0700355 UiFactory.onLauncherStateOrResumeChanged(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800356 super.onConfigurationChanged(newConfig);
357 }
358
Sunny Goyal605bcf32018-03-02 15:16:12 -0800359 @Override
Sunny Goyal59d086c2018-05-07 17:31:40 -0700360 protected void reapplyUi() {
361 getRootView().dispatchInsets();
362 getStateManager().reapplyState(true /* cancelCurrentAnimation */);
363 }
364
365 @Override
Sunny Goyal605bcf32018-03-02 15:16:12 -0800366 public void rebindModel() {
367 int currentPage = mWorkspace.getNextPage();
368 if (mModel.startLoader(currentPage)) {
369 mWorkspace.setCurrentPage(currentPage);
370 setWorkspaceLoading(true);
371 }
372 }
373
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800374 private void initDeviceProfile(InvariantDeviceProfile idp) {
375 // Load configuration-specific DeviceProfile
Sunny Goyal59d086c2018-05-07 17:31:40 -0700376 mDeviceProfile = idp.getDeviceProfile(this);
377 if (isInMultiWindowModeCompat()) {
378 Display display = getWindowManager().getDefaultDisplay();
379 Point mwSize = new Point();
380 display.getSize(mwSize);
381 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
382 }
383 onDeviceProfileInitiated();
Sunny Goyal605bcf32018-03-02 15:16:12 -0800384 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout(), true);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800385 }
386
Sunny Goyal623eddd2018-03-02 12:24:41 -0800387 public RotationHelper getRotationHelper() {
388 return mRotationHelper;
389 }
390
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700391 public LauncherStateManager getStateManager() {
392 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700393 }
394
Mario Bertschler27288382017-05-24 15:35:09 -0700395 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700396 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800397 return mLauncherView.findViewById(id);
398 }
399
400 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700401 public void onAppWidgetHostReset() {
402 if (mAppWidgetHost != null) {
403 mAppWidgetHost.startListening();
404 }
405 }
406
Adam Cohen9211d422014-10-07 18:14:53 -0700407 private LauncherCallbacks mLauncherCallbacks;
408
Sunny Goyal32554d12015-12-03 15:31:25 -0800409 /**
410 * Call this after onCreate to set or clear overlay.
411 */
412 public void setLauncherOverlay(LauncherOverlay overlay) {
413 if (overlay != null) {
414 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
415 }
416 mWorkspace.setLauncherOverlay(overlay);
417 }
418
Adam Cohen9211d422014-10-07 18:14:53 -0700419 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
420 mLauncherCallbacks = callbacks;
421 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700422 }
423
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700424 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700425 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700426 if (mLauncherCallbacks != null) {
427 mLauncherCallbacks.onLauncherProviderChange();
428 }
429 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700430
Hyunyoung Song3f471442015-04-08 19:01:34 -0700431 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700432 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
433 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700434 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700435 }
436
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000437 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700438 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
439 // This cast is safe as long as the id < 0x00FFFFFF
440 // Since we jail all the dynamically generated views, there should be no clashes
441 // with any other views.
442 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000443 }
444
Tony Wickham010d2552017-01-20 08:15:28 -0800445 public PopupDataProvider getPopupDataProvider() {
446 return mPopupDataProvider;
447 }
448
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700449 @Override
450 public BadgeInfo getBadgeInfoForItem(ItemInfo info) {
451 return mPopupDataProvider.getBadgeInfoForItem(info);
452 }
453
454 @Override
455 public void invalidateParent(ItemInfo info) {
456 FolderIconPreviewVerifier verifier = new FolderIconPreviewVerifier(getDeviceProfile().inv);
457 if (verifier.isItemInPreview(info.rank) && (info.container >= 0)) {
458 View folderIcon = getWorkspace().getHomescreenIconByItemId(info.container);
459 if (folderIcon != null) {
460 folderIcon.invalidate();
461 }
462 }
463 }
464
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000465 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700466 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
467 * a configuration step, this allows the proper animations to run after other transitions.
468 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700469 private long completeAdd(
470 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
471 long screenId = info.screenId;
472 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700473 // When the screen id represents an actual screen (as opposed to a rank) we make sure
474 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700475 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700476 }
Adam Cohendb364c32014-05-20 14:23:40 -0700477
Sunny Goyal2100c782016-08-22 16:00:03 -0700478 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800479 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700480 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700481 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800482 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700483 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700484 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700485 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700486 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700487 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700488 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700489 int widgetId = appWidgetId;
490 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700491 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700492 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700493 // Since the view was just bound, also launch the configure activity if needed
494 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
495 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800496 if (provider != null) {
497 new WidgetAddFlowHandler(provider)
498 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700499 }
500 }
501 break;
502 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800503 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700504
Adam Cohendb364c32014-05-20 14:23:40 -0700505 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800506 }
507
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800508 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700509 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700510 if (isWorkspaceLoading()) {
511 // process the result once the workspace has loaded.
512 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
513 return;
514 }
515 mPendingActivityResult = null;
516
Winson Chunge341d302013-08-16 14:31:00 -0700517 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700518 final PendingRequestArgs requestArgs = mPendingRequestArgs;
519 setWaitingForResult(null);
520 if (requestArgs == null) {
521 return;
522 }
523
524 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700525
Adam Cohenad4e15c2013-10-17 16:21:35 -0700526 Runnable exitSpringLoaded = new Runnable() {
527 @Override
528 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700529 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700530 }
531 };
532
Michael Jurka8b805b12012-04-18 14:23:14 -0700533 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700534 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700535 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700536 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
537 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700538 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700539 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700540 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700541 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700542 addAppWidgetImpl(
543 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800544 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700545 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700546 }
547 return;
548 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200549
Adam Cohened66b2b2012-01-23 17:28:51 -0800550 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700551 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700552
Adam Cohened66b2b2012-01-23 17:28:51 -0800553 // We have special handling for widgets
554 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800555 final int appWidgetId;
556 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
557 : -1;
558 if (widgetId < 0) {
559 appWidgetId = pendingAddWidgetId;
560 } else {
561 appWidgetId = widgetId;
562 }
563
Adam Cohenad4e15c2013-10-17 16:21:35 -0700564 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800565 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700566 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
567 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700568 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700569 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700570 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700571 @Override
572 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700573 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700574 }
575 };
Adam Cohendb364c32014-05-20 14:23:40 -0700576
Sunny Goyal2100c782016-08-22 16:00:03 -0700577 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
578 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
579 } else {
580 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
581 // When the screen id represents an actual screen (as opposed to a rank)
582 // we make sure that the drop page actually exists.
583 requestArgs.screenId =
584 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700585 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700586 final CellLayout dropLayout =
587 mWorkspace.getScreenWithId(requestArgs.screenId);
588
589 dropLayout.setDropPending(true);
590 final Runnable onComplete = new Runnable() {
591 @Override
592 public void run() {
593 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
594 dropLayout.setDropPending(false);
595 }
596 };
597 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
598 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700599 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800600 return;
601 }
602
Sunny Goyald478c832016-04-01 12:04:16 -0700603 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
604 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700605 if (resultCode == RESULT_OK) {
606 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700607 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700608 }
609 // Leave the widget in the pending state if the user canceled the configure.
610 return;
611 }
612
Sunny Goyalaad90582015-07-09 14:22:50 -0700613 if (requestCode == REQUEST_CREATE_SHORTCUT) {
614 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700615 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
616 completeAdd(requestCode, data, -1, requestArgs);
617 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
618 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
619
Sunny Goyalaad90582015-07-09 14:22:50 -0700620 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700621 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
622 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800623 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800624 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800625 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800626 }
627
628 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700629 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800630 final int requestCode, final int resultCode, final Intent data) {
631 handleActivityResult(requestCode, resultCode, data);
632 if (mLauncherCallbacks != null) {
633 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
634 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800635 }
636
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700637 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700638 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600639 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700640 PendingRequestArgs pendingArgs = mPendingRequestArgs;
641 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
642 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
643 setWaitingForResult(null);
644
Sunny Goyal28c6b962015-10-12 11:42:05 -0700645 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700646 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700647 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700648 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700649 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700650 Intent intent = pendingArgs.getPendingIntent();
651
Sunny Goyal28c6b962015-10-12 11:42:05 -0700652 if (grantResults.length > 0
653 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700654 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700655 } else {
656 // TODO: Show a snack bar with link to settings
657 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700658 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700659 }
660 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600661 if (mLauncherCallbacks != null) {
662 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
663 grantResults);
664 }
665 }
666
Adam Cohendb364c32014-05-20 14:23:40 -0700667 /**
668 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
669 *
670 * @param screenId the screen id to check
671 * @return the new screen, or screenId if it exists
672 */
673 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800674 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700675 if (dropLayout == null) {
676 // it's possible that the add screen was removed because it was
677 // empty and a re-bind occurred
678 mWorkspace.addExtraEmptyScreen();
679 return mWorkspace.commitExtraEmptyScreen();
680 } else {
681 return screenId;
682 }
683 }
684
Sunny Goyal2100c782016-08-22 16:00:03 -0700685 @Thunk void completeTwoStageWidgetDrop(
686 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
687 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800688 Runnable onCompleteRunnable = null;
689 int animationType = 0;
690
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700691 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800692 if (resultCode == RESULT_OK) {
693 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
694 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800695 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700696 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800697 onCompleteRunnable = new Runnable() {
698 @Override
699 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700700 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700701 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800702 }
703 };
704 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800705 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800706 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800707 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700708 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700709 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700710 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
711 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700712 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700713 // The animated view may be null in the case of a rotation during widget configuration
714 onCompleteRunnable.run();
715 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800716 }
717
718 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700719 protected void onStop() {
720 super.onStop();
721 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700722
723 if (mLauncherCallbacks != null) {
724 mLauncherCallbacks.onStop();
725 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800726 mAppWidgetHost.setListenIfResumed(false);
Tony Wickham010d2552017-01-20 08:15:28 -0800727
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700728 if (!mAppLaunchSuccess) {
729 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -0800730 mStateManager.getState().containerType, -1);
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700731 }
Tony Wickham010d2552017-01-20 08:15:28 -0800732 NotificationListener.removeNotificationsChangedListener();
Sunny Goyalaf161fd2018-02-05 16:34:57 -0800733 getStateManager().moveToRestState();
Winson Chungedb0d6b2018-03-21 17:32:55 -0700734
Winson Chungdea51352018-04-24 13:02:10 -0700735 // Workaround for b/78520668, explicitly trim memory once UI is hidden
Winson Chung473730f2018-04-26 11:54:22 -0700736 onTrimMemory(TRIM_MEMORY_UI_HIDDEN);
Michael Jurkacd496d72013-04-11 11:32:45 -0700737 }
738
739 @Override
740 protected void onStart() {
741 super.onStart();
742 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700743
744 if (mLauncherCallbacks != null) {
745 mLauncherCallbacks.onStart();
746 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800747 mAppWidgetHost.setListenIfResumed(true);
Sunny Goyalcac002d2018-01-04 09:51:45 -0800748 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
Winson Chung62a70be2018-02-23 15:10:05 -0800749 UiFactory.onStart(this);
Michael Jurkacd496d72013-04-11 11:32:45 -0700750 }
751
752 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800753 protected void onResume() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700754 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800755 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700756 TraceHelper.partitionSection("ON_RESUME", "superCall");
757
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700758 mAppLaunchSuccess = false;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700759 getUserEventDispatcher().resetElapsedSessionMillis();
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700760 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700761 // Process any items that were added while Launcher was away.
762 InstallShortcutReceiver.disableAndFlushInstallQueue(
763 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700764
Sunny Goyala474a9b2017-05-04 16:47:11 -0700765 // Refresh shortcuts if the permission changed.
766 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700767
Tracy Zhoue5575f92018-04-19 11:12:53 -0700768 DiscoveryBounce.showForHomeIfNeeded(this);
Adam Cohen9211d422014-10-07 18:14:53 -0700769 if (mLauncherCallbacks != null) {
770 mLauncherCallbacks.onResume();
771 }
Tracy Zhoua706f002018-03-28 13:55:19 -0700772 UiFactory.onLauncherStateOrResumeChanged(this);
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800773
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700774 TraceHelper.endSection("ON_RESUME");
Adam Cohen06dff352012-06-01 17:17:08 -0700775 }
776
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800777 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700778 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700779 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700780 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700781
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700782 super.onPause();
Joe Onorato24b6fd82009-11-12 13:47:09 -0800783 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700784 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700785
Adam Cohen9211d422014-10-07 18:14:53 -0700786 if (mLauncherCallbacks != null) {
787 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700788 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700789 }
790
Winson Chungd7823942018-02-16 03:23:47 +0000791 @Override
Tracy Zhoua706f002018-03-28 13:55:19 -0700792 protected void onUserLeaveHint() {
793 super.onUserLeaveHint();
794 UiFactory.onLauncherStateOrResumeChanged(this);
795 }
796
797 @Override
Winson Chungd7823942018-02-16 03:23:47 +0000798 public void onWindowFocusChanged(boolean hasFocus) {
799 super.onWindowFocusChanged(hasFocus);
800 mStateManager.onWindowFocusChanged();
801 }
802
Adam Cohenc2d6e892014-10-16 09:49:24 -0700803 public interface LauncherOverlay {
804
805 /**
806 * Touch interaction leading to overscroll has begun
807 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700808 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700809
810 /**
811 * Touch interaction related to overscroll has ended
812 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700813 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700814
815 /**
816 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
817 * screen (or in the case of RTL, the rightmost screen).
818 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700819 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700820
821 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800822 * Called when the launcher is ready to use the overlay
823 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700824 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700825 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700826 }
827
828 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700829 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700830 }
831
832 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
833
Sunny Goyalc86df472016-02-25 09:19:38 -0800834 public void onScrollChanged(float progress) {
835 if (mWorkspace != null) {
836 mWorkspace.onOverlayScrollChanged(progress);
837 }
838 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700839 }
840
Sunny Goyal16764582017-11-06 16:00:34 -0800841 public boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -0700842 if (mLauncherCallbacks != null) {
843 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700844 } else {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800845 // On O and above we there is always some setting present settings (add icon to
846 // home screen or icon badging). On earlier APIs we will have the allow rotation
847 // setting, on devices with a locked orientation,
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700848 return Utilities.ATLEAST_OREO || !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -0700849 }
Jorim Jaggid017f882014-01-14 17:08:48 -0800850 }
851
Sunny Goyalf9403d92017-10-18 10:55:56 -0700852 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -0700853 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700854 }
855
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800856 /**
857 * Restores the previous state, if it exists.
858 *
859 * @param savedState The previous state.
860 */
861 private void restoreState(Bundle savedState) {
862 if (savedState == null) {
863 return;
864 }
865
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700866 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -0700867 LauncherState[] stateValues = LauncherState.values();
868 LauncherState state = stateValues[stateOrdinal];
Sunny Goyalbc683e92017-12-06 10:25:07 -0800869 if (!state.disableRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700870 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800871 }
872
Sunny Goyal2100c782016-08-22 16:00:03 -0700873 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
874 if (requestArgs != null) {
875 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800876 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700877
878 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700879
880 SparseArray<Parcelable> widgetsState =
881 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
882 if (widgetsState != null) {
883 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
884 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800885 }
886
887 /**
888 * Finds all the views we need and configure them properly.
889 */
890 private void setupViews() {
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800891 mDragLayer = findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700892 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -0700893 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700894 mWorkspace.initParentViews(mDragLayer);
Sunny Goyal16764582017-11-06 16:00:34 -0800895 mOverviewPanel = findViewById(R.id.overview_panel);
Vadim Tryshev6d2321c2018-04-11 17:19:08 -0700896 mOverviewPanelContainer = findViewById(R.id.overview_panel_container);
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800897 mHotseat = findViewById(R.id.hotseat);
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700898 mHotseatSearchBox = findViewById(R.id.search_container_hotseat);
Craig Mautner360310b2012-10-26 15:13:08 -0700899
Sunny Goyal784f9c32016-03-23 16:56:54 -0700900 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
901 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
902 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800903
Winson Chung4c98d922011-05-31 16:50:48 -0700904 // Setup the drag layer
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700905 mDragLayer.setup(mDragController, mWorkspace);
906 UiFactory.setOnTouchControllersChangedListener(this, mDragLayer::recreateControllers);
Winson Chung4c98d922011-05-31 16:50:48 -0700907
Hyunyoung Song645764e2016-06-06 14:19:02 -0700908 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -0700909 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
910 // default state, otherwise we will update to the wrong offsets in RTL
911 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700912 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700913 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700914
Tony Wickham34d2c912015-09-11 09:27:58 -0700915 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700916 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700917
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700918 // Setup Apps
919 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -0700920
Winson Chung3d503fb2011-07-13 17:25:49 -0700921 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -0700922 mDragController.setMoveTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700923 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -0400924
Sunny Goyal7185dd62018-03-14 17:51:49 -0700925 mAllAppsController.setupViews(mAppsView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800926 }
927
928 /**
929 * Creates a view representing a shortcut.
930 *
931 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800932 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800933 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700934 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800935 }
936
937 /**
938 * Creates a view representing a shortcut inflated from the specified resource.
939 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800940 * @param parent The group the shortcut belongs to.
941 * @param info The data structure describing the shortcut.
942 *
943 * @return A View inflated from layoutResId.
944 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700945 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -0800946 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
947 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800948 favorite.applyFromShortcutInfo(info);
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800949 favorite.setOnClickListener(ItemClickHandler.INSTANCE);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700950 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800951 return favorite;
952 }
953
954 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -0700955 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800956 *
957 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800958 */
Adam Cohendcd297f2013-06-18 13:13:40 -0700959 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700960 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -0700961 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
962 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700963 return;
964 }
Adam Cohen558baaf2011-08-15 15:22:57 -0700965
Jon Mirandac476d6e2017-05-04 16:30:01 -0700966 int[] cellXY = mTmpAddItemCellCoordinates;
967 CellLayout layout = getCellLayout(container, screenId);
968
Sunny Goyal782f0c92017-01-19 10:27:54 -0800969 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700970 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -0700971 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
972 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -0800973 }
974
975 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -0700976 // Legacy shortcuts are only supported for primary profile.
977 info = Process.myUserHandle().equals(args.user)
978 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800979
Sunny Goyalb57645f2017-03-20 13:57:28 -0700980 if (info == null) {
981 Log.e(TAG, "Unable to parse a valid custom shortcut result");
982 return;
983 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -0800984 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -0800985 // The app is trying to add a shortcut without sufficient permissions
986 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
987 return;
988 }
989 }
990
Jon Mirandac476d6e2017-05-04 16:30:01 -0700991 if (container < 0) {
992 // Adding a shortcut to the Workspace.
993 final View view = createShortcut(info);
994 boolean foundCellSpan = false;
995 // First we check if we already know the exact location where we want to add this item.
996 if (cellX >= 0 && cellY >= 0) {
997 cellXY[0] = cellX;
998 cellXY[1] = cellY;
999 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001000
Jon Mirandac476d6e2017-05-04 16:30:01 -07001001 // If appropriate, either create a folder or add to an existing folder
1002 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001003 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001004 return;
1005 }
1006 DragObject dragObject = new DragObject();
1007 dragObject.dragInfo = info;
1008 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1009 true)) {
1010 return;
1011 }
1012 } else {
1013 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1014 }
1015
1016 if (!foundCellSpan) {
1017 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001018 return;
1019 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001020
1021 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1022 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001023 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001024 // Adding a shortcut to a Folder.
Sunny Goyale29897f52017-07-20 10:09:42 -07001025 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001026 if (folderIcon != null) {
1027 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1028 folderInfo.add(info, args.rank, false);
1029 } else {
Sunny Goyale29897f52017-07-20 10:09:42 -07001030 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001031 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001032 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001033 }
1034
Sunny Goyale29897f52017-07-20 10:09:42 -07001035 public FolderIcon findFolderIcon(final long folderIconId) {
1036 return (FolderIcon) mWorkspace.getFirstMatch(new ItemOperator() {
1037 @Override
1038 public boolean evaluate(ItemInfo info, View view) {
1039 return info != null && info.id == folderIconId;
1040 }
1041 });
1042 }
1043
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001044 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001045 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001046 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001047 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001048 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001049 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001050 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1051
Adam Cohened66b2b2012-01-23 17:28:51 -08001052 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001053 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001054 }
Romain Guycbb89e42009-06-08 15:52:54 -07001055
Adam Cohen59400422014-03-05 18:07:04 -08001056 LauncherAppWidgetInfo launcherInfo;
1057 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001058 launcherInfo.spanX = itemInfo.spanX;
1059 launcherInfo.spanY = itemInfo.spanY;
1060 launcherInfo.minSpanX = itemInfo.minSpanX;
1061 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001062 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001063
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001064 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001065 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001066
Sunny Goyal2100c782016-08-22 16:00:03 -07001067 if (hostView == null) {
1068 // Perform actual inflation because we're live
1069 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001070 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001071 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301072 prepareAppWidget(hostView, launcherInfo);
1073 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001074 }
Romain Guycbb89e42009-06-08 15:52:54 -07001075
Sunny Goyald5462aa2016-11-22 16:52:39 +05301076 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001077 hostView.setTag(item);
1078 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001079 hostView.setFocusable(true);
1080 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001081 }
1082
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001083 private final BroadcastReceiver mScreenOffReceiver = new BroadcastReceiver() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001084 @Override
1085 public void onReceive(Context context, Intent intent) {
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001086 // Reset AllApps to its initial state only if we are not in the middle of
1087 // processing a multi-step drop
1088 if (mPendingRequestArgs == null) {
1089 mStateManager.goToState(NORMAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001090 }
1091 }
1092 };
1093
Tony Wickham010d2552017-01-20 08:15:28 -08001094 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
Sunny Goyal29947f02017-12-18 13:49:44 -08001095 mWorkspace.updateIconBadges(updatedBadges);
Sunny Goyal60180b02018-02-07 12:56:30 -08001096 mAppsView.getAppsStore().updateIconBadges(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001097
Sunny Goyal29947f02017-12-18 13:49:44 -08001098 PopupContainerWithArrow popup = PopupContainerWithArrow.getOpen(Launcher.this);
1099 if (popup != null) {
1100 popup.updateNotificationHeader(updatedBadges);
Tony Wickham010d2552017-01-20 08:15:28 -08001101 }
1102 }
1103
Adam Cohended9f8d2010-11-03 13:25:16 -07001104 @Override
1105 public void onAttachedToWindow() {
1106 super.onAttachedToWindow();
1107
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001108 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyalc86df472016-02-25 09:19:38 -08001109 if (mLauncherCallbacks != null) {
1110 mLauncherCallbacks.onAttachedToWindow();
1111 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001112 }
1113
1114 @Override
1115 public void onDetachedFromWindow() {
1116 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001117
1118 if (mLauncherCallbacks != null) {
1119 mLauncherCallbacks.onDetachedFromWindow();
1120 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001121 }
1122
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001123 public AllAppsTransitionController getAllAppsController() {
1124 return mAllAppsController;
1125 }
1126
Sunny Goyal9d69c8d2018-03-19 13:41:31 -07001127 @Override
Winson Chung1a341002018-01-17 10:00:23 -08001128 public LauncherRootView getRootView() {
1129 return (LauncherRootView) mLauncherView;
1130 }
1131
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001132 @Override
Winson Chunga6945242014-01-08 14:04:34 -08001133 public DragLayer getDragLayer() {
1134 return mDragLayer;
1135 }
1136
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001137 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001138 return mAppsView;
1139 }
1140
Winson Chunga6945242014-01-08 14:04:34 -08001141 public Workspace getWorkspace() {
1142 return mWorkspace;
1143 }
1144
1145 public Hotseat getHotseat() {
1146 return mHotseat;
1147 }
1148
Sunny Goyal81b4c7b2018-03-26 12:10:31 -07001149 public View getHotseatSearchBox() {
1150 return mHotseatSearchBox;
1151 }
1152
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001153 public <T extends View> T getOverviewPanel() {
Winson Chung8ae41982017-11-10 11:42:13 -08001154 return (T) mOverviewPanel;
Winson Chunga6945242014-01-08 14:04:34 -08001155 }
1156
Vadim Tryshev6d2321c2018-04-11 17:19:08 -07001157 public <T extends View> T getOverviewPanelContainer() {
1158 return (T) mOverviewPanelContainer;
1159 }
1160
Sunny Goyal47328fd2016-05-25 18:56:41 -07001161 public DropTargetBar getDropTargetBar() {
1162 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001163 }
1164
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001165 public LauncherAppWidgetHost getAppWidgetHost() {
1166 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001167 }
Romain Guycbb89e42009-06-08 15:52:54 -07001168
Winson Chunga9abd0e2010-10-27 17:18:37 -07001169 public LauncherModel getModel() {
1170 return mModel;
1171 }
1172
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001173 public ModelWriter getModelWriter() {
1174 return mModelWriter;
1175 }
1176
Adam Cohen79d90c52016-04-22 13:29:20 -07001177 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001178 return mSharedPrefs;
1179 }
1180
Sunny Goyalf8d56fc2018-01-31 15:18:11 -08001181 public int getOrientation() { return mOldConfig.orientation; }
Jon Miranda6bed3502017-09-19 14:43:17 -07001182
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001183 @Override
1184 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001185 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001186 super.onNewIntent(intent);
1187
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001188 boolean alreadyOnHome = hasWindowFocus() && ((intent.getFlags() &
Winson15f8b172015-08-19 11:02:39 -07001189 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1190 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001191
1192 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001193 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001194 && AbstractFloatingView.getTopOpenView(this) == null;
Winson15f8b172015-08-19 11:02:39 -07001195 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001196 boolean internalStateHandled = InternalStateHandler
Sunny Goyal8b264562018-02-03 23:00:13 -08001197 .handleNewIntent(this, intent, isStarted());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001198
Winson15f8b172015-08-19 11:02:39 -07001199 if (isActionMain) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001200 if (!internalStateHandled) {
1201 // Note: There should be at most one log per method call. This is enforced
1202 // implicitly by using if-else statements.
1203 UserEventDispatcher ued = getUserEventDispatcher();
1204 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
1205 if (topOpenView != null) {
1206 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
1207 } else if (alreadyOnHome) {
1208 Target target = newContainerTarget(mStateManager.getState().containerType);
1209 target.pageIndex = mWorkspace.getCurrentPage();
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001210 ued.logActionCommand(Action.Command.HOME_INTENT, target,
1211 newContainerTarget(ContainerType.WORKSPACE));
Sunny Goyald3864fa2017-11-14 15:06:36 -08001212 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001213
Sunny Goyald3864fa2017-11-14 15:06:36 -08001214 // In all these cases, only animate if we're already on home
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001215 AbstractFloatingView.closeAllOpenViews(this, isStarted());
Jon Mirandafeba90f2016-10-06 10:53:29 -07001216
Sunny Goyala2467272018-03-16 14:53:05 -07001217 if (!isInState(NORMAL)) {
1218 // Only change state, if not already the same. This prevents cancelling any
1219 // animations running as part of resume
1220 mStateManager.goToState(NORMAL);
1221 }
Sunny Goyald3864fa2017-11-14 15:06:36 -08001222
1223 // Reset the apps view
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001224 if (!alreadyOnHome) {
Sunny Goyal7185dd62018-03-14 17:51:49 -07001225 mAppsView.reset(isStarted() /* animate */);
Sunny Goyald3864fa2017-11-14 15:06:36 -08001226 }
1227
1228 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()) {
1229 mWorkspace.post(mWorkspace::moveToDefaultScreen);
1230 }
1231 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001232
1233 final View v = getWindow().peekDecorView();
1234 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001235 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001236 }
1237
Adam Cohen9211d422014-10-07 18:14:53 -07001238 if (mLauncherCallbacks != null) {
Sunny Goyal85462132018-04-24 11:39:37 -07001239 mLauncherCallbacks.onHomeIntent(internalStateHandled);
Adam Cohen9211d422014-10-07 18:14:53 -07001240 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001241 }
Winson15f8b172015-08-19 11:02:39 -07001242
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001243 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001244 }
1245
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001246 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001247 public void onRestoreInstanceState(Bundle state) {
1248 super.onRestoreInstanceState(state);
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001249 mWorkspace.restoreInstanceStateForChild(mSynchronouslyBoundPage);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001250 }
1251
1252 @Override
1253 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001254 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001255 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001256
Adam Cohen21cd0022013-10-09 18:57:02 -07001257 }
Sunny Goyalea609262017-10-25 15:47:38 -07001258 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001259
1260
1261 AbstractFloatingView widgets = AbstractFloatingView
1262 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1263 if (widgets != null) {
1264 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1265 widgets.saveHierarchyState(widgetsState);
1266 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1267 } else {
1268 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1269 }
1270
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001271 // We close any open folders and shortcut containers since they will not be re-opened,
1272 // and we need to make sure this state is reflected.
1273 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001274
Sunny Goyal2100c782016-08-22 16:00:03 -07001275 if (mPendingRequestArgs != null) {
1276 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1277 }
1278 if (mPendingActivityResult != null) {
1279 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001280 }
1281
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001282 super.onSaveInstanceState(outState);
1283
Adam Cohen9211d422014-10-07 18:14:53 -07001284 if (mLauncherCallbacks != null) {
1285 mLauncherCallbacks.onSaveInstanceState(outState);
1286 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001287 }
1288
1289 @Override
1290 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001291 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001292
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001293 unregisterReceiver(mScreenOffReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001294 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001295
Tony Wickhama0068302018-04-11 16:16:11 -07001296 UiFactory.setOnTouchControllersChangedListener(this, null);
1297
Winson Chungcd2b0142011-06-08 16:02:26 -07001298 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001299 // It's possible to receive onDestroy after a new Launcher activity has
1300 // been created. In this case, don't interfere with the new Launcher.
1301 if (mModel.isCurrentCallbacks(this)) {
1302 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001303 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001304 }
Sunny Goyal623eddd2018-03-02 12:24:41 -08001305 mRotationHelper.destroy();
Sunny Goyal745bad92016-05-02 10:54:12 -07001306
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001307 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001308 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001309 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001310 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001311 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001312
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001313 TextKeyListener.getInstance().release();
Mario Bertschler27288382017-05-24 15:35:09 -07001314
Michael Jurka2ecf9952012-06-18 12:52:28 -07001315 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001316
Tony2917a8b2017-07-31 23:29:42 -07001317 clearPendingBinds();
1318
Adam Cohen9211d422014-10-07 18:14:53 -07001319 if (mLauncherCallbacks != null) {
1320 mLauncherCallbacks.onDestroy();
1321 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001322 }
1323
Sunny Goyalae502842016-06-17 08:43:56 -07001324 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1325 return mAccessibilityDelegate;
1326 }
1327
Adam Cohena9cf38f2011-05-02 15:36:58 -07001328 public DragController getDragController() {
1329 return mDragController;
1330 }
1331
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001332 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001333 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001334 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001335 }
1336
1337 @Override
1338 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1339 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001340 try {
1341 super.startIntentSenderForResult(intent, requestCode,
1342 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1343 } catch (IntentSender.SendIntentException e) {
1344 throw new ActivityNotFoundException();
1345 }
1346 }
1347
Winson Chung70d72102011-08-12 11:24:35 -07001348 /**
1349 * Indicates that we want global search for this activity by setting the globalSearch
1350 * argument for {@link #startSearch} to true.
1351 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001352 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001353 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001354 Bundle appSearchData, boolean globalSearch) {
1355 if (appSearchData == null) {
1356 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001357 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001358 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001359
Sunny Goyal6f28e712016-09-13 14:19:29 -07001360 if (mLauncherCallbacks == null ||
1361 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1362 // Starting search from the callbacks failed. Start the default global search.
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001363 super.startSearch(initialQuery, selectInitialQuery, appSearchData, true);
Ian Parkinson047036e2014-09-01 15:40:53 +01001364 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001365
1366 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001367 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001368 }
1369
Joe Onorato9c1289c2009-08-17 11:03:03 -04001370 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001371 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001372 }
1373
Adam Cohen517a7f52014-03-01 12:12:59 -08001374 public boolean isWorkspaceLoading() {
1375 return mWorkspaceLoading;
1376 }
1377
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001378 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001379 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001380 }
1381
Sunny Goyal04a324a2017-01-20 21:08:59 -08001382 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001383 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001384 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001385
Sunny Goyal2100c782016-08-22 16:00:03 -07001386 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001387 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001388 if (LOGD) {
1389 Log.d(TAG, "Adding widget from drop");
1390 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001391 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001392 }
1393
Sunny Goyal2100c782016-08-22 16:00:03 -07001394 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001395 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1396 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1397 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001398
Adam Cohenad4e15c2013-10-17 16:21:35 -07001399 Runnable onComplete = new Runnable() {
1400 @Override
1401 public void run() {
1402 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001403 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001404 }
1405 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001406 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001407 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001408 }
1409 }
Romain Guycbb89e42009-06-08 15:52:54 -07001410
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001411 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1412 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001413 info.container = container;
1414 info.screenId = screenId;
1415 if (cell != null) {
1416 info.cellX = cell[0];
1417 info.cellY = cell[1];
1418 }
1419 info.spanX = spanX;
1420 info.spanY = spanY;
1421
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001422 switch (info.itemType) {
1423 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1424 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001425 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001426 break;
1427 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001428 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001429 break;
1430 default:
1431 throw new IllegalStateException("Unknown item type: " + info.itemType);
1432 }
1433 }
1434
Adam Cohenfbba09b2011-07-18 21:43:05 -07001435 /**
1436 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001437 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001438 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001439 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1440 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001441 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1442 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1443 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001444 }
1445
Adam Cohenfbba09b2011-07-18 21:43:05 -07001446 /**
1447 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001448 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001449 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001450 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001451 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001452 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001453 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001454 // In the case where we've prebound the widget, we remove it from the DragLayer
1455 if (LOGD) {
1456 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1457 }
1458 getDragLayer().removeView(hostView);
1459
Adam Cohened66b2b2012-01-23 17:28:51 -08001460 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001461 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001462
1463 // Clear the boundWidget so that it doesn't get destroyed.
1464 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001465 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001466 // In this case, we either need to start an activity to get permission to bind
1467 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001468 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1469 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1470 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1471 this, info.componentName);
1472 } else {
1473 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1474 }
Adam Cohendd70d662012-10-04 16:53:44 -07001475 Bundle options = info.bindOptions;
1476
Sunny Goyalffe83f12014-08-14 17:39:34 -07001477 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1478 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001479 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001480 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001481 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001482 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001483 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001484 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001485 }
1486
Adam Cohendcd297f2013-06-18 13:13:40 -07001487 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001488 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001489 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001490 folderInfo.title = getText(R.string.folder_name);
1491
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001492 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001493 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001494
1495 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001496 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301497 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001498 // Force measure the new folder icon
1499 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1500 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001501 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001502 }
Romain Guycbb89e42009-06-08 15:52:54 -07001503
Winsonc0b52fe2015-09-09 16:38:15 -07001504 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001505 * Unbinds the view for the specified item, and removes the item and all its children.
1506 *
1507 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001508 * @param itemInfo the {@link ItemInfo} for this view.
1509 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001510 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001511 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001512 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001513 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001514 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1515 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001516 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001517 } else {
1518 mWorkspace.removeWorkspaceItem(v);
1519 }
Winsonc0b52fe2015-09-09 16:38:15 -07001520 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001521 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001522 }
1523 } else if (itemInfo instanceof FolderInfo) {
1524 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001525 if (v instanceof FolderIcon) {
1526 ((FolderIcon) v).removeListeners();
1527 }
Winsonc0b52fe2015-09-09 16:38:15 -07001528 mWorkspace.removeWorkspaceItem(v);
1529 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001530 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001531 }
1532 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1533 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001534 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001535 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07001536 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001537 }
1538 } else {
1539 return false;
1540 }
1541 return true;
1542 }
1543
1544 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07001545 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07001546 */
Sunny Goyal56c73602015-09-25 12:55:01 -07001547 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07001548 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07001549 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07001550 // Deleting an app widget ID is a void call but writes to disk before returning
1551 // to the caller...
1552 new AsyncTask<Void, Void, Void>() {
1553 public Void doInBackground(Void ... args) {
1554 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
1555 return null;
1556 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07001557 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07001558 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001559 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001560 }
1561
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001562 @Override
1563 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001564 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001565 }
1566
Joe Onorato88ec0992009-11-19 13:16:06 -08001567 @Override
1568 public void onBackPressed() {
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08001569 if (finishAutoCancelActionMode()) {
1570 return;
1571 }
Adam Cohen9211d422014-10-07 18:14:53 -07001572 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1573 return;
1574 }
1575
Sunny Goyal45478022015-06-08 16:52:41 -07001576 if (mDragController.isDragging()) {
1577 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001578 return;
1579 }
1580
Jon Mirandafeba90f2016-10-06 10:53:29 -07001581 // Note: There should be at most one log per method call. This is enforced implicitly
1582 // by using if-else statements.
1583 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001584 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
1585 if (topView != null) {
Sunny Goyal37920962017-09-28 13:43:24 -07001586 topView.onBackPressed();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001587 } else if (!isInState(NORMAL)) {
Sunny Goyal49bcf342018-01-11 13:59:53 -08001588 LauncherState lastState = mStateManager.getLastState();
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001589 ued.logActionCommand(Action.Command.BACK, mStateManager.getState().containerType,
1590 lastState.containerType);
Sunny Goyal49bcf342018-01-11 13:59:53 -08001591 mStateManager.goToState(lastState);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001592 } else {
1593 // Back button is a no-op here, but give at least some feedback for the button press
1594 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001595 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001596 }
1597
Sunny Goyalfe770c92016-09-27 14:38:58 -07001598 @TargetApi(Build.VERSION_CODES.M)
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001599 @Override
Tony Wickham005df0b2018-02-13 11:28:12 -08001600 public ActivityOptions getActivityLaunchOptions(View v, boolean useDefaultLaunchOptions) {
Jon Miranda17940f02018-01-18 14:46:34 -08001601 return useDefaultLaunchOptions
Jon Miranda54441f52018-01-24 15:38:25 -08001602 ? mAppTransitionManager.getDefaultActivityLaunchOptions(this, v)
1603 : mAppTransitionManager.getActivityLaunchOptions(this, v);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001604 }
1605
Tony Wickham3a6746a2018-03-29 09:39:56 -07001606 public LauncherAppTransitionManager getAppTransitionManager() {
1607 return mAppTransitionManager;
1608 }
1609
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001610 @TargetApi(Build.VERSION_CODES.M)
1611 @Override
1612 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
1613 // Due to legacy reasons, direct call shortcuts require Launchers to have the
1614 // corresponding permission. Show the appropriate permission prompt if that
1615 // is the case.
1616 if (intent.getComponent() == null
1617 && Intent.ACTION_CALL.equals(intent.getAction())
1618 && checkSelfPermission(android.Manifest.permission.CALL_PHONE) !=
1619 PackageManager.PERMISSION_GRANTED) {
1620
1621 setWaitingForResult(PendingRequestArgs
1622 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
1623 requestPermissions(new String[]{android.Manifest.permission.CALL_PHONE},
1624 REQUEST_PERMISSION_CALL_PHONE);
1625 return true;
1626 } else {
1627 return false;
1628 }
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001629 }
1630
Sunny Goyala7ce1662016-05-31 15:01:35 -07001631 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001632 mAppLaunchSuccess = super.startActivitySafely(v, intent, item);
1633 if (mAppLaunchSuccess && v instanceof BubbleTextView) {
1634 // This is set to the view that launched the activity that navigated the user away
1635 // from launcher. Since there is no callback for when the activity has finished
1636 // launching, enable the press state and keep this reference to reset the press
1637 // state when we return to launcher.
1638 BubbleTextView btv = (BubbleTextView) v;
1639 btv.setStayPressed(true);
1640 setOnResumeCallback(btv);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001641 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07001642 return mAppLaunchSuccess;
Michael Jurka86a720e2012-05-09 11:23:23 -07001643 }
1644
Winson Chung3d503fb2011-07-13 17:25:49 -07001645 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001646 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07001647 return mHotseat != null && layout != null &&
1648 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
1649 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001650
Winson Chung3d503fb2011-07-13 17:25:49 -07001651 /**
1652 * Returns the CellLayout of the specified container at the specified screen.
1653 */
Sunny Goyal92820592015-03-02 11:31:35 -08001654 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001655 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1656 if (mHotseat != null) {
1657 return mHotseat.getLayout();
1658 } else {
1659 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08001660 }
Winson Chung3d503fb2011-07-13 17:25:49 -07001661 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08001662 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08001663 }
1664 }
1665
Michael Jurkae326f182011-11-21 14:05:46 -08001666 @Override
1667 public void onTrimMemory(int level) {
1668 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07001669 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
1670 // The widget preview db can result in holding onto over
1671 // 3MB of memory for caching which isn't necessary.
1672 SQLiteDatabase.releaseMemory();
1673
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08001674 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07001675 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08001676 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01001677 if (mLauncherCallbacks != null) {
1678 mLauncherCallbacks.onTrimMemory(level);
1679 }
Winson Chung62a70be2018-02-23 15:10:05 -08001680 UiFactory.onTrimMemory(this, level);
Michael Jurkae326f182011-11-21 14:05:46 -08001681 }
1682
Michael Jurkad7c28052012-04-27 15:43:36 -07001683 @Override
1684 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07001685 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07001686 final List<CharSequence> text = event.getText();
1687 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07001688 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001689 // TODO: When can workspace be null?
1690 text.add(mWorkspace == null
1691 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07001692 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07001693 return result;
1694 }
1695
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001696 public void setOnResumeCallback(OnResumeCallback callback) {
1697 if (mOnResumeCallback != null) {
1698 mOnResumeCallback.onLauncherResume();
1699 }
1700 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07001701 }
1702
Michael Jurka7607c2f2013-04-03 14:33:19 -07001703 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001704 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001705 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07001706 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001707 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07001708 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001709 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07001710 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07001711 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07001712 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001713 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001714
Joe Onorato9c1289c2009-08-17 11:03:03 -04001715 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07001716 * Clear any pending bind callbacks. This is called when is loader is planning to
1717 * perform a full rebind from scratch.
1718 */
1719 @Override
1720 public void clearPendingBinds() {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001721 if (mPendingExecutor != null) {
1722 mPendingExecutor.markCompleted();
1723 mPendingExecutor = null;
1724 }
1725 }
1726
1727 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001728 * Refreshes the shortcuts shown on the workspace.
1729 *
1730 * Implementation of the method from LauncherModel.Callbacks.
1731 */
1732 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001733 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001734 // Floating panels (except the full widget sheet) are associated with individual icons. If
1735 // we are starting a fresh bind, close all such panels as all the icons are about
1736 // to go away.
1737 AbstractFloatingView.closeOpenViews(this, true,
Sunny Goyal7ede6112017-12-05 15:11:21 -08001738 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_REBIND_SAFE);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08001739
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001740 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08001741
Winson Chungd64d1762013-08-20 14:37:16 -07001742 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07001743 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07001744 mWorkspace.removeAllWorkspaceScreens();
Sunny Goyalc11fac32018-02-27 19:20:15 -08001745 mAppWidgetHost.clearViews();
Winson Chungd64d1762013-08-20 14:37:16 -07001746
Winson Chung3d503fb2011-07-13 17:25:49 -07001747 if (mHotseat != null) {
Sunny Goyal11e96492018-03-23 17:06:00 -07001748 mHotseat.resetLayout(mDeviceProfile.isVerticalBarLayout());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001749 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001750 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001751 }
1752
Adam Cohendcd297f2013-06-18 13:13:40 -07001753 @Override
1754 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001755 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001756 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
1757 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001758 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
1759 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001760 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001761 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
1762 // If there are no screens, we need to have an empty screen
1763 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07001764 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001765 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07001766
Winsonc7d2e832016-07-28 12:24:55 -07001767 // After we have added all the screens, if the wallpaper was locked to the default state,
1768 // then notify to indicate that it can be released and a proper wallpaper offset can be
1769 // computed before the next layout
1770 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07001771 }
1772
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001773 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001774 int count = orderedScreenIds.size();
1775 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001776 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001777 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001778 // No need to bind the first screen, as its always bound.
1779 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
1780 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001781 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001782 }
1783
Sunny Goyalb23980c2017-08-17 07:45:25 -07001784 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08001785 public void bindAppsAdded(ArrayList<Long> newScreens, ArrayList<ItemInfo> addNotAnimated,
1786 ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07001787 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08001788 if (newScreens != null) {
1789 bindAddScreens(newScreens);
1790 }
Winson Chungd64d1762013-08-20 14:37:16 -07001791
1792 // We add the items without animation on non-visible pages, and with
1793 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08001794 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001795 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001796 }
Adam Cohen76a47a12014-02-05 11:47:43 -08001797 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001798 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07001799 }
Winson Chungc58497e2013-09-03 17:48:37 -07001800
Winson Chung87412982013-10-03 18:34:14 -07001801 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07001802 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001803 }
1804
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001805 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001806 * Bind the items start-end from the list.
1807 *
1808 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001809 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07001810 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07001811 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001812 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07001813 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001814 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07001815 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001816 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001817 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07001818 int end = items.size();
1819 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001820 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07001821
1822 // Short circuit if we are loading dock items for a configuration which has no dock
1823 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
1824 mHotseat == null) {
1825 continue;
1826 }
1827
Sunny Goyal639e9062015-08-19 19:17:06 -07001828 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001829 switch (item.itemType) {
1830 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1831 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08001832 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001833 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07001834 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001835 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001836 }
1837 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07001838 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001839 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001840 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001841 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001842 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07001843 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1844 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07001845 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07001846 if (view == null) {
1847 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001848 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08001849 break;
1850 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001851 default:
1852 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001853 }
Sunny Goyal639e9062015-08-19 19:17:06 -07001854
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001855 /*
1856 * Remove colliding items.
1857 */
1858 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1859 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
1860 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
1861 View v = cl.getChildAt(item.cellX, item.cellY);
1862 Object tag = v.getTag();
1863 String desc = "Collision while binding workspace item: " + item
1864 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08001865 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001866 throw (new RuntimeException(desc));
1867 } else {
1868 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001869 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001870 continue;
1871 }
1872 }
1873 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05301874 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07001875 if (animateIcons) {
1876 // Animate all the applications up now
1877 view.setAlpha(0f);
1878 view.setScaleX(0f);
1879 view.setScaleY(0f);
1880 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08001881 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07001882 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001883 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07001884
Winson Chung997a9232013-07-24 15:33:46 -07001885 if (animateIcons) {
1886 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08001887 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07001888 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08001889 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07001890 final Runnable startBounceAnimRunnable = new Runnable() {
1891 public void run() {
1892 anim.playTogether(bounceAnims);
1893 anim.start();
1894 }
1895 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08001896 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07001897 // We post the animation slightly delayed to prevent slowdowns
1898 // when we are loading right after we return to launcher.
1899 mWorkspace.postDelayed(new Runnable() {
1900 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00001901 if (mWorkspace != null) {
Jon Miranda64eb8ea2018-03-29 11:43:58 -07001902 AbstractFloatingView.closeAllOpenViews(Launcher.this, false);
1903
Ian Parkinson94449152014-01-21 13:09:59 +00001904 mWorkspace.snapToPage(newScreenIndex);
1905 mWorkspace.postDelayed(startBounceAnimRunnable,
1906 NEW_APPS_ANIMATION_DELAY);
1907 }
Winson Chung94d67682013-09-25 16:29:40 -07001908 }
1909 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07001910 } else {
1911 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07001912 }
1913 }
Winson Chung64359a52013-07-08 17:17:08 -07001914 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001915 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001916 }
1917
Joe Onorato9c1289c2009-08-17 11:03:03 -04001918 /**
1919 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001920 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07001921 public void bindAppWidget(LauncherAppWidgetInfo item) {
1922 View view = inflateAppWidget(item);
1923 if (view != null) {
1924 mWorkspace.addInScreen(view, item);
1925 mWorkspace.requestLayout();
1926 }
1927 }
1928
1929 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001930 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05301931 PendingAppWidgetHostView view =
1932 new PendingAppWidgetHostView(this, item, mIconCache, true);
1933 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07001934 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001935 }
1936
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001937 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001938
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001939 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08001940
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001941 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
1942 // If the provider is not ready, bind as a pending widget.
1943 appWidgetInfo = null;
1944 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
1945 // The widget id is not valid. Try to find the widget based on the provider info.
1946 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
1947 } else {
1948 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
1949 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07001950
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001951 // If the provider is ready, but the width is not yet restored, try to restore it.
1952 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
1953 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07001954 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001955 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
1956 + " belongs to component " + item.providerName
1957 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001958 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07001959 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07001960 }
Sunny Goyalff572272014-07-23 13:58:07 -07001961
Sunny Goyal84b4adc2015-08-12 15:12:16 -07001962 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001963 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07001964 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
1965 // Id has not been allocated yet. Allocate a new id.
1966 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
1967 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07001968
Sunny Goyald478c832016-04-01 12:04:16 -07001969 // Also try to bind the widget. If the bind fails, the user will be shown
1970 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08001971 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07001972 pendingInfo.spanX = item.spanX;
1973 pendingInfo.spanY = item.spanY;
1974 pendingInfo.minSpanX = item.minSpanX;
1975 pendingInfo.minSpanY = item.minSpanY;
1976 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07001977
1978 boolean isDirectConfig =
1979 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
1980 if (isDirectConfig && item.bindOptions != null) {
1981 Bundle newOptions = item.bindOptions.getExtras();
1982 if (options != null) {
1983 newOptions.putAll(options);
1984 }
1985 options = newOptions;
1986 }
Sunny Goyald478c832016-04-01 12:04:16 -07001987 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1988 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07001989
Sunny Goyal86df1382016-08-10 15:03:22 -07001990 // We tried to bind once. If we were not able to bind, we would need to
1991 // go through the permission dialog, which means we cannot skip the config
1992 // activity.
1993 item.bindOptions = null;
1994 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
1995
Sunny Goyald478c832016-04-01 12:04:16 -07001996 // Bind succeeded
1997 if (success) {
1998 // If the widget has a configure activity, it is still needs to set it up,
1999 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002000 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002001 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2002 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002003 }
Sunny Goyald478c832016-04-01 12:04:16 -07002004
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002005 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002006 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002007 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002008 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002009 // The widget was marked as UI not ready, but there is no configure activity to
2010 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002011 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002012 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002013 }
Sunny Goyalff572272014-07-23 13:58:07 -07002014 }
2015
Sunny Goyald5462aa2016-11-22 16:52:39 +05302016 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002017 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002018 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002019 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002020 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07002021 deleteWidgetInfo(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002022 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002023 }
2024
Sunny Goyal233ee962015-08-03 13:05:01 -07002025 item.minSpanX = appWidgetInfo.minSpanX;
2026 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302027 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002028 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302029 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002030 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302031 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002032
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002033 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002034 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002035 }
2036
Sunny Goyalff572272014-07-23 13:58:07 -07002037 /**
2038 * Restores a pending widget.
2039 *
2040 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002041 */
Sunny Goyald478c832016-04-01 12:04:16 -07002042 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002043 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2044 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2045 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002046 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002047 }
2048
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002049 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002050 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002051 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2052 info.pendingItemInfo = null;
2053 }
Sunny Goyalff572272014-07-23 13:58:07 -07002054
Sunny Goyalba47faa2017-10-04 16:50:57 -07002055 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002056 view.reInflate();
Sunny Goyalba47faa2017-10-04 16:50:57 -07002057 }
2058
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002059 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002060 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002061 }
2062
Adam Cohen1462de32012-07-24 22:34:36 -07002063 public void onPageBoundSynchronously(int page) {
Sunny Goyalf4aceab2017-12-06 16:16:31 -08002064 mSynchronouslyBoundPage = page;
Adam Cohen1462de32012-07-24 22:34:36 -07002065 }
2066
Sunny Goyal527c7d32015-08-28 15:19:36 -07002067 @Override
2068 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2069 if (mPendingExecutor != null) {
2070 mPendingExecutor.markCompleted();
2071 }
2072 mPendingExecutor = executor;
Sunny Goyal60180b02018-02-07 12:56:30 -08002073 if (!isInState(ALL_APPS)) {
2074 mAppsView.getAppsStore().setDeferUpdates(true);
2075 mPendingExecutor.execute(() -> mAppsView.getAppsStore().setDeferUpdates(false));
2076 }
2077
Sunny Goyal527c7d32015-08-28 15:19:36 -07002078 executor.attachTo(this);
2079 }
2080
2081 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2082 if (mPendingExecutor == executor) {
2083 mPendingExecutor = null;
2084 }
2085 }
2086
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002087 @Override
2088 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
Jon Mirandadbd92862018-05-01 13:07:53 -07002089 if (mLauncherView.getAlpha() < 1) {
2090 mLauncherView.animate().alpha(1).withEndAction(
Sunny Goyal29947f02017-12-18 13:49:44 -08002091 executor == null ? null : executor::onLoadAnimationCompleted).start();
2092 } else if (executor != null) {
2093 executor.onLoadAnimationCompleted();
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002094 }
2095 }
2096
Joe Onorato9c1289c2009-08-17 11:03:03 -04002097 /**
2098 * Callback saying that there aren't any more items to bind.
2099 *
2100 * Implementation of the method from LauncherModel.Callbacks.
2101 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002102 public void finishBindingItems() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002103 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002104 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002105
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002106 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002107
Sunny Goyal2100c782016-08-22 16:00:03 -07002108 if (mPendingActivityResult != null) {
2109 handleActivityResult(mPendingActivityResult.requestCode,
2110 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2111 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002112 }
2113
Sunny Goyala474a9b2017-05-04 16:47:11 -07002114 InstallShortcutReceiver.disableAndFlushInstallQueue(
2115 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002116
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002117 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002118 }
2119
Winson Chunga2413752012-04-03 14:22:34 -07002120 private boolean canRunNewAppsAnimation() {
2121 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002122 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002123 }
2124
Winson Chungc9168342013-06-26 14:54:55 -07002125 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002126 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07002127 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
2128 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002129 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002130 return bounceAnim;
2131 }
2132
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002133 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002134 * Add the icons for all apps.
2135 *
2136 * Implementation of the method from LauncherModel.Callbacks.
2137 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002138 public void bindAllApplications(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002139 mAppsView.getAppsStore().setApps(apps);
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002140
Adam Cohen9211d422014-10-07 18:14:53 -07002141 if (mLauncherCallbacks != null) {
2142 mLauncherCallbacks.bindAllApplications(apps);
2143 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002144 }
2145
2146 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002147 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
2148 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2149 */
2150 @Override
2151 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002152 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002153 }
2154
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002155 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002156 * A package was updated.
2157 *
2158 * Implementation of the method from LauncherModel.Callbacks.
2159 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002160 @Override
2161 public void bindAppsAddedOrUpdated(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002162 mAppsView.getAppsStore().addOrUpdateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002163 }
2164
Sunny Goyal4390ace2014-10-13 11:33:11 -07002165 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002166 public void bindPromiseAppProgressUpdated(PromiseAppInfo app) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002167 mAppsView.getAppsStore().updatePromiseAppProgress(app);
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002168 }
2169
2170 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002171 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002172 mWorkspace.widgetsRestored(widgets);
2173 }
2174
Joe Onorato9c1289c2009-08-17 11:03:03 -04002175 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002176 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002177 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002178 *
2179 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002180 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002181 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002182 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002183 if (!updated.isEmpty()) {
2184 mWorkspace.updateShortcuts(updated);
2185 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002186 }
2187
2188 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002189 * Update the state of a package, typically related to install state.
2190 *
2191 * Implementation of the method from LauncherModel.Callbacks.
2192 */
Sunny Goyale755d462014-07-22 13:48:29 -07002193 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002194 public void bindRestoreItemsChange(HashSet<ItemInfo> updates) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07002195 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07002196 }
2197
2198 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002199 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07002200 * in addition to specific applications to remove, the reason being that
2201 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002202 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07002203 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002204 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07002205 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002206 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002207 mWorkspace.removeItemsByMatcher(matcher);
2208 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002209 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002210
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002211 @Override
2212 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002213 mAppsView.getAppsStore().removeApps(appInfos);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002214 }
Joe Onoratobe386092009-11-17 17:32:16 -08002215
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002216 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002217 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
2218 mPopupDataProvider.setAllWidgets(allWidgets);
Tony Wickham26b17462017-03-20 17:12:24 -07002219 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
2220 if (topView != null) {
2221 topView.onWidgetsBound();
2222 }
2223 }
2224
Tony Wickham86222d22017-03-29 15:30:43 -07002225 /**
2226 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
2227 * refreshes the widgets and shortcuts associated with the given package/user
2228 */
2229 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07002230 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002231 }
2232
Winson Chung80baf5a2010-08-09 16:03:15 -07002233 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002234 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08002235 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002236 @Override
2237 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
2238 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002239
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002240 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
2241 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07002242 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002243 writer.println(prefix + " Homescreen " + i);
2244
2245 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
2246 for (int j = 0; j < layout.getChildCount(); j++) {
2247 Object tag = layout.getChildAt(j).getTag();
2248 if (tag != null) {
2249 writer.println(prefix + " " + tag.toString());
2250 }
2251 }
2252 }
2253
2254 writer.println(prefix + " Hotseat");
2255 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07002256 for (int j = 0; j < layout.getChildCount(); j++) {
2257 Object tag = layout.getChildAt(j).getTag();
2258 if (tag != null) {
2259 writer.println(prefix + " " + tag.toString());
2260 }
Adam Cohen4caf2982013-08-20 18:54:31 -07002261 }
Sunny Goyala1365452015-10-01 15:46:24 -07002262 }
2263
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002264 writer.println(prefix + "Misc:");
2265 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
2266 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
2267 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
Sunny Goyal2bba1902018-02-27 12:20:50 -08002268 writer.println(" deviceProfile isTransposed=" + getDeviceProfile().isVerticalBarLayout());
2269 writer.println(" orientation=" + getResources().getConfiguration().orientation);
2270
2271 try {
2272 FileLog.flushAll(writer);
2273 } catch (Exception e) {
2274 // Ignore
2275 }
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002276
2277 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002278
Adam Cohen9211d422014-10-07 18:14:53 -07002279 if (mLauncherCallbacks != null) {
2280 mLauncherCallbacks.dump(prefix, fd, writer, args);
2281 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002282 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002283
Sunny Goyal66b24572016-09-21 15:57:55 -07002284 @Override
2285 @TargetApi(Build.VERSION_CODES.N)
2286 public void onProvideKeyboardShortcuts(
2287 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
2288
2289 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002290 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002291 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
2292 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
2293 }
2294 View currentFocus = getCurrentFocus();
2295 if (new CustomActionsPopup(this, currentFocus).canShow()) {
2296 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
2297 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
2298 }
Tony18c4aa42017-05-10 21:23:57 -05002299 if (currentFocus.getTag() instanceof ItemInfo
2300 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
Vadim Trysheva50d99f2018-05-04 17:58:29 -07002301 shortcutInfos.add(new KeyboardShortcutInfo(
2302 getString(R.string.shortcuts_menu_with_notifications_description),
Sunny Goyal66b24572016-09-21 15:57:55 -07002303 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
2304 }
2305 if (!shortcutInfos.isEmpty()) {
2306 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
2307 }
2308
2309 super.onProvideKeyboardShortcuts(data, menu, deviceId);
2310 }
2311
2312 @Override
2313 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
2314 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
2315 switch (keyCode) {
2316 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002317 if (isInState(NORMAL)) {
2318 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07002319 return true;
2320 }
2321 break;
2322 case KeyEvent.KEYCODE_S: {
2323 View focusedView = getCurrentFocus();
2324 if (focusedView instanceof BubbleTextView
2325 && focusedView.getTag() instanceof ItemInfo
2326 && mAccessibilityDelegate.performAction(focusedView,
2327 (ItemInfo) focusedView.getTag(),
2328 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal00ac9202017-11-09 15:24:06 -08002329 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07002330 return true;
2331 }
2332 break;
2333 }
2334 case KeyEvent.KEYCODE_O:
2335 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
2336 return true;
2337 }
2338 break;
2339 }
2340 }
2341 return super.onKeyShortcut(keyCode, event);
2342 }
2343
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002344 @Override
2345 public boolean onKeyUp(int keyCode, KeyEvent event) {
2346 if (keyCode == KeyEvent.KEYCODE_MENU) {
2347 // KEYCODE_MENU is sent by some tests, for example
2348 // LauncherJankTests#testWidgetsContainerFling. Don't just remove its handling.
2349 if (!mDragController.isDragging() && !mWorkspace.isSwitchingState() &&
2350 isInState(NORMAL)) {
2351 // Close any open floating views.
2352 AbstractFloatingView.closeAllOpenViews(this);
2353
2354 // Setting the touch point to (-1, -1) will show the options popup in the center of
2355 // the screen.
Sunny Goyal2fd7a8b2018-03-30 17:10:13 -07002356 OptionsPopupView.showDefaultOptions(this, -1, -1);
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002357 }
2358 return true;
2359 }
2360 return super.onKeyUp(keyCode, event);
2361 }
2362
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002363 public static Launcher getLauncher(Context context) {
2364 if (context instanceof Launcher) {
2365 return (Launcher) context;
2366 }
2367 return ((Launcher) ((ContextWrapper) context).getBaseContext());
2368 }
2369
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002370 /**
2371 * Callback for listening for onResume
2372 */
2373 public interface OnResumeCallback {
2374
2375 void onLauncherResume();
2376 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002377}