blob: f8d99595ebbddb9021d48627751430d589812260 [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;
vadimt00d42552018-12-11 17:59:36 -080022
Tony Wickham6a71a5b2018-08-21 11:40:23 -070023import static com.android.launcher3.AbstractFloatingView.TYPE_SNACKBAR;
Sunny Goyal4c7f2152017-10-17 17:17:16 -070024import static com.android.launcher3.LauncherAnimUtils.SPRING_LOADED_EXIT_DELAY;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -070025import static com.android.launcher3.LauncherState.ALL_APPS;
26import static com.android.launcher3.LauncherState.NORMAL;
Sunny Goyal6001ea22018-05-10 16:31:00 -070027import static com.android.launcher3.dragndrop.DragLayer.ALPHA_INDEX_LAUNCHER_LOAD;
Sunny Goyalaeb16432017-10-16 11:46:41 -070028import static com.android.launcher3.logging.LoggerUtils.newContainerTarget;
Hyunyoung Song46d07f72018-05-22 15:41:25 -070029import static com.android.launcher3.logging.LoggerUtils.newTarget;
vadimt00d42552018-12-11 17:59:36 -080030import static com.android.launcher3.util.RaceConditionTracker.ENTER;
31import static com.android.launcher3.util.RaceConditionTracker.EXIT;
Sunny Goyal326403e2017-10-02 12:45:10 -070032
Gilles Debunnedd6c9922010-10-25 11:23:41 -070033import android.animation.Animator;
Sunny Goyal6001ea22018-05-10 16:31:00 -070034import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070035import android.animation.AnimatorSet;
Sunny Goyal6001ea22018-05-10 16:31:00 -070036import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070037import android.animation.ValueAnimator;
Adam Cohen0b395352014-06-09 22:54:36 +000038import android.annotation.TargetApi;
Tony Wickham005df0b2018-02-13 11:28:12 -080039import android.app.ActivityOptions;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080040import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070041import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080043import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080044import android.content.ComponentCallbacks2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070047import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070048import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070049import android.content.SharedPreferences;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.content.pm.PackageManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080051import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070052import android.database.sqlite.SQLiteDatabase;
Sunny Goyal59d086c2018-05-07 17:31:40 -070053import android.graphics.Point;
Adam Cohen0f668f32014-09-08 19:54:17 +020054import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055import android.os.Bundle;
Hyunyoung Songbd6fba92018-05-16 15:54:31 -070056import android.os.Handler;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070057import android.os.Parcelable;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080058import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070059import android.os.StrictMode;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070060import android.os.UserHandle;
Michael Jurkaa33411c2012-06-14 16:18:21 -070061import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070063import android.util.Log;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070064import android.util.SparseArray;
Sunny Goyal59d086c2018-05-07 17:31:40 -070065import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070066import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070067import android.view.KeyboardShortcutGroup;
68import android.view.KeyboardShortcutInfo;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080069import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -070070import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070071import android.view.View;
Adam Cohen96d30a12013-07-16 18:13:21 -070072import android.view.ViewGroup;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080073import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070074import android.view.animation.OvershootInterpolator;
Adam Cohen96d30a12013-07-16 18:13:21 -070075import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070076
Sunny Goyal651077b2014-06-30 14:15:31 -070077import com.android.launcher3.DropTarget.DragObject;
Sunny Goyalae502842016-06-17 08:43:56 -070078import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070079import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070080import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal7ede6112017-12-05 15:11:21 -080081import com.android.launcher3.allapps.DiscoveryBounce;
Sunny Goyalf0b6db72018-08-13 16:10:14 -070082import com.android.launcher3.anim.PropertyListBuilder;
Sunny Goyalffe83f12014-08-14 17:39:34 -070083import com.android.launcher3.compat.AppWidgetManagerCompat;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -070084import com.android.launcher3.compat.LauncherAppsCompatVO;
Tony Wickhame0c33232016-02-08 11:37:04 -080085import com.android.launcher3.config.FeatureFlags;
vadimt00d42552018-12-11 17:59:36 -080086import com.android.launcher3.dot.DotInfo;
Vadim Tryshevfedca432015-08-19 17:55:02 -070087import com.android.launcher3.dragndrop.DragController;
88import com.android.launcher3.dragndrop.DragLayer;
89import com.android.launcher3.dragndrop.DragView;
Jon Mirandaaeb4dd02018-07-11 14:16:23 -070090import com.android.launcher3.folder.Folder;
Sunny Goyal26119432016-02-18 22:09:23 +000091import com.android.launcher3.folder.FolderIcon;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070092import com.android.launcher3.folder.FolderIconPreviewVerifier;
Sunny Goyalf840f102018-09-21 14:41:05 -070093import com.android.launcher3.icons.IconCache;
Sunny Goyal66b24572016-09-21 15:57:55 -070094import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -070095import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -070096import com.android.launcher3.logging.FileLog;
Hyunyoung Songfc007472018-10-25 14:09:50 -070097import com.android.launcher3.logging.StatsLogUtils;
Hyunyoung Songaa953652016-04-19 18:30:24 -070098import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Song46d07f72018-05-22 15:41:25 -070099import com.android.launcher3.logging.UserEventDispatcher.UserEventDelegate;
Sunny Goyal369212a2019-03-26 15:03:57 -0700100import com.android.launcher3.model.AppLaunchTracker;
Sunny Goyala535ae42017-02-27 10:07:13 -0800101import com.android.launcher3.model.ModelWriter;
Sunny Goyala535ae42017-02-27 10:07:13 -0800102import com.android.launcher3.notification.NotificationListener;
Tony Wickham540913e2017-01-23 11:47:51 -0800103import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -0800104import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700105import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyalf8088ee2017-11-10 14:52:00 -0800106import com.android.launcher3.states.InternalStateHandler;
Sunny Goyal623eddd2018-03-02 12:24:41 -0800107import com.android.launcher3.states.RotationHelper;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800108import com.android.launcher3.touch.ItemClickHandler;
Sunny Goyala5ace712017-11-15 17:12:51 -0800109import com.android.launcher3.uioverrides.UiFactory;
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700110import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530111import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
112import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
Sunny Goyalaeb16432017-10-16 11:46:41 -0700113import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyal2100c782016-08-22 16:00:03 -0700114import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700115import com.android.launcher3.util.ComponentKey;
Sunny Goyalefb7e842018-10-04 15:11:00 -0700116import com.android.launcher3.util.IntArray;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700117import com.android.launcher3.util.ItemInfoMatcher;
Sunny Goyal6001ea22018-05-10 16:31:00 -0700118import com.android.launcher3.util.MultiValueAlpha;
119import com.android.launcher3.util.MultiValueAlpha.AlphaProperty;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700120import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800121import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700122import com.android.launcher3.util.PendingRequestArgs;
vadimt00d42552018-12-11 17:59:36 -0800123import com.android.launcher3.util.RaceConditionTracker;
Sunny Goyal8392c822017-06-20 10:03:56 -0700124import com.android.launcher3.util.SystemUiController;
Mario Bertschlera6936942017-05-31 14:48:19 -0700125import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700126import com.android.launcher3.util.Thunk;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700127import com.android.launcher3.util.TraceHelper;
Sunny Goyal326403e2017-10-02 12:45:10 -0700128import com.android.launcher3.util.UiThreadHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700129import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyalab770a12018-11-14 15:17:26 -0800130import com.android.launcher3.views.ActivityContext;
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800131import com.android.launcher3.views.OptionsPopupView;
Sunny Goyal29947f02017-12-18 13:49:44 -0800132import com.android.launcher3.widget.LauncherAppWidgetHostView;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800133import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700134import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal29947f02017-12-18 13:49:44 -0800135import com.android.launcher3.widget.PendingAppWidgetHostView;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800136import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700137import com.android.launcher3.widget.WidgetHostViewLoader;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700138import com.android.launcher3.widget.WidgetListRowEntry;
139import com.android.launcher3.widget.WidgetsFullSheet;
Sunny Goyal952e63d2017-08-16 04:59:08 -0700140import com.android.launcher3.widget.custom.CustomWidgetParser;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700141
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700142import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700143import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700144import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700145import java.util.Collection;
Zak Cohen658c67a2018-10-19 14:21:05 -0700146import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700147import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700148import java.util.List;
Sunny Goyalaae6fbb2019-01-31 16:05:58 -0800149import java.util.function.Predicate;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700150
Sunny Goyal8c48d8b2019-01-25 15:10:18 -0800151import androidx.annotation.Nullable;
152
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153/**
154 * Default launcher application.
155 */
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700156public class Launcher extends BaseDraggingActivity implements LauncherExterns,
Hyunyoung Song31971a32019-01-27 12:31:12 -0800157 LauncherModel.Callbacks, LauncherProviderChangeListener, UserEventDelegate,
158 InvariantDeviceProfile.OnIDPChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700159 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700160 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161
Winson Chunga2413752012-04-03 14:22:34 -0700162 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700163
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700165 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700166
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700167 private static final int REQUEST_PICK_APPWIDGET = 9;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168
Michael Jurka8b805b12012-04-18 14:23:14 -0700169 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800170 public static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
171 public static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700172
Jon Mirandac476d6e2017-05-04 16:30:01 -0700173 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700174
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700175 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
176
Mathew Inwood876a8462013-06-14 14:12:41 +0100177 /**
178 * IntentStarter uses request codes starting with this. This must be greater than all activity
179 * request codes used internally.
180 */
181 protected static final int REQUEST_LAST = 100;
182
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 // Type: int
184 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700185 // Type: int
186 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700187 // Type: PendingRequestArgs
188 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
189 // Type: ActivityResultInfo
190 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700191 // Type: SparseArray<Parcelable>
192 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
vadimt00d42552018-12-11 17:59:36 -0800193 public static final String ON_CREATE_EVT = "Launcher.onCreate";
vadimtcb863752018-12-19 17:44:57 -0800194 private static final String ON_START_EVT = "Launcher.onStart";
195 private static final String ON_RESUME_EVT = "Launcher.onResume";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700197 private LauncherStateManager mStateManager;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700198
Adam Cohenad4e15c2013-10-17 16:21:35 -0700199 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200
Winson Chunga2413752012-04-03 14:22:34 -0700201 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700202 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
203 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
204 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700205
Jon Miranda54441f52018-01-24 15:38:25 -0800206 private LauncherAppTransitionManager mAppTransitionManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800207 private Configuration mOldConfig;
Jon Miranda54441f52018-01-24 15:38:25 -0800208
Adam Cohen091440a2015-03-18 14:16:05 -0700209 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700210 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700211 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800212 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700213
Sunny Goyalffe83f12014-08-14 17:39:34 -0700214 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700215 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700216
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700217 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700218
Sunny Goyal316490e2015-06-02 09:38:28 -0700219 @Thunk Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700220
Sunny Goyal47328fd2016-05-25 18:56:41 -0700221 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700222
223 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700224 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700225 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700226
Winson Chung8ae41982017-11-10 11:42:13 -0800227 // UI and state for the overview panel
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800228 private View mOverviewPanel;
Winson Chung8ae41982017-11-10 11:42:13 -0800229
Adam Cohen091440a2015-03-18 14:16:05 -0700230 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400231
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700232 private OnResumeCallback mOnResumeCallback;
233
Sunny Goyal527c7d32015-08-28 15:19:36 -0700234 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700235
Joe Onorato9c1289c2009-08-17 11:03:03 -0400236 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800237 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800238 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700239 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400240
Tony Wickham010d2552017-01-20 08:15:28 -0800241 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700242
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800243 private int mSynchronouslyBoundPage = PagedView.INVALID_PAGE;
Adam Cohen1462de32012-07-24 22:34:36 -0700244
Winson Chung46353de2012-02-16 14:05:10 -0800245 // We only want to get the SharedPreferences once since it does an FS stat each time we get
246 // it from the context.
247 private SharedPreferences mSharedPrefs;
248
Sunny Goyal2100c782016-08-22 16:00:03 -0700249 // Activity result which needs to be processed after workspace has loaded.
250 private ActivityResultInfo mPendingActivityResult;
251 /**
252 * Holds extra information required to handle a result from an external call, like
253 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
254 */
255 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800256
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700257 public ViewGroupFocusHelper mFocusHandler;
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700258
Sunny Goyal623eddd2018-03-02 12:24:41 -0800259 private RotationHelper mRotationHelper;
Sunny Goyal5743f862019-03-28 15:35:32 -0700260 private Runnable mCancelTouchController;
Sunny Goyal7779d622015-06-11 16:18:39 -0700261
Sunny Goyal2db53422019-03-01 16:06:12 -0800262 final Handler mHandler = new Handler();
Sunny Goyala002c6c2019-02-12 16:20:10 -0800263 private final Runnable mHandleDeferredResume = this::handleDeferredResume;
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700264
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800265 @Override
266 protected void onCreate(Bundle savedInstanceState) {
vadimt00d42552018-12-11 17:59:36 -0800267 RaceConditionTracker.onEvent(ON_CREATE_EVT, ENTER);
Winson Chunga2413752012-04-03 14:22:34 -0700268 if (DEBUG_STRICT_MODE) {
269 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
270 .detectDiskReads()
271 .detectDiskWrites()
272 .detectNetwork() // or .detectAll() for all detectable problems
273 .penaltyLog()
274 .build());
275 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
276 .detectLeakedSqlLiteObjects()
277 .detectLeakedClosableObjects()
278 .penaltyLog()
279 .penaltyDeath()
280 .build());
281 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700282 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700283
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800284 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700285 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400286
Sunny Goyal87f784c2017-01-11 10:48:34 -0800287 LauncherAppState app = LauncherAppState.getInstance(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800288 mOldConfig = new Configuration(getResources().getConfiguration());
Sunny Goyal0e7724c2018-02-22 13:22:21 -0800289 mModel = app.setLauncher(this);
Hyunyoung Song31971a32019-01-27 12:31:12 -0800290 InvariantDeviceProfile idp = app.getInvariantDeviceProfile();
291 initDeviceProfile(idp);
292 idp.addOnChangeListener(this);
Sunny Goyalf7258242015-10-19 16:59:07 -0700293 mSharedPrefs = Utilities.getPrefs(this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800294 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700295 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700296
Joe Onorato41a12d22009-10-31 18:30:00 -0400297 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700298 mAllAppsController = new AllAppsTransitionController(this);
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800299 mStateManager = new LauncherStateManager(this);
Tracy Zhou8b23c6f2018-04-30 17:13:27 -0700300 UiFactory.onCreate(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700301
Sunny Goyalffe83f12014-08-14 17:39:34 -0700302 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700303
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700304 mAppWidgetHost = new LauncherAppWidgetHost(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700305 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700306
Sunny Goyal60820d72017-05-09 12:40:11 -0700307 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700308
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800309 setupViews();
Tony Wickham010d2552017-01-20 08:15:28 -0800310 mPopupDataProvider = new PopupDataProvider(this);
311
Sunny Goyal623eddd2018-03-02 12:24:41 -0800312 mRotationHelper = new RotationHelper(this);
Sunny Goyal90548432018-04-04 17:17:00 -0700313 mAppTransitionManager = LauncherAppTransitionManager.newInstance(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800314
Sunny Goyald3864fa2017-11-14 15:06:36 -0800315 boolean internalStateHandled = InternalStateHandler.handleCreate(this, getIntent());
316 if (internalStateHandled) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800317 if (savedInstanceState != null) {
318 // InternalStateHandler has already set the appropriate state.
319 // We dont need to do anything.
320 savedInstanceState.remove(RUNTIME_STATE);
321 }
322 }
323 restoreState(savedInstanceState);
Sunny Goyalbbece862019-03-01 13:13:52 -0800324 mStateManager.reapplyState();
Winson Chungb63b44c2017-11-10 17:54:44 -0800325
Sunny Goyal2100c782016-08-22 16:00:03 -0700326 // We only load the page synchronously if the user rotates (or triggers a
327 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700328 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
329 if (savedInstanceState != null) {
330 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
331 }
Sunny Goyald3864fa2017-11-14 15:06:36 -0800332
Sunny Goyalfe770c92016-09-27 14:38:58 -0700333 if (!mModel.startLoader(currentScreen)) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800334 if (!internalStateHandled) {
335 // If we are not binding synchronously, show a fade in animation when
336 // the first page bind completes.
Sunny Goyal6001ea22018-05-10 16:31:00 -0700337 mDragLayer.getAlphaProperty(ALPHA_INDEX_LAUNCHER_LOAD).setValue(0);
Sunny Goyald3864fa2017-11-14 15:06:36 -0800338 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700339 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700340 // Pages bound synchronously.
341 mWorkspace.setCurrentPage(currentScreen);
342
Sunny Goyal2100c782016-08-22 16:00:03 -0700343 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800344 }
345
346 // For handling default keys
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800347 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800348
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800349 setContentView(mLauncherView);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800350 getRootView().dispatchInsets();
Mario Bertschlera6936942017-05-31 14:48:19 -0700351
Jon Miranda7fda2852017-07-19 16:07:01 -0700352 // Listen for broadcasts
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700353 registerReceiver(mScreenOffReceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));
Sunny Goyala616d2b2017-06-12 13:54:01 -0700354
Sunny Goyal8392c822017-06-20 10:03:56 -0700355 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
356 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700357
358 if (mLauncherCallbacks != null) {
359 mLauncherCallbacks.onCreate(savedInstanceState);
360 }
Sunny Goyal623eddd2018-03-02 12:24:41 -0800361 mRotationHelper.initialize();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700362
363 TraceHelper.endSection("Launcher-onCreate");
vadimt00d42552018-12-11 17:59:36 -0800364 RaceConditionTracker.onEvent(ON_CREATE_EVT, EXIT);
Adam Cohen9211d422014-10-07 18:14:53 -0700365 }
366
Sunny Goyal7779d622015-06-11 16:18:39 -0700367 @Override
Winson Chung8eb49e02018-07-16 13:33:31 -0700368 public void onEnterAnimationComplete() {
369 super.onEnterAnimationComplete();
370 UiFactory.onEnterAnimationComplete(this);
arangelovad783052018-08-17 14:46:45 +0100371 mAllAppsController.highlightWorkTabIfNecessary();
Winson Chung8eb49e02018-07-16 13:33:31 -0700372 }
373
374 @Override
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800375 public void onConfigurationChanged(Configuration newConfig) {
376 int diff = newConfig.diff(mOldConfig);
Jon Mirandaaeb4dd02018-07-11 14:16:23 -0700377
378 if ((diff & CONFIG_LOCALE) != 0) {
379 Folder.setLocaleDependentFields(getResources(), true /* force */);
380 }
381
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800382 if ((diff & (CONFIG_ORIENTATION | CONFIG_SCREEN_SIZE)) != 0) {
Sunny Goyal371ea052019-03-06 15:38:32 -0800383 onIdpChanged(mDeviceProfile.inv);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800384 }
385
386 mOldConfig.setTo(newConfig);
Tracy Zhoua706f002018-03-28 13:55:19 -0700387 UiFactory.onLauncherStateOrResumeChanged(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800388 super.onConfigurationChanged(newConfig);
389 }
390
Sunny Goyal605bcf32018-03-02 15:16:12 -0800391 @Override
Sunny Goyal59d086c2018-05-07 17:31:40 -0700392 protected void reapplyUi() {
393 getRootView().dispatchInsets();
394 getStateManager().reapplyState(true /* cancelCurrentAnimation */);
395 }
396
397 @Override
Sunny Goyal605bcf32018-03-02 15:16:12 -0800398 public void rebindModel() {
399 int currentPage = mWorkspace.getNextPage();
400 if (mModel.startLoader(currentPage)) {
401 mWorkspace.setCurrentPage(currentPage);
402 setWorkspaceLoading(true);
403 }
404 }
405
Hyunyoung Song31971a32019-01-27 12:31:12 -0800406 @Override
407 public void onIdpChanged(int changeFlags, InvariantDeviceProfile idp) {
Sunny Goyal371ea052019-03-06 15:38:32 -0800408 onIdpChanged(idp);
409 }
410
Govinda Wasserman24e489e2019-03-28 12:23:28 -0400411 public void setQuickSearchBarAlpha(float alpha) {
412 View qsbAllApps = findViewById(R.id.search_container_all_apps);
413 if (qsbAllApps != null) {
414 qsbAllApps.setAlpha(alpha);
415 }
416 }
417
Sunny Goyal371ea052019-03-06 15:38:32 -0800418 private void onIdpChanged(InvariantDeviceProfile idp) {
419 mUserEventDispatcher = null;
420
Hyunyoung Song31971a32019-01-27 12:31:12 -0800421 initDeviceProfile(idp);
Sunny Goyal371ea052019-03-06 15:38:32 -0800422 dispatchDeviceProfileChanged();
423 reapplyUi();
424 mDragLayer.recreateControllers();
425
426 // TODO: We can probably avoid rebind when only screen size changed.
427 rebindModel();
Hyunyoung Song31971a32019-01-27 12:31:12 -0800428 }
429
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800430 private void initDeviceProfile(InvariantDeviceProfile idp) {
431 // Load configuration-specific DeviceProfile
Sunny Goyal59d086c2018-05-07 17:31:40 -0700432 mDeviceProfile = idp.getDeviceProfile(this);
Sunny Goyal8c48d8b2019-01-25 15:10:18 -0800433 if (isInMultiWindowMode()) {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700434 Display display = getWindowManager().getDefaultDisplay();
435 Point mwSize = new Point();
436 display.getSize(mwSize);
437 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
438 }
439 onDeviceProfileInitiated();
Sunny Goyal605bcf32018-03-02 15:16:12 -0800440 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout(), true);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800441 }
442
Sunny Goyal623eddd2018-03-02 12:24:41 -0800443 public RotationHelper getRotationHelper() {
444 return mRotationHelper;
445 }
446
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700447 public LauncherStateManager getStateManager() {
448 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700449 }
450
Mario Bertschler27288382017-05-24 15:35:09 -0700451 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700452 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800453 return mLauncherView.findViewById(id);
454 }
455
456 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700457 public void onAppWidgetHostReset() {
458 if (mAppWidgetHost != null) {
459 mAppWidgetHost.startListening();
460 }
461 }
462
Adam Cohen9211d422014-10-07 18:14:53 -0700463 private LauncherCallbacks mLauncherCallbacks;
464
Sunny Goyal32554d12015-12-03 15:31:25 -0800465 /**
466 * Call this after onCreate to set or clear overlay.
467 */
468 public void setLauncherOverlay(LauncherOverlay overlay) {
469 if (overlay != null) {
470 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
471 }
472 mWorkspace.setLauncherOverlay(overlay);
473 }
474
Adam Cohen9211d422014-10-07 18:14:53 -0700475 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
476 mLauncherCallbacks = callbacks;
477 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700478 }
479
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700480 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700481 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700482 if (mLauncherCallbacks != null) {
483 mLauncherCallbacks.onLauncherProviderChange();
484 }
485 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700486
Hyunyoung Song3f471442015-04-08 19:01:34 -0700487 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700488 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
489 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700490 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700491 }
492
Tony Wickham010d2552017-01-20 08:15:28 -0800493 public PopupDataProvider getPopupDataProvider() {
494 return mPopupDataProvider;
495 }
496
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700497 @Override
Tony Wickhamf34bee82018-12-03 18:11:39 -0800498 public DotInfo getDotInfoForItem(ItemInfo info) {
499 return mPopupDataProvider.getDotInfoForItem(info);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700500 }
501
502 @Override
503 public void invalidateParent(ItemInfo info) {
Jon Mirandae6f3fa42019-03-04 09:14:40 -0800504 if (info.container >= 0) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700505 View folderIcon = getWorkspace().getHomescreenIconByItemId(info.container);
Jon Mirandae6f3fa42019-03-04 09:14:40 -0800506 if (folderIcon instanceof FolderIcon && folderIcon.getTag() instanceof FolderInfo) {
507 FolderIconPreviewVerifier verifier =
508 new FolderIconPreviewVerifier(getDeviceProfile().inv);
509 verifier.setFolderInfo((FolderInfo) folderIcon.getTag());
510 if (verifier.isItemInPreview(info.rank)) {
511 folderIcon.invalidate();
512 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700513 }
514 }
515 }
516
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000517 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700518 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
519 * a configuration step, this allows the proper animations to run after other transitions.
520 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700521 private int completeAdd(
Sunny Goyal2100c782016-08-22 16:00:03 -0700522 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
Sunny Goyalefb7e842018-10-04 15:11:00 -0700523 int screenId = info.screenId;
Sunny Goyal2100c782016-08-22 16:00:03 -0700524 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700525 // When the screen id represents an actual screen (as opposed to a rank) we make sure
526 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700527 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700528 }
Adam Cohendb364c32014-05-20 14:23:40 -0700529
Sunny Goyal2100c782016-08-22 16:00:03 -0700530 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800531 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700532 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700533 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800534 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700535 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700536 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700537 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700538 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700539 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700540 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700541 int widgetId = appWidgetId;
542 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700543 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700544 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700545 // Since the view was just bound, also launch the configure activity if needed
546 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
547 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800548 if (provider != null) {
549 new WidgetAddFlowHandler(provider)
550 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700551 }
552 }
553 break;
554 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800555 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700556
Adam Cohendb364c32014-05-20 14:23:40 -0700557 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800558 }
559
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800560 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700561 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700562 if (isWorkspaceLoading()) {
563 // process the result once the workspace has loaded.
564 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
565 return;
566 }
567 mPendingActivityResult = null;
568
Winson Chunge341d302013-08-16 14:31:00 -0700569 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700570 final PendingRequestArgs requestArgs = mPendingRequestArgs;
571 setWaitingForResult(null);
572 if (requestArgs == null) {
573 return;
574 }
575
576 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700577
Adam Cohenad4e15c2013-10-17 16:21:35 -0700578 Runnable exitSpringLoaded = new Runnable() {
579 @Override
580 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700581 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700582 }
583 };
584
Michael Jurka8b805b12012-04-18 14:23:14 -0700585 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700586 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700587 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700588 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
589 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700590 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700591 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700592 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700593 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700594 addAppWidgetImpl(
595 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800596 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700597 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700598 }
599 return;
600 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200601
Adam Cohened66b2b2012-01-23 17:28:51 -0800602 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700603 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700604
Adam Cohened66b2b2012-01-23 17:28:51 -0800605 // We have special handling for widgets
606 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800607 final int appWidgetId;
608 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
609 : -1;
610 if (widgetId < 0) {
611 appWidgetId = pendingAddWidgetId;
612 } else {
613 appWidgetId = widgetId;
614 }
615
Adam Cohenad4e15c2013-10-17 16:21:35 -0700616 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800617 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700618 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
619 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700620 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700621 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700622 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700623 @Override
624 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700625 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700626 }
627 };
Adam Cohendb364c32014-05-20 14:23:40 -0700628
Sunny Goyal2100c782016-08-22 16:00:03 -0700629 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
630 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
631 } else {
632 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
633 // When the screen id represents an actual screen (as opposed to a rank)
634 // we make sure that the drop page actually exists.
635 requestArgs.screenId =
636 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700637 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700638 final CellLayout dropLayout =
639 mWorkspace.getScreenWithId(requestArgs.screenId);
640
641 dropLayout.setDropPending(true);
642 final Runnable onComplete = new Runnable() {
643 @Override
644 public void run() {
645 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
646 dropLayout.setDropPending(false);
647 }
648 };
649 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
650 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700651 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800652 return;
653 }
654
Sunny Goyald478c832016-04-01 12:04:16 -0700655 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
656 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700657 if (resultCode == RESULT_OK) {
658 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700659 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700660 }
661 // Leave the widget in the pending state if the user canceled the configure.
662 return;
663 }
664
Sunny Goyalaad90582015-07-09 14:22:50 -0700665 if (requestCode == REQUEST_CREATE_SHORTCUT) {
666 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700667 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
668 completeAdd(requestCode, data, -1, requestArgs);
669 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
670 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
671
Sunny Goyalaad90582015-07-09 14:22:50 -0700672 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700673 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
674 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800675 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800676 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800677 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800678 }
679
680 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700681 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800682 final int requestCode, final int resultCode, final Intent data) {
683 handleActivityResult(requestCode, resultCode, data);
684 if (mLauncherCallbacks != null) {
685 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
686 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800687 }
688
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700689 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700690 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600691 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700692 PendingRequestArgs pendingArgs = mPendingRequestArgs;
693 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
694 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
695 setWaitingForResult(null);
696
Sunny Goyal28c6b962015-10-12 11:42:05 -0700697 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700698 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700699 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700700 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700701 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700702 Intent intent = pendingArgs.getPendingIntent();
703
Sunny Goyal28c6b962015-10-12 11:42:05 -0700704 if (grantResults.length > 0
705 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal369212a2019-03-26 15:03:57 -0700706 startActivitySafely(v, intent, null, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700707 } else {
708 // TODO: Show a snack bar with link to settings
709 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700710 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700711 }
712 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600713 if (mLauncherCallbacks != null) {
714 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
715 grantResults);
716 }
717 }
718
Adam Cohendb364c32014-05-20 14:23:40 -0700719 /**
720 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
721 *
722 * @param screenId the screen id to check
723 * @return the new screen, or screenId if it exists
724 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700725 private int ensurePendingDropLayoutExists(int screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800726 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700727 if (dropLayout == null) {
728 // it's possible that the add screen was removed because it was
729 // empty and a re-bind occurred
730 mWorkspace.addExtraEmptyScreen();
731 return mWorkspace.commitExtraEmptyScreen();
732 } else {
733 return screenId;
734 }
735 }
736
Sunny Goyal2100c782016-08-22 16:00:03 -0700737 @Thunk void completeTwoStageWidgetDrop(
738 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
739 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800740 Runnable onCompleteRunnable = null;
741 int animationType = 0;
742
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700743 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800744 if (resultCode == RESULT_OK) {
745 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
746 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800747 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700748 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800749 onCompleteRunnable = new Runnable() {
750 @Override
751 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700752 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700753 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800754 }
755 };
756 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800757 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800758 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800759 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700760 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700761 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700762 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
763 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700764 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700765 // The animated view may be null in the case of a rotation during widget configuration
766 onCompleteRunnable.run();
767 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800768 }
769
770 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700771 protected void onStop() {
772 super.onStop();
Adam Cohen9211d422014-10-07 18:14:53 -0700773 if (mLauncherCallbacks != null) {
774 mLauncherCallbacks.onStop();
775 }
Miranda Kephart1a359a22019-02-11 13:07:04 -0500776
Hyunyoung Song1241e612018-05-15 21:46:51 -0700777 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
778 mStateManager.getState().containerType, -1);
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700779
780 mAppWidgetHost.setListenIfResumed(false);
781
Tony Wickham010d2552017-01-20 08:15:28 -0800782 NotificationListener.removeNotificationsChangedListener();
Sunny Goyalaf161fd2018-02-05 16:34:57 -0800783 getStateManager().moveToRestState();
Winson Chungedb0d6b2018-03-21 17:32:55 -0700784
Tracy Zhou62646712018-06-26 14:19:02 -0700785 UiFactory.onLauncherStateOrResumeChanged(this);
786
Winson Chungdea51352018-04-24 13:02:10 -0700787 // Workaround for b/78520668, explicitly trim memory once UI is hidden
Winson Chung473730f2018-04-26 11:54:22 -0700788 onTrimMemory(TRIM_MEMORY_UI_HIDDEN);
Michael Jurkacd496d72013-04-11 11:32:45 -0700789 }
790
791 @Override
792 protected void onStart() {
vadimtcb863752018-12-19 17:44:57 -0800793 RaceConditionTracker.onEvent(ON_START_EVT, ENTER);
Michael Jurkacd496d72013-04-11 11:32:45 -0700794 super.onStart();
Adam Cohen9211d422014-10-07 18:14:53 -0700795 if (mLauncherCallbacks != null) {
796 mLauncherCallbacks.onStart();
797 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800798 mAppWidgetHost.setListenIfResumed(true);
Sunny Goyalcac002d2018-01-04 09:51:45 -0800799 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
vadimtcb863752018-12-19 17:44:57 -0800800 RaceConditionTracker.onEvent(ON_START_EVT, EXIT);
Michael Jurkacd496d72013-04-11 11:32:45 -0700801 }
802
Sunny Goyala002c6c2019-02-12 16:20:10 -0800803 private void handleDeferredResume() {
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700804 if (hasBeenResumed()) {
805 getUserEventDispatcher().logActionCommand(Action.Command.RESUME,
806 mStateManager.getState().containerType, -1);
807 getUserEventDispatcher().startSession();
Sunny Goyala002c6c2019-02-12 16:20:10 -0800808
809 UiFactory.onLauncherStateOrResumeChanged(this);
Sunny Goyal369212a2019-03-26 15:03:57 -0700810 AppLaunchTracker.INSTANCE.get(this).onReturnedToHome();
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700811 }
812 }
813
Michael Jurkacd496d72013-04-11 11:32:45 -0700814 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800815 protected void onResume() {
vadimtcb863752018-12-19 17:44:57 -0800816 RaceConditionTracker.onEvent(ON_RESUME_EVT, ENTER);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700817 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800818 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700819 TraceHelper.partitionSection("ON_RESUME", "superCall");
820
Sunny Goyala002c6c2019-02-12 16:20:10 -0800821 mHandler.removeCallbacks(mHandleDeferredResume);
822 Utilities.postAsyncCallback(mHandler, mHandleDeferredResume);
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700823
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700824 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700825 // Process any items that were added while Launcher was away.
826 InstallShortcutReceiver.disableAndFlushInstallQueue(
827 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700828
Sunny Goyala474a9b2017-05-04 16:47:11 -0700829 // Refresh shortcuts if the permission changed.
830 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700831
Tracy Zhoue5575f92018-04-19 11:12:53 -0700832 DiscoveryBounce.showForHomeIfNeeded(this);
Adam Cohen9211d422014-10-07 18:14:53 -0700833 if (mLauncherCallbacks != null) {
834 mLauncherCallbacks.onResume();
835 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800836
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700837 TraceHelper.endSection("ON_RESUME");
vadimtcb863752018-12-19 17:44:57 -0800838 RaceConditionTracker.onEvent(ON_RESUME_EVT, EXIT);
Adam Cohen06dff352012-06-01 17:17:08 -0700839 }
840
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800841 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700842 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700843 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700844 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700845
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700846 super.onPause();
Joe Onorato24b6fd82009-11-12 13:47:09 -0800847 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700848 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700849
Adam Cohen9211d422014-10-07 18:14:53 -0700850 if (mLauncherCallbacks != null) {
851 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700852 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700853 }
854
Winson Chungd7823942018-02-16 03:23:47 +0000855 @Override
Tracy Zhoua706f002018-03-28 13:55:19 -0700856 protected void onUserLeaveHint() {
857 super.onUserLeaveHint();
858 UiFactory.onLauncherStateOrResumeChanged(this);
859 }
860
861 @Override
Winson Chungd7823942018-02-16 03:23:47 +0000862 public void onWindowFocusChanged(boolean hasFocus) {
863 super.onWindowFocusChanged(hasFocus);
864 mStateManager.onWindowFocusChanged();
865 }
866
Adam Cohenc2d6e892014-10-16 09:49:24 -0700867 public interface LauncherOverlay {
868
869 /**
870 * Touch interaction leading to overscroll has begun
871 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700872 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700873
874 /**
875 * Touch interaction related to overscroll has ended
876 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700877 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700878
879 /**
880 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
881 * screen (or in the case of RTL, the rightmost screen).
882 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700883 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700884
885 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800886 * Called when the launcher is ready to use the overlay
887 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700888 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700889 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700890 }
891
892 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700893 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700894 }
895
896 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
897
Sunny Goyalc86df472016-02-25 09:19:38 -0800898 public void onScrollChanged(float progress) {
899 if (mWorkspace != null) {
900 mWorkspace.onOverlayScrollChanged(progress);
901 }
902 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700903 }
904
Sunny Goyalf9403d92017-10-18 10:55:56 -0700905 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -0700906 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700907 }
908
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800909 /**
910 * Restores the previous state, if it exists.
911 *
912 * @param savedState The previous state.
913 */
914 private void restoreState(Bundle savedState) {
915 if (savedState == null) {
916 return;
917 }
918
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700919 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -0700920 LauncherState[] stateValues = LauncherState.values();
921 LauncherState state = stateValues[stateOrdinal];
Sunny Goyalbc683e92017-12-06 10:25:07 -0800922 if (!state.disableRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700923 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800924 }
925
Sunny Goyal2100c782016-08-22 16:00:03 -0700926 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
927 if (requestArgs != null) {
928 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800929 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700930
931 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700932
933 SparseArray<Parcelable> widgetsState =
934 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
935 if (widgetsState != null) {
936 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
937 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800938 }
939
940 /**
941 * Finds all the views we need and configure them properly.
942 */
943 private void setupViews() {
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800944 mDragLayer = findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700945 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -0700946 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700947 mWorkspace.initParentViews(mDragLayer);
Sunny Goyal16764582017-11-06 16:00:34 -0800948 mOverviewPanel = findViewById(R.id.overview_panel);
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800949 mHotseat = findViewById(R.id.hotseat);
Craig Mautner360310b2012-10-26 15:13:08 -0700950
Sunny Goyal784f9c32016-03-23 16:56:54 -0700951 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
952 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
953 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800954
Winson Chung4c98d922011-05-31 16:50:48 -0700955 // Setup the drag layer
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700956 mDragLayer.setup(mDragController, mWorkspace);
Sunny Goyal5743f862019-03-28 15:35:32 -0700957 mCancelTouchController = UiFactory.enableLiveTouchControllerChanges(mDragLayer);
Winson Chung4c98d922011-05-31 16:50:48 -0700958
Hyunyoung Song645764e2016-06-06 14:19:02 -0700959 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -0700960 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
961 // default state, otherwise we will update to the wrong offsets in RTL
962 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700963 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700964 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700965
Tony Wickham34d2c912015-09-11 09:27:58 -0700966 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700967 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700968
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700969 // Setup Apps
970 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -0700971
Winson Chung3d503fb2011-07-13 17:25:49 -0700972 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -0700973 mDragController.setMoveTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700974 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -0400975
Sunny Goyal7185dd62018-03-14 17:51:49 -0700976 mAllAppsController.setupViews(mAppsView);
Sunny Goyalab770a12018-11-14 15:17:26 -0800977 mHotseat.setOnInterceptTouchListener(mWorkspace::onInterceptHotseatTouch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800978 }
979
980 /**
981 * Creates a view representing a shortcut.
982 *
983 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800984 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800985 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700986 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800987 }
988
989 /**
990 * Creates a view representing a shortcut inflated from the specified resource.
991 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800992 * @param parent The group the shortcut belongs to.
993 * @param info The data structure describing the shortcut.
994 *
995 * @return A View inflated from layoutResId.
996 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700997 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -0800998 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
999 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001000 favorite.applyFromShortcutInfo(info);
Sunny Goyal8a2a63b2018-03-06 22:15:18 -08001001 favorite.setOnClickListener(ItemClickHandler.INSTANCE);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001002 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001003 return favorite;
1004 }
1005
1006 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -07001007 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001008 *
1009 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001010 */
Sunny Goyalefb7e842018-10-04 15:11:00 -07001011 private void completeAddShortcut(Intent data, int container, int screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001012 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001013 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
1014 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001015 return;
1016 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001017
Jon Mirandac476d6e2017-05-04 16:30:01 -07001018 int[] cellXY = mTmpAddItemCellCoordinates;
1019 CellLayout layout = getCellLayout(container, screenId);
1020
Sunny Goyal782f0c92017-01-19 10:27:54 -08001021 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001022 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001023 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
1024 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001025 }
1026
1027 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001028 // Legacy shortcuts are only supported for primary profile.
1029 info = Process.myUserHandle().equals(args.user)
1030 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001031
Sunny Goyalb57645f2017-03-20 13:57:28 -07001032 if (info == null) {
1033 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1034 return;
1035 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001036 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001037 // The app is trying to add a shortcut without sufficient permissions
1038 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1039 return;
1040 }
1041 }
1042
Jon Mirandac476d6e2017-05-04 16:30:01 -07001043 if (container < 0) {
1044 // Adding a shortcut to the Workspace.
1045 final View view = createShortcut(info);
1046 boolean foundCellSpan = false;
1047 // First we check if we already know the exact location where we want to add this item.
1048 if (cellX >= 0 && cellY >= 0) {
1049 cellXY[0] = cellX;
1050 cellXY[1] = cellY;
1051 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001052
Jon Mirandac476d6e2017-05-04 16:30:01 -07001053 // If appropriate, either create a folder or add to an existing folder
1054 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001055 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001056 return;
1057 }
1058 DragObject dragObject = new DragObject();
1059 dragObject.dragInfo = info;
1060 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1061 true)) {
1062 return;
1063 }
1064 } else {
1065 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1066 }
1067
1068 if (!foundCellSpan) {
1069 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001070 return;
1071 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001072
1073 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1074 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001075 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001076 // Adding a shortcut to a Folder.
Sunny Goyale29897f2017-07-20 10:09:42 -07001077 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001078 if (folderIcon != null) {
1079 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1080 folderInfo.add(info, args.rank, false);
1081 } else {
Sunny Goyale29897f2017-07-20 10:09:42 -07001082 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001083 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001084 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001085 }
1086
Sunny Goyalefb7e842018-10-04 15:11:00 -07001087 public FolderIcon findFolderIcon(final int folderIconId) {
Sunny Goyal83fd25e2018-07-09 16:47:01 -07001088 return (FolderIcon) mWorkspace.getHomescreenIconByItemId(folderIconId);
Sunny Goyale29897f2017-07-20 10:09:42 -07001089 }
1090
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001091 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001092 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001093 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001094 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001095 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001096 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001097 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1098
Adam Cohened66b2b2012-01-23 17:28:51 -08001099 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001100 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001101 }
Romain Guycbb89e42009-06-08 15:52:54 -07001102
Adam Cohen59400422014-03-05 18:07:04 -08001103 LauncherAppWidgetInfo launcherInfo;
1104 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001105 launcherInfo.spanX = itemInfo.spanX;
1106 launcherInfo.spanY = itemInfo.spanY;
1107 launcherInfo.minSpanX = itemInfo.minSpanX;
1108 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001109 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001110
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001111 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001112 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001113
Sunny Goyal2100c782016-08-22 16:00:03 -07001114 if (hostView == null) {
1115 // Perform actual inflation because we're live
1116 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001117 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001118 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301119 prepareAppWidget(hostView, launcherInfo);
1120 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001121 }
Romain Guycbb89e42009-06-08 15:52:54 -07001122
Sunny Goyald5462aa2016-11-22 16:52:39 +05301123 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001124 hostView.setTag(item);
1125 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001126 hostView.setFocusable(true);
1127 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001128 }
1129
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001130 private final BroadcastReceiver mScreenOffReceiver = new BroadcastReceiver() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001131 @Override
1132 public void onReceive(Context context, Intent intent) {
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001133 // Reset AllApps to its initial state only if we are not in the middle of
1134 // processing a multi-step drop
1135 if (mPendingRequestArgs == null) {
1136 mStateManager.goToState(NORMAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001137 }
1138 }
1139 };
1140
Sunny Goyalaae6fbb2019-01-31 16:05:58 -08001141 public void updateNotificationDots(Predicate<PackageUserKey> updatedDots) {
Tony Wickhamf34bee82018-12-03 18:11:39 -08001142 mWorkspace.updateNotificationDots(updatedDots);
1143 mAppsView.getAppsStore().updateNotificationDots(updatedDots);
Tony Wickham010d2552017-01-20 08:15:28 -08001144 }
1145
Adam Cohended9f8d2010-11-03 13:25:16 -07001146 @Override
1147 public void onAttachedToWindow() {
1148 super.onAttachedToWindow();
1149
Sunny Goyalc86df472016-02-25 09:19:38 -08001150 if (mLauncherCallbacks != null) {
1151 mLauncherCallbacks.onAttachedToWindow();
1152 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001153 }
1154
1155 @Override
1156 public void onDetachedFromWindow() {
1157 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001158
1159 if (mLauncherCallbacks != null) {
1160 mLauncherCallbacks.onDetachedFromWindow();
1161 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001162 }
1163
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001164 public AllAppsTransitionController getAllAppsController() {
1165 return mAllAppsController;
1166 }
1167
Sunny Goyal9d69c8d2018-03-19 13:41:31 -07001168 @Override
Winson Chung1a341002018-01-17 10:00:23 -08001169 public LauncherRootView getRootView() {
1170 return (LauncherRootView) mLauncherView;
1171 }
1172
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001173 @Override
Winson Chunga6945242014-01-08 14:04:34 -08001174 public DragLayer getDragLayer() {
1175 return mDragLayer;
1176 }
1177
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001178 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001179 return mAppsView;
1180 }
1181
Winson Chunga6945242014-01-08 14:04:34 -08001182 public Workspace getWorkspace() {
1183 return mWorkspace;
1184 }
1185
1186 public Hotseat getHotseat() {
1187 return mHotseat;
1188 }
1189
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001190 public <T extends View> T getOverviewPanel() {
Winson Chung8ae41982017-11-10 11:42:13 -08001191 return (T) mOverviewPanel;
Winson Chunga6945242014-01-08 14:04:34 -08001192 }
1193
Sunny Goyal47328fd2016-05-25 18:56:41 -07001194 public DropTargetBar getDropTargetBar() {
1195 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001196 }
1197
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001198 public LauncherAppWidgetHost getAppWidgetHost() {
1199 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001200 }
Romain Guycbb89e42009-06-08 15:52:54 -07001201
Winson Chunga9abd0e2010-10-27 17:18:37 -07001202 public LauncherModel getModel() {
1203 return mModel;
1204 }
1205
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001206 public ModelWriter getModelWriter() {
1207 return mModelWriter;
1208 }
1209
Adam Cohen79d90c52016-04-22 13:29:20 -07001210 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001211 return mSharedPrefs;
1212 }
1213
Sunny Goyalf8d56fc2018-01-31 15:18:11 -08001214 public int getOrientation() { return mOldConfig.orientation; }
Jon Miranda6bed3502017-09-19 14:43:17 -07001215
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001216 @Override
1217 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001218 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001219 super.onNewIntent(intent);
1220
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001221 boolean alreadyOnHome = hasWindowFocus() && ((intent.getFlags() &
Winson15f8b172015-08-19 11:02:39 -07001222 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1223 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001224
1225 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001226 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001227 && AbstractFloatingView.getTopOpenView(this) == null;
Winson15f8b172015-08-19 11:02:39 -07001228 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001229 boolean internalStateHandled = InternalStateHandler
Sunny Goyal8b264562018-02-03 23:00:13 -08001230 .handleNewIntent(this, intent, isStarted());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001231
Winson15f8b172015-08-19 11:02:39 -07001232 if (isActionMain) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001233 if (!internalStateHandled) {
1234 // Note: There should be at most one log per method call. This is enforced
1235 // implicitly by using if-else statements.
1236 UserEventDispatcher ued = getUserEventDispatcher();
1237 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
1238 if (topOpenView != null) {
1239 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
1240 } else if (alreadyOnHome) {
1241 Target target = newContainerTarget(mStateManager.getState().containerType);
1242 target.pageIndex = mWorkspace.getCurrentPage();
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001243 ued.logActionCommand(Action.Command.HOME_INTENT, target,
1244 newContainerTarget(ContainerType.WORKSPACE));
Sunny Goyald3864fa2017-11-14 15:06:36 -08001245 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001246
Sunny Goyald3864fa2017-11-14 15:06:36 -08001247 // In all these cases, only animate if we're already on home
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001248 AbstractFloatingView.closeAllOpenViews(this, isStarted());
Jon Mirandafeba90f2016-10-06 10:53:29 -07001249
Sunny Goyala2467272018-03-16 14:53:05 -07001250 if (!isInState(NORMAL)) {
1251 // Only change state, if not already the same. This prevents cancelling any
1252 // animations running as part of resume
1253 mStateManager.goToState(NORMAL);
1254 }
Sunny Goyald3864fa2017-11-14 15:06:36 -08001255
1256 // Reset the apps view
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001257 if (!alreadyOnHome) {
Sunny Goyal7185dd62018-03-14 17:51:49 -07001258 mAppsView.reset(isStarted() /* animate */);
Sunny Goyald3864fa2017-11-14 15:06:36 -08001259 }
1260
Sunny Goyalaad33592018-08-07 17:20:35 -07001261 if (shouldMoveToDefaultScreen && !mWorkspace.isHandlingTouch()) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001262 mWorkspace.post(mWorkspace::moveToDefaultScreen);
1263 }
1264 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001265
1266 final View v = getWindow().peekDecorView();
1267 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001268 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001269 }
1270
Adam Cohen9211d422014-10-07 18:14:53 -07001271 if (mLauncherCallbacks != null) {
Sunny Goyal85462132018-04-24 11:39:37 -07001272 mLauncherCallbacks.onHomeIntent(internalStateHandled);
Adam Cohen9211d422014-10-07 18:14:53 -07001273 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001274 }
Winson15f8b172015-08-19 11:02:39 -07001275
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001276 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001277 }
1278
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001279 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001280 public void onRestoreInstanceState(Bundle state) {
1281 super.onRestoreInstanceState(state);
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001282 mWorkspace.restoreInstanceStateForChild(mSynchronouslyBoundPage);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001283 }
1284
1285 @Override
1286 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001287 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001288 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001289
Adam Cohen21cd0022013-10-09 18:57:02 -07001290 }
Sunny Goyalea609262017-10-25 15:47:38 -07001291 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001292
1293
1294 AbstractFloatingView widgets = AbstractFloatingView
1295 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1296 if (widgets != null) {
1297 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1298 widgets.saveHierarchyState(widgetsState);
1299 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1300 } else {
1301 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1302 }
1303
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001304 // We close any open folders and shortcut containers since they will not be re-opened,
1305 // and we need to make sure this state is reflected.
1306 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001307
Sunny Goyal2100c782016-08-22 16:00:03 -07001308 if (mPendingRequestArgs != null) {
1309 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1310 }
1311 if (mPendingActivityResult != null) {
1312 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001313 }
1314
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001315 super.onSaveInstanceState(outState);
1316
Adam Cohen9211d422014-10-07 18:14:53 -07001317 if (mLauncherCallbacks != null) {
1318 mLauncherCallbacks.onSaveInstanceState(outState);
1319 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001320 }
1321
1322 @Override
1323 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001324 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001325
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001326 unregisterReceiver(mScreenOffReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001327 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001328
Sunny Goyal5743f862019-03-28 15:35:32 -07001329 if (mCancelTouchController != null) {
1330 mCancelTouchController.run();
1331 mCancelTouchController = null;
1332 }
Tony Wickhama0068302018-04-11 16:16:11 -07001333
Winson Chungcd2b0142011-06-08 16:02:26 -07001334 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001335 // It's possible to receive onDestroy after a new Launcher activity has
1336 // been created. In this case, don't interfere with the new Launcher.
1337 if (mModel.isCurrentCallbacks(this)) {
1338 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001339 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001340 }
Sunny Goyal623eddd2018-03-02 12:24:41 -08001341 mRotationHelper.destroy();
Sunny Goyal745bad92016-05-02 10:54:12 -07001342
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001343 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001344 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001345 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001346 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001347 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001348
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001349 TextKeyListener.getInstance().release();
Tony2917a8b2017-07-31 23:29:42 -07001350 clearPendingBinds();
Hyunyoung Song31971a32019-01-27 12:31:12 -08001351 LauncherAppState.getIDP(this).removeOnChangeListener(this);
Adam Cohen9211d422014-10-07 18:14:53 -07001352 if (mLauncherCallbacks != null) {
1353 mLauncherCallbacks.onDestroy();
1354 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001355 }
1356
Sunny Goyalae502842016-06-17 08:43:56 -07001357 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1358 return mAccessibilityDelegate;
1359 }
1360
Adam Cohena9cf38f2011-05-02 15:36:58 -07001361 public DragController getDragController() {
1362 return mDragController;
1363 }
1364
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001365 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001366 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001367 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001368 }
1369
1370 @Override
1371 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1372 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001373 try {
1374 super.startIntentSenderForResult(intent, requestCode,
1375 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1376 } catch (IntentSender.SendIntentException e) {
1377 throw new ActivityNotFoundException();
1378 }
1379 }
1380
Winson Chung70d72102011-08-12 11:24:35 -07001381 /**
1382 * Indicates that we want global search for this activity by setting the globalSearch
1383 * argument for {@link #startSearch} to true.
1384 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001385 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001386 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001387 Bundle appSearchData, boolean globalSearch) {
1388 if (appSearchData == null) {
1389 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001390 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001391 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001392
Sunny Goyal6f28e712016-09-13 14:19:29 -07001393 if (mLauncherCallbacks == null ||
1394 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1395 // Starting search from the callbacks failed. Start the default global search.
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001396 super.startSearch(initialQuery, selectInitialQuery, appSearchData, true);
Ian Parkinson047036e2014-09-01 15:40:53 +01001397 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001398
1399 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001400 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001401 }
1402
Joe Onorato9c1289c2009-08-17 11:03:03 -04001403 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001404 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001405 }
1406
Adam Cohen517a7f52014-03-01 12:12:59 -08001407 public boolean isWorkspaceLoading() {
1408 return mWorkspaceLoading;
1409 }
1410
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001411 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001412 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001413 }
1414
Sunny Goyal04a324a2017-01-20 21:08:59 -08001415 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001416 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001417 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001418
Sunny Goyal2100c782016-08-22 16:00:03 -07001419 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001420 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001421 if (LOGD) {
1422 Log.d(TAG, "Adding widget from drop");
1423 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001424 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001425 }
1426
Sunny Goyal2100c782016-08-22 16:00:03 -07001427 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001428 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1429 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1430 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001431
Adam Cohenad4e15c2013-10-17 16:21:35 -07001432 Runnable onComplete = new Runnable() {
1433 @Override
1434 public void run() {
1435 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001436 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001437 }
1438 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001439 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001440 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001441 }
1442 }
Romain Guycbb89e42009-06-08 15:52:54 -07001443
Sunny Goyalefb7e842018-10-04 15:11:00 -07001444 public void addPendingItem(PendingAddItemInfo info, int container, int screenId,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001445 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001446 info.container = container;
1447 info.screenId = screenId;
1448 if (cell != null) {
1449 info.cellX = cell[0];
1450 info.cellY = cell[1];
1451 }
1452 info.spanX = spanX;
1453 info.spanY = spanY;
1454
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001455 switch (info.itemType) {
1456 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1457 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001458 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001459 break;
1460 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001461 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001462 break;
1463 default:
1464 throw new IllegalStateException("Unknown item type: " + info.itemType);
1465 }
1466 }
1467
Adam Cohenfbba09b2011-07-18 21:43:05 -07001468 /**
1469 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001470 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001471 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001472 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1473 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001474 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1475 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1476 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001477 }
1478
Adam Cohenfbba09b2011-07-18 21:43:05 -07001479 /**
1480 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001481 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001482 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001483 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001484 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001485 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001486 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001487 // In the case where we've prebound the widget, we remove it from the DragLayer
1488 if (LOGD) {
1489 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1490 }
1491 getDragLayer().removeView(hostView);
1492
Adam Cohened66b2b2012-01-23 17:28:51 -08001493 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001494 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001495
1496 // Clear the boundWidget so that it doesn't get destroyed.
1497 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001498 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001499 // In this case, we either need to start an activity to get permission to bind
1500 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001501 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1502 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1503 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1504 this, info.componentName);
1505 } else {
1506 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1507 }
Adam Cohendd70d662012-10-04 16:53:44 -07001508 Bundle options = info.bindOptions;
1509
Sunny Goyalffe83f12014-08-14 17:39:34 -07001510 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1511 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001512 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001513 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001514 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001515 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001516 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001517 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001518 }
1519
Sunny Goyalefb7e842018-10-04 15:11:00 -07001520 FolderIcon addFolder(CellLayout layout, int container, final int screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001521 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001522 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001523 folderInfo.title = getText(R.string.folder_name);
1524
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001525 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001526 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001527
1528 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001529 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301530 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001531 // Force measure the new folder icon
1532 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1533 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001534 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001535 }
Romain Guycbb89e42009-06-08 15:52:54 -07001536
Winsonc0b52fe2015-09-09 16:38:15 -07001537 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001538 * Unbinds the view for the specified item, and removes the item and all its children.
1539 *
1540 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001541 * @param itemInfo the {@link ItemInfo} for this view.
1542 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001543 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001544 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001545 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001546 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001547 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1548 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001549 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001550 } else {
1551 mWorkspace.removeWorkspaceItem(v);
1552 }
Winsonc0b52fe2015-09-09 16:38:15 -07001553 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001554 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001555 }
1556 } else if (itemInfo instanceof FolderInfo) {
1557 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001558 if (v instanceof FolderIcon) {
1559 ((FolderIcon) v).removeListeners();
1560 }
Winsonc0b52fe2015-09-09 16:38:15 -07001561 mWorkspace.removeWorkspaceItem(v);
1562 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001563 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001564 }
1565 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1566 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001567 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001568 if (deleteFromDb) {
Tony Wickham6a71a5b2018-08-21 11:40:23 -07001569 getModelWriter().deleteWidgetInfo(widgetInfo, getAppWidgetHost());
Winsonc0b52fe2015-09-09 16:38:15 -07001570 }
1571 } else {
1572 return false;
1573 }
1574 return true;
1575 }
1576
Tony Wickham6a71a5b2018-08-21 11:40:23 -07001577
Winsonc0b52fe2015-09-09 16:38:15 -07001578
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001579 @Override
1580 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001581 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001582 }
1583
Joe Onorato88ec0992009-11-19 13:16:06 -08001584 @Override
1585 public void onBackPressed() {
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08001586 if (finishAutoCancelActionMode()) {
1587 return;
1588 }
Adam Cohen9211d422014-10-07 18:14:53 -07001589 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1590 return;
1591 }
1592
Sunny Goyal45478022015-06-08 16:52:41 -07001593 if (mDragController.isDragging()) {
1594 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001595 return;
1596 }
1597
Jon Mirandafeba90f2016-10-06 10:53:29 -07001598 // Note: There should be at most one log per method call. This is enforced implicitly
1599 // by using if-else statements.
1600 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001601 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
Tony Wickham52c1b662018-05-21 13:13:58 -07001602 if (topView != null && topView.onBackPressed()) {
1603 // Handled by the floating view.
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001604 } else {
Sunny Goyale39690b2018-08-02 13:35:07 -07001605 mStateManager.getState().onBackPressed(this);
Michael Jurkaaf442092010-06-10 17:01:57 -07001606 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001607 }
1608
Sunny Goyalfe770c92016-09-27 14:38:58 -07001609 @TargetApi(Build.VERSION_CODES.M)
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001610 @Override
Jon Miranda73c27ec2018-05-16 18:06:23 -07001611 public ActivityOptions getActivityLaunchOptions(View v) {
1612 return mAppTransitionManager.getActivityLaunchOptions(this, v);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001613 }
1614
Tony Wickham3a6746a2018-03-29 09:39:56 -07001615 public LauncherAppTransitionManager getAppTransitionManager() {
1616 return mAppTransitionManager;
1617 }
1618
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001619 @TargetApi(Build.VERSION_CODES.M)
1620 @Override
1621 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
1622 // Due to legacy reasons, direct call shortcuts require Launchers to have the
1623 // corresponding permission. Show the appropriate permission prompt if that
1624 // is the case.
1625 if (intent.getComponent() == null
1626 && Intent.ACTION_CALL.equals(intent.getAction())
1627 && checkSelfPermission(android.Manifest.permission.CALL_PHONE) !=
1628 PackageManager.PERMISSION_GRANTED) {
1629
1630 setWaitingForResult(PendingRequestArgs
1631 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
1632 requestPermissions(new String[]{android.Manifest.permission.CALL_PHONE},
1633 REQUEST_PERMISSION_CALL_PHONE);
1634 return true;
1635 } else {
1636 return false;
1637 }
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001638 }
1639
Hyunyoung Song46d07f72018-05-22 15:41:25 -07001640 @Override
Hyunyoung Songfc007472018-10-25 14:09:50 -07001641 public int getCurrentState() {
1642 if(mStateManager.getState() == LauncherState.ALL_APPS) {
1643 return StatsLogUtils.LAUNCHER_STATE_ALLAPPS;
1644 } else if (mStateManager.getState() == LauncherState.OVERVIEW) {
1645 return StatsLogUtils.LAUNCHER_STATE_OVERVIEW;
1646 }
Hyunyoung Song0ae38882018-11-05 14:45:19 -08001647 return StatsLogUtils.LAUNCHER_STATE_HOME;
Hyunyoung Songfc007472018-10-25 14:09:50 -07001648 }
1649
1650 @Override
Hyunyoung Song46d07f72018-05-22 15:41:25 -07001651 public void modifyUserEvent(LauncherLogProto.LauncherEvent event) {
1652 if (event.srcTarget != null && event.srcTarget.length > 0 &&
1653 event.srcTarget[1].containerType == ContainerType.PREDICTION) {
1654 Target[] targets = new Target[3];
1655 targets[0] = event.srcTarget[0];
1656 targets[1] = event.srcTarget[1];
1657 targets[2] = newTarget(Target.Type.CONTAINER);
1658 event.srcTarget = targets;
1659 LauncherState state = mStateManager.getState();
1660 if (state == LauncherState.ALL_APPS) {
1661 event.srcTarget[2].containerType = ContainerType.ALLAPPS;
1662 } else if (state == LauncherState.OVERVIEW) {
1663 event.srcTarget[2].containerType = ContainerType.TASKSWITCHER;
1664 }
1665 }
1666 }
1667
Sunny Goyal369212a2019-03-26 15:03:57 -07001668 public boolean startActivitySafely(View v, Intent intent, ItemInfo item,
1669 @Nullable String sourceContainer) {
1670 boolean success = super.startActivitySafely(v, intent, item, sourceContainer);
Hyunyoung Song1241e612018-05-15 21:46:51 -07001671 if (success && v instanceof BubbleTextView) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001672 // This is set to the view that launched the activity that navigated the user away
1673 // from launcher. Since there is no callback for when the activity has finished
1674 // launching, enable the press state and keep this reference to reset the press
1675 // state when we return to launcher.
1676 BubbleTextView btv = (BubbleTextView) v;
1677 btv.setStayPressed(true);
1678 setOnResumeCallback(btv);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001679 }
Hyunyoung Song1241e612018-05-15 21:46:51 -07001680 return success;
Michael Jurka86a720e2012-05-09 11:23:23 -07001681 }
1682
Winson Chung3d503fb2011-07-13 17:25:49 -07001683 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001684 // TODO: Remove this method
Sunny Goyal876e4622018-11-02 13:50:40 -07001685 return mHotseat != null && (layout == mHotseat);
Winson Chung3d503fb2011-07-13 17:25:49 -07001686 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001687
Winson Chung3d503fb2011-07-13 17:25:49 -07001688 /**
1689 * Returns the CellLayout of the specified container at the specified screen.
1690 */
Sunny Goyalefb7e842018-10-04 15:11:00 -07001691 public CellLayout getCellLayout(int container, int screenId) {
Sunny Goyal876e4622018-11-02 13:50:40 -07001692 return (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1693 ? mHotseat : mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08001694 }
1695
Michael Jurkae326f182011-11-21 14:05:46 -08001696 @Override
1697 public void onTrimMemory(int level) {
1698 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07001699 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
1700 // The widget preview db can result in holding onto over
1701 // 3MB of memory for caching which isn't necessary.
1702 SQLiteDatabase.releaseMemory();
1703
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08001704 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07001705 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08001706 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01001707 if (mLauncherCallbacks != null) {
1708 mLauncherCallbacks.onTrimMemory(level);
1709 }
Winson Chung62a70be2018-02-23 15:10:05 -08001710 UiFactory.onTrimMemory(this, level);
Michael Jurkae326f182011-11-21 14:05:46 -08001711 }
1712
Michael Jurkad7c28052012-04-27 15:43:36 -07001713 @Override
1714 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07001715 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07001716 final List<CharSequence> text = event.getText();
1717 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07001718 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001719 // TODO: When can workspace be null?
1720 text.add(mWorkspace == null
1721 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07001722 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07001723 return result;
1724 }
1725
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001726 public void setOnResumeCallback(OnResumeCallback callback) {
1727 if (mOnResumeCallback != null) {
1728 mOnResumeCallback.onLauncherResume();
1729 }
1730 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07001731 }
1732
Michael Jurka7607c2f2013-04-03 14:33:19 -07001733 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001734 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001735 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07001736 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001737 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07001738 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001739 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07001740 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07001741 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07001742 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001743 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001744
Joe Onorato9c1289c2009-08-17 11:03:03 -04001745 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07001746 * Clear any pending bind callbacks. This is called when is loader is planning to
1747 * perform a full rebind from scratch.
1748 */
1749 @Override
1750 public void clearPendingBinds() {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001751 if (mPendingExecutor != null) {
1752 mPendingExecutor.markCompleted();
1753 mPendingExecutor = null;
1754 }
1755 }
1756
1757 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001758 * Refreshes the shortcuts shown on the workspace.
1759 *
1760 * Implementation of the method from LauncherModel.Callbacks.
1761 */
1762 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001763 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001764 // Floating panels (except the full widget sheet) are associated with individual icons. If
1765 // we are starting a fresh bind, close all such panels as all the icons are about
1766 // to go away.
1767 AbstractFloatingView.closeOpenViews(this, true,
Sunny Goyal7ede6112017-12-05 15:11:21 -08001768 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_REBIND_SAFE);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08001769
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001770 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08001771
Winson Chungd64d1762013-08-20 14:37:16 -07001772 // Clear the workspace because it's going to be rebound
Sunny Goyal2db53422019-03-01 16:06:12 -08001773 mDragController.cancelDrag();
1774
Adam Cohendf035382011-04-11 17:22:04 -07001775 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07001776 mWorkspace.removeAllWorkspaceScreens();
Sunny Goyalc11fac32018-02-27 19:20:15 -08001777 mAppWidgetHost.clearViews();
Winson Chungd64d1762013-08-20 14:37:16 -07001778
Winson Chung3d503fb2011-07-13 17:25:49 -07001779 if (mHotseat != null) {
Sunny Goyal11e96492018-03-23 17:06:00 -07001780 mHotseat.resetLayout(mDeviceProfile.isVerticalBarLayout());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001781 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001782 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001783 }
1784
Adam Cohendcd297f2013-06-18 13:13:40 -07001785 @Override
Sunny Goyalefb7e842018-10-04 15:11:00 -07001786 public void bindScreens(IntArray orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001787 // Make sure the first screen is always at the start.
Jon Miranda7143ba62019-03-15 09:00:05 -07001788 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001789 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalefb7e842018-10-04 15:11:00 -07001790 orderedScreenIds.removeValue(Workspace.FIRST_SCREEN_ID);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001791 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Jon Miranda7143ba62019-03-15 09:00:05 -07001792 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001793 // If there are no screens, we need to have an empty screen
1794 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07001795 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001796 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07001797
Winsonc7d2e832016-07-28 12:24:55 -07001798 // After we have added all the screens, if the wallpaper was locked to the default state,
1799 // then notify to indicate that it can be released and a proper wallpaper offset can be
1800 // computed before the next layout
1801 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07001802 }
1803
Sunny Goyalefb7e842018-10-04 15:11:00 -07001804 private void bindAddScreens(IntArray orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001805 int count = orderedScreenIds.size();
1806 for (int i = 0; i < count; i++) {
Sunny Goyalefb7e842018-10-04 15:11:00 -07001807 int screenId = orderedScreenIds.get(i);
Jon Miranda7143ba62019-03-15 09:00:05 -07001808 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001809 // No need to bind the first screen, as its always bound.
1810 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
1811 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001812 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001813 }
1814
Sunny Goyalb23980c2017-08-17 07:45:25 -07001815 @Override
Tony Wickham6a71a5b2018-08-21 11:40:23 -07001816 public void preAddApps() {
1817 // If there's an undo snackbar, force it to complete to ensure empty screens are removed
1818 // before trying to add new items.
1819 mModelWriter.commitDelete();
1820 AbstractFloatingView snackbar = AbstractFloatingView.getOpenView(this, TYPE_SNACKBAR);
1821 if (snackbar != null) {
1822 snackbar.post(() -> snackbar.close(true));
1823 }
1824 }
1825
1826 @Override
Sunny Goyalefb7e842018-10-04 15:11:00 -07001827 public void bindAppsAdded(IntArray newScreens, ArrayList<ItemInfo> addNotAnimated,
Sunny Goyal29947f02017-12-18 13:49:44 -08001828 ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07001829 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08001830 if (newScreens != null) {
1831 bindAddScreens(newScreens);
1832 }
Winson Chungd64d1762013-08-20 14:37:16 -07001833
1834 // We add the items without animation on non-visible pages, and with
1835 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08001836 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001837 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001838 }
Adam Cohen76a47a12014-02-05 11:47:43 -08001839 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001840 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07001841 }
Winson Chungc58497e2013-09-03 17:48:37 -07001842
Winson Chung87412982013-10-03 18:34:14 -07001843 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07001844 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001845 }
1846
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001847 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001848 * Bind the items start-end from the list.
1849 *
1850 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001851 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07001852 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07001853 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001854 // Get the list of added items and intersect them with the set of items here
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001855 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07001856 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001857 Workspace workspace = mWorkspace;
Sunny Goyalefb7e842018-10-04 15:11:00 -07001858 int newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07001859 int end = items.size();
1860 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001861 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07001862
1863 // Short circuit if we are loading dock items for a configuration which has no dock
1864 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
1865 mHotseat == null) {
1866 continue;
1867 }
1868
Sunny Goyal639e9062015-08-19 19:17:06 -07001869 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001870 switch (item.itemType) {
1871 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1872 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08001873 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001874 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07001875 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001876 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001877 }
1878 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07001879 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001880 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001881 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001882 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001883 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07001884 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1885 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07001886 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07001887 if (view == null) {
1888 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001889 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08001890 break;
1891 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001892 default:
1893 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001894 }
Sunny Goyal639e9062015-08-19 19:17:06 -07001895
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001896 /*
1897 * Remove colliding items.
1898 */
1899 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1900 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
1901 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
1902 View v = cl.getChildAt(item.cellX, item.cellY);
1903 Object tag = v.getTag();
1904 String desc = "Collision while binding workspace item: " + item
1905 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08001906 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001907 throw (new RuntimeException(desc));
1908 } else {
1909 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001910 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001911 continue;
1912 }
1913 }
1914 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05301915 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07001916 if (animateIcons) {
1917 // Animate all the applications up now
1918 view.setAlpha(0f);
1919 view.setScaleX(0f);
1920 view.setScaleY(0f);
1921 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08001922 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07001923 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001924 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07001925
Sunny Goyalf0b6db72018-08-13 16:10:14 -07001926 // Animate to the correct page
1927 if (animateIcons && newItemsScreenId > -1) {
1928 AnimatorSet anim = new AnimatorSet();
1929 anim.playTogether(bounceAnims);
Jon Miranda64eb8ea2018-03-29 11:43:58 -07001930
Sunny Goyalefb7e842018-10-04 15:11:00 -07001931 int currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyalf0b6db72018-08-13 16:10:14 -07001932 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
1933 final Runnable startBounceAnimRunnable = anim::start;
1934
1935 if (newItemsScreenId != currentScreenId) {
1936 // We post the animation slightly delayed to prevent slowdowns
1937 // when we are loading right after we return to launcher.
1938 mWorkspace.postDelayed(new Runnable() {
1939 public void run() {
1940 if (mWorkspace != null) {
1941 AbstractFloatingView.closeAllOpenViews(Launcher.this, false);
1942
1943 mWorkspace.snapToPage(newScreenIndex);
1944 mWorkspace.postDelayed(startBounceAnimRunnable,
1945 NEW_APPS_ANIMATION_DELAY);
Winson Chung94d67682013-09-25 16:29:40 -07001946 }
Sunny Goyalf0b6db72018-08-13 16:10:14 -07001947 }
1948 }, NEW_APPS_PAGE_MOVE_DELAY);
1949 } else {
1950 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07001951 }
Winson Chung64359a52013-07-08 17:17:08 -07001952 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001953 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001954 }
1955
Joe Onorato9c1289c2009-08-17 11:03:03 -04001956 /**
1957 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001958 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07001959 public void bindAppWidget(LauncherAppWidgetInfo item) {
1960 View view = inflateAppWidget(item);
1961 if (view != null) {
1962 mWorkspace.addInScreen(view, item);
1963 mWorkspace.requestLayout();
1964 }
1965 }
1966
1967 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001968 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05301969 PendingAppWidgetHostView view =
1970 new PendingAppWidgetHostView(this, item, mIconCache, true);
1971 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07001972 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001973 }
1974
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001975 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001976
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001977 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08001978
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001979 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
1980 // If the provider is not ready, bind as a pending widget.
1981 appWidgetInfo = null;
1982 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
1983 // The widget id is not valid. Try to find the widget based on the provider info.
1984 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
1985 } else {
1986 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
1987 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07001988
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001989 // If the provider is ready, but the width is not yet restored, try to restore it.
1990 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
1991 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07001992 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001993 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
1994 + " belongs to component " + item.providerName
1995 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001996 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07001997 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07001998 }
Sunny Goyalff572272014-07-23 13:58:07 -07001999
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002000 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002001 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07002002 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2003 // Id has not been allocated yet. Allocate a new id.
2004 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
2005 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07002006
Sunny Goyald478c832016-04-01 12:04:16 -07002007 // Also try to bind the widget. If the bind fails, the user will be shown
2008 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08002009 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07002010 pendingInfo.spanX = item.spanX;
2011 pendingInfo.spanY = item.spanY;
2012 pendingInfo.minSpanX = item.minSpanX;
2013 pendingInfo.minSpanY = item.minSpanY;
2014 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07002015
2016 boolean isDirectConfig =
2017 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2018 if (isDirectConfig && item.bindOptions != null) {
2019 Bundle newOptions = item.bindOptions.getExtras();
2020 if (options != null) {
2021 newOptions.putAll(options);
2022 }
2023 options = newOptions;
2024 }
Sunny Goyald478c832016-04-01 12:04:16 -07002025 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2026 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002027
Sunny Goyal86df1382016-08-10 15:03:22 -07002028 // We tried to bind once. If we were not able to bind, we would need to
2029 // go through the permission dialog, which means we cannot skip the config
2030 // activity.
2031 item.bindOptions = null;
2032 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2033
Sunny Goyald478c832016-04-01 12:04:16 -07002034 // Bind succeeded
2035 if (success) {
2036 // If the widget has a configure activity, it is still needs to set it up,
2037 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002038 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002039 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2040 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002041 }
Sunny Goyald478c832016-04-01 12:04:16 -07002042
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002043 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002044 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002045 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002046 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002047 // The widget was marked as UI not ready, but there is no configure activity to
2048 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002049 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002050 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002051 }
Sunny Goyalff572272014-07-23 13:58:07 -07002052 }
2053
Sunny Goyald5462aa2016-11-22 16:52:39 +05302054 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002055 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002056 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002057 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002058 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Tony Wickham6a71a5b2018-08-21 11:40:23 -07002059 getModelWriter().deleteWidgetInfo(item, getAppWidgetHost());
Sunny Goyalb23980c2017-08-17 07:45:25 -07002060 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002061 }
2062
Sunny Goyal233ee962015-08-03 13:05:01 -07002063 item.minSpanX = appWidgetInfo.minSpanX;
2064 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302065 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002066 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302067 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002068 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302069 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002070
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002071 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002072 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002073 }
2074
Sunny Goyalff572272014-07-23 13:58:07 -07002075 /**
2076 * Restores a pending widget.
2077 *
2078 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002079 */
Sunny Goyald478c832016-04-01 12:04:16 -07002080 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002081 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2082 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2083 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002084 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002085 }
2086
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002087 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002088 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002089 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2090 info.pendingItemInfo = null;
2091 }
Sunny Goyalff572272014-07-23 13:58:07 -07002092
Sunny Goyalba47faa2017-10-04 16:50:57 -07002093 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002094 view.reInflate();
Sunny Goyalba47faa2017-10-04 16:50:57 -07002095 }
2096
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002097 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002098 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002099 }
2100
Adam Cohen1462de32012-07-24 22:34:36 -07002101 public void onPageBoundSynchronously(int page) {
Sunny Goyalf4aceab2017-12-06 16:16:31 -08002102 mSynchronouslyBoundPage = page;
Adam Cohen1462de32012-07-24 22:34:36 -07002103 }
2104
Sunny Goyal527c7d32015-08-28 15:19:36 -07002105 @Override
2106 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2107 if (mPendingExecutor != null) {
2108 mPendingExecutor.markCompleted();
2109 }
2110 mPendingExecutor = executor;
Sunny Goyal60180b02018-02-07 12:56:30 -08002111 if (!isInState(ALL_APPS)) {
2112 mAppsView.getAppsStore().setDeferUpdates(true);
2113 mPendingExecutor.execute(() -> mAppsView.getAppsStore().setDeferUpdates(false));
2114 }
2115
Sunny Goyal527c7d32015-08-28 15:19:36 -07002116 executor.attachTo(this);
2117 }
2118
2119 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2120 if (mPendingExecutor == executor) {
2121 mPendingExecutor = null;
2122 }
2123 }
2124
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002125 @Override
2126 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
Sunny Goyal6001ea22018-05-10 16:31:00 -07002127 AlphaProperty property = mDragLayer.getAlphaProperty(ALPHA_INDEX_LAUNCHER_LOAD);
2128 if (property.getValue() < 1) {
2129 ObjectAnimator anim = ObjectAnimator.ofFloat(property, MultiValueAlpha.VALUE, 1);
2130 if (executor != null) {
2131 anim.addListener(new AnimatorListenerAdapter() {
2132 @Override
2133 public void onAnimationEnd(Animator animation) {
2134 executor.onLoadAnimationCompleted();
2135 }
2136 });
2137 }
2138 anim.start();
Sunny Goyal29947f02017-12-18 13:49:44 -08002139 } else if (executor != null) {
2140 executor.onLoadAnimationCompleted();
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002141 }
2142 }
2143
Joe Onorato9c1289c2009-08-17 11:03:03 -04002144 /**
2145 * Callback saying that there aren't any more items to bind.
2146 *
2147 * Implementation of the method from LauncherModel.Callbacks.
2148 */
Tonyf80e8932018-12-21 11:58:04 -08002149 public void finishBindingItems(int pageBoundFirst) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002150 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002151 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002152
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002153 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002154
Sunny Goyal2100c782016-08-22 16:00:03 -07002155 if (mPendingActivityResult != null) {
2156 handleActivityResult(mPendingActivityResult.requestCode,
2157 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2158 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002159 }
2160
Sunny Goyala474a9b2017-05-04 16:47:11 -07002161 InstallShortcutReceiver.disableAndFlushInstallQueue(
2162 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002163
Tonyf80e8932018-12-21 11:58:04 -08002164 // When undoing the removal of the last item on a page, return to that page.
2165 mWorkspace.setCurrentPage(pageBoundFirst);
Tony Wickham6a71a5b2018-08-21 11:40:23 -07002166
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002167 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002168 }
2169
Winson Chunga2413752012-04-03 14:22:34 -07002170 private boolean canRunNewAppsAnimation() {
2171 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002172 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002173 }
2174
Winson Chungc9168342013-06-26 14:54:55 -07002175 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002176 ValueAnimator bounceAnim = new PropertyListBuilder().alpha(1).scale(1).build(v)
2177 .setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
Winson Chungc9168342013-06-26 14:54:55 -07002178 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002179 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002180 return bounceAnim;
2181 }
2182
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002183 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002184 * Add the icons for all apps.
2185 *
2186 * Implementation of the method from LauncherModel.Callbacks.
2187 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002188 public void bindAllApplications(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002189 mAppsView.getAppsStore().setApps(apps);
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002190
Adam Cohen9211d422014-10-07 18:14:53 -07002191 if (mLauncherCallbacks != null) {
2192 mLauncherCallbacks.bindAllApplications(apps);
2193 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002194 }
2195
2196 /**
Zak Cohen658c67a2018-10-19 14:21:05 -07002197 * Copies LauncherModel's map of activities to shortcut counts to Launcher's. This is necessary
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002198 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2199 */
2200 @Override
Zak Cohen658c67a2018-10-19 14:21:05 -07002201 public void bindDeepShortcutMap(HashMap<ComponentKey, Integer> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002202 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002203 }
2204
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002205 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002206 * A package was updated.
2207 *
2208 * Implementation of the method from LauncherModel.Callbacks.
2209 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002210 @Override
2211 public void bindAppsAddedOrUpdated(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002212 mAppsView.getAppsStore().addOrUpdateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002213 }
2214
Sunny Goyal4390ace2014-10-13 11:33:11 -07002215 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002216 public void bindPromiseAppProgressUpdated(PromiseAppInfo app) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002217 mAppsView.getAppsStore().updatePromiseAppProgress(app);
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002218 }
2219
2220 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002221 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002222 mWorkspace.widgetsRestored(widgets);
2223 }
2224
Joe Onorato9c1289c2009-08-17 11:03:03 -04002225 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002226 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002227 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002228 *
2229 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002230 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002231 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002232 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002233 if (!updated.isEmpty()) {
2234 mWorkspace.updateShortcuts(updated);
2235 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002236 }
2237
2238 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002239 * Update the state of a package, typically related to install state.
2240 *
2241 * Implementation of the method from LauncherModel.Callbacks.
2242 */
Sunny Goyale755d462014-07-22 13:48:29 -07002243 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002244 public void bindRestoreItemsChange(HashSet<ItemInfo> updates) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07002245 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07002246 }
2247
2248 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002249 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07002250 * in addition to specific applications to remove, the reason being that
2251 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002252 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07002253 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002254 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07002255 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002256 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002257 mWorkspace.removeItemsByMatcher(matcher);
2258 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002259 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002260
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002261 @Override
2262 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002263 mAppsView.getAppsStore().removeApps(appInfos);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002264 }
Joe Onoratobe386092009-11-17 17:32:16 -08002265
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002266 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002267 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
2268 mPopupDataProvider.setAllWidgets(allWidgets);
Tony Wickham26b17462017-03-20 17:12:24 -07002269 }
2270
Tony Wickham86222d22017-03-29 15:30:43 -07002271 /**
2272 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
2273 * refreshes the widgets and shortcuts associated with the given package/user
2274 */
2275 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07002276 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002277 }
2278
Winson Chung80baf5a2010-08-09 16:03:15 -07002279 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002280 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08002281 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002282 @Override
2283 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
2284 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002285
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002286 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
2287 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07002288 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002289 writer.println(prefix + " Homescreen " + i);
2290
2291 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
2292 for (int j = 0; j < layout.getChildCount(); j++) {
2293 Object tag = layout.getChildAt(j).getTag();
2294 if (tag != null) {
2295 writer.println(prefix + " " + tag.toString());
2296 }
2297 }
2298 }
2299
2300 writer.println(prefix + " Hotseat");
Sunny Goyal876e4622018-11-02 13:50:40 -07002301 ViewGroup layout = mHotseat.getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07002302 for (int j = 0; j < layout.getChildCount(); j++) {
2303 Object tag = layout.getChildAt(j).getTag();
2304 if (tag != null) {
2305 writer.println(prefix + " " + tag.toString());
2306 }
Adam Cohen4caf2982013-08-20 18:54:31 -07002307 }
Sunny Goyala1365452015-10-01 15:46:24 -07002308 }
2309
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002310 writer.println(prefix + "Misc:");
2311 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
2312 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
2313 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
Sunny Goyale43d00d2018-05-14 14:23:18 -07002314 writer.println(" mRotationHelper: " + mRotationHelper);
Sunny Goyalcb2c5052018-10-15 12:30:29 -07002315 // Extra logging for b/116853349
2316 mDragLayer.dumpAlpha(writer);
Sunny Goyale43d00d2018-05-14 14:23:18 -07002317 dumpMisc(writer);
Sunny Goyal2bba1902018-02-27 12:20:50 -08002318
2319 try {
2320 FileLog.flushAll(writer);
2321 } catch (Exception e) {
2322 // Ignore
2323 }
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002324
2325 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002326
Adam Cohen9211d422014-10-07 18:14:53 -07002327 if (mLauncherCallbacks != null) {
2328 mLauncherCallbacks.dump(prefix, fd, writer, args);
2329 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002330 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002331
Sunny Goyal66b24572016-09-21 15:57:55 -07002332 @Override
Sunny Goyal66b24572016-09-21 15:57:55 -07002333 public void onProvideKeyboardShortcuts(
2334 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
2335
2336 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002337 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002338 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
2339 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
Vadim Tryshev74c261c2018-06-12 13:44:43 -07002340 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.widget_button_text),
2341 KeyEvent.KEYCODE_W, KeyEvent.META_CTRL_ON));
Sunny Goyal66b24572016-09-21 15:57:55 -07002342 }
Vadim Tryshev3455f8d2018-05-11 20:02:20 -07002343 final View currentFocus = getCurrentFocus();
2344 if (currentFocus != null) {
2345 if (new CustomActionsPopup(this, currentFocus).canShow()) {
2346 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
2347 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
2348 }
2349 if (currentFocus.getTag() instanceof ItemInfo
2350 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
2351 shortcutInfos.add(new KeyboardShortcutInfo(
2352 getString(R.string.shortcuts_menu_with_notifications_description),
2353 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
2354 }
Sunny Goyal66b24572016-09-21 15:57:55 -07002355 }
2356 if (!shortcutInfos.isEmpty()) {
2357 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
2358 }
2359
2360 super.onProvideKeyboardShortcuts(data, menu, deviceId);
2361 }
2362
2363 @Override
2364 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
2365 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
2366 switch (keyCode) {
2367 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002368 if (isInState(NORMAL)) {
2369 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07002370 return true;
2371 }
2372 break;
2373 case KeyEvent.KEYCODE_S: {
2374 View focusedView = getCurrentFocus();
2375 if (focusedView instanceof BubbleTextView
2376 && focusedView.getTag() instanceof ItemInfo
2377 && mAccessibilityDelegate.performAction(focusedView,
2378 (ItemInfo) focusedView.getTag(),
2379 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal00ac9202017-11-09 15:24:06 -08002380 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07002381 return true;
2382 }
2383 break;
2384 }
2385 case KeyEvent.KEYCODE_O:
2386 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
2387 return true;
2388 }
2389 break;
Vadim Tryshev74c261c2018-06-12 13:44:43 -07002390 case KeyEvent.KEYCODE_W:
2391 if (isInState(NORMAL)) {
2392 OptionsPopupView.openWidgets(this);
2393 return true;
2394 }
2395 break;
Sunny Goyal66b24572016-09-21 15:57:55 -07002396 }
2397 }
2398 return super.onKeyShortcut(keyCode, event);
2399 }
2400
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002401 @Override
2402 public boolean onKeyUp(int keyCode, KeyEvent event) {
2403 if (keyCode == KeyEvent.KEYCODE_MENU) {
2404 // KEYCODE_MENU is sent by some tests, for example
2405 // LauncherJankTests#testWidgetsContainerFling. Don't just remove its handling.
2406 if (!mDragController.isDragging() && !mWorkspace.isSwitchingState() &&
2407 isInState(NORMAL)) {
2408 // Close any open floating views.
2409 AbstractFloatingView.closeAllOpenViews(this);
2410
2411 // Setting the touch point to (-1, -1) will show the options popup in the center of
2412 // the screen.
Sunny Goyal2fd7a8b2018-03-30 17:10:13 -07002413 OptionsPopupView.showDefaultOptions(this, -1, -1);
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002414 }
2415 return true;
2416 }
2417 return super.onKeyUp(keyCode, event);
2418 }
2419
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002420 public static Launcher getLauncher(Context context) {
Sunny Goyal87b5eb62018-07-03 15:53:39 -07002421 return (Launcher) fromContext(context);
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002422 }
2423
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002424 /**
Sunny Goyalab770a12018-11-14 15:17:26 -08002425 * Just a wrapper around the type cast to allow easier tracking of calls.
2426 */
2427 public static <T extends Launcher> T cast(ActivityContext activityContext) {
2428 return (T) activityContext;
2429 }
2430
2431 /**
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002432 * Callback for listening for onResume
2433 */
2434 public interface OnResumeCallback {
2435
2436 void onLauncherResume();
2437 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002438}