blob: 5eb568853a389d38facea99be935484412b09789 [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
Jon Mirandaaeb4dd02018-07-11 14:16:23 -070019import static android.content.pm.ActivityInfo.CONFIG_LOCALE;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080020import static android.content.pm.ActivityInfo.CONFIG_ORIENTATION;
21import static android.content.pm.ActivityInfo.CONFIG_SCREEN_SIZE;
Tony Wickham6a71a5b2018-08-21 11:40:23 -070022import static com.android.launcher3.AbstractFloatingView.TYPE_SNACKBAR;
Sunny Goyal4c7f2152017-10-17 17:17:16 -070023import static com.android.launcher3.LauncherAnimUtils.SPRING_LOADED_EXIT_DELAY;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -070024import static com.android.launcher3.LauncherState.ALL_APPS;
25import static com.android.launcher3.LauncherState.NORMAL;
Sunny Goyal6001ea22018-05-10 16:31:00 -070026import static com.android.launcher3.dragndrop.DragLayer.ALPHA_INDEX_LAUNCHER_LOAD;
Sunny Goyalaeb16432017-10-16 11:46:41 -070027import static com.android.launcher3.logging.LoggerUtils.newContainerTarget;
Hyunyoung Song46d07f72018-05-22 15:41:25 -070028import static com.android.launcher3.logging.LoggerUtils.newTarget;
Sunny Goyal326403e2017-10-02 12:45:10 -070029
Gilles Debunnedd6c9922010-10-25 11:23:41 -070030import android.animation.Animator;
Sunny Goyal6001ea22018-05-10 16:31:00 -070031import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070032import android.animation.AnimatorSet;
Sunny Goyal6001ea22018-05-10 16:31:00 -070033import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070034import android.animation.ValueAnimator;
Adam Cohen0b395352014-06-09 22:54:36 +000035import android.annotation.TargetApi;
Tony Wickham005df0b2018-02-13 11:28:12 -080036import android.app.ActivityOptions;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080037import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070038import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080040import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080041import android.content.ComponentCallbacks2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070044import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070045import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070046import android.content.SharedPreferences;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.content.pm.PackageManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080048import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070049import android.database.sqlite.SQLiteDatabase;
Sunny Goyal59d086c2018-05-07 17:31:40 -070050import android.graphics.Point;
Adam Cohen0f668f32014-09-08 19:54:17 +020051import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052import android.os.Bundle;
Hyunyoung Songbd6fba92018-05-16 15:54:31 -070053import android.os.Handler;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070054import android.os.Parcelable;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080055import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070056import android.os.StrictMode;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070057import android.os.UserHandle;
Michael Jurkaa33411c2012-06-14 16:18:21 -070058import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070060import android.util.Log;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070061import android.util.SparseArray;
Sunny Goyal59d086c2018-05-07 17:31:40 -070062import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070063import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070064import android.view.KeyboardShortcutGroup;
65import android.view.KeyboardShortcutInfo;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080066import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -070067import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070068import android.view.View;
Adam Cohen96d30a12013-07-16 18:13:21 -070069import android.view.ViewGroup;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080070import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070071import android.view.animation.OvershootInterpolator;
Adam Cohen96d30a12013-07-16 18:13:21 -070072import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070073
Sunny Goyal651077b2014-06-30 14:15:31 -070074import com.android.launcher3.DropTarget.DragObject;
Sunny Goyalae502842016-06-17 08:43:56 -070075import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070076import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070077import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal7ede6112017-12-05 15:11:21 -080078import com.android.launcher3.allapps.DiscoveryBounce;
Sunny Goyalf0b6db72018-08-13 16:10:14 -070079import com.android.launcher3.anim.PropertyListBuilder;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070080import com.android.launcher3.badge.BadgeInfo;
Sunny Goyalffe83f12014-08-14 17:39:34 -070081import com.android.launcher3.compat.AppWidgetManagerCompat;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -070082import com.android.launcher3.compat.LauncherAppsCompatVO;
Tony Wickhame0c33232016-02-08 11:37:04 -080083import com.android.launcher3.config.FeatureFlags;
Vadim Tryshevfedca432015-08-19 17:55:02 -070084import com.android.launcher3.dragndrop.DragController;
85import com.android.launcher3.dragndrop.DragLayer;
86import com.android.launcher3.dragndrop.DragView;
Jon Mirandaaeb4dd02018-07-11 14:16:23 -070087import com.android.launcher3.folder.Folder;
Sunny Goyal26119432016-02-18 22:09:23 +000088import com.android.launcher3.folder.FolderIcon;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070089import com.android.launcher3.folder.FolderIconPreviewVerifier;
Sunny Goyalf840f102018-09-21 14:41:05 -070090import com.android.launcher3.icons.IconCache;
Sunny Goyal66b24572016-09-21 15:57:55 -070091import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -070092import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -070093import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -070094import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Song46d07f72018-05-22 15:41:25 -070095import com.android.launcher3.logging.UserEventDispatcher.UserEventDelegate;
Sunny Goyala535ae42017-02-27 10:07:13 -080096import com.android.launcher3.model.ModelWriter;
Sunny Goyala535ae42017-02-27 10:07:13 -080097import com.android.launcher3.notification.NotificationListener;
Tony Wickham540913e2017-01-23 11:47:51 -080098import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -080099import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700100import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyalf8088ee2017-11-10 14:52:00 -0800101import com.android.launcher3.states.InternalStateHandler;
Sunny Goyal623eddd2018-03-02 12:24:41 -0800102import com.android.launcher3.states.RotationHelper;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800103import com.android.launcher3.touch.ItemClickHandler;
Sunny Goyala5ace712017-11-15 17:12:51 -0800104import com.android.launcher3.uioverrides.UiFactory;
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700105import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530106import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
107import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
Sunny Goyalaeb16432017-10-16 11:46:41 -0700108import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyal2100c782016-08-22 16:00:03 -0700109import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700110import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700111import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700112import com.android.launcher3.util.MultiHashMap;
Sunny Goyal6001ea22018-05-10 16:31:00 -0700113import com.android.launcher3.util.MultiValueAlpha;
114import com.android.launcher3.util.MultiValueAlpha.AlphaProperty;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700115import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800116import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700117import com.android.launcher3.util.PendingRequestArgs;
Sunny Goyal8392c822017-06-20 10:03:56 -0700118import com.android.launcher3.util.SystemUiController;
Mario Bertschlera6936942017-05-31 14:48:19 -0700119import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700120import com.android.launcher3.util.Thunk;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700121import com.android.launcher3.util.TraceHelper;
Sunny Goyal326403e2017-10-02 12:45:10 -0700122import com.android.launcher3.util.UiThreadHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700123import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800124import com.android.launcher3.views.OptionsPopupView;
Sunny Goyal29947f02017-12-18 13:49:44 -0800125import com.android.launcher3.widget.LauncherAppWidgetHostView;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800126import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700127import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal29947f02017-12-18 13:49:44 -0800128import com.android.launcher3.widget.PendingAppWidgetHostView;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800129import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700130import com.android.launcher3.widget.WidgetHostViewLoader;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700131import com.android.launcher3.widget.WidgetListRowEntry;
132import com.android.launcher3.widget.WidgetsFullSheet;
Sunny Goyal952e63d2017-08-16 04:59:08 -0700133import com.android.launcher3.widget.custom.CustomWidgetParser;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700134
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700135import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700136import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700137import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700138import java.util.Collection;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700139import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700140import java.util.List;
Tony Wickham010d2552017-01-20 08:15:28 -0800141import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700142
Sunny Goyald2303072018-08-14 15:21:45 -0700143import androidx.annotation.Nullable;
144
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145/**
146 * Default launcher application.
147 */
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700148public class Launcher extends BaseDraggingActivity implements LauncherExterns,
149 LauncherModel.Callbacks, LauncherProviderChangeListener, UserEventDelegate{
Vadim Tryshevfedca432015-08-19 17:55:02 -0700150 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700151 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152
Winson Chunga2413752012-04-03 14:22:34 -0700153 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700154
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700156 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700157
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700158 private static final int REQUEST_PICK_APPWIDGET = 9;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159
Michael Jurka8b805b12012-04-18 14:23:14 -0700160 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800161 public static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
162 public static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700163
Jon Mirandac476d6e2017-05-04 16:30:01 -0700164 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700165
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700166 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
167
Mathew Inwood876a8462013-06-14 14:12:41 +0100168 /**
169 * IntentStarter uses request codes starting with this. This must be greater than all activity
170 * request codes used internally.
171 */
172 protected static final int REQUEST_LAST = 100;
173
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174 // Type: int
175 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700176 // Type: int
177 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700178 // Type: PendingRequestArgs
179 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
180 // Type: ActivityResultInfo
181 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700182 // Type: SparseArray<Parcelable>
183 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700185 private LauncherStateManager mStateManager;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700186
Adam Cohenad4e15c2013-10-17 16:21:35 -0700187 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188
Winson Chunga2413752012-04-03 14:22:34 -0700189 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700190 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
191 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
192 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700193
Jon Miranda54441f52018-01-24 15:38:25 -0800194 private LauncherAppTransitionManager mAppTransitionManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800195 private Configuration mOldConfig;
Jon Miranda54441f52018-01-24 15:38:25 -0800196
Adam Cohen091440a2015-03-18 14:16:05 -0700197 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700198 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700199 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800200 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700201
Sunny Goyalffe83f12014-08-14 17:39:34 -0700202 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700203 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700204
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700205 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700206
Sunny Goyal316490e2015-06-02 09:38:28 -0700207 @Thunk Hotseat mHotseat;
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700208 @Nullable private View mHotseatSearchBox;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700209
Sunny Goyal47328fd2016-05-25 18:56:41 -0700210 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700211
212 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700213 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700214 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700215
Winson Chung8ae41982017-11-10 11:42:13 -0800216 // UI and state for the overview panel
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800217 private View mOverviewPanel;
Winson Chung8ae41982017-11-10 11:42:13 -0800218
Adam Cohen091440a2015-03-18 14:16:05 -0700219 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400220
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700221 private OnResumeCallback mOnResumeCallback;
222
Sunny Goyal527c7d32015-08-28 15:19:36 -0700223 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700224
Joe Onorato9c1289c2009-08-17 11:03:03 -0400225 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800226 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800227 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700228 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400229
Tony Wickham010d2552017-01-20 08:15:28 -0800230 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700231
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800232 private int mSynchronouslyBoundPage = PagedView.INVALID_PAGE;
Adam Cohen1462de32012-07-24 22:34:36 -0700233
Winson Chung46353de2012-02-16 14:05:10 -0800234 // We only want to get the SharedPreferences once since it does an FS stat each time we get
235 // it from the context.
236 private SharedPreferences mSharedPrefs;
237
Sunny Goyal2100c782016-08-22 16:00:03 -0700238 // Activity result which needs to be processed after workspace has loaded.
239 private ActivityResultInfo mPendingActivityResult;
240 /**
241 * Holds extra information required to handle a result from an external call, like
242 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
243 */
244 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800245
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700246 public ViewGroupFocusHelper mFocusHandler;
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700247
Sunny Goyal623eddd2018-03-02 12:24:41 -0800248 private RotationHelper mRotationHelper;
Sunny Goyal7779d622015-06-11 16:18:39 -0700249
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700250
251 private final Handler mHandler = new Handler();
252 private final Runnable mLogOnDelayedResume = this::logOnDelayedResume;
253
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800254 @Override
255 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700256 if (DEBUG_STRICT_MODE) {
257 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
258 .detectDiskReads()
259 .detectDiskWrites()
260 .detectNetwork() // or .detectAll() for all detectable problems
261 .penaltyLog()
262 .build());
263 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
264 .detectLeakedSqlLiteObjects()
265 .detectLeakedClosableObjects()
266 .penaltyLog()
267 .penaltyDeath()
268 .build());
269 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700270 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700271
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800272 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700273 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400274
Sunny Goyal87f784c2017-01-11 10:48:34 -0800275 LauncherAppState app = LauncherAppState.getInstance(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800276 mOldConfig = new Configuration(getResources().getConfiguration());
Sunny Goyal0e7724c2018-02-22 13:22:21 -0800277 mModel = app.setLauncher(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800278 initDeviceProfile(app.getInvariantDeviceProfile());
Winson Chung6e1c0d32013-10-25 15:24:24 -0700279
Sunny Goyalf7258242015-10-19 16:59:07 -0700280 mSharedPrefs = Utilities.getPrefs(this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800281 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700282 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700283
Joe Onorato41a12d22009-10-31 18:30:00 -0400284 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700285 mAllAppsController = new AllAppsTransitionController(this);
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800286 mStateManager = new LauncherStateManager(this);
Tracy Zhou8b23c6f2018-04-30 17:13:27 -0700287 UiFactory.onCreate(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700288
Sunny Goyalffe83f12014-08-14 17:39:34 -0700289 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700290
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700291 mAppWidgetHost = new LauncherAppWidgetHost(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700292 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700293
Sunny Goyal60820d72017-05-09 12:40:11 -0700294 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700295
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800296 setupViews();
Tony Wickham010d2552017-01-20 08:15:28 -0800297 mPopupDataProvider = new PopupDataProvider(this);
298
Sunny Goyal623eddd2018-03-02 12:24:41 -0800299 mRotationHelper = new RotationHelper(this);
Sunny Goyal90548432018-04-04 17:17:00 -0700300 mAppTransitionManager = LauncherAppTransitionManager.newInstance(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800301
Sunny Goyald3864fa2017-11-14 15:06:36 -0800302 boolean internalStateHandled = InternalStateHandler.handleCreate(this, getIntent());
303 if (internalStateHandled) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800304 if (savedInstanceState != null) {
305 // InternalStateHandler has already set the appropriate state.
306 // We dont need to do anything.
307 savedInstanceState.remove(RUNTIME_STATE);
308 }
309 }
310 restoreState(savedInstanceState);
Winson Chungb63b44c2017-11-10 17:54:44 -0800311
Sunny Goyal2100c782016-08-22 16:00:03 -0700312 // We only load the page synchronously if the user rotates (or triggers a
313 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700314 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
315 if (savedInstanceState != null) {
316 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
317 }
Sunny Goyald3864fa2017-11-14 15:06:36 -0800318
Sunny Goyalfe770c92016-09-27 14:38:58 -0700319 if (!mModel.startLoader(currentScreen)) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800320 if (!internalStateHandled) {
321 // If we are not binding synchronously, show a fade in animation when
322 // the first page bind completes.
Sunny Goyal6001ea22018-05-10 16:31:00 -0700323 mDragLayer.getAlphaProperty(ALPHA_INDEX_LAUNCHER_LOAD).setValue(0);
Sunny Goyald3864fa2017-11-14 15:06:36 -0800324 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700325 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700326 // Pages bound synchronously.
327 mWorkspace.setCurrentPage(currentScreen);
328
Sunny Goyal2100c782016-08-22 16:00:03 -0700329 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800330 }
331
332 // For handling default keys
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800333 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800334
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800335 setContentView(mLauncherView);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800336 getRootView().dispatchInsets();
Mario Bertschlera6936942017-05-31 14:48:19 -0700337
Jon Miranda7fda2852017-07-19 16:07:01 -0700338 // Listen for broadcasts
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700339 registerReceiver(mScreenOffReceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));
Sunny Goyala616d2b2017-06-12 13:54:01 -0700340
Sunny Goyal8392c822017-06-20 10:03:56 -0700341 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
342 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700343
344 if (mLauncherCallbacks != null) {
345 mLauncherCallbacks.onCreate(savedInstanceState);
346 }
Sunny Goyal623eddd2018-03-02 12:24:41 -0800347 mRotationHelper.initialize();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700348
349 TraceHelper.endSection("Launcher-onCreate");
Adam Cohen9211d422014-10-07 18:14:53 -0700350 }
351
Sunny Goyal7779d622015-06-11 16:18:39 -0700352 @Override
Winson Chung8eb49e02018-07-16 13:33:31 -0700353 public void onEnterAnimationComplete() {
354 super.onEnterAnimationComplete();
355 UiFactory.onEnterAnimationComplete(this);
arangelovad783052018-08-17 14:46:45 +0100356 mAllAppsController.highlightWorkTabIfNecessary();
Winson Chung8eb49e02018-07-16 13:33:31 -0700357 }
358
359 @Override
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800360 public void onConfigurationChanged(Configuration newConfig) {
361 int diff = newConfig.diff(mOldConfig);
Jon Mirandaaeb4dd02018-07-11 14:16:23 -0700362
363 if ((diff & CONFIG_LOCALE) != 0) {
364 Folder.setLocaleDependentFields(getResources(), true /* force */);
365 }
366
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800367 if ((diff & (CONFIG_ORIENTATION | CONFIG_SCREEN_SIZE)) != 0) {
368 mUserEventDispatcher = null;
369 initDeviceProfile(mDeviceProfile.inv);
Sunny Goyalfde55052018-02-01 14:46:13 -0800370 dispatchDeviceProfileChanged();
Sunny Goyal59d086c2018-05-07 17:31:40 -0700371 reapplyUi();
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700372 mDragLayer.recreateControllers();
Sunny Goyal7185dd62018-03-14 17:51:49 -0700373
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800374 // TODO: We can probably avoid rebind when only screen size changed.
Sunny Goyal605bcf32018-03-02 15:16:12 -0800375 rebindModel();
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800376 }
377
378 mOldConfig.setTo(newConfig);
Tracy Zhoua706f002018-03-28 13:55:19 -0700379 UiFactory.onLauncherStateOrResumeChanged(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800380 super.onConfigurationChanged(newConfig);
381 }
382
Sunny Goyal605bcf32018-03-02 15:16:12 -0800383 @Override
Sunny Goyal59d086c2018-05-07 17:31:40 -0700384 protected void reapplyUi() {
385 getRootView().dispatchInsets();
386 getStateManager().reapplyState(true /* cancelCurrentAnimation */);
387 }
388
389 @Override
Sunny Goyal605bcf32018-03-02 15:16:12 -0800390 public void rebindModel() {
391 int currentPage = mWorkspace.getNextPage();
392 if (mModel.startLoader(currentPage)) {
393 mWorkspace.setCurrentPage(currentPage);
394 setWorkspaceLoading(true);
395 }
396 }
397
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800398 private void initDeviceProfile(InvariantDeviceProfile idp) {
399 // Load configuration-specific DeviceProfile
Sunny Goyal59d086c2018-05-07 17:31:40 -0700400 mDeviceProfile = idp.getDeviceProfile(this);
401 if (isInMultiWindowModeCompat()) {
402 Display display = getWindowManager().getDefaultDisplay();
403 Point mwSize = new Point();
404 display.getSize(mwSize);
405 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
406 }
407 onDeviceProfileInitiated();
Sunny Goyal605bcf32018-03-02 15:16:12 -0800408 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout(), true);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800409 }
410
Sunny Goyal623eddd2018-03-02 12:24:41 -0800411 public RotationHelper getRotationHelper() {
412 return mRotationHelper;
413 }
414
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700415 public LauncherStateManager getStateManager() {
416 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700417 }
418
Mario Bertschler27288382017-05-24 15:35:09 -0700419 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700420 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800421 return mLauncherView.findViewById(id);
422 }
423
424 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700425 public void onAppWidgetHostReset() {
426 if (mAppWidgetHost != null) {
427 mAppWidgetHost.startListening();
428 }
429 }
430
Adam Cohen9211d422014-10-07 18:14:53 -0700431 private LauncherCallbacks mLauncherCallbacks;
432
Sunny Goyal32554d12015-12-03 15:31:25 -0800433 /**
434 * Call this after onCreate to set or clear overlay.
435 */
436 public void setLauncherOverlay(LauncherOverlay overlay) {
437 if (overlay != null) {
438 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
439 }
440 mWorkspace.setLauncherOverlay(overlay);
441 }
442
Adam Cohen9211d422014-10-07 18:14:53 -0700443 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
444 mLauncherCallbacks = callbacks;
445 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700446 }
447
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700448 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700449 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700450 if (mLauncherCallbacks != null) {
451 mLauncherCallbacks.onLauncherProviderChange();
452 }
453 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700454
Hyunyoung Song3f471442015-04-08 19:01:34 -0700455 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700456 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
457 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700458 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700459 }
460
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000461 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700462 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
463 // This cast is safe as long as the id < 0x00FFFFFF
464 // Since we jail all the dynamically generated views, there should be no clashes
465 // with any other views.
466 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000467 }
468
Tony Wickham010d2552017-01-20 08:15:28 -0800469 public PopupDataProvider getPopupDataProvider() {
470 return mPopupDataProvider;
471 }
472
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700473 @Override
474 public BadgeInfo getBadgeInfoForItem(ItemInfo info) {
475 return mPopupDataProvider.getBadgeInfoForItem(info);
476 }
477
478 @Override
479 public void invalidateParent(ItemInfo info) {
480 FolderIconPreviewVerifier verifier = new FolderIconPreviewVerifier(getDeviceProfile().inv);
481 if (verifier.isItemInPreview(info.rank) && (info.container >= 0)) {
482 View folderIcon = getWorkspace().getHomescreenIconByItemId(info.container);
483 if (folderIcon != null) {
484 folderIcon.invalidate();
485 }
486 }
487 }
488
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000489 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700490 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
491 * a configuration step, this allows the proper animations to run after other transitions.
492 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700493 private long completeAdd(
494 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
495 long screenId = info.screenId;
496 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700497 // When the screen id represents an actual screen (as opposed to a rank) we make sure
498 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700499 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700500 }
Adam Cohendb364c32014-05-20 14:23:40 -0700501
Sunny Goyal2100c782016-08-22 16:00:03 -0700502 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800503 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700504 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700505 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800506 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700507 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700508 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700509 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700510 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700511 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700512 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700513 int widgetId = appWidgetId;
514 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700515 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700516 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700517 // Since the view was just bound, also launch the configure activity if needed
518 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
519 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800520 if (provider != null) {
521 new WidgetAddFlowHandler(provider)
522 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700523 }
524 }
525 break;
526 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800527 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700528
Adam Cohendb364c32014-05-20 14:23:40 -0700529 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800530 }
531
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800532 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700533 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700534 if (isWorkspaceLoading()) {
535 // process the result once the workspace has loaded.
536 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
537 return;
538 }
539 mPendingActivityResult = null;
540
Winson Chunge341d302013-08-16 14:31:00 -0700541 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700542 final PendingRequestArgs requestArgs = mPendingRequestArgs;
543 setWaitingForResult(null);
544 if (requestArgs == null) {
545 return;
546 }
547
548 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700549
Adam Cohenad4e15c2013-10-17 16:21:35 -0700550 Runnable exitSpringLoaded = new Runnable() {
551 @Override
552 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700553 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700554 }
555 };
556
Michael Jurka8b805b12012-04-18 14:23:14 -0700557 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700558 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700559 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700560 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
561 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700562 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700563 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700564 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700565 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700566 addAppWidgetImpl(
567 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800568 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700569 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700570 }
571 return;
572 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200573
Adam Cohened66b2b2012-01-23 17:28:51 -0800574 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700575 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700576
Adam Cohened66b2b2012-01-23 17:28:51 -0800577 // We have special handling for widgets
578 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800579 final int appWidgetId;
580 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
581 : -1;
582 if (widgetId < 0) {
583 appWidgetId = pendingAddWidgetId;
584 } else {
585 appWidgetId = widgetId;
586 }
587
Adam Cohenad4e15c2013-10-17 16:21:35 -0700588 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800589 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700590 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
591 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700592 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700593 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700594 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700595 @Override
596 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700597 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700598 }
599 };
Adam Cohendb364c32014-05-20 14:23:40 -0700600
Sunny Goyal2100c782016-08-22 16:00:03 -0700601 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
602 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
603 } else {
604 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
605 // When the screen id represents an actual screen (as opposed to a rank)
606 // we make sure that the drop page actually exists.
607 requestArgs.screenId =
608 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700609 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700610 final CellLayout dropLayout =
611 mWorkspace.getScreenWithId(requestArgs.screenId);
612
613 dropLayout.setDropPending(true);
614 final Runnable onComplete = new Runnable() {
615 @Override
616 public void run() {
617 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
618 dropLayout.setDropPending(false);
619 }
620 };
621 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
622 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700623 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800624 return;
625 }
626
Sunny Goyald478c832016-04-01 12:04:16 -0700627 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
628 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700629 if (resultCode == RESULT_OK) {
630 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700631 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700632 }
633 // Leave the widget in the pending state if the user canceled the configure.
634 return;
635 }
636
Sunny Goyalaad90582015-07-09 14:22:50 -0700637 if (requestCode == REQUEST_CREATE_SHORTCUT) {
638 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700639 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
640 completeAdd(requestCode, data, -1, requestArgs);
641 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
642 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
643
Sunny Goyalaad90582015-07-09 14:22:50 -0700644 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700645 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
646 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800647 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800648 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800649 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800650 }
651
652 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700653 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800654 final int requestCode, final int resultCode, final Intent data) {
655 handleActivityResult(requestCode, resultCode, data);
656 if (mLauncherCallbacks != null) {
657 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
658 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800659 }
660
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700661 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700662 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600663 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700664 PendingRequestArgs pendingArgs = mPendingRequestArgs;
665 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
666 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
667 setWaitingForResult(null);
668
Sunny Goyal28c6b962015-10-12 11:42:05 -0700669 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700670 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700671 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700672 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700673 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700674 Intent intent = pendingArgs.getPendingIntent();
675
Sunny Goyal28c6b962015-10-12 11:42:05 -0700676 if (grantResults.length > 0
677 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700678 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700679 } else {
680 // TODO: Show a snack bar with link to settings
681 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700682 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700683 }
684 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600685 if (mLauncherCallbacks != null) {
686 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
687 grantResults);
688 }
689 }
690
Adam Cohendb364c32014-05-20 14:23:40 -0700691 /**
692 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
693 *
694 * @param screenId the screen id to check
695 * @return the new screen, or screenId if it exists
696 */
697 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800698 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700699 if (dropLayout == null) {
700 // it's possible that the add screen was removed because it was
701 // empty and a re-bind occurred
702 mWorkspace.addExtraEmptyScreen();
703 return mWorkspace.commitExtraEmptyScreen();
704 } else {
705 return screenId;
706 }
707 }
708
Sunny Goyal2100c782016-08-22 16:00:03 -0700709 @Thunk void completeTwoStageWidgetDrop(
710 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
711 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800712 Runnable onCompleteRunnable = null;
713 int animationType = 0;
714
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700715 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800716 if (resultCode == RESULT_OK) {
717 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
718 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800719 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700720 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800721 onCompleteRunnable = new Runnable() {
722 @Override
723 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700724 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700725 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800726 }
727 };
728 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800729 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800730 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800731 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700732 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700733 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700734 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
735 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700736 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700737 // The animated view may be null in the case of a rotation during widget configuration
738 onCompleteRunnable.run();
739 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800740 }
741
742 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700743 protected void onStop() {
744 super.onStop();
Adam Cohen9211d422014-10-07 18:14:53 -0700745 if (mLauncherCallbacks != null) {
746 mLauncherCallbacks.onStop();
747 }
Hyunyoung Song1241e612018-05-15 21:46:51 -0700748 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
749 mStateManager.getState().containerType, -1);
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700750
751 mAppWidgetHost.setListenIfResumed(false);
752
Tony Wickham010d2552017-01-20 08:15:28 -0800753 NotificationListener.removeNotificationsChangedListener();
Sunny Goyalaf161fd2018-02-05 16:34:57 -0800754 getStateManager().moveToRestState();
Winson Chungedb0d6b2018-03-21 17:32:55 -0700755
Tracy Zhou62646712018-06-26 14:19:02 -0700756 UiFactory.onLauncherStateOrResumeChanged(this);
757
Winson Chungdea51352018-04-24 13:02:10 -0700758 // Workaround for b/78520668, explicitly trim memory once UI is hidden
Winson Chung473730f2018-04-26 11:54:22 -0700759 onTrimMemory(TRIM_MEMORY_UI_HIDDEN);
Michael Jurkacd496d72013-04-11 11:32:45 -0700760 }
761
762 @Override
763 protected void onStart() {
764 super.onStart();
Adam Cohen9211d422014-10-07 18:14:53 -0700765 if (mLauncherCallbacks != null) {
766 mLauncherCallbacks.onStart();
767 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800768 mAppWidgetHost.setListenIfResumed(true);
Sunny Goyalcac002d2018-01-04 09:51:45 -0800769 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
Winson Chung62a70be2018-02-23 15:10:05 -0800770 UiFactory.onStart(this);
Michael Jurkacd496d72013-04-11 11:32:45 -0700771 }
772
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700773 private void logOnDelayedResume() {
774 if (hasBeenResumed()) {
775 getUserEventDispatcher().logActionCommand(Action.Command.RESUME,
776 mStateManager.getState().containerType, -1);
777 getUserEventDispatcher().startSession();
778 }
779 }
780
Michael Jurkacd496d72013-04-11 11:32:45 -0700781 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800782 protected void onResume() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700783 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800784 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700785 TraceHelper.partitionSection("ON_RESUME", "superCall");
786
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700787 mHandler.removeCallbacks(mLogOnDelayedResume);
788 Utilities.postAsyncCallback(mHandler, mLogOnDelayedResume);
789
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700790 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700791 // Process any items that were added while Launcher was away.
792 InstallShortcutReceiver.disableAndFlushInstallQueue(
793 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700794
Sunny Goyala474a9b2017-05-04 16:47:11 -0700795 // Refresh shortcuts if the permission changed.
796 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700797
Tracy Zhoue5575f92018-04-19 11:12:53 -0700798 DiscoveryBounce.showForHomeIfNeeded(this);
Adam Cohen9211d422014-10-07 18:14:53 -0700799 if (mLauncherCallbacks != null) {
800 mLauncherCallbacks.onResume();
801 }
Tracy Zhoua706f002018-03-28 13:55:19 -0700802 UiFactory.onLauncherStateOrResumeChanged(this);
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800803
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700804 TraceHelper.endSection("ON_RESUME");
Adam Cohen06dff352012-06-01 17:17:08 -0700805 }
806
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800807 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700808 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700809 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700810 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700811
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700812 super.onPause();
Joe Onorato24b6fd82009-11-12 13:47:09 -0800813 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700814 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700815
Adam Cohen9211d422014-10-07 18:14:53 -0700816 if (mLauncherCallbacks != null) {
817 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700818 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700819 }
820
Winson Chungd7823942018-02-16 03:23:47 +0000821 @Override
Tracy Zhoua706f002018-03-28 13:55:19 -0700822 protected void onUserLeaveHint() {
823 super.onUserLeaveHint();
824 UiFactory.onLauncherStateOrResumeChanged(this);
825 }
826
827 @Override
Winson Chungd7823942018-02-16 03:23:47 +0000828 public void onWindowFocusChanged(boolean hasFocus) {
829 super.onWindowFocusChanged(hasFocus);
830 mStateManager.onWindowFocusChanged();
831 }
832
Adam Cohenc2d6e892014-10-16 09:49:24 -0700833 public interface LauncherOverlay {
834
835 /**
836 * Touch interaction leading to overscroll has begun
837 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700838 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700839
840 /**
841 * Touch interaction related to overscroll has ended
842 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700843 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700844
845 /**
846 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
847 * screen (or in the case of RTL, the rightmost screen).
848 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700849 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700850
851 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800852 * Called when the launcher is ready to use the overlay
853 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700854 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700855 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700856 }
857
858 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700859 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700860 }
861
862 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
863
Sunny Goyalc86df472016-02-25 09:19:38 -0800864 public void onScrollChanged(float progress) {
865 if (mWorkspace != null) {
866 mWorkspace.onOverlayScrollChanged(progress);
867 }
868 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700869 }
870
Sunny Goyalf9403d92017-10-18 10:55:56 -0700871 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -0700872 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700873 }
874
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800875 /**
876 * Restores the previous state, if it exists.
877 *
878 * @param savedState The previous state.
879 */
880 private void restoreState(Bundle savedState) {
881 if (savedState == null) {
882 return;
883 }
884
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700885 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -0700886 LauncherState[] stateValues = LauncherState.values();
887 LauncherState state = stateValues[stateOrdinal];
Sunny Goyalbc683e92017-12-06 10:25:07 -0800888 if (!state.disableRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700889 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800890 }
891
Sunny Goyal2100c782016-08-22 16:00:03 -0700892 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
893 if (requestArgs != null) {
894 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800895 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700896
897 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700898
899 SparseArray<Parcelable> widgetsState =
900 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
901 if (widgetsState != null) {
902 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
903 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800904 }
905
906 /**
907 * Finds all the views we need and configure them properly.
908 */
909 private void setupViews() {
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800910 mDragLayer = findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700911 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -0700912 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700913 mWorkspace.initParentViews(mDragLayer);
Sunny Goyal16764582017-11-06 16:00:34 -0800914 mOverviewPanel = findViewById(R.id.overview_panel);
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800915 mHotseat = findViewById(R.id.hotseat);
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700916 mHotseatSearchBox = findViewById(R.id.search_container_hotseat);
Craig Mautner360310b2012-10-26 15:13:08 -0700917
Sunny Goyal784f9c32016-03-23 16:56:54 -0700918 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
919 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
920 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800921
Winson Chung4c98d922011-05-31 16:50:48 -0700922 // Setup the drag layer
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700923 mDragLayer.setup(mDragController, mWorkspace);
924 UiFactory.setOnTouchControllersChangedListener(this, mDragLayer::recreateControllers);
Winson Chung4c98d922011-05-31 16:50:48 -0700925
Hyunyoung Song645764e2016-06-06 14:19:02 -0700926 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -0700927 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
928 // default state, otherwise we will update to the wrong offsets in RTL
929 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700930 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700931 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700932
Tony Wickham34d2c912015-09-11 09:27:58 -0700933 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700934 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700935
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700936 // Setup Apps
937 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -0700938
Winson Chung3d503fb2011-07-13 17:25:49 -0700939 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -0700940 mDragController.setMoveTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700941 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -0400942
Sunny Goyal7185dd62018-03-14 17:51:49 -0700943 mAllAppsController.setupViews(mAppsView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800944 }
945
946 /**
947 * Creates a view representing a shortcut.
948 *
949 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800950 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800951 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700952 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800953 }
954
955 /**
956 * Creates a view representing a shortcut inflated from the specified resource.
957 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800958 * @param parent The group the shortcut belongs to.
959 * @param info The data structure describing the shortcut.
960 *
961 * @return A View inflated from layoutResId.
962 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700963 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -0800964 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
965 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800966 favorite.applyFromShortcutInfo(info);
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800967 favorite.setOnClickListener(ItemClickHandler.INSTANCE);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700968 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800969 return favorite;
970 }
971
972 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -0700973 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800974 *
975 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800976 */
Adam Cohendcd297f2013-06-18 13:13:40 -0700977 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700978 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -0700979 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
980 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700981 return;
982 }
Adam Cohen558baaf2011-08-15 15:22:57 -0700983
Jon Mirandac476d6e2017-05-04 16:30:01 -0700984 int[] cellXY = mTmpAddItemCellCoordinates;
985 CellLayout layout = getCellLayout(container, screenId);
986
Sunny Goyal782f0c92017-01-19 10:27:54 -0800987 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700988 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -0700989 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
990 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -0800991 }
992
993 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -0700994 // Legacy shortcuts are only supported for primary profile.
995 info = Process.myUserHandle().equals(args.user)
996 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800997
Sunny Goyalb57645f2017-03-20 13:57:28 -0700998 if (info == null) {
999 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1000 return;
1001 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001002 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001003 // The app is trying to add a shortcut without sufficient permissions
1004 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1005 return;
1006 }
1007 }
1008
Jon Mirandac476d6e2017-05-04 16:30:01 -07001009 if (container < 0) {
1010 // Adding a shortcut to the Workspace.
1011 final View view = createShortcut(info);
1012 boolean foundCellSpan = false;
1013 // First we check if we already know the exact location where we want to add this item.
1014 if (cellX >= 0 && cellY >= 0) {
1015 cellXY[0] = cellX;
1016 cellXY[1] = cellY;
1017 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001018
Jon Mirandac476d6e2017-05-04 16:30:01 -07001019 // If appropriate, either create a folder or add to an existing folder
1020 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001021 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001022 return;
1023 }
1024 DragObject dragObject = new DragObject();
1025 dragObject.dragInfo = info;
1026 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1027 true)) {
1028 return;
1029 }
1030 } else {
1031 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1032 }
1033
1034 if (!foundCellSpan) {
1035 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001036 return;
1037 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001038
1039 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1040 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001041 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001042 // Adding a shortcut to a Folder.
Sunny Goyale29897f2017-07-20 10:09:42 -07001043 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001044 if (folderIcon != null) {
1045 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1046 folderInfo.add(info, args.rank, false);
1047 } else {
Sunny Goyale29897f2017-07-20 10:09:42 -07001048 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001049 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001050 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001051 }
1052
Sunny Goyale29897f2017-07-20 10:09:42 -07001053 public FolderIcon findFolderIcon(final long folderIconId) {
Sunny Goyal83fd25e2018-07-09 16:47:01 -07001054 return (FolderIcon) mWorkspace.getHomescreenIconByItemId(folderIconId);
Sunny Goyale29897f2017-07-20 10:09:42 -07001055 }
1056
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001057 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001058 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001059 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001060 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001061 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001062 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001063 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1064
Adam Cohened66b2b2012-01-23 17:28:51 -08001065 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001066 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001067 }
Romain Guycbb89e42009-06-08 15:52:54 -07001068
Adam Cohen59400422014-03-05 18:07:04 -08001069 LauncherAppWidgetInfo launcherInfo;
1070 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001071 launcherInfo.spanX = itemInfo.spanX;
1072 launcherInfo.spanY = itemInfo.spanY;
1073 launcherInfo.minSpanX = itemInfo.minSpanX;
1074 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001075 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001076
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001077 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001078 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001079
Sunny Goyal2100c782016-08-22 16:00:03 -07001080 if (hostView == null) {
1081 // Perform actual inflation because we're live
1082 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001083 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001084 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301085 prepareAppWidget(hostView, launcherInfo);
1086 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001087 }
Romain Guycbb89e42009-06-08 15:52:54 -07001088
Sunny Goyald5462aa2016-11-22 16:52:39 +05301089 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001090 hostView.setTag(item);
1091 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001092 hostView.setFocusable(true);
1093 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001094 }
1095
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001096 private final BroadcastReceiver mScreenOffReceiver = new BroadcastReceiver() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001097 @Override
1098 public void onReceive(Context context, Intent intent) {
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001099 // Reset AllApps to its initial state only if we are not in the middle of
1100 // processing a multi-step drop
1101 if (mPendingRequestArgs == null) {
1102 mStateManager.goToState(NORMAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001103 }
1104 }
1105 };
1106
Tony Wickham010d2552017-01-20 08:15:28 -08001107 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
Sunny Goyal29947f02017-12-18 13:49:44 -08001108 mWorkspace.updateIconBadges(updatedBadges);
Sunny Goyal60180b02018-02-07 12:56:30 -08001109 mAppsView.getAppsStore().updateIconBadges(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001110
Sunny Goyal29947f02017-12-18 13:49:44 -08001111 PopupContainerWithArrow popup = PopupContainerWithArrow.getOpen(Launcher.this);
1112 if (popup != null) {
1113 popup.updateNotificationHeader(updatedBadges);
Tony Wickham010d2552017-01-20 08:15:28 -08001114 }
1115 }
1116
Adam Cohended9f8d2010-11-03 13:25:16 -07001117 @Override
1118 public void onAttachedToWindow() {
1119 super.onAttachedToWindow();
1120
Sunny Goyalc86df472016-02-25 09:19:38 -08001121 if (mLauncherCallbacks != null) {
1122 mLauncherCallbacks.onAttachedToWindow();
1123 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001124 }
1125
1126 @Override
1127 public void onDetachedFromWindow() {
1128 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001129
1130 if (mLauncherCallbacks != null) {
1131 mLauncherCallbacks.onDetachedFromWindow();
1132 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001133 }
1134
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001135 public AllAppsTransitionController getAllAppsController() {
1136 return mAllAppsController;
1137 }
1138
Sunny Goyal9d69c8d2018-03-19 13:41:31 -07001139 @Override
Winson Chung1a341002018-01-17 10:00:23 -08001140 public LauncherRootView getRootView() {
1141 return (LauncherRootView) mLauncherView;
1142 }
1143
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001144 @Override
Winson Chunga6945242014-01-08 14:04:34 -08001145 public DragLayer getDragLayer() {
1146 return mDragLayer;
1147 }
1148
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001149 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001150 return mAppsView;
1151 }
1152
Winson Chunga6945242014-01-08 14:04:34 -08001153 public Workspace getWorkspace() {
1154 return mWorkspace;
1155 }
1156
1157 public Hotseat getHotseat() {
1158 return mHotseat;
1159 }
1160
Sunny Goyal81b4c7b2018-03-26 12:10:31 -07001161 public View getHotseatSearchBox() {
1162 return mHotseatSearchBox;
1163 }
1164
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001165 public <T extends View> T getOverviewPanel() {
Winson Chung8ae41982017-11-10 11:42:13 -08001166 return (T) mOverviewPanel;
Winson Chunga6945242014-01-08 14:04:34 -08001167 }
1168
Sunny Goyal47328fd2016-05-25 18:56:41 -07001169 public DropTargetBar getDropTargetBar() {
1170 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001171 }
1172
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001173 public LauncherAppWidgetHost getAppWidgetHost() {
1174 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001175 }
Romain Guycbb89e42009-06-08 15:52:54 -07001176
Winson Chunga9abd0e2010-10-27 17:18:37 -07001177 public LauncherModel getModel() {
1178 return mModel;
1179 }
1180
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001181 public ModelWriter getModelWriter() {
1182 return mModelWriter;
1183 }
1184
Adam Cohen79d90c52016-04-22 13:29:20 -07001185 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001186 return mSharedPrefs;
1187 }
1188
Sunny Goyalf8d56fc2018-01-31 15:18:11 -08001189 public int getOrientation() { return mOldConfig.orientation; }
Jon Miranda6bed3502017-09-19 14:43:17 -07001190
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001191 @Override
1192 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001193 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001194 super.onNewIntent(intent);
1195
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001196 boolean alreadyOnHome = hasWindowFocus() && ((intent.getFlags() &
Winson15f8b172015-08-19 11:02:39 -07001197 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1198 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001199
1200 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001201 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001202 && AbstractFloatingView.getTopOpenView(this) == null;
Winson15f8b172015-08-19 11:02:39 -07001203 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001204 boolean internalStateHandled = InternalStateHandler
Sunny Goyal8b264562018-02-03 23:00:13 -08001205 .handleNewIntent(this, intent, isStarted());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001206
Winson15f8b172015-08-19 11:02:39 -07001207 if (isActionMain) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001208 if (!internalStateHandled) {
1209 // Note: There should be at most one log per method call. This is enforced
1210 // implicitly by using if-else statements.
1211 UserEventDispatcher ued = getUserEventDispatcher();
1212 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
1213 if (topOpenView != null) {
1214 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
1215 } else if (alreadyOnHome) {
1216 Target target = newContainerTarget(mStateManager.getState().containerType);
1217 target.pageIndex = mWorkspace.getCurrentPage();
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001218 ued.logActionCommand(Action.Command.HOME_INTENT, target,
1219 newContainerTarget(ContainerType.WORKSPACE));
Sunny Goyald3864fa2017-11-14 15:06:36 -08001220 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001221
Sunny Goyald3864fa2017-11-14 15:06:36 -08001222 // In all these cases, only animate if we're already on home
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001223 AbstractFloatingView.closeAllOpenViews(this, isStarted());
Jon Mirandafeba90f2016-10-06 10:53:29 -07001224
Sunny Goyala2467272018-03-16 14:53:05 -07001225 if (!isInState(NORMAL)) {
1226 // Only change state, if not already the same. This prevents cancelling any
1227 // animations running as part of resume
1228 mStateManager.goToState(NORMAL);
1229 }
Sunny Goyald3864fa2017-11-14 15:06:36 -08001230
1231 // Reset the apps view
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001232 if (!alreadyOnHome) {
Sunny Goyal7185dd62018-03-14 17:51:49 -07001233 mAppsView.reset(isStarted() /* animate */);
Sunny Goyald3864fa2017-11-14 15:06:36 -08001234 }
1235
Sunny Goyalaad33592018-08-07 17:20:35 -07001236 if (shouldMoveToDefaultScreen && !mWorkspace.isHandlingTouch()) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001237 mWorkspace.post(mWorkspace::moveToDefaultScreen);
1238 }
1239 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001240
1241 final View v = getWindow().peekDecorView();
1242 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001243 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001244 }
1245
Adam Cohen9211d422014-10-07 18:14:53 -07001246 if (mLauncherCallbacks != null) {
Sunny Goyal85462132018-04-24 11:39:37 -07001247 mLauncherCallbacks.onHomeIntent(internalStateHandled);
Adam Cohen9211d422014-10-07 18:14:53 -07001248 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001249 }
Winson15f8b172015-08-19 11:02:39 -07001250
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001251 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001252 }
1253
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001254 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001255 public void onRestoreInstanceState(Bundle state) {
1256 super.onRestoreInstanceState(state);
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001257 mWorkspace.restoreInstanceStateForChild(mSynchronouslyBoundPage);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001258 }
1259
1260 @Override
1261 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001262 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001263 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001264
Adam Cohen21cd0022013-10-09 18:57:02 -07001265 }
Sunny Goyalea609262017-10-25 15:47:38 -07001266 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001267
1268
1269 AbstractFloatingView widgets = AbstractFloatingView
1270 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1271 if (widgets != null) {
1272 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1273 widgets.saveHierarchyState(widgetsState);
1274 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1275 } else {
1276 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1277 }
1278
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001279 // We close any open folders and shortcut containers since they will not be re-opened,
1280 // and we need to make sure this state is reflected.
1281 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001282
Sunny Goyal2100c782016-08-22 16:00:03 -07001283 if (mPendingRequestArgs != null) {
1284 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1285 }
1286 if (mPendingActivityResult != null) {
1287 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001288 }
1289
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001290 super.onSaveInstanceState(outState);
1291
Adam Cohen9211d422014-10-07 18:14:53 -07001292 if (mLauncherCallbacks != null) {
1293 mLauncherCallbacks.onSaveInstanceState(outState);
1294 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001295 }
1296
1297 @Override
1298 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001299 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001300
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001301 unregisterReceiver(mScreenOffReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001302 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001303
Tony Wickhama0068302018-04-11 16:16:11 -07001304 UiFactory.setOnTouchControllersChangedListener(this, null);
1305
Winson Chungcd2b0142011-06-08 16:02:26 -07001306 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001307 // It's possible to receive onDestroy after a new Launcher activity has
1308 // been created. In this case, don't interfere with the new Launcher.
1309 if (mModel.isCurrentCallbacks(this)) {
1310 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001311 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001312 }
Sunny Goyal623eddd2018-03-02 12:24:41 -08001313 mRotationHelper.destroy();
Sunny Goyal745bad92016-05-02 10:54:12 -07001314
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001315 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001316 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001317 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001318 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001319 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001320
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001321 TextKeyListener.getInstance().release();
Tony2917a8b2017-07-31 23:29:42 -07001322 clearPendingBinds();
1323
Adam Cohen9211d422014-10-07 18:14:53 -07001324 if (mLauncherCallbacks != null) {
1325 mLauncherCallbacks.onDestroy();
1326 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001327 }
1328
Sunny Goyalae502842016-06-17 08:43:56 -07001329 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1330 return mAccessibilityDelegate;
1331 }
1332
Adam Cohena9cf38f2011-05-02 15:36:58 -07001333 public DragController getDragController() {
1334 return mDragController;
1335 }
1336
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001337 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001338 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001339 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001340 }
1341
1342 @Override
1343 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1344 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001345 try {
1346 super.startIntentSenderForResult(intent, requestCode,
1347 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1348 } catch (IntentSender.SendIntentException e) {
1349 throw new ActivityNotFoundException();
1350 }
1351 }
1352
Winson Chung70d72102011-08-12 11:24:35 -07001353 /**
1354 * Indicates that we want global search for this activity by setting the globalSearch
1355 * argument for {@link #startSearch} to true.
1356 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001357 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001358 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001359 Bundle appSearchData, boolean globalSearch) {
1360 if (appSearchData == null) {
1361 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001362 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001363 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001364
Sunny Goyal6f28e712016-09-13 14:19:29 -07001365 if (mLauncherCallbacks == null ||
1366 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1367 // Starting search from the callbacks failed. Start the default global search.
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001368 super.startSearch(initialQuery, selectInitialQuery, appSearchData, true);
Ian Parkinson047036e2014-09-01 15:40:53 +01001369 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001370
1371 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001372 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001373 }
1374
Joe Onorato9c1289c2009-08-17 11:03:03 -04001375 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001376 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001377 }
1378
Adam Cohen517a7f52014-03-01 12:12:59 -08001379 public boolean isWorkspaceLoading() {
1380 return mWorkspaceLoading;
1381 }
1382
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001383 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001384 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001385 }
1386
Sunny Goyal04a324a2017-01-20 21:08:59 -08001387 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001388 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001389 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001390
Sunny Goyal2100c782016-08-22 16:00:03 -07001391 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001392 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001393 if (LOGD) {
1394 Log.d(TAG, "Adding widget from drop");
1395 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001396 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001397 }
1398
Sunny Goyal2100c782016-08-22 16:00:03 -07001399 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001400 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1401 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1402 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001403
Adam Cohenad4e15c2013-10-17 16:21:35 -07001404 Runnable onComplete = new Runnable() {
1405 @Override
1406 public void run() {
1407 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001408 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001409 }
1410 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001411 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001412 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001413 }
1414 }
Romain Guycbb89e42009-06-08 15:52:54 -07001415
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001416 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1417 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001418 info.container = container;
1419 info.screenId = screenId;
1420 if (cell != null) {
1421 info.cellX = cell[0];
1422 info.cellY = cell[1];
1423 }
1424 info.spanX = spanX;
1425 info.spanY = spanY;
1426
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001427 switch (info.itemType) {
1428 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1429 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001430 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001431 break;
1432 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001433 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001434 break;
1435 default:
1436 throw new IllegalStateException("Unknown item type: " + info.itemType);
1437 }
1438 }
1439
Adam Cohenfbba09b2011-07-18 21:43:05 -07001440 /**
1441 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001442 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001443 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001444 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1445 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001446 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1447 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1448 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001449 }
1450
Adam Cohenfbba09b2011-07-18 21:43:05 -07001451 /**
1452 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001453 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001454 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001455 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001456 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001457 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001458 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001459 // In the case where we've prebound the widget, we remove it from the DragLayer
1460 if (LOGD) {
1461 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1462 }
1463 getDragLayer().removeView(hostView);
1464
Adam Cohened66b2b2012-01-23 17:28:51 -08001465 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001466 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001467
1468 // Clear the boundWidget so that it doesn't get destroyed.
1469 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001470 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001471 // In this case, we either need to start an activity to get permission to bind
1472 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001473 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1474 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1475 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1476 this, info.componentName);
1477 } else {
1478 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1479 }
Adam Cohendd70d662012-10-04 16:53:44 -07001480 Bundle options = info.bindOptions;
1481
Sunny Goyalffe83f12014-08-14 17:39:34 -07001482 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1483 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001484 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001485 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001486 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001487 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001488 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001489 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001490 }
1491
Adam Cohendcd297f2013-06-18 13:13:40 -07001492 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001493 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001494 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001495 folderInfo.title = getText(R.string.folder_name);
1496
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001497 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001498 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001499
1500 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001501 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301502 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001503 // Force measure the new folder icon
1504 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1505 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001506 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001507 }
Romain Guycbb89e42009-06-08 15:52:54 -07001508
Winsonc0b52fe2015-09-09 16:38:15 -07001509 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001510 * Unbinds the view for the specified item, and removes the item and all its children.
1511 *
1512 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001513 * @param itemInfo the {@link ItemInfo} for this view.
1514 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001515 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001516 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001517 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001518 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001519 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1520 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001521 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001522 } else {
1523 mWorkspace.removeWorkspaceItem(v);
1524 }
Winsonc0b52fe2015-09-09 16:38:15 -07001525 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001526 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001527 }
1528 } else if (itemInfo instanceof FolderInfo) {
1529 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001530 if (v instanceof FolderIcon) {
1531 ((FolderIcon) v).removeListeners();
1532 }
Winsonc0b52fe2015-09-09 16:38:15 -07001533 mWorkspace.removeWorkspaceItem(v);
1534 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001535 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001536 }
1537 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1538 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001539 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001540 if (deleteFromDb) {
Tony Wickham6a71a5b2018-08-21 11:40:23 -07001541 getModelWriter().deleteWidgetInfo(widgetInfo, getAppWidgetHost());
Winsonc0b52fe2015-09-09 16:38:15 -07001542 }
1543 } else {
1544 return false;
1545 }
1546 return true;
1547 }
1548
Tony Wickham6a71a5b2018-08-21 11:40:23 -07001549
Winsonc0b52fe2015-09-09 16:38:15 -07001550
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001551 @Override
1552 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001553 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001554 }
1555
Joe Onorato88ec0992009-11-19 13:16:06 -08001556 @Override
1557 public void onBackPressed() {
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08001558 if (finishAutoCancelActionMode()) {
1559 return;
1560 }
Adam Cohen9211d422014-10-07 18:14:53 -07001561 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1562 return;
1563 }
1564
Sunny Goyal45478022015-06-08 16:52:41 -07001565 if (mDragController.isDragging()) {
1566 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001567 return;
1568 }
1569
Jon Mirandafeba90f2016-10-06 10:53:29 -07001570 // Note: There should be at most one log per method call. This is enforced implicitly
1571 // by using if-else statements.
1572 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001573 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
Tony Wickham52c1b662018-05-21 13:13:58 -07001574 if (topView != null && topView.onBackPressed()) {
1575 // Handled by the floating view.
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001576 } else {
Sunny Goyale39690b2018-08-02 13:35:07 -07001577 mStateManager.getState().onBackPressed(this);
Michael Jurkaaf442092010-06-10 17:01:57 -07001578 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001579 }
1580
Sunny Goyalfe770c92016-09-27 14:38:58 -07001581 @TargetApi(Build.VERSION_CODES.M)
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001582 @Override
Jon Miranda73c27ec2018-05-16 18:06:23 -07001583 public ActivityOptions getActivityLaunchOptions(View v) {
1584 return mAppTransitionManager.getActivityLaunchOptions(this, v);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001585 }
1586
Tony Wickham3a6746a2018-03-29 09:39:56 -07001587 public LauncherAppTransitionManager getAppTransitionManager() {
1588 return mAppTransitionManager;
1589 }
1590
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001591 @TargetApi(Build.VERSION_CODES.M)
1592 @Override
1593 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
1594 // Due to legacy reasons, direct call shortcuts require Launchers to have the
1595 // corresponding permission. Show the appropriate permission prompt if that
1596 // is the case.
1597 if (intent.getComponent() == null
1598 && Intent.ACTION_CALL.equals(intent.getAction())
1599 && checkSelfPermission(android.Manifest.permission.CALL_PHONE) !=
1600 PackageManager.PERMISSION_GRANTED) {
1601
1602 setWaitingForResult(PendingRequestArgs
1603 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
1604 requestPermissions(new String[]{android.Manifest.permission.CALL_PHONE},
1605 REQUEST_PERMISSION_CALL_PHONE);
1606 return true;
1607 } else {
1608 return false;
1609 }
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001610 }
1611
Hyunyoung Song46d07f72018-05-22 15:41:25 -07001612 @Override
1613 public void modifyUserEvent(LauncherLogProto.LauncherEvent event) {
1614 if (event.srcTarget != null && event.srcTarget.length > 0 &&
1615 event.srcTarget[1].containerType == ContainerType.PREDICTION) {
1616 Target[] targets = new Target[3];
1617 targets[0] = event.srcTarget[0];
1618 targets[1] = event.srcTarget[1];
1619 targets[2] = newTarget(Target.Type.CONTAINER);
1620 event.srcTarget = targets;
1621 LauncherState state = mStateManager.getState();
1622 if (state == LauncherState.ALL_APPS) {
1623 event.srcTarget[2].containerType = ContainerType.ALLAPPS;
1624 } else if (state == LauncherState.OVERVIEW) {
1625 event.srcTarget[2].containerType = ContainerType.TASKSWITCHER;
1626 }
1627 }
1628 }
1629
Sunny Goyala7ce1662016-05-31 15:01:35 -07001630 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Hyunyoung Song1241e612018-05-15 21:46:51 -07001631 boolean success = super.startActivitySafely(v, intent, item);
1632 if (success && v instanceof BubbleTextView) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001633 // This is set to the view that launched the activity that navigated the user away
1634 // from launcher. Since there is no callback for when the activity has finished
1635 // launching, enable the press state and keep this reference to reset the press
1636 // state when we return to launcher.
1637 BubbleTextView btv = (BubbleTextView) v;
1638 btv.setStayPressed(true);
1639 setOnResumeCallback(btv);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001640 }
Hyunyoung Song1241e612018-05-15 21:46:51 -07001641 return success;
Michael Jurka86a720e2012-05-09 11:23:23 -07001642 }
1643
Winson Chung3d503fb2011-07-13 17:25:49 -07001644 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001645 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07001646 return mHotseat != null && layout != null &&
1647 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
1648 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001649
Winson Chung3d503fb2011-07-13 17:25:49 -07001650 /**
1651 * Returns the CellLayout of the specified container at the specified screen.
1652 */
Sunny Goyal92820592015-03-02 11:31:35 -08001653 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001654 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1655 if (mHotseat != null) {
1656 return mHotseat.getLayout();
1657 } else {
1658 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08001659 }
Winson Chung3d503fb2011-07-13 17:25:49 -07001660 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08001661 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08001662 }
1663 }
1664
Michael Jurkae326f182011-11-21 14:05:46 -08001665 @Override
1666 public void onTrimMemory(int level) {
1667 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07001668 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
1669 // The widget preview db can result in holding onto over
1670 // 3MB of memory for caching which isn't necessary.
1671 SQLiteDatabase.releaseMemory();
1672
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08001673 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07001674 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08001675 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01001676 if (mLauncherCallbacks != null) {
1677 mLauncherCallbacks.onTrimMemory(level);
1678 }
Winson Chung62a70be2018-02-23 15:10:05 -08001679 UiFactory.onTrimMemory(this, level);
Michael Jurkae326f182011-11-21 14:05:46 -08001680 }
1681
Michael Jurkad7c28052012-04-27 15:43:36 -07001682 @Override
1683 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07001684 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07001685 final List<CharSequence> text = event.getText();
1686 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07001687 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001688 // TODO: When can workspace be null?
1689 text.add(mWorkspace == null
1690 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07001691 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07001692 return result;
1693 }
1694
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001695 public void setOnResumeCallback(OnResumeCallback callback) {
1696 if (mOnResumeCallback != null) {
1697 mOnResumeCallback.onLauncherResume();
1698 }
1699 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07001700 }
1701
Michael Jurka7607c2f2013-04-03 14:33:19 -07001702 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001703 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001704 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07001705 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001706 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07001707 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001708 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07001709 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07001710 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07001711 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001712 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001713
Joe Onorato9c1289c2009-08-17 11:03:03 -04001714 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07001715 * Clear any pending bind callbacks. This is called when is loader is planning to
1716 * perform a full rebind from scratch.
1717 */
1718 @Override
1719 public void clearPendingBinds() {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001720 if (mPendingExecutor != null) {
1721 mPendingExecutor.markCompleted();
1722 mPendingExecutor = null;
1723 }
1724 }
1725
1726 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001727 * Refreshes the shortcuts shown on the workspace.
1728 *
1729 * Implementation of the method from LauncherModel.Callbacks.
1730 */
1731 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001732 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001733 // Floating panels (except the full widget sheet) are associated with individual icons. If
1734 // we are starting a fresh bind, close all such panels as all the icons are about
1735 // to go away.
1736 AbstractFloatingView.closeOpenViews(this, true,
Sunny Goyal7ede6112017-12-05 15:11:21 -08001737 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_REBIND_SAFE);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08001738
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001739 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08001740
Winson Chungd64d1762013-08-20 14:37:16 -07001741 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07001742 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07001743 mWorkspace.removeAllWorkspaceScreens();
Sunny Goyalc11fac32018-02-27 19:20:15 -08001744 mAppWidgetHost.clearViews();
Winson Chungd64d1762013-08-20 14:37:16 -07001745
Winson Chung3d503fb2011-07-13 17:25:49 -07001746 if (mHotseat != null) {
Sunny Goyal11e96492018-03-23 17:06:00 -07001747 mHotseat.resetLayout(mDeviceProfile.isVerticalBarLayout());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001748 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001749 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001750 }
1751
Adam Cohendcd297f2013-06-18 13:13:40 -07001752 @Override
1753 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001754 // Make sure the first screen is always at the start.
Ryan Lothian688e9892018-10-03 13:32:01 -04001755 if (FeatureFlags.getInstance(this).isQsbOnFirstScreenEnabled() &&
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001756 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001757 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
1758 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001759 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Ryan Lothian688e9892018-10-03 13:32:01 -04001760 } else if (!FeatureFlags.getInstance(this).isQsbOnFirstScreenEnabled()
1761 && orderedScreenIds.isEmpty()) {
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001762 // 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);
Ryan Lothian688e9892018-10-03 13:32:01 -04001777 if (!FeatureFlags.getInstance(this).isQsbOnFirstScreenEnabled()
1778 || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001779 // No need to bind the first screen, as its always bound.
1780 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
1781 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001782 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001783 }
1784
Sunny Goyalb23980c2017-08-17 07:45:25 -07001785 @Override
Tony Wickham6a71a5b2018-08-21 11:40:23 -07001786 public void preAddApps() {
1787 // If there's an undo snackbar, force it to complete to ensure empty screens are removed
1788 // before trying to add new items.
1789 mModelWriter.commitDelete();
1790 AbstractFloatingView snackbar = AbstractFloatingView.getOpenView(this, TYPE_SNACKBAR);
1791 if (snackbar != null) {
1792 snackbar.post(() -> snackbar.close(true));
1793 }
1794 }
1795
1796 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08001797 public void bindAppsAdded(ArrayList<Long> newScreens, ArrayList<ItemInfo> addNotAnimated,
1798 ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07001799 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08001800 if (newScreens != null) {
1801 bindAddScreens(newScreens);
1802 }
Winson Chungd64d1762013-08-20 14:37:16 -07001803
1804 // We add the items without animation on non-visible pages, and with
1805 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08001806 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001807 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001808 }
Adam Cohen76a47a12014-02-05 11:47:43 -08001809 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001810 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07001811 }
Winson Chungc58497e2013-09-03 17:48:37 -07001812
Winson Chung87412982013-10-03 18:34:14 -07001813 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07001814 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001815 }
1816
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001817 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001818 * Bind the items start-end from the list.
1819 *
1820 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001821 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07001822 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07001823 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001824 // Get the list of added items and intersect them with the set of items here
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001825 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07001826 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001827 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001828 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07001829 int end = items.size();
1830 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001831 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07001832
1833 // Short circuit if we are loading dock items for a configuration which has no dock
1834 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
1835 mHotseat == null) {
1836 continue;
1837 }
1838
Sunny Goyal639e9062015-08-19 19:17:06 -07001839 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001840 switch (item.itemType) {
1841 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1842 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08001843 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001844 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07001845 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001846 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001847 }
1848 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07001849 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001850 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001851 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001852 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001853 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07001854 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1855 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07001856 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07001857 if (view == null) {
1858 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001859 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08001860 break;
1861 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001862 default:
1863 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001864 }
Sunny Goyal639e9062015-08-19 19:17:06 -07001865
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001866 /*
1867 * Remove colliding items.
1868 */
1869 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1870 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
1871 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
1872 View v = cl.getChildAt(item.cellX, item.cellY);
1873 Object tag = v.getTag();
1874 String desc = "Collision while binding workspace item: " + item
1875 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08001876 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001877 throw (new RuntimeException(desc));
1878 } else {
1879 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001880 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001881 continue;
1882 }
1883 }
1884 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05301885 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07001886 if (animateIcons) {
1887 // Animate all the applications up now
1888 view.setAlpha(0f);
1889 view.setScaleX(0f);
1890 view.setScaleY(0f);
1891 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08001892 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07001893 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001894 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07001895
Sunny Goyalf0b6db72018-08-13 16:10:14 -07001896 // Animate to the correct page
1897 if (animateIcons && newItemsScreenId > -1) {
1898 AnimatorSet anim = new AnimatorSet();
1899 anim.playTogether(bounceAnims);
Jon Miranda64eb8ea2018-03-29 11:43:58 -07001900
Sunny Goyalf0b6db72018-08-13 16:10:14 -07001901 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
1902 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
1903 final Runnable startBounceAnimRunnable = anim::start;
1904
1905 if (newItemsScreenId != currentScreenId) {
1906 // We post the animation slightly delayed to prevent slowdowns
1907 // when we are loading right after we return to launcher.
1908 mWorkspace.postDelayed(new Runnable() {
1909 public void run() {
1910 if (mWorkspace != null) {
1911 AbstractFloatingView.closeAllOpenViews(Launcher.this, false);
1912
1913 mWorkspace.snapToPage(newScreenIndex);
1914 mWorkspace.postDelayed(startBounceAnimRunnable,
1915 NEW_APPS_ANIMATION_DELAY);
Winson Chung94d67682013-09-25 16:29:40 -07001916 }
Sunny Goyalf0b6db72018-08-13 16:10:14 -07001917 }
1918 }, NEW_APPS_PAGE_MOVE_DELAY);
1919 } else {
1920 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07001921 }
Winson Chung64359a52013-07-08 17:17:08 -07001922 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001923 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001924 }
1925
Joe Onorato9c1289c2009-08-17 11:03:03 -04001926 /**
1927 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001928 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07001929 public void bindAppWidget(LauncherAppWidgetInfo item) {
1930 View view = inflateAppWidget(item);
1931 if (view != null) {
1932 mWorkspace.addInScreen(view, item);
1933 mWorkspace.requestLayout();
1934 }
1935 }
1936
1937 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001938 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05301939 PendingAppWidgetHostView view =
1940 new PendingAppWidgetHostView(this, item, mIconCache, true);
1941 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07001942 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001943 }
1944
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001945 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001946
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001947 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08001948
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001949 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
1950 // If the provider is not ready, bind as a pending widget.
1951 appWidgetInfo = null;
1952 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
1953 // The widget id is not valid. Try to find the widget based on the provider info.
1954 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
1955 } else {
1956 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
1957 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07001958
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001959 // If the provider is ready, but the width is not yet restored, try to restore it.
1960 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
1961 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07001962 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001963 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
1964 + " belongs to component " + item.providerName
1965 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001966 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07001967 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07001968 }
Sunny Goyalff572272014-07-23 13:58:07 -07001969
Sunny Goyal84b4adc2015-08-12 15:12:16 -07001970 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001971 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07001972 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
1973 // Id has not been allocated yet. Allocate a new id.
1974 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
1975 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07001976
Sunny Goyald478c832016-04-01 12:04:16 -07001977 // Also try to bind the widget. If the bind fails, the user will be shown
1978 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08001979 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07001980 pendingInfo.spanX = item.spanX;
1981 pendingInfo.spanY = item.spanY;
1982 pendingInfo.minSpanX = item.minSpanX;
1983 pendingInfo.minSpanY = item.minSpanY;
1984 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07001985
1986 boolean isDirectConfig =
1987 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
1988 if (isDirectConfig && item.bindOptions != null) {
1989 Bundle newOptions = item.bindOptions.getExtras();
1990 if (options != null) {
1991 newOptions.putAll(options);
1992 }
1993 options = newOptions;
1994 }
Sunny Goyald478c832016-04-01 12:04:16 -07001995 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1996 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07001997
Sunny Goyal86df1382016-08-10 15:03:22 -07001998 // We tried to bind once. If we were not able to bind, we would need to
1999 // go through the permission dialog, which means we cannot skip the config
2000 // activity.
2001 item.bindOptions = null;
2002 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2003
Sunny Goyald478c832016-04-01 12:04:16 -07002004 // Bind succeeded
2005 if (success) {
2006 // If the widget has a configure activity, it is still needs to set it up,
2007 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002008 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002009 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2010 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002011 }
Sunny Goyald478c832016-04-01 12:04:16 -07002012
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002013 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002014 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002015 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002016 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002017 // The widget was marked as UI not ready, but there is no configure activity to
2018 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002019 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002020 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002021 }
Sunny Goyalff572272014-07-23 13:58:07 -07002022 }
2023
Sunny Goyald5462aa2016-11-22 16:52:39 +05302024 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002025 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002026 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002027 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002028 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Tony Wickham6a71a5b2018-08-21 11:40:23 -07002029 getModelWriter().deleteWidgetInfo(item, getAppWidgetHost());
Sunny Goyalb23980c2017-08-17 07:45:25 -07002030 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002031 }
2032
Sunny Goyal233ee962015-08-03 13:05:01 -07002033 item.minSpanX = appWidgetInfo.minSpanX;
2034 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302035 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002036 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302037 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002038 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302039 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002040
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002041 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002042 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002043 }
2044
Sunny Goyalff572272014-07-23 13:58:07 -07002045 /**
2046 * Restores a pending widget.
2047 *
2048 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002049 */
Sunny Goyald478c832016-04-01 12:04:16 -07002050 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002051 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2052 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2053 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002054 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002055 }
2056
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002057 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002058 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002059 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2060 info.pendingItemInfo = null;
2061 }
Sunny Goyalff572272014-07-23 13:58:07 -07002062
Sunny Goyalba47faa2017-10-04 16:50:57 -07002063 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002064 view.reInflate();
Sunny Goyalba47faa2017-10-04 16:50:57 -07002065 }
2066
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002067 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002068 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002069 }
2070
Adam Cohen1462de32012-07-24 22:34:36 -07002071 public void onPageBoundSynchronously(int page) {
Sunny Goyalf4aceab2017-12-06 16:16:31 -08002072 mSynchronouslyBoundPage = page;
Adam Cohen1462de32012-07-24 22:34:36 -07002073 }
2074
Sunny Goyal527c7d32015-08-28 15:19:36 -07002075 @Override
2076 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2077 if (mPendingExecutor != null) {
2078 mPendingExecutor.markCompleted();
2079 }
2080 mPendingExecutor = executor;
Sunny Goyal60180b02018-02-07 12:56:30 -08002081 if (!isInState(ALL_APPS)) {
2082 mAppsView.getAppsStore().setDeferUpdates(true);
2083 mPendingExecutor.execute(() -> mAppsView.getAppsStore().setDeferUpdates(false));
2084 }
2085
Sunny Goyal527c7d32015-08-28 15:19:36 -07002086 executor.attachTo(this);
2087 }
2088
2089 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2090 if (mPendingExecutor == executor) {
2091 mPendingExecutor = null;
2092 }
2093 }
2094
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002095 @Override
2096 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
Sunny Goyal6001ea22018-05-10 16:31:00 -07002097 AlphaProperty property = mDragLayer.getAlphaProperty(ALPHA_INDEX_LAUNCHER_LOAD);
2098 if (property.getValue() < 1) {
2099 ObjectAnimator anim = ObjectAnimator.ofFloat(property, MultiValueAlpha.VALUE, 1);
2100 if (executor != null) {
2101 anim.addListener(new AnimatorListenerAdapter() {
2102 @Override
2103 public void onAnimationEnd(Animator animation) {
2104 executor.onLoadAnimationCompleted();
2105 }
2106 });
2107 }
2108 anim.start();
Sunny Goyal29947f02017-12-18 13:49:44 -08002109 } else if (executor != null) {
2110 executor.onLoadAnimationCompleted();
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002111 }
2112 }
2113
Joe Onorato9c1289c2009-08-17 11:03:03 -04002114 /**
2115 * Callback saying that there aren't any more items to bind.
2116 *
2117 * Implementation of the method from LauncherModel.Callbacks.
2118 */
Tony Wickham6a71a5b2018-08-21 11:40:23 -07002119 public void finishBindingItems(int currentScreen) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002120 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002121 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002122
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002123 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002124
Sunny Goyal2100c782016-08-22 16:00:03 -07002125 if (mPendingActivityResult != null) {
2126 handleActivityResult(mPendingActivityResult.requestCode,
2127 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2128 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002129 }
2130
Sunny Goyala474a9b2017-05-04 16:47:11 -07002131 InstallShortcutReceiver.disableAndFlushInstallQueue(
2132 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002133
Tony Wickham6a71a5b2018-08-21 11:40:23 -07002134 mWorkspace.setCurrentPage(currentScreen);
2135
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002136 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002137 }
2138
Winson Chunga2413752012-04-03 14:22:34 -07002139 private boolean canRunNewAppsAnimation() {
2140 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002141 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002142 }
2143
Winson Chungc9168342013-06-26 14:54:55 -07002144 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002145 ValueAnimator bounceAnim = new PropertyListBuilder().alpha(1).scale(1).build(v)
2146 .setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
Winson Chungc9168342013-06-26 14:54:55 -07002147 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002148 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002149 return bounceAnim;
2150 }
2151
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002152 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002153 * Add the icons for all apps.
2154 *
2155 * Implementation of the method from LauncherModel.Callbacks.
2156 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002157 public void bindAllApplications(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002158 mAppsView.getAppsStore().setApps(apps);
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002159
Adam Cohen9211d422014-10-07 18:14:53 -07002160 if (mLauncherCallbacks != null) {
2161 mLauncherCallbacks.bindAllApplications(apps);
2162 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002163 }
2164
2165 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002166 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
2167 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2168 */
2169 @Override
2170 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002171 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002172 }
2173
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002174 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002175 * A package was updated.
2176 *
2177 * Implementation of the method from LauncherModel.Callbacks.
2178 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002179 @Override
2180 public void bindAppsAddedOrUpdated(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002181 mAppsView.getAppsStore().addOrUpdateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002182 }
2183
Sunny Goyal4390ace2014-10-13 11:33:11 -07002184 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002185 public void bindPromiseAppProgressUpdated(PromiseAppInfo app) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002186 mAppsView.getAppsStore().updatePromiseAppProgress(app);
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002187 }
2188
2189 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002190 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002191 mWorkspace.widgetsRestored(widgets);
2192 }
2193
Joe Onorato9c1289c2009-08-17 11:03:03 -04002194 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002195 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002196 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002197 *
2198 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002199 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002200 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002201 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002202 if (!updated.isEmpty()) {
2203 mWorkspace.updateShortcuts(updated);
2204 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002205 }
2206
2207 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002208 * Update the state of a package, typically related to install state.
2209 *
2210 * Implementation of the method from LauncherModel.Callbacks.
2211 */
Sunny Goyale755d462014-07-22 13:48:29 -07002212 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002213 public void bindRestoreItemsChange(HashSet<ItemInfo> updates) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07002214 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07002215 }
2216
2217 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002218 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07002219 * in addition to specific applications to remove, the reason being that
2220 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002221 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07002222 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002223 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07002224 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002225 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002226 mWorkspace.removeItemsByMatcher(matcher);
2227 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002228 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002229
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002230 @Override
2231 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002232 mAppsView.getAppsStore().removeApps(appInfos);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002233 }
Joe Onoratobe386092009-11-17 17:32:16 -08002234
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002235 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002236 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
2237 mPopupDataProvider.setAllWidgets(allWidgets);
Tony Wickham26b17462017-03-20 17:12:24 -07002238 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
2239 if (topView != null) {
2240 topView.onWidgetsBound();
2241 }
2242 }
2243
Tony Wickham86222d22017-03-29 15:30:43 -07002244 /**
2245 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
2246 * refreshes the widgets and shortcuts associated with the given package/user
2247 */
2248 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07002249 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002250 }
2251
Winson Chung80baf5a2010-08-09 16:03:15 -07002252 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002253 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08002254 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002255 @Override
2256 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
2257 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002258
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002259 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
2260 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07002261 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002262 writer.println(prefix + " Homescreen " + i);
2263
2264 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
2265 for (int j = 0; j < layout.getChildCount(); j++) {
2266 Object tag = layout.getChildAt(j).getTag();
2267 if (tag != null) {
2268 writer.println(prefix + " " + tag.toString());
2269 }
2270 }
2271 }
2272
2273 writer.println(prefix + " Hotseat");
2274 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07002275 for (int j = 0; j < layout.getChildCount(); j++) {
2276 Object tag = layout.getChildAt(j).getTag();
2277 if (tag != null) {
2278 writer.println(prefix + " " + tag.toString());
2279 }
Adam Cohen4caf2982013-08-20 18:54:31 -07002280 }
Sunny Goyala1365452015-10-01 15:46:24 -07002281 }
2282
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002283 writer.println(prefix + "Misc:");
2284 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
2285 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
2286 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
Sunny Goyale43d00d2018-05-14 14:23:18 -07002287 writer.println(" mRotationHelper: " + mRotationHelper);
2288 dumpMisc(writer);
Sunny Goyal2bba1902018-02-27 12:20:50 -08002289
2290 try {
2291 FileLog.flushAll(writer);
2292 } catch (Exception e) {
2293 // Ignore
2294 }
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002295
2296 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002297
Adam Cohen9211d422014-10-07 18:14:53 -07002298 if (mLauncherCallbacks != null) {
2299 mLauncherCallbacks.dump(prefix, fd, writer, args);
2300 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002301 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002302
Sunny Goyal66b24572016-09-21 15:57:55 -07002303 @Override
2304 @TargetApi(Build.VERSION_CODES.N)
2305 public void onProvideKeyboardShortcuts(
2306 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
2307
2308 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002309 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002310 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
2311 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
Vadim Tryshev74c261c2018-06-12 13:44:43 -07002312 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.widget_button_text),
2313 KeyEvent.KEYCODE_W, KeyEvent.META_CTRL_ON));
Sunny Goyal66b24572016-09-21 15:57:55 -07002314 }
Vadim Tryshev3455f8d2018-05-11 20:02:20 -07002315 final View currentFocus = getCurrentFocus();
2316 if (currentFocus != null) {
2317 if (new CustomActionsPopup(this, currentFocus).canShow()) {
2318 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
2319 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
2320 }
2321 if (currentFocus.getTag() instanceof ItemInfo
2322 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
2323 shortcutInfos.add(new KeyboardShortcutInfo(
2324 getString(R.string.shortcuts_menu_with_notifications_description),
2325 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
2326 }
Sunny Goyal66b24572016-09-21 15:57:55 -07002327 }
2328 if (!shortcutInfos.isEmpty()) {
2329 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
2330 }
2331
2332 super.onProvideKeyboardShortcuts(data, menu, deviceId);
2333 }
2334
2335 @Override
2336 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
2337 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
2338 switch (keyCode) {
2339 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002340 if (isInState(NORMAL)) {
2341 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07002342 return true;
2343 }
2344 break;
2345 case KeyEvent.KEYCODE_S: {
2346 View focusedView = getCurrentFocus();
2347 if (focusedView instanceof BubbleTextView
2348 && focusedView.getTag() instanceof ItemInfo
2349 && mAccessibilityDelegate.performAction(focusedView,
2350 (ItemInfo) focusedView.getTag(),
2351 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal00ac9202017-11-09 15:24:06 -08002352 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07002353 return true;
2354 }
2355 break;
2356 }
2357 case KeyEvent.KEYCODE_O:
2358 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
2359 return true;
2360 }
2361 break;
Vadim Tryshev74c261c2018-06-12 13:44:43 -07002362 case KeyEvent.KEYCODE_W:
2363 if (isInState(NORMAL)) {
2364 OptionsPopupView.openWidgets(this);
2365 return true;
2366 }
2367 break;
Sunny Goyal66b24572016-09-21 15:57:55 -07002368 }
2369 }
2370 return super.onKeyShortcut(keyCode, event);
2371 }
2372
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002373 @Override
2374 public boolean onKeyUp(int keyCode, KeyEvent event) {
2375 if (keyCode == KeyEvent.KEYCODE_MENU) {
2376 // KEYCODE_MENU is sent by some tests, for example
2377 // LauncherJankTests#testWidgetsContainerFling. Don't just remove its handling.
2378 if (!mDragController.isDragging() && !mWorkspace.isSwitchingState() &&
2379 isInState(NORMAL)) {
2380 // Close any open floating views.
2381 AbstractFloatingView.closeAllOpenViews(this);
2382
2383 // Setting the touch point to (-1, -1) will show the options popup in the center of
2384 // the screen.
Sunny Goyal2fd7a8b2018-03-30 17:10:13 -07002385 OptionsPopupView.showDefaultOptions(this, -1, -1);
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002386 }
2387 return true;
2388 }
2389 return super.onKeyUp(keyCode, event);
2390 }
2391
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002392 public static Launcher getLauncher(Context context) {
Sunny Goyal87b5eb62018-07-03 15:53:39 -07002393 return (Launcher) fromContext(context);
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002394 }
2395
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002396 /**
2397 * Callback for listening for onResume
2398 */
2399 public interface OnResumeCallback {
2400
2401 void onLauncherResume();
2402 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002403}