blob: 40eb912df5b0ce8cc51d60dd48a97600aeb4dea0 [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;
Govinda Wassermanc06e1512019-04-09 09:56:53 -040027import static com.android.launcher3.LauncherState.OVERVIEW;
28import static com.android.launcher3.LauncherState.OVERVIEW_PEEK;
Sunny Goyal6001ea22018-05-10 16:31:00 -070029import static com.android.launcher3.dragndrop.DragLayer.ALPHA_INDEX_LAUNCHER_LOAD;
Sunny Goyalaeb16432017-10-16 11:46:41 -070030import static com.android.launcher3.logging.LoggerUtils.newContainerTarget;
Hyunyoung Song46d07f72018-05-22 15:41:25 -070031import static com.android.launcher3.logging.LoggerUtils.newTarget;
vadimt00d42552018-12-11 17:59:36 -080032import static com.android.launcher3.util.RaceConditionTracker.ENTER;
33import static com.android.launcher3.util.RaceConditionTracker.EXIT;
Sunny Goyal326403e2017-10-02 12:45:10 -070034
Gilles Debunnedd6c9922010-10-25 11:23:41 -070035import android.animation.Animator;
Sunny Goyal6001ea22018-05-10 16:31:00 -070036import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070037import android.animation.AnimatorSet;
Sunny Goyal6001ea22018-05-10 16:31:00 -070038import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070039import android.animation.ValueAnimator;
Adam Cohen0b395352014-06-09 22:54:36 +000040import android.annotation.TargetApi;
Tony Wickham005df0b2018-02-13 11:28:12 -080041import android.app.ActivityOptions;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080042import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070043import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080045import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080046import android.content.ComponentCallbacks2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070049import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070050import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070051import android.content.SharedPreferences;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052import android.content.pm.PackageManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080053import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070054import android.database.sqlite.SQLiteDatabase;
Sunny Goyal59d086c2018-05-07 17:31:40 -070055import android.graphics.Point;
Sunny Goyalae6e3182019-04-30 12:04:37 -070056import android.graphics.Rect;
Adam Cohen0f668f32014-09-08 19:54:17 +020057import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058import android.os.Bundle;
Hyunyoung Songbd6fba92018-05-16 15:54:31 -070059import android.os.Handler;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070060import android.os.Parcelable;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080061import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070062import android.os.StrictMode;
Michael Jurkaa33411c2012-06-14 16:18:21 -070063import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070065import android.util.Log;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070066import android.util.SparseArray;
Sunny Goyal59d086c2018-05-07 17:31:40 -070067import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070068import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070069import android.view.KeyboardShortcutGroup;
70import android.view.KeyboardShortcutInfo;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080071import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -070072import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070073import android.view.View;
Adam Cohen96d30a12013-07-16 18:13:21 -070074import android.view.ViewGroup;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080075import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070076import android.view.animation.OvershootInterpolator;
Adam Cohen96d30a12013-07-16 18:13:21 -070077import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070078
Tony Wickham03f27012019-04-25 14:22:54 -070079import androidx.annotation.Nullable;
80
Sunny Goyal651077b2014-06-30 14:15:31 -070081import com.android.launcher3.DropTarget.DragObject;
Sunny Goyalae502842016-06-17 08:43:56 -070082import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070083import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070084import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal7ede6112017-12-05 15:11:21 -080085import com.android.launcher3.allapps.DiscoveryBounce;
Sunny Goyalf0b6db72018-08-13 16:10:14 -070086import com.android.launcher3.anim.PropertyListBuilder;
Sunny Goyalffe83f12014-08-14 17:39:34 -070087import com.android.launcher3.compat.AppWidgetManagerCompat;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -070088import com.android.launcher3.compat.LauncherAppsCompatVO;
Tony Wickhame0c33232016-02-08 11:37:04 -080089import com.android.launcher3.config.FeatureFlags;
vadimt00d42552018-12-11 17:59:36 -080090import com.android.launcher3.dot.DotInfo;
Vadim Tryshevfedca432015-08-19 17:55:02 -070091import com.android.launcher3.dragndrop.DragController;
92import com.android.launcher3.dragndrop.DragLayer;
93import com.android.launcher3.dragndrop.DragView;
Jon Mirandaaeb4dd02018-07-11 14:16:23 -070094import com.android.launcher3.folder.Folder;
Sunny Goyal26119432016-02-18 22:09:23 +000095import com.android.launcher3.folder.FolderIcon;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070096import com.android.launcher3.folder.FolderIconPreviewVerifier;
Sunny Goyalae6e3182019-04-30 12:04:37 -070097import com.android.launcher3.graphics.RotationMode;
Sunny Goyalf840f102018-09-21 14:41:05 -070098import com.android.launcher3.icons.IconCache;
Sunny Goyal66b24572016-09-21 15:57:55 -070099import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700100import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700101import com.android.launcher3.logging.FileLog;
Hyunyoung Songfc007472018-10-25 14:09:50 -0700102import com.android.launcher3.logging.StatsLogUtils;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700103import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700104import com.android.launcher3.logging.UserEventDispatcher.UserEventDelegate;
Sunny Goyal369212a2019-03-26 15:03:57 -0700105import com.android.launcher3.model.AppLaunchTracker;
Sunny Goyala535ae42017-02-27 10:07:13 -0800106import com.android.launcher3.model.ModelWriter;
Sunny Goyala535ae42017-02-27 10:07:13 -0800107import com.android.launcher3.notification.NotificationListener;
Tony Wickham540913e2017-01-23 11:47:51 -0800108import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -0800109import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700110import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyalf8088ee2017-11-10 14:52:00 -0800111import com.android.launcher3.states.InternalStateHandler;
Sunny Goyal623eddd2018-03-02 12:24:41 -0800112import com.android.launcher3.states.RotationHelper;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800113import com.android.launcher3.touch.ItemClickHandler;
Sunny Goyala5ace712017-11-15 17:12:51 -0800114import com.android.launcher3.uioverrides.UiFactory;
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700115import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530116import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
117import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
Sunny Goyalaeb16432017-10-16 11:46:41 -0700118import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyal2100c782016-08-22 16:00:03 -0700119import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700120import com.android.launcher3.util.ComponentKey;
Sunny Goyalefb7e842018-10-04 15:11:00 -0700121import com.android.launcher3.util.IntArray;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700122import com.android.launcher3.util.ItemInfoMatcher;
Sunny Goyal6001ea22018-05-10 16:31:00 -0700123import com.android.launcher3.util.MultiValueAlpha;
124import com.android.launcher3.util.MultiValueAlpha.AlphaProperty;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700125import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800126import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700127import com.android.launcher3.util.PendingRequestArgs;
vadimt00d42552018-12-11 17:59:36 -0800128import com.android.launcher3.util.RaceConditionTracker;
Sunny Goyal8392c822017-06-20 10:03:56 -0700129import com.android.launcher3.util.SystemUiController;
Mario Bertschlera6936942017-05-31 14:48:19 -0700130import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700131import com.android.launcher3.util.Thunk;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700132import com.android.launcher3.util.TraceHelper;
Sunny Goyal326403e2017-10-02 12:45:10 -0700133import com.android.launcher3.util.UiThreadHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700134import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyalab770a12018-11-14 15:17:26 -0800135import com.android.launcher3.views.ActivityContext;
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800136import com.android.launcher3.views.OptionsPopupView;
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400137import com.android.launcher3.views.ScrimView;
Sunny Goyal29947f02017-12-18 13:49:44 -0800138import com.android.launcher3.widget.LauncherAppWidgetHostView;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800139import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700140import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal29947f02017-12-18 13:49:44 -0800141import com.android.launcher3.widget.PendingAppWidgetHostView;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800142import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700143import com.android.launcher3.widget.WidgetHostViewLoader;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700144import com.android.launcher3.widget.WidgetListRowEntry;
145import com.android.launcher3.widget.WidgetsFullSheet;
Sunny Goyal952e63d2017-08-16 04:59:08 -0700146import com.android.launcher3.widget.custom.CustomWidgetParser;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700147
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700148import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700149import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700150import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700151import java.util.Collection;
Zak Cohen658c67a2018-10-19 14:21:05 -0700152import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700153import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700154import java.util.List;
Sunny Goyalaae6fbb2019-01-31 16:05:58 -0800155import java.util.function.Predicate;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700156
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157/**
158 * Default launcher application.
159 */
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700160public class Launcher extends BaseDraggingActivity implements LauncherExterns,
Hyunyoung Song31971a32019-01-27 12:31:12 -0800161 LauncherModel.Callbacks, LauncherProviderChangeListener, UserEventDelegate,
162 InvariantDeviceProfile.OnIDPChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700163 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700164 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165
Winson Chunga2413752012-04-03 14:22:34 -0700166 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700167
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700169 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700170
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700171 private static final int REQUEST_PICK_APPWIDGET = 9;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172
Michael Jurka8b805b12012-04-18 14:23:14 -0700173 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800174 public static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
175 public static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700176
Jon Mirandac476d6e2017-05-04 16:30:01 -0700177 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700178
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700179 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
180
Mathew Inwood876a8462013-06-14 14:12:41 +0100181 /**
182 * IntentStarter uses request codes starting with this. This must be greater than all activity
183 * request codes used internally.
184 */
185 protected static final int REQUEST_LAST = 100;
186
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 // Type: int
188 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700189 // Type: int
190 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700191 // Type: PendingRequestArgs
192 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
Sunny Goyaldedda052019-05-14 15:23:48 -0700193 // Type: int
194 private static final String RUNTIME_STATE_PENDING_REQUEST_CODE = "launcher.request_code";
Sunny Goyal2100c782016-08-22 16:00:03 -0700195 // Type: ActivityResultInfo
196 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700197 // Type: SparseArray<Parcelable>
198 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
vadimt00d42552018-12-11 17:59:36 -0800199 public static final String ON_CREATE_EVT = "Launcher.onCreate";
vadimtcb863752018-12-19 17:44:57 -0800200 private static final String ON_START_EVT = "Launcher.onStart";
201 private static final String ON_RESUME_EVT = "Launcher.onResume";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800202
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700203 private LauncherStateManager mStateManager;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700204
Adam Cohenad4e15c2013-10-17 16:21:35 -0700205 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206
Winson Chunga2413752012-04-03 14:22:34 -0700207 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700208 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
209 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
210 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700211
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400212 private static final int APPS_VIEW_ALPHA_CHANNEL_INDEX = 1;
213 private static final int SCRIM_VIEW_ALPHA_CHANNEL_INDEX = 0;
214
Jon Miranda54441f52018-01-24 15:38:25 -0800215 private LauncherAppTransitionManager mAppTransitionManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800216 private Configuration mOldConfig;
Jon Miranda54441f52018-01-24 15:38:25 -0800217
Adam Cohen091440a2015-03-18 14:16:05 -0700218 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700219 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700220 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800221 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700222
Sunny Goyalffe83f12014-08-14 17:39:34 -0700223 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700224 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700225
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700226 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700227
Sunny Goyal316490e2015-06-02 09:38:28 -0700228 @Thunk Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700229
Sunny Goyal47328fd2016-05-25 18:56:41 -0700230 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700231
232 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700233 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700234 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700235
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400236 // Scrim view for the all apps and overview state.
237 @Thunk ScrimView mScrimView;
238
Winson Chung8ae41982017-11-10 11:42:13 -0800239 // UI and state for the overview panel
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800240 private View mOverviewPanel;
Winson Chung8ae41982017-11-10 11:42:13 -0800241
Adam Cohen091440a2015-03-18 14:16:05 -0700242 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400243
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700244 private OnResumeCallback mOnResumeCallback;
245
Sunny Goyal527c7d32015-08-28 15:19:36 -0700246 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700247
Joe Onorato9c1289c2009-08-17 11:03:03 -0400248 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800249 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800250 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700251 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400252
Tony Wickham010d2552017-01-20 08:15:28 -0800253 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700254
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800255 private int mSynchronouslyBoundPage = PagedView.INVALID_PAGE;
Adam Cohen1462de32012-07-24 22:34:36 -0700256
Winson Chung46353de2012-02-16 14:05:10 -0800257 // We only want to get the SharedPreferences once since it does an FS stat each time we get
258 // it from the context.
259 private SharedPreferences mSharedPrefs;
260
Sunny Goyal2100c782016-08-22 16:00:03 -0700261 // Activity result which needs to be processed after workspace has loaded.
262 private ActivityResultInfo mPendingActivityResult;
263 /**
264 * Holds extra information required to handle a result from an external call, like
265 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
266 */
267 private PendingRequestArgs mPendingRequestArgs;
Sunny Goyaldedda052019-05-14 15:23:48 -0700268 // Request id for any pending activity result
269 private int mPendingActivityRequestCode = -1;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800270
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700271 public ViewGroupFocusHelper mFocusHandler;
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700272
Sunny Goyal623eddd2018-03-02 12:24:41 -0800273 private RotationHelper mRotationHelper;
Sunny Goyal5743f862019-03-28 15:35:32 -0700274 private Runnable mCancelTouchController;
Sunny Goyal7779d622015-06-11 16:18:39 -0700275
Sunny Goyal2db53422019-03-01 16:06:12 -0800276 final Handler mHandler = new Handler();
Sunny Goyala002c6c2019-02-12 16:20:10 -0800277 private final Runnable mHandleDeferredResume = this::handleDeferredResume;
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700278
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400279 private float mCurrentAssistantVisibility = 0f;
280
Sunny Goyalae6e3182019-04-30 12:04:37 -0700281 private DeviceProfile mStableDeviceProfile;
282 private RotationMode mRotationMode = RotationMode.NORMAL;
283
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800284 @Override
285 protected void onCreate(Bundle savedInstanceState) {
vadimt00d42552018-12-11 17:59:36 -0800286 RaceConditionTracker.onEvent(ON_CREATE_EVT, ENTER);
Winson Chunga2413752012-04-03 14:22:34 -0700287 if (DEBUG_STRICT_MODE) {
288 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
289 .detectDiskReads()
290 .detectDiskWrites()
291 .detectNetwork() // or .detectAll() for all detectable problems
292 .penaltyLog()
293 .build());
294 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
295 .detectLeakedSqlLiteObjects()
296 .detectLeakedClosableObjects()
297 .penaltyLog()
298 .penaltyDeath()
299 .build());
300 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700301 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700302
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800303 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700304 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400305
Sunny Goyal87f784c2017-01-11 10:48:34 -0800306 LauncherAppState app = LauncherAppState.getInstance(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800307 mOldConfig = new Configuration(getResources().getConfiguration());
Sunny Goyal0e7724c2018-02-22 13:22:21 -0800308 mModel = app.setLauncher(this);
Sunny Goyal41d51a02019-05-14 09:44:34 -0700309 mRotationHelper = new RotationHelper(this);
Hyunyoung Song31971a32019-01-27 12:31:12 -0800310 InvariantDeviceProfile idp = app.getInvariantDeviceProfile();
311 initDeviceProfile(idp);
312 idp.addOnChangeListener(this);
Sunny Goyalf7258242015-10-19 16:59:07 -0700313 mSharedPrefs = Utilities.getPrefs(this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800314 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700315 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700316
Joe Onorato41a12d22009-10-31 18:30:00 -0400317 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700318 mAllAppsController = new AllAppsTransitionController(this);
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800319 mStateManager = new LauncherStateManager(this);
Tracy Zhou8b23c6f2018-04-30 17:13:27 -0700320 UiFactory.onCreate(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700321
Sunny Goyalffe83f12014-08-14 17:39:34 -0700322 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700323
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700324 mAppWidgetHost = new LauncherAppWidgetHost(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700325 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700326
Sunny Goyal60820d72017-05-09 12:40:11 -0700327 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700328
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800329 setupViews();
Tony Wickham010d2552017-01-20 08:15:28 -0800330 mPopupDataProvider = new PopupDataProvider(this);
331
Sunny Goyal90548432018-04-04 17:17:00 -0700332 mAppTransitionManager = LauncherAppTransitionManager.newInstance(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800333
Sunny Goyald3864fa2017-11-14 15:06:36 -0800334 boolean internalStateHandled = InternalStateHandler.handleCreate(this, getIntent());
335 if (internalStateHandled) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800336 if (savedInstanceState != null) {
337 // InternalStateHandler has already set the appropriate state.
338 // We dont need to do anything.
339 savedInstanceState.remove(RUNTIME_STATE);
340 }
341 }
342 restoreState(savedInstanceState);
Sunny Goyalbbece862019-03-01 13:13:52 -0800343 mStateManager.reapplyState();
Winson Chungb63b44c2017-11-10 17:54:44 -0800344
Sunny Goyal2100c782016-08-22 16:00:03 -0700345 // We only load the page synchronously if the user rotates (or triggers a
346 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700347 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
348 if (savedInstanceState != null) {
349 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
350 }
Sunny Goyald3864fa2017-11-14 15:06:36 -0800351
Sunny Goyalfe770c92016-09-27 14:38:58 -0700352 if (!mModel.startLoader(currentScreen)) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800353 if (!internalStateHandled) {
354 // If we are not binding synchronously, show a fade in animation when
355 // the first page bind completes.
Sunny Goyal6001ea22018-05-10 16:31:00 -0700356 mDragLayer.getAlphaProperty(ALPHA_INDEX_LAUNCHER_LOAD).setValue(0);
Sunny Goyald3864fa2017-11-14 15:06:36 -0800357 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700358 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700359 // Pages bound synchronously.
360 mWorkspace.setCurrentPage(currentScreen);
361
Sunny Goyal2100c782016-08-22 16:00:03 -0700362 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800363 }
364
365 // For handling default keys
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800366 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800367
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800368 setContentView(mLauncherView);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800369 getRootView().dispatchInsets();
Mario Bertschlera6936942017-05-31 14:48:19 -0700370
Jon Miranda7fda2852017-07-19 16:07:01 -0700371 // Listen for broadcasts
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700372 registerReceiver(mScreenOffReceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));
Sunny Goyala616d2b2017-06-12 13:54:01 -0700373
Sunny Goyal8392c822017-06-20 10:03:56 -0700374 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
375 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700376
377 if (mLauncherCallbacks != null) {
378 mLauncherCallbacks.onCreate(savedInstanceState);
379 }
Sunny Goyal623eddd2018-03-02 12:24:41 -0800380 mRotationHelper.initialize();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700381
382 TraceHelper.endSection("Launcher-onCreate");
vadimt00d42552018-12-11 17:59:36 -0800383 RaceConditionTracker.onEvent(ON_CREATE_EVT, EXIT);
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400384 mStateManager.addStateListener(new LauncherStateManager.StateListener() {
385 @Override
386 public void onStateTransitionStart(LauncherState toState) {}
387
388 @Override
389 public void onStateTransitionComplete(LauncherState finalState) {
390 float alpha = 1f - mCurrentAssistantVisibility;
391 if (finalState == NORMAL) {
392 mAppsView.getAlphaProperty(APPS_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
393 } else if (finalState == OVERVIEW || finalState == OVERVIEW_PEEK) {
394 mAppsView.getAlphaProperty(APPS_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
395 mScrimView.getAlphaProperty(SCRIM_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
396 } else {
397 mAppsView.getAlphaProperty(APPS_VIEW_ALPHA_CHANNEL_INDEX).setValue(1f);
398 mScrimView.getAlphaProperty(SCRIM_VIEW_ALPHA_CHANNEL_INDEX).setValue(1f);
399 }
400 }
401 });
Adam Cohen9211d422014-10-07 18:14:53 -0700402 }
403
Sunny Goyal7779d622015-06-11 16:18:39 -0700404 @Override
Winson Chung8eb49e02018-07-16 13:33:31 -0700405 public void onEnterAnimationComplete() {
406 super.onEnterAnimationComplete();
407 UiFactory.onEnterAnimationComplete(this);
arangelovad783052018-08-17 14:46:45 +0100408 mAllAppsController.highlightWorkTabIfNecessary();
Winson Chung8eb49e02018-07-16 13:33:31 -0700409 }
410
411 @Override
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800412 public void onConfigurationChanged(Configuration newConfig) {
413 int diff = newConfig.diff(mOldConfig);
Jon Mirandaaeb4dd02018-07-11 14:16:23 -0700414
415 if ((diff & CONFIG_LOCALE) != 0) {
416 Folder.setLocaleDependentFields(getResources(), true /* force */);
417 }
418
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800419 if ((diff & (CONFIG_ORIENTATION | CONFIG_SCREEN_SIZE)) != 0) {
Sunny Goyal371ea052019-03-06 15:38:32 -0800420 onIdpChanged(mDeviceProfile.inv);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800421 }
422
423 mOldConfig.setTo(newConfig);
Tracy Zhoua706f002018-03-28 13:55:19 -0700424 UiFactory.onLauncherStateOrResumeChanged(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800425 super.onConfigurationChanged(newConfig);
426 }
427
Sunny Goyal41d51a02019-05-14 09:44:34 -0700428 private boolean supportsFakeLandscapeUI() {
429 return FeatureFlags.FAKE_LANDSCAPE_UI.get() && !mRotationHelper.homeScreenCanRotate();
430 }
431
Sunny Goyal605bcf32018-03-02 15:16:12 -0800432 @Override
Sunny Goyal41d51a02019-05-14 09:44:34 -0700433 public void reapplyUi() {
434 if (supportsFakeLandscapeUI()) {
Sunny Goyal7a39b1b2019-05-02 13:39:16 -0700435 mRotationMode = mStableDeviceProfile == null
436 ? RotationMode.NORMAL : UiFactory.getRotationMode(mDeviceProfile);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700437 }
Sunny Goyal59d086c2018-05-07 17:31:40 -0700438 getRootView().dispatchInsets();
439 getStateManager().reapplyState(true /* cancelCurrentAnimation */);
440 }
441
442 @Override
Sunny Goyal605bcf32018-03-02 15:16:12 -0800443 public void rebindModel() {
444 int currentPage = mWorkspace.getNextPage();
445 if (mModel.startLoader(currentPage)) {
446 mWorkspace.setCurrentPage(currentPage);
447 setWorkspaceLoading(true);
448 }
449 }
450
Hyunyoung Song31971a32019-01-27 12:31:12 -0800451 @Override
452 public void onIdpChanged(int changeFlags, InvariantDeviceProfile idp) {
Sunny Goyal371ea052019-03-06 15:38:32 -0800453 onIdpChanged(idp);
454 }
455
456 private void onIdpChanged(InvariantDeviceProfile idp) {
457 mUserEventDispatcher = null;
458
Hyunyoung Song31971a32019-01-27 12:31:12 -0800459 initDeviceProfile(idp);
Sunny Goyal371ea052019-03-06 15:38:32 -0800460 dispatchDeviceProfileChanged();
461 reapplyUi();
462 mDragLayer.recreateControllers();
463
464 // TODO: We can probably avoid rebind when only screen size changed.
465 rebindModel();
Hyunyoung Song31971a32019-01-27 12:31:12 -0800466 }
467
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400468 public void onAssistantVisibilityChanged(float visibility) {
469 mCurrentAssistantVisibility = visibility;
470 float alpha = 1f - visibility;
471 LauncherState state = mStateManager.getState();
472 if (state == NORMAL) {
473 mAppsView.getAlphaProperty(APPS_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
474 } else if (state == OVERVIEW || state == OVERVIEW_PEEK) {
475 mAppsView.getAlphaProperty(APPS_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
476 mScrimView.getAlphaProperty(SCRIM_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
477 }
478 }
479
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800480 private void initDeviceProfile(InvariantDeviceProfile idp) {
481 // Load configuration-specific DeviceProfile
Sunny Goyal59d086c2018-05-07 17:31:40 -0700482 mDeviceProfile = idp.getDeviceProfile(this);
Sunny Goyal8c48d8b2019-01-25 15:10:18 -0800483 if (isInMultiWindowMode()) {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700484 Display display = getWindowManager().getDefaultDisplay();
485 Point mwSize = new Point();
486 display.getSize(mwSize);
487 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
488 }
Sunny Goyalae6e3182019-04-30 12:04:37 -0700489
Sunny Goyal41d51a02019-05-14 09:44:34 -0700490 if (supportsFakeLandscapeUI()
491 && mDeviceProfile.isVerticalBarLayout()
Sunny Goyalae6e3182019-04-30 12:04:37 -0700492 && !mDeviceProfile.isMultiWindowMode) {
493 mStableDeviceProfile = mDeviceProfile.inv.portraitProfile;
Sunny Goyal7a39b1b2019-05-02 13:39:16 -0700494 mRotationMode = UiFactory.getRotationMode(mDeviceProfile);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700495 } else {
496 mStableDeviceProfile = null;
497 mRotationMode = RotationMode.NORMAL;
498 }
499
Sunny Goyal59d086c2018-05-07 17:31:40 -0700500 onDeviceProfileInitiated();
Sunny Goyalae6e3182019-04-30 12:04:37 -0700501 mModelWriter = mModel.getWriter(getWallpaperDeviceProfile().isVerticalBarLayout(), true);
502 }
503
504 public void updateInsets(Rect insets) {
505 mDeviceProfile.updateInsets(insets);
506 if (mStableDeviceProfile != null) {
Sunny Goyal7a39b1b2019-05-02 13:39:16 -0700507 Rect r = mStableDeviceProfile.getInsets();
508 mRotationMode.mapInsets(this, insets, r);
509 mStableDeviceProfile.updateInsets(r);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700510 }
511 }
512
513 @Override
514 public RotationMode getRotationMode() {
515 return mRotationMode;
516 }
517
518 /**
519 * Device profile to be used by UI elements which are shown directly on top of the wallpaper
520 * and whose presentation is tied to the wallpaper (and physical device) and not the activity
521 * configuration.
522 */
523 @Override
524 public DeviceProfile getWallpaperDeviceProfile() {
525 return mStableDeviceProfile == null ? mDeviceProfile : mStableDeviceProfile;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800526 }
527
Sunny Goyal623eddd2018-03-02 12:24:41 -0800528 public RotationHelper getRotationHelper() {
529 return mRotationHelper;
530 }
531
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700532 public LauncherStateManager getStateManager() {
533 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700534 }
535
Mario Bertschler27288382017-05-24 15:35:09 -0700536 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700537 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800538 return mLauncherView.findViewById(id);
539 }
540
541 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700542 public void onAppWidgetHostReset() {
543 if (mAppWidgetHost != null) {
544 mAppWidgetHost.startListening();
545 }
546 }
547
Adam Cohen9211d422014-10-07 18:14:53 -0700548 private LauncherCallbacks mLauncherCallbacks;
549
Sunny Goyal32554d12015-12-03 15:31:25 -0800550 /**
551 * Call this after onCreate to set or clear overlay.
552 */
553 public void setLauncherOverlay(LauncherOverlay overlay) {
554 if (overlay != null) {
555 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
556 }
557 mWorkspace.setLauncherOverlay(overlay);
558 }
559
Adam Cohen9211d422014-10-07 18:14:53 -0700560 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
561 mLauncherCallbacks = callbacks;
562 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700563 }
564
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700565 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700566 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700567 if (mLauncherCallbacks != null) {
568 mLauncherCallbacks.onLauncherProviderChange();
569 }
570 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700571
Hyunyoung Song3f471442015-04-08 19:01:34 -0700572 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700573 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
574 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700575 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700576 }
577
Tony Wickham010d2552017-01-20 08:15:28 -0800578 public PopupDataProvider getPopupDataProvider() {
579 return mPopupDataProvider;
580 }
581
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700582 @Override
Tony Wickhamf34bee82018-12-03 18:11:39 -0800583 public DotInfo getDotInfoForItem(ItemInfo info) {
584 return mPopupDataProvider.getDotInfoForItem(info);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700585 }
586
587 @Override
588 public void invalidateParent(ItemInfo info) {
Jon Mirandae6f3fa42019-03-04 09:14:40 -0800589 if (info.container >= 0) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700590 View folderIcon = getWorkspace().getHomescreenIconByItemId(info.container);
Jon Mirandae6f3fa42019-03-04 09:14:40 -0800591 if (folderIcon instanceof FolderIcon && folderIcon.getTag() instanceof FolderInfo) {
592 FolderIconPreviewVerifier verifier =
593 new FolderIconPreviewVerifier(getDeviceProfile().inv);
594 verifier.setFolderInfo((FolderInfo) folderIcon.getTag());
595 if (verifier.isItemInPreview(info.rank)) {
596 folderIcon.invalidate();
597 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700598 }
599 }
600 }
601
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000602 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700603 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
604 * a configuration step, this allows the proper animations to run after other transitions.
605 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700606 private int completeAdd(
Sunny Goyal2100c782016-08-22 16:00:03 -0700607 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
Sunny Goyalefb7e842018-10-04 15:11:00 -0700608 int screenId = info.screenId;
Sunny Goyal2100c782016-08-22 16:00:03 -0700609 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700610 // When the screen id represents an actual screen (as opposed to a rank) we make sure
611 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700612 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700613 }
Adam Cohendb364c32014-05-20 14:23:40 -0700614
Sunny Goyal2100c782016-08-22 16:00:03 -0700615 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800616 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700617 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700618 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800619 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700620 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700621 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700622 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700623 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700624 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700625 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700626 int widgetId = appWidgetId;
627 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700628 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700629 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700630 // Since the view was just bound, also launch the configure activity if needed
631 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
632 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800633 if (provider != null) {
634 new WidgetAddFlowHandler(provider)
635 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700636 }
637 }
638 break;
639 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800640 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700641
Adam Cohendb364c32014-05-20 14:23:40 -0700642 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800643 }
644
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800645 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700646 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700647 if (isWorkspaceLoading()) {
648 // process the result once the workspace has loaded.
649 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
650 return;
651 }
652 mPendingActivityResult = null;
653
Winson Chunge341d302013-08-16 14:31:00 -0700654 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700655 final PendingRequestArgs requestArgs = mPendingRequestArgs;
656 setWaitingForResult(null);
657 if (requestArgs == null) {
658 return;
659 }
660
661 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700662
Adam Cohenad4e15c2013-10-17 16:21:35 -0700663 Runnable exitSpringLoaded = new Runnable() {
664 @Override
665 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700666 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700667 }
668 };
669
Michael Jurka8b805b12012-04-18 14:23:14 -0700670 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700671 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700672 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700673 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
674 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700675 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700676 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700677 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700678 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700679 addAppWidgetImpl(
680 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800681 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700682 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700683 }
684 return;
685 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200686
Adam Cohened66b2b2012-01-23 17:28:51 -0800687 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700688 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700689
Adam Cohened66b2b2012-01-23 17:28:51 -0800690 // We have special handling for widgets
691 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800692 final int appWidgetId;
693 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
694 : -1;
695 if (widgetId < 0) {
696 appWidgetId = pendingAddWidgetId;
697 } else {
698 appWidgetId = widgetId;
699 }
700
Adam Cohenad4e15c2013-10-17 16:21:35 -0700701 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800702 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700703 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
704 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700705 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700706 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700707 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700708 @Override
709 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700710 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700711 }
712 };
Adam Cohendb364c32014-05-20 14:23:40 -0700713
Sunny Goyal2100c782016-08-22 16:00:03 -0700714 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
715 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
716 } else {
717 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
718 // When the screen id represents an actual screen (as opposed to a rank)
719 // we make sure that the drop page actually exists.
720 requestArgs.screenId =
721 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700722 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700723 final CellLayout dropLayout =
724 mWorkspace.getScreenWithId(requestArgs.screenId);
725
726 dropLayout.setDropPending(true);
727 final Runnable onComplete = new Runnable() {
728 @Override
729 public void run() {
730 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
731 dropLayout.setDropPending(false);
732 }
733 };
734 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
735 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700736 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800737 return;
738 }
739
Sunny Goyald478c832016-04-01 12:04:16 -0700740 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
741 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700742 if (resultCode == RESULT_OK) {
743 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700744 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700745 }
746 // Leave the widget in the pending state if the user canceled the configure.
747 return;
748 }
749
Sunny Goyalaad90582015-07-09 14:22:50 -0700750 if (requestCode == REQUEST_CREATE_SHORTCUT) {
751 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700752 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
753 completeAdd(requestCode, data, -1, requestArgs);
754 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
755 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
756
Sunny Goyalaad90582015-07-09 14:22:50 -0700757 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700758 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
759 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800760 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800761 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800762 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800763 }
764
765 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700766 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800767 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyaldedda052019-05-14 15:23:48 -0700768 mPendingActivityRequestCode = -1;
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800769 handleActivityResult(requestCode, resultCode, data);
770 if (mLauncherCallbacks != null) {
771 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
772 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800773 }
774
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700775 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700776 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600777 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700778 PendingRequestArgs pendingArgs = mPendingRequestArgs;
779 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
780 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
781 setWaitingForResult(null);
782
Sunny Goyal28c6b962015-10-12 11:42:05 -0700783 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700784 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700785 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700786 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700787 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700788 Intent intent = pendingArgs.getPendingIntent();
789
Sunny Goyal28c6b962015-10-12 11:42:05 -0700790 if (grantResults.length > 0
791 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal369212a2019-03-26 15:03:57 -0700792 startActivitySafely(v, intent, null, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700793 } else {
794 // TODO: Show a snack bar with link to settings
795 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700796 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700797 }
798 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600799 if (mLauncherCallbacks != null) {
800 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
801 grantResults);
802 }
803 }
804
Adam Cohendb364c32014-05-20 14:23:40 -0700805 /**
806 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
807 *
808 * @param screenId the screen id to check
809 * @return the new screen, or screenId if it exists
810 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700811 private int ensurePendingDropLayoutExists(int screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800812 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700813 if (dropLayout == null) {
814 // it's possible that the add screen was removed because it was
815 // empty and a re-bind occurred
816 mWorkspace.addExtraEmptyScreen();
817 return mWorkspace.commitExtraEmptyScreen();
818 } else {
819 return screenId;
820 }
821 }
822
Sunny Goyal2100c782016-08-22 16:00:03 -0700823 @Thunk void completeTwoStageWidgetDrop(
824 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
825 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800826 Runnable onCompleteRunnable = null;
827 int animationType = 0;
828
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700829 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800830 if (resultCode == RESULT_OK) {
831 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
832 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800833 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700834 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800835 onCompleteRunnable = new Runnable() {
836 @Override
837 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700838 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700839 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800840 }
841 };
842 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800843 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800844 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800845 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700846 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700847 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700848 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
849 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700850 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700851 // The animated view may be null in the case of a rotation during widget configuration
852 onCompleteRunnable.run();
853 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800854 }
855
856 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700857 protected void onStop() {
858 super.onStop();
Adam Cohen9211d422014-10-07 18:14:53 -0700859 if (mLauncherCallbacks != null) {
860 mLauncherCallbacks.onStop();
861 }
Miranda Kephart1a359a22019-02-11 13:07:04 -0500862
Hyunyoung Song1241e612018-05-15 21:46:51 -0700863 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
864 mStateManager.getState().containerType, -1);
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700865
866 mAppWidgetHost.setListenIfResumed(false);
867
Tony Wickham010d2552017-01-20 08:15:28 -0800868 NotificationListener.removeNotificationsChangedListener();
Sunny Goyalaf161fd2018-02-05 16:34:57 -0800869 getStateManager().moveToRestState();
Winson Chungedb0d6b2018-03-21 17:32:55 -0700870
Tracy Zhou62646712018-06-26 14:19:02 -0700871 UiFactory.onLauncherStateOrResumeChanged(this);
872
Winson Chungdea51352018-04-24 13:02:10 -0700873 // Workaround for b/78520668, explicitly trim memory once UI is hidden
Winson Chung473730f2018-04-26 11:54:22 -0700874 onTrimMemory(TRIM_MEMORY_UI_HIDDEN);
Michael Jurkacd496d72013-04-11 11:32:45 -0700875 }
876
877 @Override
878 protected void onStart() {
vadimtcb863752018-12-19 17:44:57 -0800879 RaceConditionTracker.onEvent(ON_START_EVT, ENTER);
Michael Jurkacd496d72013-04-11 11:32:45 -0700880 super.onStart();
Adam Cohen9211d422014-10-07 18:14:53 -0700881 if (mLauncherCallbacks != null) {
882 mLauncherCallbacks.onStart();
883 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800884 mAppWidgetHost.setListenIfResumed(true);
Sunny Goyalcac002d2018-01-04 09:51:45 -0800885 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
vadimtcb863752018-12-19 17:44:57 -0800886 RaceConditionTracker.onEvent(ON_START_EVT, EXIT);
Michael Jurkacd496d72013-04-11 11:32:45 -0700887 }
888
Sunny Goyala002c6c2019-02-12 16:20:10 -0800889 private void handleDeferredResume() {
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700890 if (hasBeenResumed()) {
891 getUserEventDispatcher().logActionCommand(Action.Command.RESUME,
892 mStateManager.getState().containerType, -1);
893 getUserEventDispatcher().startSession();
Sunny Goyala002c6c2019-02-12 16:20:10 -0800894
895 UiFactory.onLauncherStateOrResumeChanged(this);
Sunny Goyal369212a2019-03-26 15:03:57 -0700896 AppLaunchTracker.INSTANCE.get(this).onReturnedToHome();
Sunny Goyaldedda052019-05-14 15:23:48 -0700897 resetPendingActivityResultIfNeeded();
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700898 }
899 }
900
Sunny Goyaldedda052019-05-14 15:23:48 -0700901 private void resetPendingActivityResultIfNeeded() {
902 if (hasBeenResumed() && mPendingActivityRequestCode != -1 && isInState(NORMAL)) {
903 UiFactory.resetPendingActivityResults(this, mPendingActivityRequestCode);
904 }
905 }
906
907 protected void onStateSet(LauncherState state) {
908 getAppWidgetHost().setResumed(state == LauncherState.NORMAL);
909 resetPendingActivityResultIfNeeded();
910 }
911
Michael Jurkacd496d72013-04-11 11:32:45 -0700912 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800913 protected void onResume() {
vadimtcb863752018-12-19 17:44:57 -0800914 RaceConditionTracker.onEvent(ON_RESUME_EVT, ENTER);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700915 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800916 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700917 TraceHelper.partitionSection("ON_RESUME", "superCall");
918
Sunny Goyala002c6c2019-02-12 16:20:10 -0800919 mHandler.removeCallbacks(mHandleDeferredResume);
920 Utilities.postAsyncCallback(mHandler, mHandleDeferredResume);
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700921
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700922 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700923 // Process any items that were added while Launcher was away.
924 InstallShortcutReceiver.disableAndFlushInstallQueue(
925 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700926
Sunny Goyala474a9b2017-05-04 16:47:11 -0700927 // Refresh shortcuts if the permission changed.
928 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700929
Tracy Zhoue5575f92018-04-19 11:12:53 -0700930 DiscoveryBounce.showForHomeIfNeeded(this);
Adam Cohen9211d422014-10-07 18:14:53 -0700931 if (mLauncherCallbacks != null) {
932 mLauncherCallbacks.onResume();
933 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800934
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700935 TraceHelper.endSection("ON_RESUME");
vadimtcb863752018-12-19 17:44:57 -0800936 RaceConditionTracker.onEvent(ON_RESUME_EVT, EXIT);
Adam Cohen06dff352012-06-01 17:17:08 -0700937 }
938
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800939 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700940 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700941 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700942 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700943
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700944 super.onPause();
Joe Onorato24b6fd82009-11-12 13:47:09 -0800945 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700946 mDragController.resetLastGestureUpTime();
Hyunyoung Song497708c2019-05-01 16:16:53 -0700947 mDropTargetBar.animateToVisibility(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700948 if (mLauncherCallbacks != null) {
949 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700950 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700951 }
952
Winson Chungd7823942018-02-16 03:23:47 +0000953 @Override
Tracy Zhoua706f002018-03-28 13:55:19 -0700954 protected void onUserLeaveHint() {
955 super.onUserLeaveHint();
956 UiFactory.onLauncherStateOrResumeChanged(this);
957 }
958
959 @Override
Winson Chungd7823942018-02-16 03:23:47 +0000960 public void onWindowFocusChanged(boolean hasFocus) {
961 super.onWindowFocusChanged(hasFocus);
962 mStateManager.onWindowFocusChanged();
963 }
964
Adam Cohenc2d6e892014-10-16 09:49:24 -0700965 public interface LauncherOverlay {
966
967 /**
968 * Touch interaction leading to overscroll has begun
969 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700970 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700971
972 /**
973 * Touch interaction related to overscroll has ended
974 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700975 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700976
977 /**
978 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
979 * screen (or in the case of RTL, the rightmost screen).
980 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700981 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700982
983 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800984 * Called when the launcher is ready to use the overlay
985 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700986 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700987 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700988 }
989
990 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700991 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700992 }
993
994 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
995
Sunny Goyalc86df472016-02-25 09:19:38 -0800996 public void onScrollChanged(float progress) {
997 if (mWorkspace != null) {
998 mWorkspace.onOverlayScrollChanged(progress);
999 }
1000 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001001 }
1002
Sunny Goyalf9403d92017-10-18 10:55:56 -07001003 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -07001004 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -07001005 }
1006
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001007 /**
1008 * Restores the previous state, if it exists.
1009 *
1010 * @param savedState The previous state.
1011 */
1012 private void restoreState(Bundle savedState) {
1013 if (savedState == null) {
1014 return;
1015 }
1016
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001017 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -07001018 LauncherState[] stateValues = LauncherState.values();
1019 LauncherState state = stateValues[stateOrdinal];
Sunny Goyalbc683e92017-12-06 10:25:07 -08001020 if (!state.disableRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001021 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001022 }
1023
Sunny Goyal2100c782016-08-22 16:00:03 -07001024 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
1025 if (requestArgs != null) {
1026 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001027 }
Sunny Goyaldedda052019-05-14 15:23:48 -07001028 mPendingActivityRequestCode = savedState.getInt(RUNTIME_STATE_PENDING_REQUEST_CODE);
Sunny Goyal2100c782016-08-22 16:00:03 -07001029
1030 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001031
1032 SparseArray<Parcelable> widgetsState =
1033 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
1034 if (widgetsState != null) {
1035 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
1036 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001037 }
1038
1039 /**
1040 * Finds all the views we need and configure them properly.
1041 */
1042 private void setupViews() {
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001043 mDragLayer = findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001044 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -07001045 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001046 mWorkspace.initParentViews(mDragLayer);
Sunny Goyal16764582017-11-06 16:00:34 -08001047 mOverviewPanel = findViewById(R.id.overview_panel);
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001048 mHotseat = findViewById(R.id.hotseat);
Craig Mautner360310b2012-10-26 15:13:08 -07001049
Sunny Goyal784f9c32016-03-23 16:56:54 -07001050 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1051 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1052 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001053
Winson Chung4c98d922011-05-31 16:50:48 -07001054 // Setup the drag layer
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001055 mDragLayer.setup(mDragController, mWorkspace);
Sunny Goyal5743f862019-03-28 15:35:32 -07001056 mCancelTouchController = UiFactory.enableLiveTouchControllerChanges(mDragLayer);
Winson Chung4c98d922011-05-31 16:50:48 -07001057
Hyunyoung Song645764e2016-06-06 14:19:02 -07001058 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001059 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1060 // default state, otherwise we will update to the wrong offsets in RTL
1061 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001062 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001063 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001064
Tony Wickham34d2c912015-09-11 09:27:58 -07001065 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001066 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001067
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001068 // Setup Apps
1069 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001070
Govinda Wassermanc06e1512019-04-09 09:56:53 -04001071 // Setup Scrim
1072 mScrimView = findViewById(R.id.scrim_view);
1073
Winson Chung3d503fb2011-07-13 17:25:49 -07001074 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001075 mDragController.setMoveTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001076 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001077
Sunny Goyal7185dd62018-03-14 17:51:49 -07001078 mAllAppsController.setupViews(mAppsView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001079 }
1080
1081 /**
1082 * Creates a view representing a shortcut.
1083 *
1084 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001085 */
Sunny Goyal95899162019-03-27 16:03:06 -07001086 View createShortcut(WorkspaceItemInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001087 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001088 }
1089
1090 /**
1091 * Creates a view representing a shortcut inflated from the specified resource.
1092 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001093 * @param parent The group the shortcut belongs to.
1094 * @param info The data structure describing the shortcut.
1095 *
1096 * @return A View inflated from layoutResId.
1097 */
Sunny Goyal95899162019-03-27 16:03:06 -07001098 public View createShortcut(ViewGroup parent, WorkspaceItemInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -08001099 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
1100 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal95899162019-03-27 16:03:06 -07001101 favorite.applyFromWorkspaceItem(info);
Sunny Goyal8a2a63b2018-03-06 22:15:18 -08001102 favorite.setOnClickListener(ItemClickHandler.INSTANCE);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001103 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001104 return favorite;
1105 }
1106
1107 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -07001108 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001109 *
1110 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001111 */
Sunny Goyalefb7e842018-10-04 15:11:00 -07001112 private void completeAddShortcut(Intent data, int container, int screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001113 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001114 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
1115 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001116 return;
1117 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001118
Jon Mirandac476d6e2017-05-04 16:30:01 -07001119 int[] cellXY = mTmpAddItemCellCoordinates;
1120 CellLayout layout = getCellLayout(container, screenId);
1121
Sunny Goyal95899162019-03-27 16:03:06 -07001122 WorkspaceItemInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001123 if (Utilities.ATLEAST_OREO) {
Sunny Goyal95899162019-03-27 16:03:06 -07001124 info = LauncherAppsCompatVO.createWorkspaceItemFromPinItemRequest(
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001125 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001126 }
1127
1128 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001129 // Legacy shortcuts are only supported for primary profile.
1130 info = Process.myUserHandle().equals(args.user)
1131 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001132
Sunny Goyalb57645f2017-03-20 13:57:28 -07001133 if (info == null) {
1134 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1135 return;
1136 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001137 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001138 // The app is trying to add a shortcut without sufficient permissions
1139 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1140 return;
1141 }
1142 }
1143
Jon Mirandac476d6e2017-05-04 16:30:01 -07001144 if (container < 0) {
1145 // Adding a shortcut to the Workspace.
1146 final View view = createShortcut(info);
1147 boolean foundCellSpan = false;
1148 // First we check if we already know the exact location where we want to add this item.
1149 if (cellX >= 0 && cellY >= 0) {
1150 cellXY[0] = cellX;
1151 cellXY[1] = cellY;
1152 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001153
Jon Mirandac476d6e2017-05-04 16:30:01 -07001154 // If appropriate, either create a folder or add to an existing folder
1155 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001156 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001157 return;
1158 }
1159 DragObject dragObject = new DragObject();
1160 dragObject.dragInfo = info;
1161 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1162 true)) {
1163 return;
1164 }
1165 } else {
1166 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1167 }
1168
1169 if (!foundCellSpan) {
1170 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001171 return;
1172 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001173
1174 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1175 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001176 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001177 // Adding a shortcut to a Folder.
Sunny Goyale29897f2017-07-20 10:09:42 -07001178 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001179 if (folderIcon != null) {
1180 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1181 folderInfo.add(info, args.rank, false);
1182 } else {
Sunny Goyale29897f2017-07-20 10:09:42 -07001183 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001184 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001185 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001186 }
1187
Sunny Goyalefb7e842018-10-04 15:11:00 -07001188 public FolderIcon findFolderIcon(final int folderIconId) {
Sunny Goyal83fd25e2018-07-09 16:47:01 -07001189 return (FolderIcon) mWorkspace.getHomescreenIconByItemId(folderIconId);
Sunny Goyale29897f2017-07-20 10:09:42 -07001190 }
1191
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001192 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001193 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001194 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001195 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001196 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001197 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001198 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1199
Adam Cohened66b2b2012-01-23 17:28:51 -08001200 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001201 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001202 }
Romain Guycbb89e42009-06-08 15:52:54 -07001203
Adam Cohen59400422014-03-05 18:07:04 -08001204 LauncherAppWidgetInfo launcherInfo;
1205 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001206 launcherInfo.spanX = itemInfo.spanX;
1207 launcherInfo.spanY = itemInfo.spanY;
1208 launcherInfo.minSpanX = itemInfo.minSpanX;
1209 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001210 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001211
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001212 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001213 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001214
Sunny Goyal2100c782016-08-22 16:00:03 -07001215 if (hostView == null) {
1216 // Perform actual inflation because we're live
1217 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001218 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001219 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301220 prepareAppWidget(hostView, launcherInfo);
1221 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001222 }
Romain Guycbb89e42009-06-08 15:52:54 -07001223
Sunny Goyald5462aa2016-11-22 16:52:39 +05301224 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001225 hostView.setTag(item);
1226 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001227 hostView.setFocusable(true);
1228 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001229 }
1230
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001231 private final BroadcastReceiver mScreenOffReceiver = new BroadcastReceiver() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001232 @Override
1233 public void onReceive(Context context, Intent intent) {
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001234 // Reset AllApps to its initial state only if we are not in the middle of
1235 // processing a multi-step drop
1236 if (mPendingRequestArgs == null) {
1237 mStateManager.goToState(NORMAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001238 }
1239 }
1240 };
1241
Sunny Goyalaae6fbb2019-01-31 16:05:58 -08001242 public void updateNotificationDots(Predicate<PackageUserKey> updatedDots) {
Tony Wickhamf34bee82018-12-03 18:11:39 -08001243 mWorkspace.updateNotificationDots(updatedDots);
1244 mAppsView.getAppsStore().updateNotificationDots(updatedDots);
Tony Wickham010d2552017-01-20 08:15:28 -08001245 }
1246
Adam Cohended9f8d2010-11-03 13:25:16 -07001247 @Override
1248 public void onAttachedToWindow() {
1249 super.onAttachedToWindow();
1250
Sunny Goyalc86df472016-02-25 09:19:38 -08001251 if (mLauncherCallbacks != null) {
1252 mLauncherCallbacks.onAttachedToWindow();
1253 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001254 }
1255
1256 @Override
1257 public void onDetachedFromWindow() {
1258 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001259
1260 if (mLauncherCallbacks != null) {
1261 mLauncherCallbacks.onDetachedFromWindow();
1262 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001263 }
1264
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001265 public AllAppsTransitionController getAllAppsController() {
1266 return mAllAppsController;
1267 }
1268
Sunny Goyal9d69c8d2018-03-19 13:41:31 -07001269 @Override
Winson Chung1a341002018-01-17 10:00:23 -08001270 public LauncherRootView getRootView() {
1271 return (LauncherRootView) mLauncherView;
1272 }
1273
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001274 @Override
Winson Chunga6945242014-01-08 14:04:34 -08001275 public DragLayer getDragLayer() {
1276 return mDragLayer;
1277 }
1278
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001279 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001280 return mAppsView;
1281 }
1282
Winson Chunga6945242014-01-08 14:04:34 -08001283 public Workspace getWorkspace() {
1284 return mWorkspace;
1285 }
1286
1287 public Hotseat getHotseat() {
1288 return mHotseat;
1289 }
1290
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001291 public <T extends View> T getOverviewPanel() {
Winson Chung8ae41982017-11-10 11:42:13 -08001292 return (T) mOverviewPanel;
Winson Chunga6945242014-01-08 14:04:34 -08001293 }
1294
Sunny Goyal47328fd2016-05-25 18:56:41 -07001295 public DropTargetBar getDropTargetBar() {
1296 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001297 }
1298
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001299 public LauncherAppWidgetHost getAppWidgetHost() {
1300 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001301 }
Romain Guycbb89e42009-06-08 15:52:54 -07001302
Winson Chunga9abd0e2010-10-27 17:18:37 -07001303 public LauncherModel getModel() {
1304 return mModel;
1305 }
1306
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001307 public ModelWriter getModelWriter() {
1308 return mModelWriter;
1309 }
1310
Adam Cohen79d90c52016-04-22 13:29:20 -07001311 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001312 return mSharedPrefs;
1313 }
1314
Sunny Goyalf8d56fc2018-01-31 15:18:11 -08001315 public int getOrientation() { return mOldConfig.orientation; }
Jon Miranda6bed3502017-09-19 14:43:17 -07001316
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001317 @Override
1318 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001319 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001320 super.onNewIntent(intent);
1321
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001322 boolean alreadyOnHome = hasWindowFocus() && ((intent.getFlags() &
Winson15f8b172015-08-19 11:02:39 -07001323 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1324 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001325
1326 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001327 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001328 && AbstractFloatingView.getTopOpenView(this) == null;
Winson15f8b172015-08-19 11:02:39 -07001329 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001330 boolean internalStateHandled = InternalStateHandler
Sunny Goyal8b264562018-02-03 23:00:13 -08001331 .handleNewIntent(this, intent, isStarted());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001332
Winson15f8b172015-08-19 11:02:39 -07001333 if (isActionMain) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001334 if (!internalStateHandled) {
1335 // Note: There should be at most one log per method call. This is enforced
1336 // implicitly by using if-else statements.
1337 UserEventDispatcher ued = getUserEventDispatcher();
1338 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
1339 if (topOpenView != null) {
1340 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
1341 } else if (alreadyOnHome) {
1342 Target target = newContainerTarget(mStateManager.getState().containerType);
1343 target.pageIndex = mWorkspace.getCurrentPage();
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001344 ued.logActionCommand(Action.Command.HOME_INTENT, target,
1345 newContainerTarget(ContainerType.WORKSPACE));
Sunny Goyald3864fa2017-11-14 15:06:36 -08001346 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001347
Sunny Goyald3864fa2017-11-14 15:06:36 -08001348 // In all these cases, only animate if we're already on home
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001349 AbstractFloatingView.closeAllOpenViews(this, isStarted());
Jon Mirandafeba90f2016-10-06 10:53:29 -07001350
Sunny Goyala2467272018-03-16 14:53:05 -07001351 if (!isInState(NORMAL)) {
1352 // Only change state, if not already the same. This prevents cancelling any
1353 // animations running as part of resume
1354 mStateManager.goToState(NORMAL);
1355 }
Sunny Goyald3864fa2017-11-14 15:06:36 -08001356
1357 // Reset the apps view
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001358 if (!alreadyOnHome) {
Sunny Goyal7185dd62018-03-14 17:51:49 -07001359 mAppsView.reset(isStarted() /* animate */);
Sunny Goyald3864fa2017-11-14 15:06:36 -08001360 }
1361
Sunny Goyalaad33592018-08-07 17:20:35 -07001362 if (shouldMoveToDefaultScreen && !mWorkspace.isHandlingTouch()) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001363 mWorkspace.post(mWorkspace::moveToDefaultScreen);
1364 }
1365 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001366
1367 final View v = getWindow().peekDecorView();
1368 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001369 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001370 }
1371
Adam Cohen9211d422014-10-07 18:14:53 -07001372 if (mLauncherCallbacks != null) {
Sunny Goyal85462132018-04-24 11:39:37 -07001373 mLauncherCallbacks.onHomeIntent(internalStateHandled);
Adam Cohen9211d422014-10-07 18:14:53 -07001374 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001375 }
Winson15f8b172015-08-19 11:02:39 -07001376
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001377 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001378 }
1379
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001380 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001381 public void onRestoreInstanceState(Bundle state) {
1382 super.onRestoreInstanceState(state);
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001383 mWorkspace.restoreInstanceStateForChild(mSynchronouslyBoundPage);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001384 }
1385
1386 @Override
1387 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001388 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001389 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001390
Adam Cohen21cd0022013-10-09 18:57:02 -07001391 }
Sunny Goyalea609262017-10-25 15:47:38 -07001392 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001393
1394
1395 AbstractFloatingView widgets = AbstractFloatingView
1396 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1397 if (widgets != null) {
1398 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1399 widgets.saveHierarchyState(widgetsState);
1400 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1401 } else {
1402 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1403 }
1404
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001405 // We close any open folders and shortcut containers since they will not be re-opened,
1406 // and we need to make sure this state is reflected.
1407 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001408
Sunny Goyal2100c782016-08-22 16:00:03 -07001409 if (mPendingRequestArgs != null) {
1410 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1411 }
Sunny Goyaldedda052019-05-14 15:23:48 -07001412 outState.putInt(RUNTIME_STATE_PENDING_REQUEST_CODE, mPendingActivityRequestCode);
1413
Sunny Goyal2100c782016-08-22 16:00:03 -07001414 if (mPendingActivityResult != null) {
1415 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001416 }
1417
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001418 super.onSaveInstanceState(outState);
1419
Adam Cohen9211d422014-10-07 18:14:53 -07001420 if (mLauncherCallbacks != null) {
1421 mLauncherCallbacks.onSaveInstanceState(outState);
1422 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001423 }
1424
1425 @Override
1426 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001427 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001428
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001429 unregisterReceiver(mScreenOffReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001430 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001431
Sunny Goyal5743f862019-03-28 15:35:32 -07001432 if (mCancelTouchController != null) {
1433 mCancelTouchController.run();
1434 mCancelTouchController = null;
1435 }
Tony Wickhama0068302018-04-11 16:16:11 -07001436
Winson Chungcd2b0142011-06-08 16:02:26 -07001437 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001438 // It's possible to receive onDestroy after a new Launcher activity has
1439 // been created. In this case, don't interfere with the new Launcher.
1440 if (mModel.isCurrentCallbacks(this)) {
1441 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001442 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001443 }
Sunny Goyal623eddd2018-03-02 12:24:41 -08001444 mRotationHelper.destroy();
Sunny Goyal745bad92016-05-02 10:54:12 -07001445
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001446 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001447 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001448 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001449 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001450 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001451
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001452 TextKeyListener.getInstance().release();
Tony2917a8b2017-07-31 23:29:42 -07001453 clearPendingBinds();
Hyunyoung Song31971a32019-01-27 12:31:12 -08001454 LauncherAppState.getIDP(this).removeOnChangeListener(this);
Adam Cohen9211d422014-10-07 18:14:53 -07001455 if (mLauncherCallbacks != null) {
1456 mLauncherCallbacks.onDestroy();
1457 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001458 }
1459
Sunny Goyalae502842016-06-17 08:43:56 -07001460 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1461 return mAccessibilityDelegate;
1462 }
1463
Adam Cohena9cf38f2011-05-02 15:36:58 -07001464 public DragController getDragController() {
1465 return mDragController;
1466 }
1467
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001468 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001469 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyaldedda052019-05-14 15:23:48 -07001470 if (requestCode != -1) {
1471 mPendingActivityRequestCode = requestCode;
1472 }
1473 if (requestCode == -1
1474 || !UiFactory.startActivityForResult(this, intent, requestCode, options)) {
1475 super.startActivityForResult(intent, requestCode, options);
1476 }
Adam Cohen173f7112015-03-27 15:14:00 -07001477 }
1478
1479 @Override
Sunny Goyaldedda052019-05-14 15:23:48 -07001480 public void startIntentSenderForResult(IntentSender intent, int requestCode,
Adam Cohen173f7112015-03-27 15:14:00 -07001481 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Sunny Goyaldedda052019-05-14 15:23:48 -07001482 if (requestCode != -1) {
1483 mPendingActivityRequestCode = requestCode;
1484 }
1485 if (requestCode == -1 || !UiFactory.startIntentSenderForResult(this, intent, requestCode,
1486 fillInIntent, flagsMask, flagsValues, extraFlags, options)) {
1487 try {
1488 super.startIntentSenderForResult(intent, requestCode,
1489 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1490 } catch (IntentSender.SendIntentException e) {
1491 throw new ActivityNotFoundException();
1492 }
Adam Cohen173f7112015-03-27 15:14:00 -07001493 }
1494 }
1495
Winson Chung70d72102011-08-12 11:24:35 -07001496 /**
1497 * Indicates that we want global search for this activity by setting the globalSearch
1498 * argument for {@link #startSearch} to true.
1499 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001500 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001501 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001502 Bundle appSearchData, boolean globalSearch) {
1503 if (appSearchData == null) {
1504 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001505 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001506 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001507
Sunny Goyal6f28e712016-09-13 14:19:29 -07001508 if (mLauncherCallbacks == null ||
1509 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1510 // Starting search from the callbacks failed. Start the default global search.
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001511 super.startSearch(initialQuery, selectInitialQuery, appSearchData, true);
Ian Parkinson047036e2014-09-01 15:40:53 +01001512 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001513
1514 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001515 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001516 }
1517
Joe Onorato9c1289c2009-08-17 11:03:03 -04001518 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001519 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001520 }
1521
Adam Cohen517a7f52014-03-01 12:12:59 -08001522 public boolean isWorkspaceLoading() {
1523 return mWorkspaceLoading;
1524 }
1525
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001526 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001527 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001528 }
1529
Sunny Goyal04a324a2017-01-20 21:08:59 -08001530 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001531 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001532 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001533
Sunny Goyal2100c782016-08-22 16:00:03 -07001534 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001535 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001536 if (LOGD) {
1537 Log.d(TAG, "Adding widget from drop");
1538 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001539 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001540 }
1541
Sunny Goyal2100c782016-08-22 16:00:03 -07001542 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001543 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1544 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1545 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001546
Adam Cohenad4e15c2013-10-17 16:21:35 -07001547 Runnable onComplete = new Runnable() {
1548 @Override
1549 public void run() {
1550 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001551 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001552 }
1553 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001554 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001555 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001556 }
1557 }
Romain Guycbb89e42009-06-08 15:52:54 -07001558
Sunny Goyalefb7e842018-10-04 15:11:00 -07001559 public void addPendingItem(PendingAddItemInfo info, int container, int screenId,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001560 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001561 info.container = container;
1562 info.screenId = screenId;
1563 if (cell != null) {
1564 info.cellX = cell[0];
1565 info.cellY = cell[1];
1566 }
1567 info.spanX = spanX;
1568 info.spanY = spanY;
1569
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001570 switch (info.itemType) {
1571 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1572 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001573 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001574 break;
1575 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001576 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001577 break;
1578 default:
1579 throw new IllegalStateException("Unknown item type: " + info.itemType);
1580 }
1581 }
1582
Adam Cohenfbba09b2011-07-18 21:43:05 -07001583 /**
1584 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001585 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001586 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001587 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1588 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001589 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1590 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1591 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001592 }
1593
Adam Cohenfbba09b2011-07-18 21:43:05 -07001594 /**
1595 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001596 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001597 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001598 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001599 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001600 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001601 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001602 // In the case where we've prebound the widget, we remove it from the DragLayer
1603 if (LOGD) {
1604 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1605 }
1606 getDragLayer().removeView(hostView);
1607
Adam Cohened66b2b2012-01-23 17:28:51 -08001608 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001609 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001610
1611 // Clear the boundWidget so that it doesn't get destroyed.
1612 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001613 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001614 // In this case, we either need to start an activity to get permission to bind
1615 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001616 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1617 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1618 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1619 this, info.componentName);
1620 } else {
1621 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1622 }
Adam Cohendd70d662012-10-04 16:53:44 -07001623 Bundle options = info.bindOptions;
1624
Sunny Goyalffe83f12014-08-14 17:39:34 -07001625 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1626 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001627 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001628 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001629 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001630 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001631 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001632 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001633 }
1634
Sunny Goyalefb7e842018-10-04 15:11:00 -07001635 FolderIcon addFolder(CellLayout layout, int container, final int screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001636 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001637 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001638 folderInfo.title = getText(R.string.folder_name);
1639
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001640 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001641 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001642
1643 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001644 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301645 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001646 // Force measure the new folder icon
1647 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1648 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001649 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001650 }
Romain Guycbb89e42009-06-08 15:52:54 -07001651
Winsonc0b52fe2015-09-09 16:38:15 -07001652 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001653 * Unbinds the view for the specified item, and removes the item and all its children.
1654 *
1655 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001656 * @param itemInfo the {@link ItemInfo} for this view.
1657 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001658 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001659 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Sunny Goyal95899162019-03-27 16:03:06 -07001660 if (itemInfo instanceof WorkspaceItemInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001661 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001662 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1663 if (folderIcon instanceof FolderIcon) {
Sunny Goyal95899162019-03-27 16:03:06 -07001664 ((FolderInfo) folderIcon.getTag()).remove((WorkspaceItemInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001665 } else {
1666 mWorkspace.removeWorkspaceItem(v);
1667 }
Winsonc0b52fe2015-09-09 16:38:15 -07001668 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001669 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001670 }
1671 } else if (itemInfo instanceof FolderInfo) {
1672 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001673 if (v instanceof FolderIcon) {
1674 ((FolderIcon) v).removeListeners();
1675 }
Winsonc0b52fe2015-09-09 16:38:15 -07001676 mWorkspace.removeWorkspaceItem(v);
1677 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001678 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001679 }
1680 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1681 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001682 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001683 if (deleteFromDb) {
Tony Wickham6a71a5b2018-08-21 11:40:23 -07001684 getModelWriter().deleteWidgetInfo(widgetInfo, getAppWidgetHost());
Winsonc0b52fe2015-09-09 16:38:15 -07001685 }
1686 } else {
1687 return false;
1688 }
1689 return true;
1690 }
1691
Tony Wickham6a71a5b2018-08-21 11:40:23 -07001692
Winsonc0b52fe2015-09-09 16:38:15 -07001693
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001694 @Override
1695 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001696 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001697 }
1698
Joe Onorato88ec0992009-11-19 13:16:06 -08001699 @Override
1700 public void onBackPressed() {
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08001701 if (finishAutoCancelActionMode()) {
1702 return;
1703 }
Adam Cohen9211d422014-10-07 18:14:53 -07001704 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1705 return;
1706 }
1707
Sunny Goyal45478022015-06-08 16:52:41 -07001708 if (mDragController.isDragging()) {
1709 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001710 return;
1711 }
1712
Jon Mirandafeba90f2016-10-06 10:53:29 -07001713 // Note: There should be at most one log per method call. This is enforced implicitly
1714 // by using if-else statements.
1715 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001716 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
Tony Wickham52c1b662018-05-21 13:13:58 -07001717 if (topView != null && topView.onBackPressed()) {
1718 // Handled by the floating view.
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001719 } else {
Sunny Goyale39690b2018-08-02 13:35:07 -07001720 mStateManager.getState().onBackPressed(this);
Michael Jurkaaf442092010-06-10 17:01:57 -07001721 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001722 }
1723
Sunny Goyalfe770c92016-09-27 14:38:58 -07001724 @TargetApi(Build.VERSION_CODES.M)
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001725 @Override
Jon Miranda73c27ec2018-05-16 18:06:23 -07001726 public ActivityOptions getActivityLaunchOptions(View v) {
1727 return mAppTransitionManager.getActivityLaunchOptions(this, v);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001728 }
1729
Tony Wickham3a6746a2018-03-29 09:39:56 -07001730 public LauncherAppTransitionManager getAppTransitionManager() {
1731 return mAppTransitionManager;
1732 }
1733
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001734 @TargetApi(Build.VERSION_CODES.M)
1735 @Override
1736 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
1737 // Due to legacy reasons, direct call shortcuts require Launchers to have the
1738 // corresponding permission. Show the appropriate permission prompt if that
1739 // is the case.
1740 if (intent.getComponent() == null
1741 && Intent.ACTION_CALL.equals(intent.getAction())
1742 && checkSelfPermission(android.Manifest.permission.CALL_PHONE) !=
1743 PackageManager.PERMISSION_GRANTED) {
1744
1745 setWaitingForResult(PendingRequestArgs
1746 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
1747 requestPermissions(new String[]{android.Manifest.permission.CALL_PHONE},
1748 REQUEST_PERMISSION_CALL_PHONE);
1749 return true;
1750 } else {
1751 return false;
1752 }
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001753 }
1754
Hyunyoung Song46d07f72018-05-22 15:41:25 -07001755 @Override
Hyunyoung Songfc007472018-10-25 14:09:50 -07001756 public int getCurrentState() {
1757 if(mStateManager.getState() == LauncherState.ALL_APPS) {
1758 return StatsLogUtils.LAUNCHER_STATE_ALLAPPS;
Govinda Wassermanc06e1512019-04-09 09:56:53 -04001759 } else if (mStateManager.getState() == OVERVIEW) {
Hyunyoung Songfc007472018-10-25 14:09:50 -07001760 return StatsLogUtils.LAUNCHER_STATE_OVERVIEW;
1761 }
Hyunyoung Song0ae38882018-11-05 14:45:19 -08001762 return StatsLogUtils.LAUNCHER_STATE_HOME;
Hyunyoung Songfc007472018-10-25 14:09:50 -07001763 }
1764
1765 @Override
Hyunyoung Song46d07f72018-05-22 15:41:25 -07001766 public void modifyUserEvent(LauncherLogProto.LauncherEvent event) {
1767 if (event.srcTarget != null && event.srcTarget.length > 0 &&
1768 event.srcTarget[1].containerType == ContainerType.PREDICTION) {
1769 Target[] targets = new Target[3];
1770 targets[0] = event.srcTarget[0];
1771 targets[1] = event.srcTarget[1];
1772 targets[2] = newTarget(Target.Type.CONTAINER);
1773 event.srcTarget = targets;
1774 LauncherState state = mStateManager.getState();
1775 if (state == LauncherState.ALL_APPS) {
1776 event.srcTarget[2].containerType = ContainerType.ALLAPPS;
Govinda Wassermanc06e1512019-04-09 09:56:53 -04001777 } else if (state == OVERVIEW) {
Hyunyoung Song46d07f72018-05-22 15:41:25 -07001778 event.srcTarget[2].containerType = ContainerType.TASKSWITCHER;
1779 }
1780 }
1781 }
1782
Sunny Goyal369212a2019-03-26 15:03:57 -07001783 public boolean startActivitySafely(View v, Intent intent, ItemInfo item,
1784 @Nullable String sourceContainer) {
1785 boolean success = super.startActivitySafely(v, intent, item, sourceContainer);
Hyunyoung Song1241e612018-05-15 21:46:51 -07001786 if (success && v instanceof BubbleTextView) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001787 // This is set to the view that launched the activity that navigated the user away
1788 // from launcher. Since there is no callback for when the activity has finished
1789 // launching, enable the press state and keep this reference to reset the press
1790 // state when we return to launcher.
1791 BubbleTextView btv = (BubbleTextView) v;
1792 btv.setStayPressed(true);
1793 setOnResumeCallback(btv);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001794 }
Hyunyoung Song1241e612018-05-15 21:46:51 -07001795 return success;
Michael Jurka86a720e2012-05-09 11:23:23 -07001796 }
1797
Winson Chung3d503fb2011-07-13 17:25:49 -07001798 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001799 // TODO: Remove this method
Sunny Goyal876e4622018-11-02 13:50:40 -07001800 return mHotseat != null && (layout == mHotseat);
Winson Chung3d503fb2011-07-13 17:25:49 -07001801 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001802
Winson Chung3d503fb2011-07-13 17:25:49 -07001803 /**
1804 * Returns the CellLayout of the specified container at the specified screen.
1805 */
Sunny Goyalefb7e842018-10-04 15:11:00 -07001806 public CellLayout getCellLayout(int container, int screenId) {
Sunny Goyal876e4622018-11-02 13:50:40 -07001807 return (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1808 ? mHotseat : mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08001809 }
1810
Michael Jurkae326f182011-11-21 14:05:46 -08001811 @Override
1812 public void onTrimMemory(int level) {
1813 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07001814 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
1815 // The widget preview db can result in holding onto over
1816 // 3MB of memory for caching which isn't necessary.
1817 SQLiteDatabase.releaseMemory();
1818
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08001819 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07001820 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08001821 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01001822 if (mLauncherCallbacks != null) {
1823 mLauncherCallbacks.onTrimMemory(level);
1824 }
Winson Chung62a70be2018-02-23 15:10:05 -08001825 UiFactory.onTrimMemory(this, level);
Michael Jurkae326f182011-11-21 14:05:46 -08001826 }
1827
Michael Jurkad7c28052012-04-27 15:43:36 -07001828 @Override
1829 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07001830 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07001831 final List<CharSequence> text = event.getText();
1832 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07001833 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001834 // TODO: When can workspace be null?
1835 text.add(mWorkspace == null
1836 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07001837 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07001838 return result;
1839 }
1840
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001841 public void setOnResumeCallback(OnResumeCallback callback) {
1842 if (mOnResumeCallback != null) {
1843 mOnResumeCallback.onLauncherResume();
1844 }
1845 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07001846 }
1847
Michael Jurka7607c2f2013-04-03 14:33:19 -07001848 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001849 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001850 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07001851 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001852 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07001853 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001854 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07001855 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07001856 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07001857 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001858 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001859
Joe Onorato9c1289c2009-08-17 11:03:03 -04001860 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07001861 * Clear any pending bind callbacks. This is called when is loader is planning to
1862 * perform a full rebind from scratch.
1863 */
1864 @Override
1865 public void clearPendingBinds() {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001866 if (mPendingExecutor != null) {
1867 mPendingExecutor.markCompleted();
1868 mPendingExecutor = null;
1869 }
1870 }
1871
1872 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001873 * Refreshes the shortcuts shown on the workspace.
1874 *
1875 * Implementation of the method from LauncherModel.Callbacks.
1876 */
1877 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001878 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001879 // Floating panels (except the full widget sheet) are associated with individual icons. If
1880 // we are starting a fresh bind, close all such panels as all the icons are about
1881 // to go away.
1882 AbstractFloatingView.closeOpenViews(this, true,
Sunny Goyal7ede6112017-12-05 15:11:21 -08001883 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_REBIND_SAFE);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08001884
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001885 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08001886
Winson Chungd64d1762013-08-20 14:37:16 -07001887 // Clear the workspace because it's going to be rebound
Sunny Goyal2db53422019-03-01 16:06:12 -08001888 mDragController.cancelDrag();
1889
Adam Cohendf035382011-04-11 17:22:04 -07001890 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07001891 mWorkspace.removeAllWorkspaceScreens();
Sunny Goyalc11fac32018-02-27 19:20:15 -08001892 mAppWidgetHost.clearViews();
Winson Chungd64d1762013-08-20 14:37:16 -07001893
Winson Chung3d503fb2011-07-13 17:25:49 -07001894 if (mHotseat != null) {
Sunny Goyalae6e3182019-04-30 12:04:37 -07001895 mHotseat.resetLayout(getWallpaperDeviceProfile().isVerticalBarLayout());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001896 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001897 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001898 }
1899
Adam Cohendcd297f2013-06-18 13:13:40 -07001900 @Override
Sunny Goyalefb7e842018-10-04 15:11:00 -07001901 public void bindScreens(IntArray orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001902 // Make sure the first screen is always at the start.
Jon Miranda7143ba62019-03-15 09:00:05 -07001903 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001904 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalefb7e842018-10-04 15:11:00 -07001905 orderedScreenIds.removeValue(Workspace.FIRST_SCREEN_ID);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001906 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Jon Miranda7143ba62019-03-15 09:00:05 -07001907 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001908 // If there are no screens, we need to have an empty screen
1909 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07001910 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001911 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07001912
Winsonc7d2e832016-07-28 12:24:55 -07001913 // After we have added all the screens, if the wallpaper was locked to the default state,
1914 // then notify to indicate that it can be released and a proper wallpaper offset can be
1915 // computed before the next layout
1916 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07001917 }
1918
Sunny Goyalefb7e842018-10-04 15:11:00 -07001919 private void bindAddScreens(IntArray orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001920 int count = orderedScreenIds.size();
1921 for (int i = 0; i < count; i++) {
Sunny Goyalefb7e842018-10-04 15:11:00 -07001922 int screenId = orderedScreenIds.get(i);
Jon Miranda7143ba62019-03-15 09:00:05 -07001923 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001924 // No need to bind the first screen, as its always bound.
1925 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
1926 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001927 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001928 }
1929
Sunny Goyalb23980c2017-08-17 07:45:25 -07001930 @Override
Tony Wickham6a71a5b2018-08-21 11:40:23 -07001931 public void preAddApps() {
1932 // If there's an undo snackbar, force it to complete to ensure empty screens are removed
1933 // before trying to add new items.
1934 mModelWriter.commitDelete();
1935 AbstractFloatingView snackbar = AbstractFloatingView.getOpenView(this, TYPE_SNACKBAR);
1936 if (snackbar != null) {
1937 snackbar.post(() -> snackbar.close(true));
1938 }
1939 }
1940
1941 @Override
Sunny Goyalefb7e842018-10-04 15:11:00 -07001942 public void bindAppsAdded(IntArray newScreens, ArrayList<ItemInfo> addNotAnimated,
Sunny Goyal29947f02017-12-18 13:49:44 -08001943 ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07001944 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08001945 if (newScreens != null) {
1946 bindAddScreens(newScreens);
1947 }
Winson Chungd64d1762013-08-20 14:37:16 -07001948
1949 // We add the items without animation on non-visible pages, and with
1950 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08001951 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001952 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001953 }
Adam Cohen76a47a12014-02-05 11:47:43 -08001954 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001955 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07001956 }
Winson Chungc58497e2013-09-03 17:48:37 -07001957
Winson Chung87412982013-10-03 18:34:14 -07001958 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07001959 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001960 }
1961
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001962 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001963 * Bind the items start-end from the list.
1964 *
1965 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001966 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07001967 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07001968 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001969 // Get the list of added items and intersect them with the set of items here
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001970 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07001971 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001972 Workspace workspace = mWorkspace;
Sunny Goyalefb7e842018-10-04 15:11:00 -07001973 int newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07001974 int end = items.size();
1975 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001976 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07001977
1978 // Short circuit if we are loading dock items for a configuration which has no dock
1979 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
1980 mHotseat == null) {
1981 continue;
1982 }
1983
Sunny Goyal639e9062015-08-19 19:17:06 -07001984 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001985 switch (item.itemType) {
1986 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1987 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08001988 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Sunny Goyal95899162019-03-27 16:03:06 -07001989 WorkspaceItemInfo info = (WorkspaceItemInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07001990 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001991 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001992 }
1993 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07001994 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001995 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001996 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001997 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001998 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07001999 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2000 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07002001 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002002 if (view == null) {
2003 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002004 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08002005 break;
2006 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002007 default:
2008 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002009 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002010
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002011 /*
2012 * Remove colliding items.
2013 */
2014 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
2015 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
2016 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
2017 View v = cl.getChildAt(item.cellX, item.cellY);
2018 Object tag = v.getTag();
2019 String desc = "Collision while binding workspace item: " + item
2020 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08002021 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002022 throw (new RuntimeException(desc));
2023 } else {
2024 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002025 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002026 continue;
2027 }
2028 }
2029 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302030 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07002031 if (animateIcons) {
2032 // Animate all the applications up now
2033 view.setAlpha(0f);
2034 view.setScaleX(0f);
2035 view.setScaleY(0f);
2036 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08002037 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07002038 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002039 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07002040
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002041 // Animate to the correct page
2042 if (animateIcons && newItemsScreenId > -1) {
2043 AnimatorSet anim = new AnimatorSet();
2044 anim.playTogether(bounceAnims);
Jon Miranda64eb8ea2018-03-29 11:43:58 -07002045
Sunny Goyalefb7e842018-10-04 15:11:00 -07002046 int currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002047 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
2048 final Runnable startBounceAnimRunnable = anim::start;
2049
2050 if (newItemsScreenId != currentScreenId) {
2051 // We post the animation slightly delayed to prevent slowdowns
2052 // when we are loading right after we return to launcher.
2053 mWorkspace.postDelayed(new Runnable() {
2054 public void run() {
2055 if (mWorkspace != null) {
2056 AbstractFloatingView.closeAllOpenViews(Launcher.this, false);
2057
2058 mWorkspace.snapToPage(newScreenIndex);
2059 mWorkspace.postDelayed(startBounceAnimRunnable,
2060 NEW_APPS_ANIMATION_DELAY);
Winson Chung94d67682013-09-25 16:29:40 -07002061 }
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002062 }
2063 }, NEW_APPS_PAGE_MOVE_DELAY);
2064 } else {
2065 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07002066 }
Winson Chung64359a52013-07-08 17:17:08 -07002067 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002068 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002069 }
2070
Joe Onorato9c1289c2009-08-17 11:03:03 -04002071 /**
2072 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002073 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07002074 public void bindAppWidget(LauncherAppWidgetInfo item) {
2075 View view = inflateAppWidget(item);
2076 if (view != null) {
2077 mWorkspace.addInScreen(view, item);
2078 mWorkspace.requestLayout();
2079 }
2080 }
2081
2082 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002083 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302084 PendingAppWidgetHostView view =
2085 new PendingAppWidgetHostView(this, item, mIconCache, true);
2086 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002087 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002088 }
2089
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002090 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002091
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002092 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08002093
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002094 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2095 // If the provider is not ready, bind as a pending widget.
2096 appWidgetInfo = null;
2097 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2098 // The widget id is not valid. Try to find the widget based on the provider info.
2099 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
2100 } else {
2101 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
2102 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002103
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002104 // If the provider is ready, but the width is not yet restored, try to restore it.
2105 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
2106 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07002107 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002108 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
2109 + " belongs to component " + item.providerName
2110 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002111 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002112 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002113 }
Sunny Goyalff572272014-07-23 13:58:07 -07002114
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002115 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002116 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07002117 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2118 // Id has not been allocated yet. Allocate a new id.
2119 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
2120 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07002121
Sunny Goyald478c832016-04-01 12:04:16 -07002122 // Also try to bind the widget. If the bind fails, the user will be shown
2123 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08002124 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07002125 pendingInfo.spanX = item.spanX;
2126 pendingInfo.spanY = item.spanY;
2127 pendingInfo.minSpanX = item.minSpanX;
2128 pendingInfo.minSpanY = item.minSpanY;
2129 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07002130
2131 boolean isDirectConfig =
2132 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2133 if (isDirectConfig && item.bindOptions != null) {
2134 Bundle newOptions = item.bindOptions.getExtras();
2135 if (options != null) {
2136 newOptions.putAll(options);
2137 }
2138 options = newOptions;
2139 }
Sunny Goyald478c832016-04-01 12:04:16 -07002140 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2141 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002142
Sunny Goyal86df1382016-08-10 15:03:22 -07002143 // We tried to bind once. If we were not able to bind, we would need to
2144 // go through the permission dialog, which means we cannot skip the config
2145 // activity.
2146 item.bindOptions = null;
2147 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2148
Sunny Goyald478c832016-04-01 12:04:16 -07002149 // Bind succeeded
2150 if (success) {
2151 // If the widget has a configure activity, it is still needs to set it up,
2152 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002153 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002154 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2155 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002156 }
Sunny Goyald478c832016-04-01 12:04:16 -07002157
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002158 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002159 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002160 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002161 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002162 // The widget was marked as UI not ready, but there is no configure activity to
2163 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002164 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002165 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002166 }
Sunny Goyalff572272014-07-23 13:58:07 -07002167 }
2168
Sunny Goyald5462aa2016-11-22 16:52:39 +05302169 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002170 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002171 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002172 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002173 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Tony Wickham6a71a5b2018-08-21 11:40:23 -07002174 getModelWriter().deleteWidgetInfo(item, getAppWidgetHost());
Sunny Goyalb23980c2017-08-17 07:45:25 -07002175 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002176 }
2177
Sunny Goyal233ee962015-08-03 13:05:01 -07002178 item.minSpanX = appWidgetInfo.minSpanX;
2179 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302180 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002181 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302182 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002183 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302184 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002185
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002186 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002187 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002188 }
2189
Sunny Goyalff572272014-07-23 13:58:07 -07002190 /**
2191 * Restores a pending widget.
2192 *
2193 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002194 */
Sunny Goyald478c832016-04-01 12:04:16 -07002195 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002196 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2197 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2198 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002199 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002200 }
2201
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002202 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002203 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002204 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2205 info.pendingItemInfo = null;
2206 }
Sunny Goyalff572272014-07-23 13:58:07 -07002207
Sunny Goyalba47faa2017-10-04 16:50:57 -07002208 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002209 view.reInflate();
Sunny Goyalba47faa2017-10-04 16:50:57 -07002210 }
2211
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002212 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002213 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002214 }
2215
Adam Cohen1462de32012-07-24 22:34:36 -07002216 public void onPageBoundSynchronously(int page) {
Sunny Goyalf4aceab2017-12-06 16:16:31 -08002217 mSynchronouslyBoundPage = page;
Adam Cohen1462de32012-07-24 22:34:36 -07002218 }
2219
Sunny Goyal527c7d32015-08-28 15:19:36 -07002220 @Override
2221 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2222 if (mPendingExecutor != null) {
2223 mPendingExecutor.markCompleted();
2224 }
2225 mPendingExecutor = executor;
Sunny Goyal60180b02018-02-07 12:56:30 -08002226 if (!isInState(ALL_APPS)) {
2227 mAppsView.getAppsStore().setDeferUpdates(true);
2228 mPendingExecutor.execute(() -> mAppsView.getAppsStore().setDeferUpdates(false));
2229 }
2230
Sunny Goyal527c7d32015-08-28 15:19:36 -07002231 executor.attachTo(this);
2232 }
2233
2234 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2235 if (mPendingExecutor == executor) {
2236 mPendingExecutor = null;
2237 }
2238 }
2239
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002240 @Override
2241 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
Sunny Goyal6001ea22018-05-10 16:31:00 -07002242 AlphaProperty property = mDragLayer.getAlphaProperty(ALPHA_INDEX_LAUNCHER_LOAD);
2243 if (property.getValue() < 1) {
2244 ObjectAnimator anim = ObjectAnimator.ofFloat(property, MultiValueAlpha.VALUE, 1);
2245 if (executor != null) {
2246 anim.addListener(new AnimatorListenerAdapter() {
2247 @Override
2248 public void onAnimationEnd(Animator animation) {
2249 executor.onLoadAnimationCompleted();
2250 }
2251 });
2252 }
2253 anim.start();
Sunny Goyal29947f02017-12-18 13:49:44 -08002254 } else if (executor != null) {
2255 executor.onLoadAnimationCompleted();
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002256 }
2257 }
2258
Joe Onorato9c1289c2009-08-17 11:03:03 -04002259 /**
2260 * Callback saying that there aren't any more items to bind.
2261 *
2262 * Implementation of the method from LauncherModel.Callbacks.
2263 */
Tonyf80e8932018-12-21 11:58:04 -08002264 public void finishBindingItems(int pageBoundFirst) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002265 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002266 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002267
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002268 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002269
Sunny Goyal2100c782016-08-22 16:00:03 -07002270 if (mPendingActivityResult != null) {
2271 handleActivityResult(mPendingActivityResult.requestCode,
2272 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2273 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002274 }
2275
Sunny Goyala474a9b2017-05-04 16:47:11 -07002276 InstallShortcutReceiver.disableAndFlushInstallQueue(
2277 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002278
Tonyf80e8932018-12-21 11:58:04 -08002279 // When undoing the removal of the last item on a page, return to that page.
Tony Wickham03f27012019-04-25 14:22:54 -07002280 // Since we are just resetting the current page without user interaction,
2281 // override the previous page so we don't log the page switch.
2282 mWorkspace.setCurrentPage(pageBoundFirst, pageBoundFirst /* overridePrevPage */);
Tony Wickham6a71a5b2018-08-21 11:40:23 -07002283
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002284 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002285 }
2286
Winson Chunga2413752012-04-03 14:22:34 -07002287 private boolean canRunNewAppsAnimation() {
2288 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002289 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002290 }
2291
Winson Chungc9168342013-06-26 14:54:55 -07002292 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002293 ValueAnimator bounceAnim = new PropertyListBuilder().alpha(1).scale(1).build(v)
2294 .setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
Winson Chungc9168342013-06-26 14:54:55 -07002295 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002296 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002297 return bounceAnim;
2298 }
2299
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002300 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002301 * Add the icons for all apps.
2302 *
2303 * Implementation of the method from LauncherModel.Callbacks.
2304 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002305 public void bindAllApplications(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002306 mAppsView.getAppsStore().setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002307 }
2308
2309 /**
Zak Cohen658c67a2018-10-19 14:21:05 -07002310 * Copies LauncherModel's map of activities to shortcut counts to Launcher's. This is necessary
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002311 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2312 */
2313 @Override
Zak Cohen658c67a2018-10-19 14:21:05 -07002314 public void bindDeepShortcutMap(HashMap<ComponentKey, Integer> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002315 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002316 }
2317
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002318 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002319 * A package was updated.
2320 *
2321 * Implementation of the method from LauncherModel.Callbacks.
2322 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002323 @Override
2324 public void bindAppsAddedOrUpdated(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002325 mAppsView.getAppsStore().addOrUpdateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002326 }
2327
Sunny Goyal4390ace2014-10-13 11:33:11 -07002328 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002329 public void bindPromiseAppProgressUpdated(PromiseAppInfo app) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002330 mAppsView.getAppsStore().updatePromiseAppProgress(app);
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002331 }
2332
2333 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002334 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002335 mWorkspace.widgetsRestored(widgets);
2336 }
2337
Joe Onorato9c1289c2009-08-17 11:03:03 -04002338 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002339 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002340 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002341 *
2342 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002343 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002344 @Override
Sunny Goyal95899162019-03-27 16:03:06 -07002345 public void bindWorkspaceItemsChanged(ArrayList<WorkspaceItemInfo> updated) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002346 if (!updated.isEmpty()) {
2347 mWorkspace.updateShortcuts(updated);
2348 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002349 }
2350
2351 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002352 * Update the state of a package, typically related to install state.
2353 *
2354 * Implementation of the method from LauncherModel.Callbacks.
2355 */
Sunny Goyale755d462014-07-22 13:48:29 -07002356 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002357 public void bindRestoreItemsChange(HashSet<ItemInfo> updates) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07002358 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07002359 }
2360
2361 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002362 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07002363 * in addition to specific applications to remove, the reason being that
2364 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002365 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07002366 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002367 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07002368 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002369 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002370 mWorkspace.removeItemsByMatcher(matcher);
2371 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002372 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002373
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002374 @Override
2375 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002376 mAppsView.getAppsStore().removeApps(appInfos);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002377 }
Joe Onoratobe386092009-11-17 17:32:16 -08002378
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002379 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002380 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
2381 mPopupDataProvider.setAllWidgets(allWidgets);
Tony Wickham26b17462017-03-20 17:12:24 -07002382 }
2383
Tony Wickham86222d22017-03-29 15:30:43 -07002384 /**
2385 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
2386 * refreshes the widgets and shortcuts associated with the given package/user
2387 */
2388 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07002389 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002390 }
2391
Winson Chung80baf5a2010-08-09 16:03:15 -07002392 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002393 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08002394 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002395 @Override
2396 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
2397 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002398
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002399 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
2400 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07002401 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002402 writer.println(prefix + " Homescreen " + i);
2403
2404 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
2405 for (int j = 0; j < layout.getChildCount(); j++) {
2406 Object tag = layout.getChildAt(j).getTag();
2407 if (tag != null) {
2408 writer.println(prefix + " " + tag.toString());
2409 }
2410 }
2411 }
2412
2413 writer.println(prefix + " Hotseat");
Sunny Goyal876e4622018-11-02 13:50:40 -07002414 ViewGroup layout = mHotseat.getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07002415 for (int j = 0; j < layout.getChildCount(); j++) {
2416 Object tag = layout.getChildAt(j).getTag();
2417 if (tag != null) {
2418 writer.println(prefix + " " + tag.toString());
2419 }
Adam Cohen4caf2982013-08-20 18:54:31 -07002420 }
Sunny Goyala1365452015-10-01 15:46:24 -07002421 }
2422
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002423 writer.println(prefix + "Misc:");
2424 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
2425 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
2426 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
Sunny Goyale43d00d2018-05-14 14:23:18 -07002427 writer.println(" mRotationHelper: " + mRotationHelper);
Sunny Goyalcb2c5052018-10-15 12:30:29 -07002428 // Extra logging for b/116853349
Hyunyoung Songa310a802019-04-25 13:57:16 -07002429 mDragLayer.dump(prefix, writer);
2430 mStateManager.dump(prefix, writer);
Sunny Goyale43d00d2018-05-14 14:23:18 -07002431 dumpMisc(writer);
Sunny Goyal2bba1902018-02-27 12:20:50 -08002432
2433 try {
2434 FileLog.flushAll(writer);
2435 } catch (Exception e) {
2436 // Ignore
2437 }
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002438
2439 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002440
Adam Cohen9211d422014-10-07 18:14:53 -07002441 if (mLauncherCallbacks != null) {
2442 mLauncherCallbacks.dump(prefix, fd, writer, args);
2443 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002444 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002445
Sunny Goyal66b24572016-09-21 15:57:55 -07002446 @Override
Sunny Goyal66b24572016-09-21 15:57:55 -07002447 public void onProvideKeyboardShortcuts(
2448 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
2449
2450 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002451 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002452 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
2453 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
Vadim Tryshev74c261c2018-06-12 13:44:43 -07002454 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.widget_button_text),
2455 KeyEvent.KEYCODE_W, KeyEvent.META_CTRL_ON));
Sunny Goyal66b24572016-09-21 15:57:55 -07002456 }
Vadim Tryshev3455f8d2018-05-11 20:02:20 -07002457 final View currentFocus = getCurrentFocus();
2458 if (currentFocus != null) {
2459 if (new CustomActionsPopup(this, currentFocus).canShow()) {
2460 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
2461 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
2462 }
2463 if (currentFocus.getTag() instanceof ItemInfo
2464 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
2465 shortcutInfos.add(new KeyboardShortcutInfo(
2466 getString(R.string.shortcuts_menu_with_notifications_description),
2467 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
2468 }
Sunny Goyal66b24572016-09-21 15:57:55 -07002469 }
2470 if (!shortcutInfos.isEmpty()) {
2471 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
2472 }
2473
2474 super.onProvideKeyboardShortcuts(data, menu, deviceId);
2475 }
2476
2477 @Override
2478 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
2479 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
2480 switch (keyCode) {
2481 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002482 if (isInState(NORMAL)) {
2483 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07002484 return true;
2485 }
2486 break;
2487 case KeyEvent.KEYCODE_S: {
2488 View focusedView = getCurrentFocus();
2489 if (focusedView instanceof BubbleTextView
2490 && focusedView.getTag() instanceof ItemInfo
2491 && mAccessibilityDelegate.performAction(focusedView,
2492 (ItemInfo) focusedView.getTag(),
2493 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal00ac9202017-11-09 15:24:06 -08002494 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07002495 return true;
2496 }
2497 break;
2498 }
2499 case KeyEvent.KEYCODE_O:
2500 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
2501 return true;
2502 }
2503 break;
Vadim Tryshev74c261c2018-06-12 13:44:43 -07002504 case KeyEvent.KEYCODE_W:
2505 if (isInState(NORMAL)) {
2506 OptionsPopupView.openWidgets(this);
2507 return true;
2508 }
2509 break;
Sunny Goyal66b24572016-09-21 15:57:55 -07002510 }
2511 }
2512 return super.onKeyShortcut(keyCode, event);
2513 }
2514
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002515 @Override
2516 public boolean onKeyUp(int keyCode, KeyEvent event) {
2517 if (keyCode == KeyEvent.KEYCODE_MENU) {
2518 // KEYCODE_MENU is sent by some tests, for example
2519 // LauncherJankTests#testWidgetsContainerFling. Don't just remove its handling.
2520 if (!mDragController.isDragging() && !mWorkspace.isSwitchingState() &&
2521 isInState(NORMAL)) {
2522 // Close any open floating views.
2523 AbstractFloatingView.closeAllOpenViews(this);
2524
2525 // Setting the touch point to (-1, -1) will show the options popup in the center of
2526 // the screen.
Sunny Goyal2fd7a8b2018-03-30 17:10:13 -07002527 OptionsPopupView.showDefaultOptions(this, -1, -1);
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002528 }
2529 return true;
2530 }
2531 return super.onKeyUp(keyCode, event);
2532 }
2533
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002534 public static Launcher getLauncher(Context context) {
Sunny Goyal87b5eb62018-07-03 15:53:39 -07002535 return (Launcher) fromContext(context);
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002536 }
2537
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002538 /**
Sunny Goyalab770a12018-11-14 15:17:26 -08002539 * Just a wrapper around the type cast to allow easier tracking of calls.
2540 */
2541 public static <T extends Launcher> T cast(ActivityContext activityContext) {
2542 return (T) activityContext;
2543 }
2544
2545 /**
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002546 * Callback for listening for onResume
2547 */
2548 public interface OnResumeCallback {
2549
2550 void onLauncherResume();
2551 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002552}