blob: 823fb6b0a21c0275625f2abd7f5a5875f567e4e2 [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;
Sunny Goyalae6e3182019-04-30 12:04:37 -070075import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080076import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070077import android.view.animation.OvershootInterpolator;
Adam Cohen96d30a12013-07-16 18:13:21 -070078import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070079
Tony Wickham03f27012019-04-25 14:22:54 -070080import androidx.annotation.Nullable;
81
Sunny Goyal651077b2014-06-30 14:15:31 -070082import com.android.launcher3.DropTarget.DragObject;
Sunny Goyalae502842016-06-17 08:43:56 -070083import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070084import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070085import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal7ede6112017-12-05 15:11:21 -080086import com.android.launcher3.allapps.DiscoveryBounce;
Sunny Goyalf0b6db72018-08-13 16:10:14 -070087import com.android.launcher3.anim.PropertyListBuilder;
Sunny Goyalffe83f12014-08-14 17:39:34 -070088import com.android.launcher3.compat.AppWidgetManagerCompat;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -070089import com.android.launcher3.compat.LauncherAppsCompatVO;
Tony Wickhame0c33232016-02-08 11:37:04 -080090import com.android.launcher3.config.FeatureFlags;
vadimt00d42552018-12-11 17:59:36 -080091import com.android.launcher3.dot.DotInfo;
Vadim Tryshevfedca432015-08-19 17:55:02 -070092import com.android.launcher3.dragndrop.DragController;
93import com.android.launcher3.dragndrop.DragLayer;
94import com.android.launcher3.dragndrop.DragView;
Jon Mirandaaeb4dd02018-07-11 14:16:23 -070095import com.android.launcher3.folder.Folder;
Sunny Goyal26119432016-02-18 22:09:23 +000096import com.android.launcher3.folder.FolderIcon;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070097import com.android.launcher3.folder.FolderIconPreviewVerifier;
Sunny Goyalae6e3182019-04-30 12:04:37 -070098import com.android.launcher3.graphics.RotationMode;
Sunny Goyalf840f102018-09-21 14:41:05 -070099import com.android.launcher3.icons.IconCache;
Sunny Goyal66b24572016-09-21 15:57:55 -0700100import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700101import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700102import com.android.launcher3.logging.FileLog;
Hyunyoung Songfc007472018-10-25 14:09:50 -0700103import com.android.launcher3.logging.StatsLogUtils;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700104import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700105import com.android.launcher3.logging.UserEventDispatcher.UserEventDelegate;
Sunny Goyal369212a2019-03-26 15:03:57 -0700106import com.android.launcher3.model.AppLaunchTracker;
Sunny Goyala535ae42017-02-27 10:07:13 -0800107import com.android.launcher3.model.ModelWriter;
Sunny Goyala535ae42017-02-27 10:07:13 -0800108import com.android.launcher3.notification.NotificationListener;
Tony Wickham540913e2017-01-23 11:47:51 -0800109import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -0800110import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700111import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyalf8088ee2017-11-10 14:52:00 -0800112import com.android.launcher3.states.InternalStateHandler;
Sunny Goyal623eddd2018-03-02 12:24:41 -0800113import com.android.launcher3.states.RotationHelper;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800114import com.android.launcher3.touch.ItemClickHandler;
Sunny Goyala5ace712017-11-15 17:12:51 -0800115import com.android.launcher3.uioverrides.UiFactory;
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700116import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530117import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
118import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
Sunny Goyalaeb16432017-10-16 11:46:41 -0700119import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyal2100c782016-08-22 16:00:03 -0700120import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700121import com.android.launcher3.util.ComponentKey;
Sunny Goyalefb7e842018-10-04 15:11:00 -0700122import com.android.launcher3.util.IntArray;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700123import com.android.launcher3.util.ItemInfoMatcher;
Sunny Goyal6001ea22018-05-10 16:31:00 -0700124import com.android.launcher3.util.MultiValueAlpha;
125import com.android.launcher3.util.MultiValueAlpha.AlphaProperty;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700126import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800127import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700128import com.android.launcher3.util.PendingRequestArgs;
vadimt00d42552018-12-11 17:59:36 -0800129import com.android.launcher3.util.RaceConditionTracker;
Sunny Goyal8392c822017-06-20 10:03:56 -0700130import com.android.launcher3.util.SystemUiController;
Mario Bertschlera6936942017-05-31 14:48:19 -0700131import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700132import com.android.launcher3.util.Thunk;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700133import com.android.launcher3.util.TraceHelper;
Sunny Goyal326403e2017-10-02 12:45:10 -0700134import com.android.launcher3.util.UiThreadHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700135import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyalab770a12018-11-14 15:17:26 -0800136import com.android.launcher3.views.ActivityContext;
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800137import com.android.launcher3.views.OptionsPopupView;
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400138import com.android.launcher3.views.ScrimView;
Sunny Goyal29947f02017-12-18 13:49:44 -0800139import com.android.launcher3.widget.LauncherAppWidgetHostView;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800140import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700141import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal29947f02017-12-18 13:49:44 -0800142import com.android.launcher3.widget.PendingAppWidgetHostView;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800143import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700144import com.android.launcher3.widget.WidgetHostViewLoader;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700145import com.android.launcher3.widget.WidgetListRowEntry;
146import com.android.launcher3.widget.WidgetsFullSheet;
Sunny Goyal952e63d2017-08-16 04:59:08 -0700147import com.android.launcher3.widget.custom.CustomWidgetParser;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700148
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700149import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700150import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700151import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700152import java.util.Collection;
Zak Cohen658c67a2018-10-19 14:21:05 -0700153import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700154import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700155import java.util.List;
Sunny Goyalaae6fbb2019-01-31 16:05:58 -0800156import java.util.function.Predicate;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700157
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158/**
159 * Default launcher application.
160 */
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700161public class Launcher extends BaseDraggingActivity implements LauncherExterns,
Hyunyoung Song31971a32019-01-27 12:31:12 -0800162 LauncherModel.Callbacks, LauncherProviderChangeListener, UserEventDelegate,
163 InvariantDeviceProfile.OnIDPChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700164 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700165 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166
Winson Chunga2413752012-04-03 14:22:34 -0700167 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700168
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700170 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700171
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700172 private static final int REQUEST_PICK_APPWIDGET = 9;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173
Michael Jurka8b805b12012-04-18 14:23:14 -0700174 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800175 public static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
176 public static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700177
Jon Mirandac476d6e2017-05-04 16:30:01 -0700178 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700179
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700180 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
181
Mathew Inwood876a8462013-06-14 14:12:41 +0100182 /**
183 * IntentStarter uses request codes starting with this. This must be greater than all activity
184 * request codes used internally.
185 */
186 protected static final int REQUEST_LAST = 100;
187
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188 // Type: int
189 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700190 // Type: int
191 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700192 // Type: PendingRequestArgs
193 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
194 // Type: ActivityResultInfo
195 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700196 // Type: SparseArray<Parcelable>
197 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
vadimt00d42552018-12-11 17:59:36 -0800198 public static final String ON_CREATE_EVT = "Launcher.onCreate";
vadimtcb863752018-12-19 17:44:57 -0800199 private static final String ON_START_EVT = "Launcher.onStart";
200 private static final String ON_RESUME_EVT = "Launcher.onResume";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800201
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700202 private LauncherStateManager mStateManager;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700203
Adam Cohenad4e15c2013-10-17 16:21:35 -0700204 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205
Winson Chunga2413752012-04-03 14:22:34 -0700206 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700207 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
208 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
209 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700210
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400211 private static final int APPS_VIEW_ALPHA_CHANNEL_INDEX = 1;
212 private static final int SCRIM_VIEW_ALPHA_CHANNEL_INDEX = 0;
213
Jon Miranda54441f52018-01-24 15:38:25 -0800214 private LauncherAppTransitionManager mAppTransitionManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800215 private Configuration mOldConfig;
Jon Miranda54441f52018-01-24 15:38:25 -0800216
Adam Cohen091440a2015-03-18 14:16:05 -0700217 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700218 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700219 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800220 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700221
Sunny Goyalffe83f12014-08-14 17:39:34 -0700222 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700223 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700224
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700225 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700226
Sunny Goyal316490e2015-06-02 09:38:28 -0700227 @Thunk Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700228
Sunny Goyal47328fd2016-05-25 18:56:41 -0700229 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700230
231 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700232 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700233 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700234
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400235 // Scrim view for the all apps and overview state.
236 @Thunk ScrimView mScrimView;
237
Winson Chung8ae41982017-11-10 11:42:13 -0800238 // UI and state for the overview panel
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800239 private View mOverviewPanel;
Winson Chung8ae41982017-11-10 11:42:13 -0800240
Adam Cohen091440a2015-03-18 14:16:05 -0700241 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400242
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700243 private OnResumeCallback mOnResumeCallback;
244
Sunny Goyal527c7d32015-08-28 15:19:36 -0700245 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700246
Joe Onorato9c1289c2009-08-17 11:03:03 -0400247 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800248 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800249 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700250 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400251
Tony Wickham010d2552017-01-20 08:15:28 -0800252 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700253
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800254 private int mSynchronouslyBoundPage = PagedView.INVALID_PAGE;
Adam Cohen1462de32012-07-24 22:34:36 -0700255
Winson Chung46353de2012-02-16 14:05:10 -0800256 // We only want to get the SharedPreferences once since it does an FS stat each time we get
257 // it from the context.
258 private SharedPreferences mSharedPrefs;
259
Sunny Goyal2100c782016-08-22 16:00:03 -0700260 // Activity result which needs to be processed after workspace has loaded.
261 private ActivityResultInfo mPendingActivityResult;
262 /**
263 * Holds extra information required to handle a result from an external call, like
264 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
265 */
266 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800267
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700268 public ViewGroupFocusHelper mFocusHandler;
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700269
Sunny Goyal623eddd2018-03-02 12:24:41 -0800270 private RotationHelper mRotationHelper;
Sunny Goyal5743f862019-03-28 15:35:32 -0700271 private Runnable mCancelTouchController;
Sunny Goyal7779d622015-06-11 16:18:39 -0700272
Sunny Goyal2db53422019-03-01 16:06:12 -0800273 final Handler mHandler = new Handler();
Sunny Goyala002c6c2019-02-12 16:20:10 -0800274 private final Runnable mHandleDeferredResume = this::handleDeferredResume;
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700275
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400276 private float mCurrentAssistantVisibility = 0f;
277
Sunny Goyalae6e3182019-04-30 12:04:37 -0700278 private DeviceProfile mStableDeviceProfile;
279 private RotationMode mRotationMode = RotationMode.NORMAL;
280
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800281 @Override
282 protected void onCreate(Bundle savedInstanceState) {
vadimt00d42552018-12-11 17:59:36 -0800283 RaceConditionTracker.onEvent(ON_CREATE_EVT, ENTER);
Winson Chunga2413752012-04-03 14:22:34 -0700284 if (DEBUG_STRICT_MODE) {
285 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
286 .detectDiskReads()
287 .detectDiskWrites()
288 .detectNetwork() // or .detectAll() for all detectable problems
289 .penaltyLog()
290 .build());
291 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
292 .detectLeakedSqlLiteObjects()
293 .detectLeakedClosableObjects()
294 .penaltyLog()
295 .penaltyDeath()
296 .build());
297 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700298 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700299
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800300 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700301 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400302
Sunny Goyal87f784c2017-01-11 10:48:34 -0800303 LauncherAppState app = LauncherAppState.getInstance(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800304 mOldConfig = new Configuration(getResources().getConfiguration());
Sunny Goyal0e7724c2018-02-22 13:22:21 -0800305 mModel = app.setLauncher(this);
Hyunyoung Song31971a32019-01-27 12:31:12 -0800306 InvariantDeviceProfile idp = app.getInvariantDeviceProfile();
307 initDeviceProfile(idp);
308 idp.addOnChangeListener(this);
Sunny Goyalf7258242015-10-19 16:59:07 -0700309 mSharedPrefs = Utilities.getPrefs(this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800310 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700311 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700312
Joe Onorato41a12d22009-10-31 18:30:00 -0400313 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700314 mAllAppsController = new AllAppsTransitionController(this);
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800315 mStateManager = new LauncherStateManager(this);
Tracy Zhou8b23c6f2018-04-30 17:13:27 -0700316 UiFactory.onCreate(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700317
Sunny Goyalffe83f12014-08-14 17:39:34 -0700318 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700319
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700320 mAppWidgetHost = new LauncherAppWidgetHost(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700321 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700322
Sunny Goyal60820d72017-05-09 12:40:11 -0700323 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700324
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800325 setupViews();
Tony Wickham010d2552017-01-20 08:15:28 -0800326 mPopupDataProvider = new PopupDataProvider(this);
327
Sunny Goyal623eddd2018-03-02 12:24:41 -0800328 mRotationHelper = new RotationHelper(this);
Sunny Goyal90548432018-04-04 17:17:00 -0700329 mAppTransitionManager = LauncherAppTransitionManager.newInstance(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800330
Sunny Goyald3864fa2017-11-14 15:06:36 -0800331 boolean internalStateHandled = InternalStateHandler.handleCreate(this, getIntent());
332 if (internalStateHandled) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800333 if (savedInstanceState != null) {
334 // InternalStateHandler has already set the appropriate state.
335 // We dont need to do anything.
336 savedInstanceState.remove(RUNTIME_STATE);
337 }
338 }
339 restoreState(savedInstanceState);
Sunny Goyalbbece862019-03-01 13:13:52 -0800340 mStateManager.reapplyState();
Winson Chungb63b44c2017-11-10 17:54:44 -0800341
Sunny Goyal2100c782016-08-22 16:00:03 -0700342 // We only load the page synchronously if the user rotates (or triggers a
343 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700344 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
345 if (savedInstanceState != null) {
346 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
347 }
Sunny Goyald3864fa2017-11-14 15:06:36 -0800348
Sunny Goyalfe770c92016-09-27 14:38:58 -0700349 if (!mModel.startLoader(currentScreen)) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800350 if (!internalStateHandled) {
351 // If we are not binding synchronously, show a fade in animation when
352 // the first page bind completes.
Sunny Goyal6001ea22018-05-10 16:31:00 -0700353 mDragLayer.getAlphaProperty(ALPHA_INDEX_LAUNCHER_LOAD).setValue(0);
Sunny Goyald3864fa2017-11-14 15:06:36 -0800354 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700355 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700356 // Pages bound synchronously.
357 mWorkspace.setCurrentPage(currentScreen);
358
Sunny Goyal2100c782016-08-22 16:00:03 -0700359 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800360 }
361
362 // For handling default keys
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800363 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800364
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800365 setContentView(mLauncherView);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800366 getRootView().dispatchInsets();
Mario Bertschlera6936942017-05-31 14:48:19 -0700367
Jon Miranda7fda2852017-07-19 16:07:01 -0700368 // Listen for broadcasts
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700369 registerReceiver(mScreenOffReceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));
Sunny Goyala616d2b2017-06-12 13:54:01 -0700370
Sunny Goyal8392c822017-06-20 10:03:56 -0700371 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
372 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700373
374 if (mLauncherCallbacks != null) {
375 mLauncherCallbacks.onCreate(savedInstanceState);
376 }
Sunny Goyal623eddd2018-03-02 12:24:41 -0800377 mRotationHelper.initialize();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700378
379 TraceHelper.endSection("Launcher-onCreate");
vadimt00d42552018-12-11 17:59:36 -0800380 RaceConditionTracker.onEvent(ON_CREATE_EVT, EXIT);
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400381 mStateManager.addStateListener(new LauncherStateManager.StateListener() {
382 @Override
383 public void onStateTransitionStart(LauncherState toState) {}
384
385 @Override
386 public void onStateTransitionComplete(LauncherState finalState) {
387 float alpha = 1f - mCurrentAssistantVisibility;
388 if (finalState == NORMAL) {
389 mAppsView.getAlphaProperty(APPS_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
390 } else if (finalState == OVERVIEW || finalState == OVERVIEW_PEEK) {
391 mAppsView.getAlphaProperty(APPS_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
392 mScrimView.getAlphaProperty(SCRIM_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
393 } else {
394 mAppsView.getAlphaProperty(APPS_VIEW_ALPHA_CHANNEL_INDEX).setValue(1f);
395 mScrimView.getAlphaProperty(SCRIM_VIEW_ALPHA_CHANNEL_INDEX).setValue(1f);
396 }
397 }
398 });
Sunny Goyalae6e3182019-04-30 12:04:37 -0700399
400 if (FeatureFlags.FAKE_LANDSCAPE_UI.get()) {
401 WindowManager.LayoutParams lp = getWindow().getAttributes();
402 lp.rotationAnimation = WindowManager.LayoutParams.ROTATION_ANIMATION_SEAMLESS;
403 getWindow().setAttributes(lp);
404 }
Adam Cohen9211d422014-10-07 18:14:53 -0700405 }
406
Sunny Goyal7779d622015-06-11 16:18:39 -0700407 @Override
Winson Chung8eb49e02018-07-16 13:33:31 -0700408 public void onEnterAnimationComplete() {
409 super.onEnterAnimationComplete();
410 UiFactory.onEnterAnimationComplete(this);
arangelovad783052018-08-17 14:46:45 +0100411 mAllAppsController.highlightWorkTabIfNecessary();
Winson Chung8eb49e02018-07-16 13:33:31 -0700412 }
413
414 @Override
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800415 public void onConfigurationChanged(Configuration newConfig) {
416 int diff = newConfig.diff(mOldConfig);
Jon Mirandaaeb4dd02018-07-11 14:16:23 -0700417
418 if ((diff & CONFIG_LOCALE) != 0) {
419 Folder.setLocaleDependentFields(getResources(), true /* force */);
420 }
421
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800422 if ((diff & (CONFIG_ORIENTATION | CONFIG_SCREEN_SIZE)) != 0) {
Sunny Goyal371ea052019-03-06 15:38:32 -0800423 onIdpChanged(mDeviceProfile.inv);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800424 }
425
426 mOldConfig.setTo(newConfig);
Tracy Zhoua706f002018-03-28 13:55:19 -0700427 UiFactory.onLauncherStateOrResumeChanged(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800428 super.onConfigurationChanged(newConfig);
429 }
430
Sunny Goyal605bcf32018-03-02 15:16:12 -0800431 @Override
Sunny Goyal59d086c2018-05-07 17:31:40 -0700432 protected void reapplyUi() {
Sunny Goyalae6e3182019-04-30 12:04:37 -0700433 if (FeatureFlags.FAKE_LANDSCAPE_UI.get()) {
Sunny Goyal7a39b1b2019-05-02 13:39:16 -0700434 mRotationMode = mStableDeviceProfile == null
435 ? RotationMode.NORMAL : UiFactory.getRotationMode(mDeviceProfile);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700436 }
Sunny Goyal59d086c2018-05-07 17:31:40 -0700437 getRootView().dispatchInsets();
438 getStateManager().reapplyState(true /* cancelCurrentAnimation */);
439 }
440
441 @Override
Sunny Goyal605bcf32018-03-02 15:16:12 -0800442 public void rebindModel() {
443 int currentPage = mWorkspace.getNextPage();
444 if (mModel.startLoader(currentPage)) {
445 mWorkspace.setCurrentPage(currentPage);
446 setWorkspaceLoading(true);
447 }
448 }
449
Hyunyoung Song31971a32019-01-27 12:31:12 -0800450 @Override
451 public void onIdpChanged(int changeFlags, InvariantDeviceProfile idp) {
Sunny Goyal371ea052019-03-06 15:38:32 -0800452 onIdpChanged(idp);
453 }
454
455 private void onIdpChanged(InvariantDeviceProfile idp) {
456 mUserEventDispatcher = null;
457
Hyunyoung Song31971a32019-01-27 12:31:12 -0800458 initDeviceProfile(idp);
Sunny Goyal371ea052019-03-06 15:38:32 -0800459 dispatchDeviceProfileChanged();
460 reapplyUi();
461 mDragLayer.recreateControllers();
462
463 // TODO: We can probably avoid rebind when only screen size changed.
464 rebindModel();
Hyunyoung Song31971a32019-01-27 12:31:12 -0800465 }
466
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400467 public void onAssistantVisibilityChanged(float visibility) {
468 mCurrentAssistantVisibility = visibility;
469 float alpha = 1f - visibility;
470 LauncherState state = mStateManager.getState();
471 if (state == NORMAL) {
472 mAppsView.getAlphaProperty(APPS_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
473 } else if (state == OVERVIEW || state == OVERVIEW_PEEK) {
474 mAppsView.getAlphaProperty(APPS_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
475 mScrimView.getAlphaProperty(SCRIM_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
476 }
477 }
478
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800479 private void initDeviceProfile(InvariantDeviceProfile idp) {
480 // Load configuration-specific DeviceProfile
Sunny Goyal59d086c2018-05-07 17:31:40 -0700481 mDeviceProfile = idp.getDeviceProfile(this);
Sunny Goyal8c48d8b2019-01-25 15:10:18 -0800482 if (isInMultiWindowMode()) {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700483 Display display = getWindowManager().getDefaultDisplay();
484 Point mwSize = new Point();
485 display.getSize(mwSize);
486 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
487 }
Sunny Goyalae6e3182019-04-30 12:04:37 -0700488
489 if (FeatureFlags.FAKE_LANDSCAPE_UI.get() && mDeviceProfile.isVerticalBarLayout()
490 && !mDeviceProfile.isMultiWindowMode) {
491 mStableDeviceProfile = mDeviceProfile.inv.portraitProfile;
Sunny Goyal7a39b1b2019-05-02 13:39:16 -0700492 mRotationMode = UiFactory.getRotationMode(mDeviceProfile);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700493 } else {
494 mStableDeviceProfile = null;
495 mRotationMode = RotationMode.NORMAL;
496 }
497
Sunny Goyal59d086c2018-05-07 17:31:40 -0700498 onDeviceProfileInitiated();
Sunny Goyalae6e3182019-04-30 12:04:37 -0700499 mModelWriter = mModel.getWriter(getWallpaperDeviceProfile().isVerticalBarLayout(), true);
500 }
501
502 public void updateInsets(Rect insets) {
503 mDeviceProfile.updateInsets(insets);
504 if (mStableDeviceProfile != null) {
Sunny Goyal7a39b1b2019-05-02 13:39:16 -0700505 Rect r = mStableDeviceProfile.getInsets();
506 mRotationMode.mapInsets(this, insets, r);
507 mStableDeviceProfile.updateInsets(r);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700508 }
509 }
510
511 @Override
512 public RotationMode getRotationMode() {
513 return mRotationMode;
514 }
515
516 /**
517 * Device profile to be used by UI elements which are shown directly on top of the wallpaper
518 * and whose presentation is tied to the wallpaper (and physical device) and not the activity
519 * configuration.
520 */
521 @Override
522 public DeviceProfile getWallpaperDeviceProfile() {
523 return mStableDeviceProfile == null ? mDeviceProfile : mStableDeviceProfile;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800524 }
525
Sunny Goyal623eddd2018-03-02 12:24:41 -0800526 public RotationHelper getRotationHelper() {
527 return mRotationHelper;
528 }
529
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700530 public LauncherStateManager getStateManager() {
531 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700532 }
533
Mario Bertschler27288382017-05-24 15:35:09 -0700534 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700535 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800536 return mLauncherView.findViewById(id);
537 }
538
539 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700540 public void onAppWidgetHostReset() {
541 if (mAppWidgetHost != null) {
542 mAppWidgetHost.startListening();
543 }
544 }
545
Adam Cohen9211d422014-10-07 18:14:53 -0700546 private LauncherCallbacks mLauncherCallbacks;
547
Sunny Goyal32554d12015-12-03 15:31:25 -0800548 /**
549 * Call this after onCreate to set or clear overlay.
550 */
551 public void setLauncherOverlay(LauncherOverlay overlay) {
552 if (overlay != null) {
553 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
554 }
555 mWorkspace.setLauncherOverlay(overlay);
556 }
557
Adam Cohen9211d422014-10-07 18:14:53 -0700558 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
559 mLauncherCallbacks = callbacks;
560 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700561 }
562
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700563 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700564 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700565 if (mLauncherCallbacks != null) {
566 mLauncherCallbacks.onLauncherProviderChange();
567 }
568 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700569
Hyunyoung Song3f471442015-04-08 19:01:34 -0700570 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700571 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
572 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700573 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700574 }
575
Tony Wickham010d2552017-01-20 08:15:28 -0800576 public PopupDataProvider getPopupDataProvider() {
577 return mPopupDataProvider;
578 }
579
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700580 @Override
Tony Wickhamf34bee82018-12-03 18:11:39 -0800581 public DotInfo getDotInfoForItem(ItemInfo info) {
582 return mPopupDataProvider.getDotInfoForItem(info);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700583 }
584
585 @Override
586 public void invalidateParent(ItemInfo info) {
Jon Mirandae6f3fa42019-03-04 09:14:40 -0800587 if (info.container >= 0) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700588 View folderIcon = getWorkspace().getHomescreenIconByItemId(info.container);
Jon Mirandae6f3fa42019-03-04 09:14:40 -0800589 if (folderIcon instanceof FolderIcon && folderIcon.getTag() instanceof FolderInfo) {
590 FolderIconPreviewVerifier verifier =
591 new FolderIconPreviewVerifier(getDeviceProfile().inv);
592 verifier.setFolderInfo((FolderInfo) folderIcon.getTag());
593 if (verifier.isItemInPreview(info.rank)) {
594 folderIcon.invalidate();
595 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700596 }
597 }
598 }
599
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000600 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700601 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
602 * a configuration step, this allows the proper animations to run after other transitions.
603 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700604 private int completeAdd(
Sunny Goyal2100c782016-08-22 16:00:03 -0700605 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
Sunny Goyalefb7e842018-10-04 15:11:00 -0700606 int screenId = info.screenId;
Sunny Goyal2100c782016-08-22 16:00:03 -0700607 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700608 // When the screen id represents an actual screen (as opposed to a rank) we make sure
609 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700610 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700611 }
Adam Cohendb364c32014-05-20 14:23:40 -0700612
Sunny Goyal2100c782016-08-22 16:00:03 -0700613 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800614 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700615 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700616 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800617 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700618 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700619 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700620 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700621 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700622 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700623 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700624 int widgetId = appWidgetId;
625 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700626 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700627 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700628 // Since the view was just bound, also launch the configure activity if needed
629 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
630 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800631 if (provider != null) {
632 new WidgetAddFlowHandler(provider)
633 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700634 }
635 }
636 break;
637 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800638 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700639
Adam Cohendb364c32014-05-20 14:23:40 -0700640 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800641 }
642
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800643 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700644 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700645 if (isWorkspaceLoading()) {
646 // process the result once the workspace has loaded.
647 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
648 return;
649 }
650 mPendingActivityResult = null;
651
Winson Chunge341d302013-08-16 14:31:00 -0700652 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700653 final PendingRequestArgs requestArgs = mPendingRequestArgs;
654 setWaitingForResult(null);
655 if (requestArgs == null) {
656 return;
657 }
658
659 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700660
Adam Cohenad4e15c2013-10-17 16:21:35 -0700661 Runnable exitSpringLoaded = new Runnable() {
662 @Override
663 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700664 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700665 }
666 };
667
Michael Jurka8b805b12012-04-18 14:23:14 -0700668 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700669 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700670 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700671 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
672 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700673 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700674 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700675 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700676 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700677 addAppWidgetImpl(
678 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800679 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700680 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700681 }
682 return;
683 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200684
Adam Cohened66b2b2012-01-23 17:28:51 -0800685 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700686 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700687
Adam Cohened66b2b2012-01-23 17:28:51 -0800688 // We have special handling for widgets
689 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800690 final int appWidgetId;
691 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
692 : -1;
693 if (widgetId < 0) {
694 appWidgetId = pendingAddWidgetId;
695 } else {
696 appWidgetId = widgetId;
697 }
698
Adam Cohenad4e15c2013-10-17 16:21:35 -0700699 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800700 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700701 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
702 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700703 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700704 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700705 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700706 @Override
707 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700708 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700709 }
710 };
Adam Cohendb364c32014-05-20 14:23:40 -0700711
Sunny Goyal2100c782016-08-22 16:00:03 -0700712 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
713 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
714 } else {
715 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
716 // When the screen id represents an actual screen (as opposed to a rank)
717 // we make sure that the drop page actually exists.
718 requestArgs.screenId =
719 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700720 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700721 final CellLayout dropLayout =
722 mWorkspace.getScreenWithId(requestArgs.screenId);
723
724 dropLayout.setDropPending(true);
725 final Runnable onComplete = new Runnable() {
726 @Override
727 public void run() {
728 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
729 dropLayout.setDropPending(false);
730 }
731 };
732 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
733 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700734 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800735 return;
736 }
737
Sunny Goyald478c832016-04-01 12:04:16 -0700738 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
739 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700740 if (resultCode == RESULT_OK) {
741 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700742 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700743 }
744 // Leave the widget in the pending state if the user canceled the configure.
745 return;
746 }
747
Sunny Goyalaad90582015-07-09 14:22:50 -0700748 if (requestCode == REQUEST_CREATE_SHORTCUT) {
749 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700750 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
751 completeAdd(requestCode, data, -1, requestArgs);
752 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
753 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
754
Sunny Goyalaad90582015-07-09 14:22:50 -0700755 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700756 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
757 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800758 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800759 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800760 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800761 }
762
763 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700764 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800765 final int requestCode, final int resultCode, final Intent data) {
766 handleActivityResult(requestCode, resultCode, data);
767 if (mLauncherCallbacks != null) {
768 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
769 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800770 }
771
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700772 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700773 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600774 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700775 PendingRequestArgs pendingArgs = mPendingRequestArgs;
776 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
777 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
778 setWaitingForResult(null);
779
Sunny Goyal28c6b962015-10-12 11:42:05 -0700780 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700781 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700782 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700783 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700784 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700785 Intent intent = pendingArgs.getPendingIntent();
786
Sunny Goyal28c6b962015-10-12 11:42:05 -0700787 if (grantResults.length > 0
788 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal369212a2019-03-26 15:03:57 -0700789 startActivitySafely(v, intent, null, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700790 } else {
791 // TODO: Show a snack bar with link to settings
792 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700793 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700794 }
795 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600796 if (mLauncherCallbacks != null) {
797 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
798 grantResults);
799 }
800 }
801
Adam Cohendb364c32014-05-20 14:23:40 -0700802 /**
803 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
804 *
805 * @param screenId the screen id to check
806 * @return the new screen, or screenId if it exists
807 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700808 private int ensurePendingDropLayoutExists(int screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800809 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700810 if (dropLayout == null) {
811 // it's possible that the add screen was removed because it was
812 // empty and a re-bind occurred
813 mWorkspace.addExtraEmptyScreen();
814 return mWorkspace.commitExtraEmptyScreen();
815 } else {
816 return screenId;
817 }
818 }
819
Sunny Goyal2100c782016-08-22 16:00:03 -0700820 @Thunk void completeTwoStageWidgetDrop(
821 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
822 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800823 Runnable onCompleteRunnable = null;
824 int animationType = 0;
825
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700826 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800827 if (resultCode == RESULT_OK) {
828 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
829 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800830 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700831 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800832 onCompleteRunnable = new Runnable() {
833 @Override
834 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700835 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700836 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800837 }
838 };
839 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800840 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800841 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800842 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700843 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700844 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700845 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
846 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700847 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700848 // The animated view may be null in the case of a rotation during widget configuration
849 onCompleteRunnable.run();
850 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800851 }
852
853 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700854 protected void onStop() {
855 super.onStop();
Adam Cohen9211d422014-10-07 18:14:53 -0700856 if (mLauncherCallbacks != null) {
857 mLauncherCallbacks.onStop();
858 }
Miranda Kephart1a359a22019-02-11 13:07:04 -0500859
Hyunyoung Song1241e612018-05-15 21:46:51 -0700860 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
861 mStateManager.getState().containerType, -1);
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700862
863 mAppWidgetHost.setListenIfResumed(false);
864
Tony Wickham010d2552017-01-20 08:15:28 -0800865 NotificationListener.removeNotificationsChangedListener();
Sunny Goyalaf161fd2018-02-05 16:34:57 -0800866 getStateManager().moveToRestState();
Winson Chungedb0d6b2018-03-21 17:32:55 -0700867
Tracy Zhou62646712018-06-26 14:19:02 -0700868 UiFactory.onLauncherStateOrResumeChanged(this);
869
Winson Chungdea51352018-04-24 13:02:10 -0700870 // Workaround for b/78520668, explicitly trim memory once UI is hidden
Winson Chung473730f2018-04-26 11:54:22 -0700871 onTrimMemory(TRIM_MEMORY_UI_HIDDEN);
Michael Jurkacd496d72013-04-11 11:32:45 -0700872 }
873
874 @Override
875 protected void onStart() {
vadimtcb863752018-12-19 17:44:57 -0800876 RaceConditionTracker.onEvent(ON_START_EVT, ENTER);
Michael Jurkacd496d72013-04-11 11:32:45 -0700877 super.onStart();
Adam Cohen9211d422014-10-07 18:14:53 -0700878 if (mLauncherCallbacks != null) {
879 mLauncherCallbacks.onStart();
880 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800881 mAppWidgetHost.setListenIfResumed(true);
Sunny Goyalcac002d2018-01-04 09:51:45 -0800882 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
vadimtcb863752018-12-19 17:44:57 -0800883 RaceConditionTracker.onEvent(ON_START_EVT, EXIT);
Michael Jurkacd496d72013-04-11 11:32:45 -0700884 }
885
Sunny Goyala002c6c2019-02-12 16:20:10 -0800886 private void handleDeferredResume() {
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700887 if (hasBeenResumed()) {
888 getUserEventDispatcher().logActionCommand(Action.Command.RESUME,
889 mStateManager.getState().containerType, -1);
890 getUserEventDispatcher().startSession();
Sunny Goyala002c6c2019-02-12 16:20:10 -0800891
892 UiFactory.onLauncherStateOrResumeChanged(this);
Sunny Goyal369212a2019-03-26 15:03:57 -0700893 AppLaunchTracker.INSTANCE.get(this).onReturnedToHome();
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700894 }
895 }
896
Michael Jurkacd496d72013-04-11 11:32:45 -0700897 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800898 protected void onResume() {
vadimtcb863752018-12-19 17:44:57 -0800899 RaceConditionTracker.onEvent(ON_RESUME_EVT, ENTER);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700900 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800901 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700902 TraceHelper.partitionSection("ON_RESUME", "superCall");
903
Sunny Goyala002c6c2019-02-12 16:20:10 -0800904 mHandler.removeCallbacks(mHandleDeferredResume);
905 Utilities.postAsyncCallback(mHandler, mHandleDeferredResume);
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700906
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700907 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700908 // Process any items that were added while Launcher was away.
909 InstallShortcutReceiver.disableAndFlushInstallQueue(
910 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700911
Sunny Goyala474a9b2017-05-04 16:47:11 -0700912 // Refresh shortcuts if the permission changed.
913 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700914
Tracy Zhoue5575f92018-04-19 11:12:53 -0700915 DiscoveryBounce.showForHomeIfNeeded(this);
Adam Cohen9211d422014-10-07 18:14:53 -0700916 if (mLauncherCallbacks != null) {
917 mLauncherCallbacks.onResume();
918 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800919
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700920 TraceHelper.endSection("ON_RESUME");
vadimtcb863752018-12-19 17:44:57 -0800921 RaceConditionTracker.onEvent(ON_RESUME_EVT, EXIT);
Adam Cohen06dff352012-06-01 17:17:08 -0700922 }
923
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800924 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700925 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700926 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700927 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700928
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700929 super.onPause();
Joe Onorato24b6fd82009-11-12 13:47:09 -0800930 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700931 mDragController.resetLastGestureUpTime();
Hyunyoung Song497708c2019-05-01 16:16:53 -0700932 mDropTargetBar.animateToVisibility(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700933 if (mLauncherCallbacks != null) {
934 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700935 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700936 }
937
Winson Chungd7823942018-02-16 03:23:47 +0000938 @Override
Tracy Zhoua706f002018-03-28 13:55:19 -0700939 protected void onUserLeaveHint() {
940 super.onUserLeaveHint();
941 UiFactory.onLauncherStateOrResumeChanged(this);
942 }
943
944 @Override
Winson Chungd7823942018-02-16 03:23:47 +0000945 public void onWindowFocusChanged(boolean hasFocus) {
946 super.onWindowFocusChanged(hasFocus);
947 mStateManager.onWindowFocusChanged();
948 }
949
Adam Cohenc2d6e892014-10-16 09:49:24 -0700950 public interface LauncherOverlay {
951
952 /**
953 * Touch interaction leading to overscroll has begun
954 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700955 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700956
957 /**
958 * Touch interaction related to overscroll has ended
959 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700960 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700961
962 /**
963 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
964 * screen (or in the case of RTL, the rightmost screen).
965 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700966 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700967
968 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800969 * Called when the launcher is ready to use the overlay
970 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700971 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700972 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700973 }
974
975 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700976 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700977 }
978
979 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
980
Sunny Goyalc86df472016-02-25 09:19:38 -0800981 public void onScrollChanged(float progress) {
982 if (mWorkspace != null) {
983 mWorkspace.onOverlayScrollChanged(progress);
984 }
985 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700986 }
987
Sunny Goyalf9403d92017-10-18 10:55:56 -0700988 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -0700989 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700990 }
991
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800992 /**
993 * Restores the previous state, if it exists.
994 *
995 * @param savedState The previous state.
996 */
997 private void restoreState(Bundle savedState) {
998 if (savedState == null) {
999 return;
1000 }
1001
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001002 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -07001003 LauncherState[] stateValues = LauncherState.values();
1004 LauncherState state = stateValues[stateOrdinal];
Sunny Goyalbc683e92017-12-06 10:25:07 -08001005 if (!state.disableRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001006 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001007 }
1008
Sunny Goyal2100c782016-08-22 16:00:03 -07001009 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
1010 if (requestArgs != null) {
1011 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001012 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001013
1014 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001015
1016 SparseArray<Parcelable> widgetsState =
1017 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
1018 if (widgetsState != null) {
1019 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
1020 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001021 }
1022
1023 /**
1024 * Finds all the views we need and configure them properly.
1025 */
1026 private void setupViews() {
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001027 mDragLayer = findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001028 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -07001029 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001030 mWorkspace.initParentViews(mDragLayer);
Sunny Goyal16764582017-11-06 16:00:34 -08001031 mOverviewPanel = findViewById(R.id.overview_panel);
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001032 mHotseat = findViewById(R.id.hotseat);
Craig Mautner360310b2012-10-26 15:13:08 -07001033
Sunny Goyal784f9c32016-03-23 16:56:54 -07001034 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1035 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1036 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001037
Winson Chung4c98d922011-05-31 16:50:48 -07001038 // Setup the drag layer
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001039 mDragLayer.setup(mDragController, mWorkspace);
Sunny Goyal5743f862019-03-28 15:35:32 -07001040 mCancelTouchController = UiFactory.enableLiveTouchControllerChanges(mDragLayer);
Winson Chung4c98d922011-05-31 16:50:48 -07001041
Hyunyoung Song645764e2016-06-06 14:19:02 -07001042 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001043 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1044 // default state, otherwise we will update to the wrong offsets in RTL
1045 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001046 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001047 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001048
Tony Wickham34d2c912015-09-11 09:27:58 -07001049 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001050 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001051
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001052 // Setup Apps
1053 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001054
Govinda Wassermanc06e1512019-04-09 09:56:53 -04001055 // Setup Scrim
1056 mScrimView = findViewById(R.id.scrim_view);
1057
Winson Chung3d503fb2011-07-13 17:25:49 -07001058 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001059 mDragController.setMoveTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001060 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001061
Sunny Goyal7185dd62018-03-14 17:51:49 -07001062 mAllAppsController.setupViews(mAppsView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001063 }
1064
1065 /**
1066 * Creates a view representing a shortcut.
1067 *
1068 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001069 */
Sunny Goyal95899162019-03-27 16:03:06 -07001070 View createShortcut(WorkspaceItemInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001071 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001072 }
1073
1074 /**
1075 * Creates a view representing a shortcut inflated from the specified resource.
1076 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001077 * @param parent The group the shortcut belongs to.
1078 * @param info The data structure describing the shortcut.
1079 *
1080 * @return A View inflated from layoutResId.
1081 */
Sunny Goyal95899162019-03-27 16:03:06 -07001082 public View createShortcut(ViewGroup parent, WorkspaceItemInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -08001083 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
1084 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal95899162019-03-27 16:03:06 -07001085 favorite.applyFromWorkspaceItem(info);
Sunny Goyal8a2a63b2018-03-06 22:15:18 -08001086 favorite.setOnClickListener(ItemClickHandler.INSTANCE);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001087 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001088 return favorite;
1089 }
1090
1091 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -07001092 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001093 *
1094 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001095 */
Sunny Goyalefb7e842018-10-04 15:11:00 -07001096 private void completeAddShortcut(Intent data, int container, int screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001097 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001098 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
1099 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001100 return;
1101 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001102
Jon Mirandac476d6e2017-05-04 16:30:01 -07001103 int[] cellXY = mTmpAddItemCellCoordinates;
1104 CellLayout layout = getCellLayout(container, screenId);
1105
Sunny Goyal95899162019-03-27 16:03:06 -07001106 WorkspaceItemInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001107 if (Utilities.ATLEAST_OREO) {
Sunny Goyal95899162019-03-27 16:03:06 -07001108 info = LauncherAppsCompatVO.createWorkspaceItemFromPinItemRequest(
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001109 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001110 }
1111
1112 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001113 // Legacy shortcuts are only supported for primary profile.
1114 info = Process.myUserHandle().equals(args.user)
1115 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001116
Sunny Goyalb57645f2017-03-20 13:57:28 -07001117 if (info == null) {
1118 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1119 return;
1120 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001121 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001122 // The app is trying to add a shortcut without sufficient permissions
1123 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1124 return;
1125 }
1126 }
1127
Jon Mirandac476d6e2017-05-04 16:30:01 -07001128 if (container < 0) {
1129 // Adding a shortcut to the Workspace.
1130 final View view = createShortcut(info);
1131 boolean foundCellSpan = false;
1132 // First we check if we already know the exact location where we want to add this item.
1133 if (cellX >= 0 && cellY >= 0) {
1134 cellXY[0] = cellX;
1135 cellXY[1] = cellY;
1136 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001137
Jon Mirandac476d6e2017-05-04 16:30:01 -07001138 // If appropriate, either create a folder or add to an existing folder
1139 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001140 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001141 return;
1142 }
1143 DragObject dragObject = new DragObject();
1144 dragObject.dragInfo = info;
1145 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1146 true)) {
1147 return;
1148 }
1149 } else {
1150 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1151 }
1152
1153 if (!foundCellSpan) {
1154 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001155 return;
1156 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001157
1158 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1159 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001160 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001161 // Adding a shortcut to a Folder.
Sunny Goyale29897f2017-07-20 10:09:42 -07001162 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001163 if (folderIcon != null) {
1164 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1165 folderInfo.add(info, args.rank, false);
1166 } else {
Sunny Goyale29897f2017-07-20 10:09:42 -07001167 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001168 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001169 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001170 }
1171
Sunny Goyalefb7e842018-10-04 15:11:00 -07001172 public FolderIcon findFolderIcon(final int folderIconId) {
Sunny Goyal83fd25e2018-07-09 16:47:01 -07001173 return (FolderIcon) mWorkspace.getHomescreenIconByItemId(folderIconId);
Sunny Goyale29897f2017-07-20 10:09:42 -07001174 }
1175
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001176 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001177 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001178 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001179 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001180 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001181 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001182 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1183
Adam Cohened66b2b2012-01-23 17:28:51 -08001184 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001185 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001186 }
Romain Guycbb89e42009-06-08 15:52:54 -07001187
Adam Cohen59400422014-03-05 18:07:04 -08001188 LauncherAppWidgetInfo launcherInfo;
1189 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001190 launcherInfo.spanX = itemInfo.spanX;
1191 launcherInfo.spanY = itemInfo.spanY;
1192 launcherInfo.minSpanX = itemInfo.minSpanX;
1193 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001194 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001195
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001196 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001197 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001198
Sunny Goyal2100c782016-08-22 16:00:03 -07001199 if (hostView == null) {
1200 // Perform actual inflation because we're live
1201 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001202 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001203 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301204 prepareAppWidget(hostView, launcherInfo);
1205 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001206 }
Romain Guycbb89e42009-06-08 15:52:54 -07001207
Sunny Goyald5462aa2016-11-22 16:52:39 +05301208 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001209 hostView.setTag(item);
1210 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001211 hostView.setFocusable(true);
1212 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001213 }
1214
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001215 private final BroadcastReceiver mScreenOffReceiver = new BroadcastReceiver() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001216 @Override
1217 public void onReceive(Context context, Intent intent) {
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001218 // Reset AllApps to its initial state only if we are not in the middle of
1219 // processing a multi-step drop
1220 if (mPendingRequestArgs == null) {
1221 mStateManager.goToState(NORMAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001222 }
1223 }
1224 };
1225
Sunny Goyalaae6fbb2019-01-31 16:05:58 -08001226 public void updateNotificationDots(Predicate<PackageUserKey> updatedDots) {
Tony Wickhamf34bee82018-12-03 18:11:39 -08001227 mWorkspace.updateNotificationDots(updatedDots);
1228 mAppsView.getAppsStore().updateNotificationDots(updatedDots);
Tony Wickham010d2552017-01-20 08:15:28 -08001229 }
1230
Adam Cohended9f8d2010-11-03 13:25:16 -07001231 @Override
1232 public void onAttachedToWindow() {
1233 super.onAttachedToWindow();
1234
Sunny Goyalc86df472016-02-25 09:19:38 -08001235 if (mLauncherCallbacks != null) {
1236 mLauncherCallbacks.onAttachedToWindow();
1237 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001238 }
1239
1240 @Override
1241 public void onDetachedFromWindow() {
1242 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001243
1244 if (mLauncherCallbacks != null) {
1245 mLauncherCallbacks.onDetachedFromWindow();
1246 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001247 }
1248
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001249 public AllAppsTransitionController getAllAppsController() {
1250 return mAllAppsController;
1251 }
1252
Sunny Goyal9d69c8d2018-03-19 13:41:31 -07001253 @Override
Winson Chung1a341002018-01-17 10:00:23 -08001254 public LauncherRootView getRootView() {
1255 return (LauncherRootView) mLauncherView;
1256 }
1257
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001258 @Override
Winson Chunga6945242014-01-08 14:04:34 -08001259 public DragLayer getDragLayer() {
1260 return mDragLayer;
1261 }
1262
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001263 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001264 return mAppsView;
1265 }
1266
Winson Chunga6945242014-01-08 14:04:34 -08001267 public Workspace getWorkspace() {
1268 return mWorkspace;
1269 }
1270
1271 public Hotseat getHotseat() {
1272 return mHotseat;
1273 }
1274
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001275 public <T extends View> T getOverviewPanel() {
Winson Chung8ae41982017-11-10 11:42:13 -08001276 return (T) mOverviewPanel;
Winson Chunga6945242014-01-08 14:04:34 -08001277 }
1278
Sunny Goyal47328fd2016-05-25 18:56:41 -07001279 public DropTargetBar getDropTargetBar() {
1280 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001281 }
1282
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001283 public LauncherAppWidgetHost getAppWidgetHost() {
1284 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001285 }
Romain Guycbb89e42009-06-08 15:52:54 -07001286
Winson Chunga9abd0e2010-10-27 17:18:37 -07001287 public LauncherModel getModel() {
1288 return mModel;
1289 }
1290
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001291 public ModelWriter getModelWriter() {
1292 return mModelWriter;
1293 }
1294
Adam Cohen79d90c52016-04-22 13:29:20 -07001295 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001296 return mSharedPrefs;
1297 }
1298
Sunny Goyalf8d56fc2018-01-31 15:18:11 -08001299 public int getOrientation() { return mOldConfig.orientation; }
Jon Miranda6bed3502017-09-19 14:43:17 -07001300
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001301 @Override
1302 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001303 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001304 super.onNewIntent(intent);
1305
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001306 boolean alreadyOnHome = hasWindowFocus() && ((intent.getFlags() &
Winson15f8b172015-08-19 11:02:39 -07001307 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1308 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001309
1310 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001311 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001312 && AbstractFloatingView.getTopOpenView(this) == null;
Winson15f8b172015-08-19 11:02:39 -07001313 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001314 boolean internalStateHandled = InternalStateHandler
Sunny Goyal8b264562018-02-03 23:00:13 -08001315 .handleNewIntent(this, intent, isStarted());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001316
Winson15f8b172015-08-19 11:02:39 -07001317 if (isActionMain) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001318 if (!internalStateHandled) {
1319 // Note: There should be at most one log per method call. This is enforced
1320 // implicitly by using if-else statements.
1321 UserEventDispatcher ued = getUserEventDispatcher();
1322 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
1323 if (topOpenView != null) {
1324 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
1325 } else if (alreadyOnHome) {
1326 Target target = newContainerTarget(mStateManager.getState().containerType);
1327 target.pageIndex = mWorkspace.getCurrentPage();
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001328 ued.logActionCommand(Action.Command.HOME_INTENT, target,
1329 newContainerTarget(ContainerType.WORKSPACE));
Sunny Goyald3864fa2017-11-14 15:06:36 -08001330 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001331
Sunny Goyald3864fa2017-11-14 15:06:36 -08001332 // In all these cases, only animate if we're already on home
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001333 AbstractFloatingView.closeAllOpenViews(this, isStarted());
Jon Mirandafeba90f2016-10-06 10:53:29 -07001334
Sunny Goyala2467272018-03-16 14:53:05 -07001335 if (!isInState(NORMAL)) {
1336 // Only change state, if not already the same. This prevents cancelling any
1337 // animations running as part of resume
1338 mStateManager.goToState(NORMAL);
1339 }
Sunny Goyald3864fa2017-11-14 15:06:36 -08001340
1341 // Reset the apps view
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001342 if (!alreadyOnHome) {
Sunny Goyal7185dd62018-03-14 17:51:49 -07001343 mAppsView.reset(isStarted() /* animate */);
Sunny Goyald3864fa2017-11-14 15:06:36 -08001344 }
1345
Sunny Goyalaad33592018-08-07 17:20:35 -07001346 if (shouldMoveToDefaultScreen && !mWorkspace.isHandlingTouch()) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001347 mWorkspace.post(mWorkspace::moveToDefaultScreen);
1348 }
1349 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001350
1351 final View v = getWindow().peekDecorView();
1352 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001353 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001354 }
1355
Adam Cohen9211d422014-10-07 18:14:53 -07001356 if (mLauncherCallbacks != null) {
Sunny Goyal85462132018-04-24 11:39:37 -07001357 mLauncherCallbacks.onHomeIntent(internalStateHandled);
Adam Cohen9211d422014-10-07 18:14:53 -07001358 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001359 }
Winson15f8b172015-08-19 11:02:39 -07001360
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001361 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001362 }
1363
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001364 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001365 public void onRestoreInstanceState(Bundle state) {
1366 super.onRestoreInstanceState(state);
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001367 mWorkspace.restoreInstanceStateForChild(mSynchronouslyBoundPage);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001368 }
1369
1370 @Override
1371 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001372 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001373 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001374
Adam Cohen21cd0022013-10-09 18:57:02 -07001375 }
Sunny Goyalea609262017-10-25 15:47:38 -07001376 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001377
1378
1379 AbstractFloatingView widgets = AbstractFloatingView
1380 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1381 if (widgets != null) {
1382 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1383 widgets.saveHierarchyState(widgetsState);
1384 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1385 } else {
1386 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1387 }
1388
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001389 // We close any open folders and shortcut containers since they will not be re-opened,
1390 // and we need to make sure this state is reflected.
1391 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001392
Sunny Goyal2100c782016-08-22 16:00:03 -07001393 if (mPendingRequestArgs != null) {
1394 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1395 }
1396 if (mPendingActivityResult != null) {
1397 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001398 }
1399
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001400 super.onSaveInstanceState(outState);
1401
Adam Cohen9211d422014-10-07 18:14:53 -07001402 if (mLauncherCallbacks != null) {
1403 mLauncherCallbacks.onSaveInstanceState(outState);
1404 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001405 }
1406
1407 @Override
1408 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001409 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001410
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001411 unregisterReceiver(mScreenOffReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001412 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001413
Sunny Goyal5743f862019-03-28 15:35:32 -07001414 if (mCancelTouchController != null) {
1415 mCancelTouchController.run();
1416 mCancelTouchController = null;
1417 }
Tony Wickhama0068302018-04-11 16:16:11 -07001418
Winson Chungcd2b0142011-06-08 16:02:26 -07001419 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001420 // It's possible to receive onDestroy after a new Launcher activity has
1421 // been created. In this case, don't interfere with the new Launcher.
1422 if (mModel.isCurrentCallbacks(this)) {
1423 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001424 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001425 }
Sunny Goyal623eddd2018-03-02 12:24:41 -08001426 mRotationHelper.destroy();
Sunny Goyal745bad92016-05-02 10:54:12 -07001427
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001428 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001429 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001430 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001431 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001432 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001433
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001434 TextKeyListener.getInstance().release();
Tony2917a8b2017-07-31 23:29:42 -07001435 clearPendingBinds();
Hyunyoung Song31971a32019-01-27 12:31:12 -08001436 LauncherAppState.getIDP(this).removeOnChangeListener(this);
Adam Cohen9211d422014-10-07 18:14:53 -07001437 if (mLauncherCallbacks != null) {
1438 mLauncherCallbacks.onDestroy();
1439 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001440 }
1441
Sunny Goyalae502842016-06-17 08:43:56 -07001442 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1443 return mAccessibilityDelegate;
1444 }
1445
Adam Cohena9cf38f2011-05-02 15:36:58 -07001446 public DragController getDragController() {
1447 return mDragController;
1448 }
1449
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001450 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001451 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001452 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001453 }
1454
1455 @Override
1456 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1457 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001458 try {
1459 super.startIntentSenderForResult(intent, requestCode,
1460 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1461 } catch (IntentSender.SendIntentException e) {
1462 throw new ActivityNotFoundException();
1463 }
1464 }
1465
Winson Chung70d72102011-08-12 11:24:35 -07001466 /**
1467 * Indicates that we want global search for this activity by setting the globalSearch
1468 * argument for {@link #startSearch} to true.
1469 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001470 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001471 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472 Bundle appSearchData, boolean globalSearch) {
1473 if (appSearchData == null) {
1474 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001475 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001477
Sunny Goyal6f28e712016-09-13 14:19:29 -07001478 if (mLauncherCallbacks == null ||
1479 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1480 // Starting search from the callbacks failed. Start the default global search.
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001481 super.startSearch(initialQuery, selectInitialQuery, appSearchData, true);
Ian Parkinson047036e2014-09-01 15:40:53 +01001482 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001483
1484 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001485 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001486 }
1487
Joe Onorato9c1289c2009-08-17 11:03:03 -04001488 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001489 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001490 }
1491
Adam Cohen517a7f52014-03-01 12:12:59 -08001492 public boolean isWorkspaceLoading() {
1493 return mWorkspaceLoading;
1494 }
1495
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001496 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001497 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001498 }
1499
Sunny Goyal04a324a2017-01-20 21:08:59 -08001500 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001501 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001502 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001503
Sunny Goyal2100c782016-08-22 16:00:03 -07001504 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001505 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001506 if (LOGD) {
1507 Log.d(TAG, "Adding widget from drop");
1508 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001509 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001510 }
1511
Sunny Goyal2100c782016-08-22 16:00:03 -07001512 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001513 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1514 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1515 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001516
Adam Cohenad4e15c2013-10-17 16:21:35 -07001517 Runnable onComplete = new Runnable() {
1518 @Override
1519 public void run() {
1520 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001521 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001522 }
1523 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001524 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001525 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001526 }
1527 }
Romain Guycbb89e42009-06-08 15:52:54 -07001528
Sunny Goyalefb7e842018-10-04 15:11:00 -07001529 public void addPendingItem(PendingAddItemInfo info, int container, int screenId,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001530 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001531 info.container = container;
1532 info.screenId = screenId;
1533 if (cell != null) {
1534 info.cellX = cell[0];
1535 info.cellY = cell[1];
1536 }
1537 info.spanX = spanX;
1538 info.spanY = spanY;
1539
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001540 switch (info.itemType) {
1541 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1542 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001543 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001544 break;
1545 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001546 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001547 break;
1548 default:
1549 throw new IllegalStateException("Unknown item type: " + info.itemType);
1550 }
1551 }
1552
Adam Cohenfbba09b2011-07-18 21:43:05 -07001553 /**
1554 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001555 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001556 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001557 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1558 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001559 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1560 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1561 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001562 }
1563
Adam Cohenfbba09b2011-07-18 21:43:05 -07001564 /**
1565 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001566 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001567 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001568 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001569 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001570 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001571 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001572 // In the case where we've prebound the widget, we remove it from the DragLayer
1573 if (LOGD) {
1574 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1575 }
1576 getDragLayer().removeView(hostView);
1577
Adam Cohened66b2b2012-01-23 17:28:51 -08001578 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001579 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001580
1581 // Clear the boundWidget so that it doesn't get destroyed.
1582 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001583 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001584 // In this case, we either need to start an activity to get permission to bind
1585 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001586 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1587 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1588 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1589 this, info.componentName);
1590 } else {
1591 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1592 }
Adam Cohendd70d662012-10-04 16:53:44 -07001593 Bundle options = info.bindOptions;
1594
Sunny Goyalffe83f12014-08-14 17:39:34 -07001595 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1596 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001597 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001598 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001599 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001600 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001601 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001602 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001603 }
1604
Sunny Goyalefb7e842018-10-04 15:11:00 -07001605 FolderIcon addFolder(CellLayout layout, int container, final int screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001606 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001607 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001608 folderInfo.title = getText(R.string.folder_name);
1609
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001610 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001611 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001612
1613 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001614 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301615 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001616 // Force measure the new folder icon
1617 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1618 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001619 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001620 }
Romain Guycbb89e42009-06-08 15:52:54 -07001621
Winsonc0b52fe2015-09-09 16:38:15 -07001622 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001623 * Unbinds the view for the specified item, and removes the item and all its children.
1624 *
1625 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001626 * @param itemInfo the {@link ItemInfo} for this view.
1627 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001628 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001629 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Sunny Goyal95899162019-03-27 16:03:06 -07001630 if (itemInfo instanceof WorkspaceItemInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001631 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001632 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1633 if (folderIcon instanceof FolderIcon) {
Sunny Goyal95899162019-03-27 16:03:06 -07001634 ((FolderInfo) folderIcon.getTag()).remove((WorkspaceItemInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001635 } else {
1636 mWorkspace.removeWorkspaceItem(v);
1637 }
Winsonc0b52fe2015-09-09 16:38:15 -07001638 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001639 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001640 }
1641 } else if (itemInfo instanceof FolderInfo) {
1642 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001643 if (v instanceof FolderIcon) {
1644 ((FolderIcon) v).removeListeners();
1645 }
Winsonc0b52fe2015-09-09 16:38:15 -07001646 mWorkspace.removeWorkspaceItem(v);
1647 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001648 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001649 }
1650 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1651 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001652 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001653 if (deleteFromDb) {
Tony Wickham6a71a5b2018-08-21 11:40:23 -07001654 getModelWriter().deleteWidgetInfo(widgetInfo, getAppWidgetHost());
Winsonc0b52fe2015-09-09 16:38:15 -07001655 }
1656 } else {
1657 return false;
1658 }
1659 return true;
1660 }
1661
Tony Wickham6a71a5b2018-08-21 11:40:23 -07001662
Winsonc0b52fe2015-09-09 16:38:15 -07001663
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001664 @Override
1665 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001666 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001667 }
1668
Joe Onorato88ec0992009-11-19 13:16:06 -08001669 @Override
1670 public void onBackPressed() {
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08001671 if (finishAutoCancelActionMode()) {
1672 return;
1673 }
Adam Cohen9211d422014-10-07 18:14:53 -07001674 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1675 return;
1676 }
1677
Sunny Goyal45478022015-06-08 16:52:41 -07001678 if (mDragController.isDragging()) {
1679 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001680 return;
1681 }
1682
Jon Mirandafeba90f2016-10-06 10:53:29 -07001683 // Note: There should be at most one log per method call. This is enforced implicitly
1684 // by using if-else statements.
1685 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001686 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
Tony Wickham52c1b662018-05-21 13:13:58 -07001687 if (topView != null && topView.onBackPressed()) {
1688 // Handled by the floating view.
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001689 } else {
Sunny Goyale39690b2018-08-02 13:35:07 -07001690 mStateManager.getState().onBackPressed(this);
Michael Jurkaaf442092010-06-10 17:01:57 -07001691 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001692 }
1693
Sunny Goyalfe770c92016-09-27 14:38:58 -07001694 @TargetApi(Build.VERSION_CODES.M)
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001695 @Override
Jon Miranda73c27ec2018-05-16 18:06:23 -07001696 public ActivityOptions getActivityLaunchOptions(View v) {
1697 return mAppTransitionManager.getActivityLaunchOptions(this, v);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001698 }
1699
Tony Wickham3a6746a2018-03-29 09:39:56 -07001700 public LauncherAppTransitionManager getAppTransitionManager() {
1701 return mAppTransitionManager;
1702 }
1703
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001704 @TargetApi(Build.VERSION_CODES.M)
1705 @Override
1706 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
1707 // Due to legacy reasons, direct call shortcuts require Launchers to have the
1708 // corresponding permission. Show the appropriate permission prompt if that
1709 // is the case.
1710 if (intent.getComponent() == null
1711 && Intent.ACTION_CALL.equals(intent.getAction())
1712 && checkSelfPermission(android.Manifest.permission.CALL_PHONE) !=
1713 PackageManager.PERMISSION_GRANTED) {
1714
1715 setWaitingForResult(PendingRequestArgs
1716 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
1717 requestPermissions(new String[]{android.Manifest.permission.CALL_PHONE},
1718 REQUEST_PERMISSION_CALL_PHONE);
1719 return true;
1720 } else {
1721 return false;
1722 }
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001723 }
1724
Hyunyoung Song46d07f72018-05-22 15:41:25 -07001725 @Override
Hyunyoung Songfc007472018-10-25 14:09:50 -07001726 public int getCurrentState() {
1727 if(mStateManager.getState() == LauncherState.ALL_APPS) {
1728 return StatsLogUtils.LAUNCHER_STATE_ALLAPPS;
Govinda Wassermanc06e1512019-04-09 09:56:53 -04001729 } else if (mStateManager.getState() == OVERVIEW) {
Hyunyoung Songfc007472018-10-25 14:09:50 -07001730 return StatsLogUtils.LAUNCHER_STATE_OVERVIEW;
1731 }
Hyunyoung Song0ae38882018-11-05 14:45:19 -08001732 return StatsLogUtils.LAUNCHER_STATE_HOME;
Hyunyoung Songfc007472018-10-25 14:09:50 -07001733 }
1734
1735 @Override
Hyunyoung Song46d07f72018-05-22 15:41:25 -07001736 public void modifyUserEvent(LauncherLogProto.LauncherEvent event) {
1737 if (event.srcTarget != null && event.srcTarget.length > 0 &&
1738 event.srcTarget[1].containerType == ContainerType.PREDICTION) {
1739 Target[] targets = new Target[3];
1740 targets[0] = event.srcTarget[0];
1741 targets[1] = event.srcTarget[1];
1742 targets[2] = newTarget(Target.Type.CONTAINER);
1743 event.srcTarget = targets;
1744 LauncherState state = mStateManager.getState();
1745 if (state == LauncherState.ALL_APPS) {
1746 event.srcTarget[2].containerType = ContainerType.ALLAPPS;
Govinda Wassermanc06e1512019-04-09 09:56:53 -04001747 } else if (state == OVERVIEW) {
Hyunyoung Song46d07f72018-05-22 15:41:25 -07001748 event.srcTarget[2].containerType = ContainerType.TASKSWITCHER;
1749 }
1750 }
1751 }
1752
Sunny Goyal369212a2019-03-26 15:03:57 -07001753 public boolean startActivitySafely(View v, Intent intent, ItemInfo item,
1754 @Nullable String sourceContainer) {
1755 boolean success = super.startActivitySafely(v, intent, item, sourceContainer);
Hyunyoung Song1241e612018-05-15 21:46:51 -07001756 if (success && v instanceof BubbleTextView) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001757 // This is set to the view that launched the activity that navigated the user away
1758 // from launcher. Since there is no callback for when the activity has finished
1759 // launching, enable the press state and keep this reference to reset the press
1760 // state when we return to launcher.
1761 BubbleTextView btv = (BubbleTextView) v;
1762 btv.setStayPressed(true);
1763 setOnResumeCallback(btv);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001764 }
Hyunyoung Song1241e612018-05-15 21:46:51 -07001765 return success;
Michael Jurka86a720e2012-05-09 11:23:23 -07001766 }
1767
Winson Chung3d503fb2011-07-13 17:25:49 -07001768 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001769 // TODO: Remove this method
Sunny Goyal876e4622018-11-02 13:50:40 -07001770 return mHotseat != null && (layout == mHotseat);
Winson Chung3d503fb2011-07-13 17:25:49 -07001771 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001772
Winson Chung3d503fb2011-07-13 17:25:49 -07001773 /**
1774 * Returns the CellLayout of the specified container at the specified screen.
1775 */
Sunny Goyalefb7e842018-10-04 15:11:00 -07001776 public CellLayout getCellLayout(int container, int screenId) {
Sunny Goyal876e4622018-11-02 13:50:40 -07001777 return (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1778 ? mHotseat : mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08001779 }
1780
Michael Jurkae326f182011-11-21 14:05:46 -08001781 @Override
1782 public void onTrimMemory(int level) {
1783 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07001784 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
1785 // The widget preview db can result in holding onto over
1786 // 3MB of memory for caching which isn't necessary.
1787 SQLiteDatabase.releaseMemory();
1788
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08001789 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07001790 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08001791 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01001792 if (mLauncherCallbacks != null) {
1793 mLauncherCallbacks.onTrimMemory(level);
1794 }
Winson Chung62a70be2018-02-23 15:10:05 -08001795 UiFactory.onTrimMemory(this, level);
Michael Jurkae326f182011-11-21 14:05:46 -08001796 }
1797
Michael Jurkad7c28052012-04-27 15:43:36 -07001798 @Override
1799 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07001800 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07001801 final List<CharSequence> text = event.getText();
1802 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07001803 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001804 // TODO: When can workspace be null?
1805 text.add(mWorkspace == null
1806 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07001807 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07001808 return result;
1809 }
1810
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001811 public void setOnResumeCallback(OnResumeCallback callback) {
1812 if (mOnResumeCallback != null) {
1813 mOnResumeCallback.onLauncherResume();
1814 }
1815 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07001816 }
1817
Michael Jurka7607c2f2013-04-03 14:33:19 -07001818 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001819 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001820 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07001821 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001822 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07001823 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001824 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07001825 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07001826 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07001827 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001828 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001829
Joe Onorato9c1289c2009-08-17 11:03:03 -04001830 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07001831 * Clear any pending bind callbacks. This is called when is loader is planning to
1832 * perform a full rebind from scratch.
1833 */
1834 @Override
1835 public void clearPendingBinds() {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001836 if (mPendingExecutor != null) {
1837 mPendingExecutor.markCompleted();
1838 mPendingExecutor = null;
1839 }
1840 }
1841
1842 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001843 * Refreshes the shortcuts shown on the workspace.
1844 *
1845 * Implementation of the method from LauncherModel.Callbacks.
1846 */
1847 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001848 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001849 // Floating panels (except the full widget sheet) are associated with individual icons. If
1850 // we are starting a fresh bind, close all such panels as all the icons are about
1851 // to go away.
1852 AbstractFloatingView.closeOpenViews(this, true,
Sunny Goyal7ede6112017-12-05 15:11:21 -08001853 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_REBIND_SAFE);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08001854
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001855 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08001856
Winson Chungd64d1762013-08-20 14:37:16 -07001857 // Clear the workspace because it's going to be rebound
Sunny Goyal2db53422019-03-01 16:06:12 -08001858 mDragController.cancelDrag();
1859
Adam Cohendf035382011-04-11 17:22:04 -07001860 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07001861 mWorkspace.removeAllWorkspaceScreens();
Sunny Goyalc11fac32018-02-27 19:20:15 -08001862 mAppWidgetHost.clearViews();
Winson Chungd64d1762013-08-20 14:37:16 -07001863
Winson Chung3d503fb2011-07-13 17:25:49 -07001864 if (mHotseat != null) {
Sunny Goyalae6e3182019-04-30 12:04:37 -07001865 mHotseat.resetLayout(getWallpaperDeviceProfile().isVerticalBarLayout());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001866 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001867 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001868 }
1869
Adam Cohendcd297f2013-06-18 13:13:40 -07001870 @Override
Sunny Goyalefb7e842018-10-04 15:11:00 -07001871 public void bindScreens(IntArray orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001872 // Make sure the first screen is always at the start.
Jon Miranda7143ba62019-03-15 09:00:05 -07001873 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001874 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalefb7e842018-10-04 15:11:00 -07001875 orderedScreenIds.removeValue(Workspace.FIRST_SCREEN_ID);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001876 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Jon Miranda7143ba62019-03-15 09:00:05 -07001877 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001878 // If there are no screens, we need to have an empty screen
1879 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07001880 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001881 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07001882
Winsonc7d2e832016-07-28 12:24:55 -07001883 // After we have added all the screens, if the wallpaper was locked to the default state,
1884 // then notify to indicate that it can be released and a proper wallpaper offset can be
1885 // computed before the next layout
1886 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07001887 }
1888
Sunny Goyalefb7e842018-10-04 15:11:00 -07001889 private void bindAddScreens(IntArray orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001890 int count = orderedScreenIds.size();
1891 for (int i = 0; i < count; i++) {
Sunny Goyalefb7e842018-10-04 15:11:00 -07001892 int screenId = orderedScreenIds.get(i);
Jon Miranda7143ba62019-03-15 09:00:05 -07001893 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001894 // No need to bind the first screen, as its always bound.
1895 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
1896 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001897 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001898 }
1899
Sunny Goyalb23980c2017-08-17 07:45:25 -07001900 @Override
Tony Wickham6a71a5b2018-08-21 11:40:23 -07001901 public void preAddApps() {
1902 // If there's an undo snackbar, force it to complete to ensure empty screens are removed
1903 // before trying to add new items.
1904 mModelWriter.commitDelete();
1905 AbstractFloatingView snackbar = AbstractFloatingView.getOpenView(this, TYPE_SNACKBAR);
1906 if (snackbar != null) {
1907 snackbar.post(() -> snackbar.close(true));
1908 }
1909 }
1910
1911 @Override
Sunny Goyalefb7e842018-10-04 15:11:00 -07001912 public void bindAppsAdded(IntArray newScreens, ArrayList<ItemInfo> addNotAnimated,
Sunny Goyal29947f02017-12-18 13:49:44 -08001913 ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07001914 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08001915 if (newScreens != null) {
1916 bindAddScreens(newScreens);
1917 }
Winson Chungd64d1762013-08-20 14:37:16 -07001918
1919 // We add the items without animation on non-visible pages, and with
1920 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08001921 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001922 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001923 }
Adam Cohen76a47a12014-02-05 11:47:43 -08001924 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001925 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07001926 }
Winson Chungc58497e2013-09-03 17:48:37 -07001927
Winson Chung87412982013-10-03 18:34:14 -07001928 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07001929 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001930 }
1931
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001932 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001933 * Bind the items start-end from the list.
1934 *
1935 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001936 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07001937 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07001938 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001939 // Get the list of added items and intersect them with the set of items here
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001940 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07001941 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001942 Workspace workspace = mWorkspace;
Sunny Goyalefb7e842018-10-04 15:11:00 -07001943 int newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07001944 int end = items.size();
1945 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001946 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07001947
1948 // Short circuit if we are loading dock items for a configuration which has no dock
1949 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
1950 mHotseat == null) {
1951 continue;
1952 }
1953
Sunny Goyal639e9062015-08-19 19:17:06 -07001954 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001955 switch (item.itemType) {
1956 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1957 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08001958 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Sunny Goyal95899162019-03-27 16:03:06 -07001959 WorkspaceItemInfo info = (WorkspaceItemInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07001960 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001961 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001962 }
1963 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07001964 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001965 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001966 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001967 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001968 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07001969 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1970 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07001971 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07001972 if (view == null) {
1973 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001974 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08001975 break;
1976 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001977 default:
1978 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001979 }
Sunny Goyal639e9062015-08-19 19:17:06 -07001980
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001981 /*
1982 * Remove colliding items.
1983 */
1984 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1985 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
1986 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
1987 View v = cl.getChildAt(item.cellX, item.cellY);
1988 Object tag = v.getTag();
1989 String desc = "Collision while binding workspace item: " + item
1990 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08001991 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001992 throw (new RuntimeException(desc));
1993 } else {
1994 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001995 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001996 continue;
1997 }
1998 }
1999 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302000 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07002001 if (animateIcons) {
2002 // Animate all the applications up now
2003 view.setAlpha(0f);
2004 view.setScaleX(0f);
2005 view.setScaleY(0f);
2006 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08002007 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07002008 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002009 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07002010
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002011 // Animate to the correct page
2012 if (animateIcons && newItemsScreenId > -1) {
2013 AnimatorSet anim = new AnimatorSet();
2014 anim.playTogether(bounceAnims);
Jon Miranda64eb8ea2018-03-29 11:43:58 -07002015
Sunny Goyalefb7e842018-10-04 15:11:00 -07002016 int currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002017 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
2018 final Runnable startBounceAnimRunnable = anim::start;
2019
2020 if (newItemsScreenId != currentScreenId) {
2021 // We post the animation slightly delayed to prevent slowdowns
2022 // when we are loading right after we return to launcher.
2023 mWorkspace.postDelayed(new Runnable() {
2024 public void run() {
2025 if (mWorkspace != null) {
2026 AbstractFloatingView.closeAllOpenViews(Launcher.this, false);
2027
2028 mWorkspace.snapToPage(newScreenIndex);
2029 mWorkspace.postDelayed(startBounceAnimRunnable,
2030 NEW_APPS_ANIMATION_DELAY);
Winson Chung94d67682013-09-25 16:29:40 -07002031 }
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002032 }
2033 }, NEW_APPS_PAGE_MOVE_DELAY);
2034 } else {
2035 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07002036 }
Winson Chung64359a52013-07-08 17:17:08 -07002037 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002038 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002039 }
2040
Joe Onorato9c1289c2009-08-17 11:03:03 -04002041 /**
2042 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002043 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07002044 public void bindAppWidget(LauncherAppWidgetInfo item) {
2045 View view = inflateAppWidget(item);
2046 if (view != null) {
2047 mWorkspace.addInScreen(view, item);
2048 mWorkspace.requestLayout();
2049 }
2050 }
2051
2052 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002053 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302054 PendingAppWidgetHostView view =
2055 new PendingAppWidgetHostView(this, item, mIconCache, true);
2056 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002057 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002058 }
2059
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002060 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002061
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002062 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08002063
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002064 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2065 // If the provider is not ready, bind as a pending widget.
2066 appWidgetInfo = null;
2067 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2068 // The widget id is not valid. Try to find the widget based on the provider info.
2069 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
2070 } else {
2071 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
2072 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002073
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002074 // If the provider is ready, but the width is not yet restored, try to restore it.
2075 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
2076 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07002077 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002078 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
2079 + " belongs to component " + item.providerName
2080 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002081 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002082 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002083 }
Sunny Goyalff572272014-07-23 13:58:07 -07002084
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002085 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002086 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07002087 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2088 // Id has not been allocated yet. Allocate a new id.
2089 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
2090 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07002091
Sunny Goyald478c832016-04-01 12:04:16 -07002092 // Also try to bind the widget. If the bind fails, the user will be shown
2093 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08002094 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07002095 pendingInfo.spanX = item.spanX;
2096 pendingInfo.spanY = item.spanY;
2097 pendingInfo.minSpanX = item.minSpanX;
2098 pendingInfo.minSpanY = item.minSpanY;
2099 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07002100
2101 boolean isDirectConfig =
2102 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2103 if (isDirectConfig && item.bindOptions != null) {
2104 Bundle newOptions = item.bindOptions.getExtras();
2105 if (options != null) {
2106 newOptions.putAll(options);
2107 }
2108 options = newOptions;
2109 }
Sunny Goyald478c832016-04-01 12:04:16 -07002110 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2111 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002112
Sunny Goyal86df1382016-08-10 15:03:22 -07002113 // We tried to bind once. If we were not able to bind, we would need to
2114 // go through the permission dialog, which means we cannot skip the config
2115 // activity.
2116 item.bindOptions = null;
2117 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2118
Sunny Goyald478c832016-04-01 12:04:16 -07002119 // Bind succeeded
2120 if (success) {
2121 // If the widget has a configure activity, it is still needs to set it up,
2122 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002123 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002124 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2125 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002126 }
Sunny Goyald478c832016-04-01 12:04:16 -07002127
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002128 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002129 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002130 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002131 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002132 // The widget was marked as UI not ready, but there is no configure activity to
2133 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002134 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002135 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002136 }
Sunny Goyalff572272014-07-23 13:58:07 -07002137 }
2138
Sunny Goyald5462aa2016-11-22 16:52:39 +05302139 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002140 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002141 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002142 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002143 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Tony Wickham6a71a5b2018-08-21 11:40:23 -07002144 getModelWriter().deleteWidgetInfo(item, getAppWidgetHost());
Sunny Goyalb23980c2017-08-17 07:45:25 -07002145 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002146 }
2147
Sunny Goyal233ee962015-08-03 13:05:01 -07002148 item.minSpanX = appWidgetInfo.minSpanX;
2149 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302150 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002151 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302152 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002153 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302154 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002155
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002156 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002157 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002158 }
2159
Sunny Goyalff572272014-07-23 13:58:07 -07002160 /**
2161 * Restores a pending widget.
2162 *
2163 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002164 */
Sunny Goyald478c832016-04-01 12:04:16 -07002165 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002166 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2167 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2168 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002169 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002170 }
2171
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002172 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002173 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002174 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2175 info.pendingItemInfo = null;
2176 }
Sunny Goyalff572272014-07-23 13:58:07 -07002177
Sunny Goyalba47faa2017-10-04 16:50:57 -07002178 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002179 view.reInflate();
Sunny Goyalba47faa2017-10-04 16:50:57 -07002180 }
2181
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002182 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002183 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002184 }
2185
Adam Cohen1462de32012-07-24 22:34:36 -07002186 public void onPageBoundSynchronously(int page) {
Sunny Goyalf4aceab2017-12-06 16:16:31 -08002187 mSynchronouslyBoundPage = page;
Adam Cohen1462de32012-07-24 22:34:36 -07002188 }
2189
Sunny Goyal527c7d32015-08-28 15:19:36 -07002190 @Override
2191 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2192 if (mPendingExecutor != null) {
2193 mPendingExecutor.markCompleted();
2194 }
2195 mPendingExecutor = executor;
Sunny Goyal60180b02018-02-07 12:56:30 -08002196 if (!isInState(ALL_APPS)) {
2197 mAppsView.getAppsStore().setDeferUpdates(true);
2198 mPendingExecutor.execute(() -> mAppsView.getAppsStore().setDeferUpdates(false));
2199 }
2200
Sunny Goyal527c7d32015-08-28 15:19:36 -07002201 executor.attachTo(this);
2202 }
2203
2204 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2205 if (mPendingExecutor == executor) {
2206 mPendingExecutor = null;
2207 }
2208 }
2209
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002210 @Override
2211 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
Sunny Goyal6001ea22018-05-10 16:31:00 -07002212 AlphaProperty property = mDragLayer.getAlphaProperty(ALPHA_INDEX_LAUNCHER_LOAD);
2213 if (property.getValue() < 1) {
2214 ObjectAnimator anim = ObjectAnimator.ofFloat(property, MultiValueAlpha.VALUE, 1);
2215 if (executor != null) {
2216 anim.addListener(new AnimatorListenerAdapter() {
2217 @Override
2218 public void onAnimationEnd(Animator animation) {
2219 executor.onLoadAnimationCompleted();
2220 }
2221 });
2222 }
2223 anim.start();
Sunny Goyal29947f02017-12-18 13:49:44 -08002224 } else if (executor != null) {
2225 executor.onLoadAnimationCompleted();
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002226 }
2227 }
2228
Joe Onorato9c1289c2009-08-17 11:03:03 -04002229 /**
2230 * Callback saying that there aren't any more items to bind.
2231 *
2232 * Implementation of the method from LauncherModel.Callbacks.
2233 */
Tonyf80e8932018-12-21 11:58:04 -08002234 public void finishBindingItems(int pageBoundFirst) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002235 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002236 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002237
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002238 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002239
Sunny Goyal2100c782016-08-22 16:00:03 -07002240 if (mPendingActivityResult != null) {
2241 handleActivityResult(mPendingActivityResult.requestCode,
2242 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2243 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002244 }
2245
Sunny Goyala474a9b2017-05-04 16:47:11 -07002246 InstallShortcutReceiver.disableAndFlushInstallQueue(
2247 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002248
Tonyf80e8932018-12-21 11:58:04 -08002249 // When undoing the removal of the last item on a page, return to that page.
Tony Wickham03f27012019-04-25 14:22:54 -07002250 // Since we are just resetting the current page without user interaction,
2251 // override the previous page so we don't log the page switch.
2252 mWorkspace.setCurrentPage(pageBoundFirst, pageBoundFirst /* overridePrevPage */);
Tony Wickham6a71a5b2018-08-21 11:40:23 -07002253
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002254 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002255 }
2256
Winson Chunga2413752012-04-03 14:22:34 -07002257 private boolean canRunNewAppsAnimation() {
2258 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002259 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002260 }
2261
Winson Chungc9168342013-06-26 14:54:55 -07002262 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002263 ValueAnimator bounceAnim = new PropertyListBuilder().alpha(1).scale(1).build(v)
2264 .setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
Winson Chungc9168342013-06-26 14:54:55 -07002265 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002266 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002267 return bounceAnim;
2268 }
2269
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002270 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002271 * Add the icons for all apps.
2272 *
2273 * Implementation of the method from LauncherModel.Callbacks.
2274 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002275 public void bindAllApplications(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002276 mAppsView.getAppsStore().setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002277 }
2278
2279 /**
Zak Cohen658c67a2018-10-19 14:21:05 -07002280 * Copies LauncherModel's map of activities to shortcut counts to Launcher's. This is necessary
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002281 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2282 */
2283 @Override
Zak Cohen658c67a2018-10-19 14:21:05 -07002284 public void bindDeepShortcutMap(HashMap<ComponentKey, Integer> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002285 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002286 }
2287
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002288 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002289 * A package was updated.
2290 *
2291 * Implementation of the method from LauncherModel.Callbacks.
2292 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002293 @Override
2294 public void bindAppsAddedOrUpdated(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002295 mAppsView.getAppsStore().addOrUpdateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002296 }
2297
Sunny Goyal4390ace2014-10-13 11:33:11 -07002298 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002299 public void bindPromiseAppProgressUpdated(PromiseAppInfo app) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002300 mAppsView.getAppsStore().updatePromiseAppProgress(app);
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002301 }
2302
2303 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002304 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002305 mWorkspace.widgetsRestored(widgets);
2306 }
2307
Joe Onorato9c1289c2009-08-17 11:03:03 -04002308 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002309 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002310 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002311 *
2312 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002313 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002314 @Override
Sunny Goyal95899162019-03-27 16:03:06 -07002315 public void bindWorkspaceItemsChanged(ArrayList<WorkspaceItemInfo> updated) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002316 if (!updated.isEmpty()) {
2317 mWorkspace.updateShortcuts(updated);
2318 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002319 }
2320
2321 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002322 * Update the state of a package, typically related to install state.
2323 *
2324 * Implementation of the method from LauncherModel.Callbacks.
2325 */
Sunny Goyale755d462014-07-22 13:48:29 -07002326 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002327 public void bindRestoreItemsChange(HashSet<ItemInfo> updates) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07002328 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07002329 }
2330
2331 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002332 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07002333 * in addition to specific applications to remove, the reason being that
2334 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002335 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07002336 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002337 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07002338 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002339 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002340 mWorkspace.removeItemsByMatcher(matcher);
2341 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002342 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002343
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002344 @Override
2345 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002346 mAppsView.getAppsStore().removeApps(appInfos);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002347 }
Joe Onoratobe386092009-11-17 17:32:16 -08002348
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002349 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002350 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
2351 mPopupDataProvider.setAllWidgets(allWidgets);
Tony Wickham26b17462017-03-20 17:12:24 -07002352 }
2353
Tony Wickham86222d22017-03-29 15:30:43 -07002354 /**
2355 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
2356 * refreshes the widgets and shortcuts associated with the given package/user
2357 */
2358 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07002359 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002360 }
2361
Winson Chung80baf5a2010-08-09 16:03:15 -07002362 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002363 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08002364 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002365 @Override
2366 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
2367 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002368
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002369 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
2370 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07002371 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002372 writer.println(prefix + " Homescreen " + i);
2373
2374 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
2375 for (int j = 0; j < layout.getChildCount(); j++) {
2376 Object tag = layout.getChildAt(j).getTag();
2377 if (tag != null) {
2378 writer.println(prefix + " " + tag.toString());
2379 }
2380 }
2381 }
2382
2383 writer.println(prefix + " Hotseat");
Sunny Goyal876e4622018-11-02 13:50:40 -07002384 ViewGroup layout = mHotseat.getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07002385 for (int j = 0; j < layout.getChildCount(); j++) {
2386 Object tag = layout.getChildAt(j).getTag();
2387 if (tag != null) {
2388 writer.println(prefix + " " + tag.toString());
2389 }
Adam Cohen4caf2982013-08-20 18:54:31 -07002390 }
Sunny Goyala1365452015-10-01 15:46:24 -07002391 }
2392
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002393 writer.println(prefix + "Misc:");
2394 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
2395 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
2396 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
Sunny Goyale43d00d2018-05-14 14:23:18 -07002397 writer.println(" mRotationHelper: " + mRotationHelper);
Sunny Goyalcb2c5052018-10-15 12:30:29 -07002398 // Extra logging for b/116853349
Hyunyoung Songa310a802019-04-25 13:57:16 -07002399 mDragLayer.dump(prefix, writer);
2400 mStateManager.dump(prefix, writer);
Sunny Goyale43d00d2018-05-14 14:23:18 -07002401 dumpMisc(writer);
Sunny Goyal2bba1902018-02-27 12:20:50 -08002402
2403 try {
2404 FileLog.flushAll(writer);
2405 } catch (Exception e) {
2406 // Ignore
2407 }
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002408
2409 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002410
Adam Cohen9211d422014-10-07 18:14:53 -07002411 if (mLauncherCallbacks != null) {
2412 mLauncherCallbacks.dump(prefix, fd, writer, args);
2413 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002414 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002415
Sunny Goyal66b24572016-09-21 15:57:55 -07002416 @Override
Sunny Goyal66b24572016-09-21 15:57:55 -07002417 public void onProvideKeyboardShortcuts(
2418 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
2419
2420 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002421 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002422 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
2423 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
Vadim Tryshev74c261c2018-06-12 13:44:43 -07002424 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.widget_button_text),
2425 KeyEvent.KEYCODE_W, KeyEvent.META_CTRL_ON));
Sunny Goyal66b24572016-09-21 15:57:55 -07002426 }
Vadim Tryshev3455f8d2018-05-11 20:02:20 -07002427 final View currentFocus = getCurrentFocus();
2428 if (currentFocus != null) {
2429 if (new CustomActionsPopup(this, currentFocus).canShow()) {
2430 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
2431 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
2432 }
2433 if (currentFocus.getTag() instanceof ItemInfo
2434 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
2435 shortcutInfos.add(new KeyboardShortcutInfo(
2436 getString(R.string.shortcuts_menu_with_notifications_description),
2437 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
2438 }
Sunny Goyal66b24572016-09-21 15:57:55 -07002439 }
2440 if (!shortcutInfos.isEmpty()) {
2441 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
2442 }
2443
2444 super.onProvideKeyboardShortcuts(data, menu, deviceId);
2445 }
2446
2447 @Override
2448 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
2449 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
2450 switch (keyCode) {
2451 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002452 if (isInState(NORMAL)) {
2453 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07002454 return true;
2455 }
2456 break;
2457 case KeyEvent.KEYCODE_S: {
2458 View focusedView = getCurrentFocus();
2459 if (focusedView instanceof BubbleTextView
2460 && focusedView.getTag() instanceof ItemInfo
2461 && mAccessibilityDelegate.performAction(focusedView,
2462 (ItemInfo) focusedView.getTag(),
2463 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal00ac9202017-11-09 15:24:06 -08002464 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07002465 return true;
2466 }
2467 break;
2468 }
2469 case KeyEvent.KEYCODE_O:
2470 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
2471 return true;
2472 }
2473 break;
Vadim Tryshev74c261c2018-06-12 13:44:43 -07002474 case KeyEvent.KEYCODE_W:
2475 if (isInState(NORMAL)) {
2476 OptionsPopupView.openWidgets(this);
2477 return true;
2478 }
2479 break;
Sunny Goyal66b24572016-09-21 15:57:55 -07002480 }
2481 }
2482 return super.onKeyShortcut(keyCode, event);
2483 }
2484
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002485 @Override
2486 public boolean onKeyUp(int keyCode, KeyEvent event) {
2487 if (keyCode == KeyEvent.KEYCODE_MENU) {
2488 // KEYCODE_MENU is sent by some tests, for example
2489 // LauncherJankTests#testWidgetsContainerFling. Don't just remove its handling.
2490 if (!mDragController.isDragging() && !mWorkspace.isSwitchingState() &&
2491 isInState(NORMAL)) {
2492 // Close any open floating views.
2493 AbstractFloatingView.closeAllOpenViews(this);
2494
2495 // Setting the touch point to (-1, -1) will show the options popup in the center of
2496 // the screen.
Sunny Goyal2fd7a8b2018-03-30 17:10:13 -07002497 OptionsPopupView.showDefaultOptions(this, -1, -1);
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002498 }
2499 return true;
2500 }
2501 return super.onKeyUp(keyCode, event);
2502 }
2503
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002504 public static Launcher getLauncher(Context context) {
Sunny Goyal87b5eb62018-07-03 15:53:39 -07002505 return (Launcher) fromContext(context);
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002506 }
2507
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002508 /**
Sunny Goyalab770a12018-11-14 15:17:26 -08002509 * Just a wrapper around the type cast to allow easier tracking of calls.
2510 */
2511 public static <T extends Launcher> T cast(ActivityContext activityContext) {
2512 return (T) activityContext;
2513 }
2514
2515 /**
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002516 * Callback for listening for onResume
2517 */
2518 public interface OnResumeCallback {
2519
2520 void onLauncherResume();
2521 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002522}