blob: 359d8d932040951f02a052ea5e328610a89dfc21 [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);
Sunny Goyal41d51a02019-05-14 09:44:34 -0700306 mRotationHelper = new RotationHelper(this);
Hyunyoung Song31971a32019-01-27 12:31:12 -0800307 InvariantDeviceProfile idp = app.getInvariantDeviceProfile();
308 initDeviceProfile(idp);
309 idp.addOnChangeListener(this);
Sunny Goyalf7258242015-10-19 16:59:07 -0700310 mSharedPrefs = Utilities.getPrefs(this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800311 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700312 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700313
Joe Onorato41a12d22009-10-31 18:30:00 -0400314 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700315 mAllAppsController = new AllAppsTransitionController(this);
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800316 mStateManager = new LauncherStateManager(this);
Tracy Zhou8b23c6f2018-04-30 17:13:27 -0700317 UiFactory.onCreate(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700318
Sunny Goyalffe83f12014-08-14 17:39:34 -0700319 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700320
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700321 mAppWidgetHost = new LauncherAppWidgetHost(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700322 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700323
Sunny Goyal60820d72017-05-09 12:40:11 -0700324 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700325
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800326 setupViews();
Tony Wickham010d2552017-01-20 08:15:28 -0800327 mPopupDataProvider = new PopupDataProvider(this);
328
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 });
Adam Cohen9211d422014-10-07 18:14:53 -0700399 }
400
Sunny Goyal7779d622015-06-11 16:18:39 -0700401 @Override
Winson Chung8eb49e02018-07-16 13:33:31 -0700402 public void onEnterAnimationComplete() {
403 super.onEnterAnimationComplete();
404 UiFactory.onEnterAnimationComplete(this);
arangelovad783052018-08-17 14:46:45 +0100405 mAllAppsController.highlightWorkTabIfNecessary();
Winson Chung8eb49e02018-07-16 13:33:31 -0700406 }
407
408 @Override
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800409 public void onConfigurationChanged(Configuration newConfig) {
410 int diff = newConfig.diff(mOldConfig);
Jon Mirandaaeb4dd02018-07-11 14:16:23 -0700411
412 if ((diff & CONFIG_LOCALE) != 0) {
413 Folder.setLocaleDependentFields(getResources(), true /* force */);
414 }
415
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800416 if ((diff & (CONFIG_ORIENTATION | CONFIG_SCREEN_SIZE)) != 0) {
Sunny Goyal371ea052019-03-06 15:38:32 -0800417 onIdpChanged(mDeviceProfile.inv);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800418 }
419
420 mOldConfig.setTo(newConfig);
Tracy Zhoua706f002018-03-28 13:55:19 -0700421 UiFactory.onLauncherStateOrResumeChanged(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800422 super.onConfigurationChanged(newConfig);
423 }
424
Sunny Goyal41d51a02019-05-14 09:44:34 -0700425 private boolean supportsFakeLandscapeUI() {
426 return FeatureFlags.FAKE_LANDSCAPE_UI.get() && !mRotationHelper.homeScreenCanRotate();
427 }
428
Sunny Goyal605bcf32018-03-02 15:16:12 -0800429 @Override
Sunny Goyal41d51a02019-05-14 09:44:34 -0700430 public void reapplyUi() {
431 if (supportsFakeLandscapeUI()) {
Sunny Goyal7a39b1b2019-05-02 13:39:16 -0700432 mRotationMode = mStableDeviceProfile == null
433 ? RotationMode.NORMAL : UiFactory.getRotationMode(mDeviceProfile);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700434 }
Sunny Goyal59d086c2018-05-07 17:31:40 -0700435 getRootView().dispatchInsets();
436 getStateManager().reapplyState(true /* cancelCurrentAnimation */);
437 }
438
439 @Override
Sunny Goyal605bcf32018-03-02 15:16:12 -0800440 public void rebindModel() {
441 int currentPage = mWorkspace.getNextPage();
442 if (mModel.startLoader(currentPage)) {
443 mWorkspace.setCurrentPage(currentPage);
444 setWorkspaceLoading(true);
445 }
446 }
447
Hyunyoung Song31971a32019-01-27 12:31:12 -0800448 @Override
449 public void onIdpChanged(int changeFlags, InvariantDeviceProfile idp) {
Sunny Goyal371ea052019-03-06 15:38:32 -0800450 onIdpChanged(idp);
451 }
452
453 private void onIdpChanged(InvariantDeviceProfile idp) {
454 mUserEventDispatcher = null;
455
Hyunyoung Song31971a32019-01-27 12:31:12 -0800456 initDeviceProfile(idp);
Sunny Goyal371ea052019-03-06 15:38:32 -0800457 dispatchDeviceProfileChanged();
458 reapplyUi();
459 mDragLayer.recreateControllers();
460
461 // TODO: We can probably avoid rebind when only screen size changed.
462 rebindModel();
Hyunyoung Song31971a32019-01-27 12:31:12 -0800463 }
464
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400465 public void onAssistantVisibilityChanged(float visibility) {
466 mCurrentAssistantVisibility = visibility;
467 float alpha = 1f - visibility;
468 LauncherState state = mStateManager.getState();
469 if (state == NORMAL) {
470 mAppsView.getAlphaProperty(APPS_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
471 } else if (state == OVERVIEW || state == OVERVIEW_PEEK) {
472 mAppsView.getAlphaProperty(APPS_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
473 mScrimView.getAlphaProperty(SCRIM_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
474 }
475 }
476
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800477 private void initDeviceProfile(InvariantDeviceProfile idp) {
478 // Load configuration-specific DeviceProfile
Sunny Goyal59d086c2018-05-07 17:31:40 -0700479 mDeviceProfile = idp.getDeviceProfile(this);
Sunny Goyal8c48d8b2019-01-25 15:10:18 -0800480 if (isInMultiWindowMode()) {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700481 Display display = getWindowManager().getDefaultDisplay();
482 Point mwSize = new Point();
483 display.getSize(mwSize);
484 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
485 }
Sunny Goyalae6e3182019-04-30 12:04:37 -0700486
Sunny Goyal41d51a02019-05-14 09:44:34 -0700487 if (supportsFakeLandscapeUI()
488 && mDeviceProfile.isVerticalBarLayout()
Sunny Goyalae6e3182019-04-30 12:04:37 -0700489 && !mDeviceProfile.isMultiWindowMode) {
490 mStableDeviceProfile = mDeviceProfile.inv.portraitProfile;
Sunny Goyal7a39b1b2019-05-02 13:39:16 -0700491 mRotationMode = UiFactory.getRotationMode(mDeviceProfile);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700492 } else {
493 mStableDeviceProfile = null;
494 mRotationMode = RotationMode.NORMAL;
495 }
496
Sunny Goyal59d086c2018-05-07 17:31:40 -0700497 onDeviceProfileInitiated();
Sunny Goyalae6e3182019-04-30 12:04:37 -0700498 mModelWriter = mModel.getWriter(getWallpaperDeviceProfile().isVerticalBarLayout(), true);
499 }
500
501 public void updateInsets(Rect insets) {
502 mDeviceProfile.updateInsets(insets);
503 if (mStableDeviceProfile != null) {
Sunny Goyal7a39b1b2019-05-02 13:39:16 -0700504 Rect r = mStableDeviceProfile.getInsets();
505 mRotationMode.mapInsets(this, insets, r);
506 mStableDeviceProfile.updateInsets(r);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700507 }
508 }
509
510 @Override
511 public RotationMode getRotationMode() {
512 return mRotationMode;
513 }
514
515 /**
516 * Device profile to be used by UI elements which are shown directly on top of the wallpaper
517 * and whose presentation is tied to the wallpaper (and physical device) and not the activity
518 * configuration.
519 */
520 @Override
521 public DeviceProfile getWallpaperDeviceProfile() {
522 return mStableDeviceProfile == null ? mDeviceProfile : mStableDeviceProfile;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800523 }
524
Sunny Goyal623eddd2018-03-02 12:24:41 -0800525 public RotationHelper getRotationHelper() {
526 return mRotationHelper;
527 }
528
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700529 public LauncherStateManager getStateManager() {
530 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700531 }
532
Mario Bertschler27288382017-05-24 15:35:09 -0700533 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700534 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800535 return mLauncherView.findViewById(id);
536 }
537
538 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700539 public void onAppWidgetHostReset() {
540 if (mAppWidgetHost != null) {
541 mAppWidgetHost.startListening();
542 }
543 }
544
Adam Cohen9211d422014-10-07 18:14:53 -0700545 private LauncherCallbacks mLauncherCallbacks;
546
Sunny Goyal32554d12015-12-03 15:31:25 -0800547 /**
548 * Call this after onCreate to set or clear overlay.
549 */
550 public void setLauncherOverlay(LauncherOverlay overlay) {
551 if (overlay != null) {
552 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
553 }
554 mWorkspace.setLauncherOverlay(overlay);
555 }
556
Adam Cohen9211d422014-10-07 18:14:53 -0700557 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
558 mLauncherCallbacks = callbacks;
559 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700560 }
561
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700562 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700563 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700564 if (mLauncherCallbacks != null) {
565 mLauncherCallbacks.onLauncherProviderChange();
566 }
567 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700568
Hyunyoung Song3f471442015-04-08 19:01:34 -0700569 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700570 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
571 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700572 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700573 }
574
Tony Wickham010d2552017-01-20 08:15:28 -0800575 public PopupDataProvider getPopupDataProvider() {
576 return mPopupDataProvider;
577 }
578
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700579 @Override
Tony Wickhamf34bee82018-12-03 18:11:39 -0800580 public DotInfo getDotInfoForItem(ItemInfo info) {
581 return mPopupDataProvider.getDotInfoForItem(info);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700582 }
583
584 @Override
585 public void invalidateParent(ItemInfo info) {
Jon Mirandae6f3fa42019-03-04 09:14:40 -0800586 if (info.container >= 0) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700587 View folderIcon = getWorkspace().getHomescreenIconByItemId(info.container);
Jon Mirandae6f3fa42019-03-04 09:14:40 -0800588 if (folderIcon instanceof FolderIcon && folderIcon.getTag() instanceof FolderInfo) {
589 FolderIconPreviewVerifier verifier =
590 new FolderIconPreviewVerifier(getDeviceProfile().inv);
591 verifier.setFolderInfo((FolderInfo) folderIcon.getTag());
592 if (verifier.isItemInPreview(info.rank)) {
593 folderIcon.invalidate();
594 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700595 }
596 }
597 }
598
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000599 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700600 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
601 * a configuration step, this allows the proper animations to run after other transitions.
602 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700603 private int completeAdd(
Sunny Goyal2100c782016-08-22 16:00:03 -0700604 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
Sunny Goyalefb7e842018-10-04 15:11:00 -0700605 int screenId = info.screenId;
Sunny Goyal2100c782016-08-22 16:00:03 -0700606 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700607 // When the screen id represents an actual screen (as opposed to a rank) we make sure
608 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700609 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700610 }
Adam Cohendb364c32014-05-20 14:23:40 -0700611
Sunny Goyal2100c782016-08-22 16:00:03 -0700612 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800613 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700614 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700615 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800616 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700617 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700618 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700619 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700620 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700621 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700622 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700623 int widgetId = appWidgetId;
624 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700625 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700626 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700627 // Since the view was just bound, also launch the configure activity if needed
628 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
629 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800630 if (provider != null) {
631 new WidgetAddFlowHandler(provider)
632 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700633 }
634 }
635 break;
636 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800637 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700638
Adam Cohendb364c32014-05-20 14:23:40 -0700639 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800640 }
641
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800642 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700643 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700644 if (isWorkspaceLoading()) {
645 // process the result once the workspace has loaded.
646 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
647 return;
648 }
649 mPendingActivityResult = null;
650
Winson Chunge341d302013-08-16 14:31:00 -0700651 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700652 final PendingRequestArgs requestArgs = mPendingRequestArgs;
653 setWaitingForResult(null);
654 if (requestArgs == null) {
655 return;
656 }
657
658 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700659
Adam Cohenad4e15c2013-10-17 16:21:35 -0700660 Runnable exitSpringLoaded = new Runnable() {
661 @Override
662 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700663 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700664 }
665 };
666
Michael Jurka8b805b12012-04-18 14:23:14 -0700667 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700668 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700669 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700670 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
671 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700672 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700673 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700674 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700675 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700676 addAppWidgetImpl(
677 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800678 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700679 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700680 }
681 return;
682 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200683
Adam Cohened66b2b2012-01-23 17:28:51 -0800684 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700685 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700686
Adam Cohened66b2b2012-01-23 17:28:51 -0800687 // We have special handling for widgets
688 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800689 final int appWidgetId;
690 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
691 : -1;
692 if (widgetId < 0) {
693 appWidgetId = pendingAddWidgetId;
694 } else {
695 appWidgetId = widgetId;
696 }
697
Adam Cohenad4e15c2013-10-17 16:21:35 -0700698 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800699 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700700 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
701 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700702 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700703 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700704 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700705 @Override
706 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700707 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700708 }
709 };
Adam Cohendb364c32014-05-20 14:23:40 -0700710
Sunny Goyal2100c782016-08-22 16:00:03 -0700711 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
712 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
713 } else {
714 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
715 // When the screen id represents an actual screen (as opposed to a rank)
716 // we make sure that the drop page actually exists.
717 requestArgs.screenId =
718 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700719 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700720 final CellLayout dropLayout =
721 mWorkspace.getScreenWithId(requestArgs.screenId);
722
723 dropLayout.setDropPending(true);
724 final Runnable onComplete = new Runnable() {
725 @Override
726 public void run() {
727 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
728 dropLayout.setDropPending(false);
729 }
730 };
731 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
732 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700733 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800734 return;
735 }
736
Sunny Goyald478c832016-04-01 12:04:16 -0700737 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
738 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700739 if (resultCode == RESULT_OK) {
740 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700741 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700742 }
743 // Leave the widget in the pending state if the user canceled the configure.
744 return;
745 }
746
Sunny Goyalaad90582015-07-09 14:22:50 -0700747 if (requestCode == REQUEST_CREATE_SHORTCUT) {
748 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700749 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
750 completeAdd(requestCode, data, -1, requestArgs);
751 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
752 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
753
Sunny Goyalaad90582015-07-09 14:22:50 -0700754 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700755 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
756 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800757 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800758 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800759 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800760 }
761
762 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700763 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800764 final int requestCode, final int resultCode, final Intent data) {
765 handleActivityResult(requestCode, resultCode, data);
766 if (mLauncherCallbacks != null) {
767 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
768 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800769 }
770
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700771 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700772 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600773 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700774 PendingRequestArgs pendingArgs = mPendingRequestArgs;
775 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
776 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
777 setWaitingForResult(null);
778
Sunny Goyal28c6b962015-10-12 11:42:05 -0700779 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700780 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700781 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700782 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700783 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700784 Intent intent = pendingArgs.getPendingIntent();
785
Sunny Goyal28c6b962015-10-12 11:42:05 -0700786 if (grantResults.length > 0
787 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal369212a2019-03-26 15:03:57 -0700788 startActivitySafely(v, intent, null, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700789 } else {
790 // TODO: Show a snack bar with link to settings
791 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700792 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700793 }
794 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600795 if (mLauncherCallbacks != null) {
796 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
797 grantResults);
798 }
799 }
800
Adam Cohendb364c32014-05-20 14:23:40 -0700801 /**
802 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
803 *
804 * @param screenId the screen id to check
805 * @return the new screen, or screenId if it exists
806 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700807 private int ensurePendingDropLayoutExists(int screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800808 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700809 if (dropLayout == null) {
810 // it's possible that the add screen was removed because it was
811 // empty and a re-bind occurred
812 mWorkspace.addExtraEmptyScreen();
813 return mWorkspace.commitExtraEmptyScreen();
814 } else {
815 return screenId;
816 }
817 }
818
Sunny Goyal2100c782016-08-22 16:00:03 -0700819 @Thunk void completeTwoStageWidgetDrop(
820 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
821 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800822 Runnable onCompleteRunnable = null;
823 int animationType = 0;
824
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700825 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800826 if (resultCode == RESULT_OK) {
827 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
828 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800829 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700830 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800831 onCompleteRunnable = new Runnable() {
832 @Override
833 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700834 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700835 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800836 }
837 };
838 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800839 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800840 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800841 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700842 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700843 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700844 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
845 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700846 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700847 // The animated view may be null in the case of a rotation during widget configuration
848 onCompleteRunnable.run();
849 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800850 }
851
852 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700853 protected void onStop() {
854 super.onStop();
Adam Cohen9211d422014-10-07 18:14:53 -0700855 if (mLauncherCallbacks != null) {
856 mLauncherCallbacks.onStop();
857 }
Miranda Kephart1a359a22019-02-11 13:07:04 -0500858
Hyunyoung Song1241e612018-05-15 21:46:51 -0700859 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
860 mStateManager.getState().containerType, -1);
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700861
862 mAppWidgetHost.setListenIfResumed(false);
863
Tony Wickham010d2552017-01-20 08:15:28 -0800864 NotificationListener.removeNotificationsChangedListener();
Sunny Goyalaf161fd2018-02-05 16:34:57 -0800865 getStateManager().moveToRestState();
Winson Chungedb0d6b2018-03-21 17:32:55 -0700866
Tracy Zhou62646712018-06-26 14:19:02 -0700867 UiFactory.onLauncherStateOrResumeChanged(this);
868
Winson Chungdea51352018-04-24 13:02:10 -0700869 // Workaround for b/78520668, explicitly trim memory once UI is hidden
Winson Chung473730f2018-04-26 11:54:22 -0700870 onTrimMemory(TRIM_MEMORY_UI_HIDDEN);
Michael Jurkacd496d72013-04-11 11:32:45 -0700871 }
872
873 @Override
874 protected void onStart() {
vadimtcb863752018-12-19 17:44:57 -0800875 RaceConditionTracker.onEvent(ON_START_EVT, ENTER);
Michael Jurkacd496d72013-04-11 11:32:45 -0700876 super.onStart();
Adam Cohen9211d422014-10-07 18:14:53 -0700877 if (mLauncherCallbacks != null) {
878 mLauncherCallbacks.onStart();
879 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800880 mAppWidgetHost.setListenIfResumed(true);
Sunny Goyalcac002d2018-01-04 09:51:45 -0800881 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
vadimtcb863752018-12-19 17:44:57 -0800882 RaceConditionTracker.onEvent(ON_START_EVT, EXIT);
Michael Jurkacd496d72013-04-11 11:32:45 -0700883 }
884
Sunny Goyala002c6c2019-02-12 16:20:10 -0800885 private void handleDeferredResume() {
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700886 if (hasBeenResumed()) {
887 getUserEventDispatcher().logActionCommand(Action.Command.RESUME,
888 mStateManager.getState().containerType, -1);
889 getUserEventDispatcher().startSession();
Sunny Goyala002c6c2019-02-12 16:20:10 -0800890
891 UiFactory.onLauncherStateOrResumeChanged(this);
Sunny Goyal369212a2019-03-26 15:03:57 -0700892 AppLaunchTracker.INSTANCE.get(this).onReturnedToHome();
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700893 }
894 }
895
Michael Jurkacd496d72013-04-11 11:32:45 -0700896 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800897 protected void onResume() {
vadimtcb863752018-12-19 17:44:57 -0800898 RaceConditionTracker.onEvent(ON_RESUME_EVT, ENTER);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700899 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800900 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700901 TraceHelper.partitionSection("ON_RESUME", "superCall");
902
Sunny Goyala002c6c2019-02-12 16:20:10 -0800903 mHandler.removeCallbacks(mHandleDeferredResume);
904 Utilities.postAsyncCallback(mHandler, mHandleDeferredResume);
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700905
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700906 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700907 // Process any items that were added while Launcher was away.
908 InstallShortcutReceiver.disableAndFlushInstallQueue(
909 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700910
Sunny Goyala474a9b2017-05-04 16:47:11 -0700911 // Refresh shortcuts if the permission changed.
912 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700913
Tracy Zhoue5575f92018-04-19 11:12:53 -0700914 DiscoveryBounce.showForHomeIfNeeded(this);
Adam Cohen9211d422014-10-07 18:14:53 -0700915 if (mLauncherCallbacks != null) {
916 mLauncherCallbacks.onResume();
917 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800918
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700919 TraceHelper.endSection("ON_RESUME");
vadimtcb863752018-12-19 17:44:57 -0800920 RaceConditionTracker.onEvent(ON_RESUME_EVT, EXIT);
Adam Cohen06dff352012-06-01 17:17:08 -0700921 }
922
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800923 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700924 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700925 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700926 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700927
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700928 super.onPause();
Joe Onorato24b6fd82009-11-12 13:47:09 -0800929 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700930 mDragController.resetLastGestureUpTime();
Hyunyoung Song497708c2019-05-01 16:16:53 -0700931 mDropTargetBar.animateToVisibility(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700932 if (mLauncherCallbacks != null) {
933 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700934 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700935 }
936
Winson Chungd7823942018-02-16 03:23:47 +0000937 @Override
Tracy Zhoua706f002018-03-28 13:55:19 -0700938 protected void onUserLeaveHint() {
939 super.onUserLeaveHint();
940 UiFactory.onLauncherStateOrResumeChanged(this);
941 }
942
943 @Override
Winson Chungd7823942018-02-16 03:23:47 +0000944 public void onWindowFocusChanged(boolean hasFocus) {
945 super.onWindowFocusChanged(hasFocus);
946 mStateManager.onWindowFocusChanged();
947 }
948
Adam Cohenc2d6e892014-10-16 09:49:24 -0700949 public interface LauncherOverlay {
950
951 /**
952 * Touch interaction leading to overscroll has begun
953 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700954 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700955
956 /**
957 * Touch interaction related to overscroll has ended
958 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700959 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700960
961 /**
962 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
963 * screen (or in the case of RTL, the rightmost screen).
964 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700965 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700966
967 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800968 * Called when the launcher is ready to use the overlay
969 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700970 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700971 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700972 }
973
974 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700975 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700976 }
977
978 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
979
Sunny Goyalc86df472016-02-25 09:19:38 -0800980 public void onScrollChanged(float progress) {
981 if (mWorkspace != null) {
982 mWorkspace.onOverlayScrollChanged(progress);
983 }
984 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700985 }
986
Sunny Goyalf9403d92017-10-18 10:55:56 -0700987 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -0700988 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700989 }
990
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800991 /**
992 * Restores the previous state, if it exists.
993 *
994 * @param savedState The previous state.
995 */
996 private void restoreState(Bundle savedState) {
997 if (savedState == null) {
998 return;
999 }
1000
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001001 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -07001002 LauncherState[] stateValues = LauncherState.values();
1003 LauncherState state = stateValues[stateOrdinal];
Sunny Goyalbc683e92017-12-06 10:25:07 -08001004 if (!state.disableRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001005 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001006 }
1007
Sunny Goyal2100c782016-08-22 16:00:03 -07001008 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
1009 if (requestArgs != null) {
1010 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001011 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001012
1013 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001014
1015 SparseArray<Parcelable> widgetsState =
1016 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
1017 if (widgetsState != null) {
1018 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
1019 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001020 }
1021
1022 /**
1023 * Finds all the views we need and configure them properly.
1024 */
1025 private void setupViews() {
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001026 mDragLayer = findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001027 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -07001028 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001029 mWorkspace.initParentViews(mDragLayer);
Sunny Goyal16764582017-11-06 16:00:34 -08001030 mOverviewPanel = findViewById(R.id.overview_panel);
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001031 mHotseat = findViewById(R.id.hotseat);
Craig Mautner360310b2012-10-26 15:13:08 -07001032
Sunny Goyal784f9c32016-03-23 16:56:54 -07001033 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1034 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1035 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001036
Winson Chung4c98d922011-05-31 16:50:48 -07001037 // Setup the drag layer
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001038 mDragLayer.setup(mDragController, mWorkspace);
Sunny Goyal5743f862019-03-28 15:35:32 -07001039 mCancelTouchController = UiFactory.enableLiveTouchControllerChanges(mDragLayer);
Winson Chung4c98d922011-05-31 16:50:48 -07001040
Hyunyoung Song645764e2016-06-06 14:19:02 -07001041 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001042 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1043 // default state, otherwise we will update to the wrong offsets in RTL
1044 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001045 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001046 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001047
Tony Wickham34d2c912015-09-11 09:27:58 -07001048 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001049 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001050
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001051 // Setup Apps
1052 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001053
Govinda Wassermanc06e1512019-04-09 09:56:53 -04001054 // Setup Scrim
1055 mScrimView = findViewById(R.id.scrim_view);
1056
Winson Chung3d503fb2011-07-13 17:25:49 -07001057 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001058 mDragController.setMoveTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001059 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001060
Sunny Goyal7185dd62018-03-14 17:51:49 -07001061 mAllAppsController.setupViews(mAppsView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001062 }
1063
1064 /**
1065 * Creates a view representing a shortcut.
1066 *
1067 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001068 */
Sunny Goyal95899162019-03-27 16:03:06 -07001069 View createShortcut(WorkspaceItemInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001070 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001071 }
1072
1073 /**
1074 * Creates a view representing a shortcut inflated from the specified resource.
1075 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001076 * @param parent The group the shortcut belongs to.
1077 * @param info The data structure describing the shortcut.
1078 *
1079 * @return A View inflated from layoutResId.
1080 */
Sunny Goyal95899162019-03-27 16:03:06 -07001081 public View createShortcut(ViewGroup parent, WorkspaceItemInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -08001082 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
1083 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal95899162019-03-27 16:03:06 -07001084 favorite.applyFromWorkspaceItem(info);
Sunny Goyal8a2a63b2018-03-06 22:15:18 -08001085 favorite.setOnClickListener(ItemClickHandler.INSTANCE);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001086 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001087 return favorite;
1088 }
1089
1090 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -07001091 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001092 *
1093 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001094 */
Sunny Goyalefb7e842018-10-04 15:11:00 -07001095 private void completeAddShortcut(Intent data, int container, int screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001096 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001097 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
1098 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001099 return;
1100 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001101
Jon Mirandac476d6e2017-05-04 16:30:01 -07001102 int[] cellXY = mTmpAddItemCellCoordinates;
1103 CellLayout layout = getCellLayout(container, screenId);
1104
Sunny Goyal95899162019-03-27 16:03:06 -07001105 WorkspaceItemInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001106 if (Utilities.ATLEAST_OREO) {
Sunny Goyal95899162019-03-27 16:03:06 -07001107 info = LauncherAppsCompatVO.createWorkspaceItemFromPinItemRequest(
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001108 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001109 }
1110
1111 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001112 // Legacy shortcuts are only supported for primary profile.
1113 info = Process.myUserHandle().equals(args.user)
1114 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001115
Sunny Goyalb57645f2017-03-20 13:57:28 -07001116 if (info == null) {
1117 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1118 return;
1119 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001120 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001121 // The app is trying to add a shortcut without sufficient permissions
1122 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1123 return;
1124 }
1125 }
1126
Jon Mirandac476d6e2017-05-04 16:30:01 -07001127 if (container < 0) {
1128 // Adding a shortcut to the Workspace.
1129 final View view = createShortcut(info);
1130 boolean foundCellSpan = false;
1131 // First we check if we already know the exact location where we want to add this item.
1132 if (cellX >= 0 && cellY >= 0) {
1133 cellXY[0] = cellX;
1134 cellXY[1] = cellY;
1135 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001136
Jon Mirandac476d6e2017-05-04 16:30:01 -07001137 // If appropriate, either create a folder or add to an existing folder
1138 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001139 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001140 return;
1141 }
1142 DragObject dragObject = new DragObject();
1143 dragObject.dragInfo = info;
1144 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1145 true)) {
1146 return;
1147 }
1148 } else {
1149 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1150 }
1151
1152 if (!foundCellSpan) {
1153 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001154 return;
1155 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001156
1157 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1158 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001159 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001160 // Adding a shortcut to a Folder.
Sunny Goyale29897f2017-07-20 10:09:42 -07001161 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001162 if (folderIcon != null) {
1163 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1164 folderInfo.add(info, args.rank, false);
1165 } else {
Sunny Goyale29897f2017-07-20 10:09:42 -07001166 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001167 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001168 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001169 }
1170
Sunny Goyalefb7e842018-10-04 15:11:00 -07001171 public FolderIcon findFolderIcon(final int folderIconId) {
Sunny Goyal83fd25e2018-07-09 16:47:01 -07001172 return (FolderIcon) mWorkspace.getHomescreenIconByItemId(folderIconId);
Sunny Goyale29897f2017-07-20 10:09:42 -07001173 }
1174
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001175 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001176 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001177 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001178 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001179 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001180 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001181 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1182
Adam Cohened66b2b2012-01-23 17:28:51 -08001183 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001184 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001185 }
Romain Guycbb89e42009-06-08 15:52:54 -07001186
Adam Cohen59400422014-03-05 18:07:04 -08001187 LauncherAppWidgetInfo launcherInfo;
1188 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001189 launcherInfo.spanX = itemInfo.spanX;
1190 launcherInfo.spanY = itemInfo.spanY;
1191 launcherInfo.minSpanX = itemInfo.minSpanX;
1192 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001193 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001194
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001195 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001196 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001197
Sunny Goyal2100c782016-08-22 16:00:03 -07001198 if (hostView == null) {
1199 // Perform actual inflation because we're live
1200 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001201 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001202 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301203 prepareAppWidget(hostView, launcherInfo);
1204 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001205 }
Romain Guycbb89e42009-06-08 15:52:54 -07001206
Sunny Goyald5462aa2016-11-22 16:52:39 +05301207 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001208 hostView.setTag(item);
1209 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001210 hostView.setFocusable(true);
1211 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001212 }
1213
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001214 private final BroadcastReceiver mScreenOffReceiver = new BroadcastReceiver() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001215 @Override
1216 public void onReceive(Context context, Intent intent) {
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001217 // Reset AllApps to its initial state only if we are not in the middle of
1218 // processing a multi-step drop
1219 if (mPendingRequestArgs == null) {
1220 mStateManager.goToState(NORMAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001221 }
1222 }
1223 };
1224
Sunny Goyalaae6fbb2019-01-31 16:05:58 -08001225 public void updateNotificationDots(Predicate<PackageUserKey> updatedDots) {
Tony Wickhamf34bee82018-12-03 18:11:39 -08001226 mWorkspace.updateNotificationDots(updatedDots);
1227 mAppsView.getAppsStore().updateNotificationDots(updatedDots);
Tony Wickham010d2552017-01-20 08:15:28 -08001228 }
1229
Adam Cohended9f8d2010-11-03 13:25:16 -07001230 @Override
1231 public void onAttachedToWindow() {
1232 super.onAttachedToWindow();
1233
Sunny Goyalc86df472016-02-25 09:19:38 -08001234 if (mLauncherCallbacks != null) {
1235 mLauncherCallbacks.onAttachedToWindow();
1236 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001237 }
1238
1239 @Override
1240 public void onDetachedFromWindow() {
1241 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001242
1243 if (mLauncherCallbacks != null) {
1244 mLauncherCallbacks.onDetachedFromWindow();
1245 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001246 }
1247
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001248 public AllAppsTransitionController getAllAppsController() {
1249 return mAllAppsController;
1250 }
1251
Sunny Goyal9d69c8d2018-03-19 13:41:31 -07001252 @Override
Winson Chung1a341002018-01-17 10:00:23 -08001253 public LauncherRootView getRootView() {
1254 return (LauncherRootView) mLauncherView;
1255 }
1256
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001257 @Override
Winson Chunga6945242014-01-08 14:04:34 -08001258 public DragLayer getDragLayer() {
1259 return mDragLayer;
1260 }
1261
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001262 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001263 return mAppsView;
1264 }
1265
Winson Chunga6945242014-01-08 14:04:34 -08001266 public Workspace getWorkspace() {
1267 return mWorkspace;
1268 }
1269
1270 public Hotseat getHotseat() {
1271 return mHotseat;
1272 }
1273
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001274 public <T extends View> T getOverviewPanel() {
Winson Chung8ae41982017-11-10 11:42:13 -08001275 return (T) mOverviewPanel;
Winson Chunga6945242014-01-08 14:04:34 -08001276 }
1277
Sunny Goyal47328fd2016-05-25 18:56:41 -07001278 public DropTargetBar getDropTargetBar() {
1279 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001280 }
1281
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001282 public LauncherAppWidgetHost getAppWidgetHost() {
1283 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001284 }
Romain Guycbb89e42009-06-08 15:52:54 -07001285
Winson Chunga9abd0e2010-10-27 17:18:37 -07001286 public LauncherModel getModel() {
1287 return mModel;
1288 }
1289
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001290 public ModelWriter getModelWriter() {
1291 return mModelWriter;
1292 }
1293
Adam Cohen79d90c52016-04-22 13:29:20 -07001294 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001295 return mSharedPrefs;
1296 }
1297
Sunny Goyalf8d56fc2018-01-31 15:18:11 -08001298 public int getOrientation() { return mOldConfig.orientation; }
Jon Miranda6bed3502017-09-19 14:43:17 -07001299
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001300 @Override
1301 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001302 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001303 super.onNewIntent(intent);
1304
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001305 boolean alreadyOnHome = hasWindowFocus() && ((intent.getFlags() &
Winson15f8b172015-08-19 11:02:39 -07001306 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1307 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001308
1309 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001310 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001311 && AbstractFloatingView.getTopOpenView(this) == null;
Winson15f8b172015-08-19 11:02:39 -07001312 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001313 boolean internalStateHandled = InternalStateHandler
Sunny Goyal8b264562018-02-03 23:00:13 -08001314 .handleNewIntent(this, intent, isStarted());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001315
Winson15f8b172015-08-19 11:02:39 -07001316 if (isActionMain) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001317 if (!internalStateHandled) {
1318 // Note: There should be at most one log per method call. This is enforced
1319 // implicitly by using if-else statements.
1320 UserEventDispatcher ued = getUserEventDispatcher();
1321 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
1322 if (topOpenView != null) {
1323 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
1324 } else if (alreadyOnHome) {
1325 Target target = newContainerTarget(mStateManager.getState().containerType);
1326 target.pageIndex = mWorkspace.getCurrentPage();
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001327 ued.logActionCommand(Action.Command.HOME_INTENT, target,
1328 newContainerTarget(ContainerType.WORKSPACE));
Sunny Goyald3864fa2017-11-14 15:06:36 -08001329 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001330
Sunny Goyald3864fa2017-11-14 15:06:36 -08001331 // In all these cases, only animate if we're already on home
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001332 AbstractFloatingView.closeAllOpenViews(this, isStarted());
Jon Mirandafeba90f2016-10-06 10:53:29 -07001333
Sunny Goyala2467272018-03-16 14:53:05 -07001334 if (!isInState(NORMAL)) {
1335 // Only change state, if not already the same. This prevents cancelling any
1336 // animations running as part of resume
1337 mStateManager.goToState(NORMAL);
1338 }
Sunny Goyald3864fa2017-11-14 15:06:36 -08001339
1340 // Reset the apps view
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001341 if (!alreadyOnHome) {
Sunny Goyal7185dd62018-03-14 17:51:49 -07001342 mAppsView.reset(isStarted() /* animate */);
Sunny Goyald3864fa2017-11-14 15:06:36 -08001343 }
1344
Sunny Goyalaad33592018-08-07 17:20:35 -07001345 if (shouldMoveToDefaultScreen && !mWorkspace.isHandlingTouch()) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001346 mWorkspace.post(mWorkspace::moveToDefaultScreen);
1347 }
1348 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001349
1350 final View v = getWindow().peekDecorView();
1351 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001352 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001353 }
1354
Adam Cohen9211d422014-10-07 18:14:53 -07001355 if (mLauncherCallbacks != null) {
Sunny Goyal85462132018-04-24 11:39:37 -07001356 mLauncherCallbacks.onHomeIntent(internalStateHandled);
Adam Cohen9211d422014-10-07 18:14:53 -07001357 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001358 }
Winson15f8b172015-08-19 11:02:39 -07001359
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001360 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001361 }
1362
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001363 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001364 public void onRestoreInstanceState(Bundle state) {
1365 super.onRestoreInstanceState(state);
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001366 mWorkspace.restoreInstanceStateForChild(mSynchronouslyBoundPage);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001367 }
1368
1369 @Override
1370 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001371 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001372 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001373
Adam Cohen21cd0022013-10-09 18:57:02 -07001374 }
Sunny Goyalea609262017-10-25 15:47:38 -07001375 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001376
1377
1378 AbstractFloatingView widgets = AbstractFloatingView
1379 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1380 if (widgets != null) {
1381 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1382 widgets.saveHierarchyState(widgetsState);
1383 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1384 } else {
1385 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1386 }
1387
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001388 // We close any open folders and shortcut containers since they will not be re-opened,
1389 // and we need to make sure this state is reflected.
1390 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001391
Sunny Goyal2100c782016-08-22 16:00:03 -07001392 if (mPendingRequestArgs != null) {
1393 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1394 }
1395 if (mPendingActivityResult != null) {
1396 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001397 }
1398
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001399 super.onSaveInstanceState(outState);
1400
Adam Cohen9211d422014-10-07 18:14:53 -07001401 if (mLauncherCallbacks != null) {
1402 mLauncherCallbacks.onSaveInstanceState(outState);
1403 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001404 }
1405
1406 @Override
1407 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001408 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001409
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001410 unregisterReceiver(mScreenOffReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001411 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001412
Sunny Goyal5743f862019-03-28 15:35:32 -07001413 if (mCancelTouchController != null) {
1414 mCancelTouchController.run();
1415 mCancelTouchController = null;
1416 }
Tony Wickhama0068302018-04-11 16:16:11 -07001417
Winson Chungcd2b0142011-06-08 16:02:26 -07001418 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001419 // It's possible to receive onDestroy after a new Launcher activity has
1420 // been created. In this case, don't interfere with the new Launcher.
1421 if (mModel.isCurrentCallbacks(this)) {
1422 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001423 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001424 }
Sunny Goyal623eddd2018-03-02 12:24:41 -08001425 mRotationHelper.destroy();
Sunny Goyal745bad92016-05-02 10:54:12 -07001426
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001427 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001428 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001429 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001430 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001431 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001432
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001433 TextKeyListener.getInstance().release();
Tony2917a8b2017-07-31 23:29:42 -07001434 clearPendingBinds();
Hyunyoung Song31971a32019-01-27 12:31:12 -08001435 LauncherAppState.getIDP(this).removeOnChangeListener(this);
Adam Cohen9211d422014-10-07 18:14:53 -07001436 if (mLauncherCallbacks != null) {
1437 mLauncherCallbacks.onDestroy();
1438 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001439 }
1440
Sunny Goyalae502842016-06-17 08:43:56 -07001441 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1442 return mAccessibilityDelegate;
1443 }
1444
Adam Cohena9cf38f2011-05-02 15:36:58 -07001445 public DragController getDragController() {
1446 return mDragController;
1447 }
1448
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001449 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001450 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001451 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001452 }
1453
1454 @Override
1455 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1456 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001457 try {
1458 super.startIntentSenderForResult(intent, requestCode,
1459 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1460 } catch (IntentSender.SendIntentException e) {
1461 throw new ActivityNotFoundException();
1462 }
1463 }
1464
Winson Chung70d72102011-08-12 11:24:35 -07001465 /**
1466 * Indicates that we want global search for this activity by setting the globalSearch
1467 * argument for {@link #startSearch} to true.
1468 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001469 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001470 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001471 Bundle appSearchData, boolean globalSearch) {
1472 if (appSearchData == null) {
1473 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001474 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001475 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001476
Sunny Goyal6f28e712016-09-13 14:19:29 -07001477 if (mLauncherCallbacks == null ||
1478 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1479 // Starting search from the callbacks failed. Start the default global search.
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001480 super.startSearch(initialQuery, selectInitialQuery, appSearchData, true);
Ian Parkinson047036e2014-09-01 15:40:53 +01001481 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001482
1483 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001484 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001485 }
1486
Joe Onorato9c1289c2009-08-17 11:03:03 -04001487 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001488 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001489 }
1490
Adam Cohen517a7f52014-03-01 12:12:59 -08001491 public boolean isWorkspaceLoading() {
1492 return mWorkspaceLoading;
1493 }
1494
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001495 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001496 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001497 }
1498
Sunny Goyal04a324a2017-01-20 21:08:59 -08001499 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001500 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001501 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001502
Sunny Goyal2100c782016-08-22 16:00:03 -07001503 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001504 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001505 if (LOGD) {
1506 Log.d(TAG, "Adding widget from drop");
1507 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001508 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001509 }
1510
Sunny Goyal2100c782016-08-22 16:00:03 -07001511 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001512 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1513 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1514 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001515
Adam Cohenad4e15c2013-10-17 16:21:35 -07001516 Runnable onComplete = new Runnable() {
1517 @Override
1518 public void run() {
1519 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001520 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001521 }
1522 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001523 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001524 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001525 }
1526 }
Romain Guycbb89e42009-06-08 15:52:54 -07001527
Sunny Goyalefb7e842018-10-04 15:11:00 -07001528 public void addPendingItem(PendingAddItemInfo info, int container, int screenId,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001529 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001530 info.container = container;
1531 info.screenId = screenId;
1532 if (cell != null) {
1533 info.cellX = cell[0];
1534 info.cellY = cell[1];
1535 }
1536 info.spanX = spanX;
1537 info.spanY = spanY;
1538
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001539 switch (info.itemType) {
1540 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1541 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001542 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001543 break;
1544 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001545 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001546 break;
1547 default:
1548 throw new IllegalStateException("Unknown item type: " + info.itemType);
1549 }
1550 }
1551
Adam Cohenfbba09b2011-07-18 21:43:05 -07001552 /**
1553 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001554 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001555 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001556 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1557 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001558 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1559 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1560 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001561 }
1562
Adam Cohenfbba09b2011-07-18 21:43:05 -07001563 /**
1564 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001565 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001566 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001567 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001568 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001569 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001570 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001571 // In the case where we've prebound the widget, we remove it from the DragLayer
1572 if (LOGD) {
1573 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1574 }
1575 getDragLayer().removeView(hostView);
1576
Adam Cohened66b2b2012-01-23 17:28:51 -08001577 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001578 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001579
1580 // Clear the boundWidget so that it doesn't get destroyed.
1581 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001582 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001583 // In this case, we either need to start an activity to get permission to bind
1584 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001585 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1586 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1587 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1588 this, info.componentName);
1589 } else {
1590 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1591 }
Adam Cohendd70d662012-10-04 16:53:44 -07001592 Bundle options = info.bindOptions;
1593
Sunny Goyalffe83f12014-08-14 17:39:34 -07001594 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1595 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001596 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001597 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001598 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001599 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001600 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001601 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001602 }
1603
Sunny Goyalefb7e842018-10-04 15:11:00 -07001604 FolderIcon addFolder(CellLayout layout, int container, final int screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001605 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001606 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001607 folderInfo.title = getText(R.string.folder_name);
1608
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001609 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001610 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001611
1612 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001613 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301614 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001615 // Force measure the new folder icon
1616 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1617 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001618 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001619 }
Romain Guycbb89e42009-06-08 15:52:54 -07001620
Winsonc0b52fe2015-09-09 16:38:15 -07001621 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001622 * Unbinds the view for the specified item, and removes the item and all its children.
1623 *
1624 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001625 * @param itemInfo the {@link ItemInfo} for this view.
1626 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001627 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001628 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Sunny Goyal95899162019-03-27 16:03:06 -07001629 if (itemInfo instanceof WorkspaceItemInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001630 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001631 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1632 if (folderIcon instanceof FolderIcon) {
Sunny Goyal95899162019-03-27 16:03:06 -07001633 ((FolderInfo) folderIcon.getTag()).remove((WorkspaceItemInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001634 } else {
1635 mWorkspace.removeWorkspaceItem(v);
1636 }
Winsonc0b52fe2015-09-09 16:38:15 -07001637 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001638 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001639 }
1640 } else if (itemInfo instanceof FolderInfo) {
1641 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001642 if (v instanceof FolderIcon) {
1643 ((FolderIcon) v).removeListeners();
1644 }
Winsonc0b52fe2015-09-09 16:38:15 -07001645 mWorkspace.removeWorkspaceItem(v);
1646 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001647 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001648 }
1649 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1650 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001651 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001652 if (deleteFromDb) {
Tony Wickham6a71a5b2018-08-21 11:40:23 -07001653 getModelWriter().deleteWidgetInfo(widgetInfo, getAppWidgetHost());
Winsonc0b52fe2015-09-09 16:38:15 -07001654 }
1655 } else {
1656 return false;
1657 }
1658 return true;
1659 }
1660
Tony Wickham6a71a5b2018-08-21 11:40:23 -07001661
Winsonc0b52fe2015-09-09 16:38:15 -07001662
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001663 @Override
1664 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001665 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001666 }
1667
Joe Onorato88ec0992009-11-19 13:16:06 -08001668 @Override
1669 public void onBackPressed() {
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08001670 if (finishAutoCancelActionMode()) {
1671 return;
1672 }
Adam Cohen9211d422014-10-07 18:14:53 -07001673 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1674 return;
1675 }
1676
Sunny Goyal45478022015-06-08 16:52:41 -07001677 if (mDragController.isDragging()) {
1678 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001679 return;
1680 }
1681
Jon Mirandafeba90f2016-10-06 10:53:29 -07001682 // Note: There should be at most one log per method call. This is enforced implicitly
1683 // by using if-else statements.
1684 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001685 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
Tony Wickham52c1b662018-05-21 13:13:58 -07001686 if (topView != null && topView.onBackPressed()) {
1687 // Handled by the floating view.
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001688 } else {
Sunny Goyale39690b2018-08-02 13:35:07 -07001689 mStateManager.getState().onBackPressed(this);
Michael Jurkaaf442092010-06-10 17:01:57 -07001690 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001691 }
1692
Sunny Goyalfe770c92016-09-27 14:38:58 -07001693 @TargetApi(Build.VERSION_CODES.M)
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001694 @Override
Jon Miranda73c27ec2018-05-16 18:06:23 -07001695 public ActivityOptions getActivityLaunchOptions(View v) {
1696 return mAppTransitionManager.getActivityLaunchOptions(this, v);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001697 }
1698
Tony Wickham3a6746a2018-03-29 09:39:56 -07001699 public LauncherAppTransitionManager getAppTransitionManager() {
1700 return mAppTransitionManager;
1701 }
1702
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001703 @TargetApi(Build.VERSION_CODES.M)
1704 @Override
1705 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
1706 // Due to legacy reasons, direct call shortcuts require Launchers to have the
1707 // corresponding permission. Show the appropriate permission prompt if that
1708 // is the case.
1709 if (intent.getComponent() == null
1710 && Intent.ACTION_CALL.equals(intent.getAction())
1711 && checkSelfPermission(android.Manifest.permission.CALL_PHONE) !=
1712 PackageManager.PERMISSION_GRANTED) {
1713
1714 setWaitingForResult(PendingRequestArgs
1715 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
1716 requestPermissions(new String[]{android.Manifest.permission.CALL_PHONE},
1717 REQUEST_PERMISSION_CALL_PHONE);
1718 return true;
1719 } else {
1720 return false;
1721 }
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001722 }
1723
Hyunyoung Song46d07f72018-05-22 15:41:25 -07001724 @Override
Hyunyoung Songfc007472018-10-25 14:09:50 -07001725 public int getCurrentState() {
1726 if(mStateManager.getState() == LauncherState.ALL_APPS) {
1727 return StatsLogUtils.LAUNCHER_STATE_ALLAPPS;
Govinda Wassermanc06e1512019-04-09 09:56:53 -04001728 } else if (mStateManager.getState() == OVERVIEW) {
Hyunyoung Songfc007472018-10-25 14:09:50 -07001729 return StatsLogUtils.LAUNCHER_STATE_OVERVIEW;
1730 }
Hyunyoung Song0ae38882018-11-05 14:45:19 -08001731 return StatsLogUtils.LAUNCHER_STATE_HOME;
Hyunyoung Songfc007472018-10-25 14:09:50 -07001732 }
1733
1734 @Override
Hyunyoung Song46d07f72018-05-22 15:41:25 -07001735 public void modifyUserEvent(LauncherLogProto.LauncherEvent event) {
1736 if (event.srcTarget != null && event.srcTarget.length > 0 &&
1737 event.srcTarget[1].containerType == ContainerType.PREDICTION) {
1738 Target[] targets = new Target[3];
1739 targets[0] = event.srcTarget[0];
1740 targets[1] = event.srcTarget[1];
1741 targets[2] = newTarget(Target.Type.CONTAINER);
1742 event.srcTarget = targets;
1743 LauncherState state = mStateManager.getState();
1744 if (state == LauncherState.ALL_APPS) {
1745 event.srcTarget[2].containerType = ContainerType.ALLAPPS;
Govinda Wassermanc06e1512019-04-09 09:56:53 -04001746 } else if (state == OVERVIEW) {
Hyunyoung Song46d07f72018-05-22 15:41:25 -07001747 event.srcTarget[2].containerType = ContainerType.TASKSWITCHER;
1748 }
1749 }
1750 }
1751
Sunny Goyal369212a2019-03-26 15:03:57 -07001752 public boolean startActivitySafely(View v, Intent intent, ItemInfo item,
1753 @Nullable String sourceContainer) {
1754 boolean success = super.startActivitySafely(v, intent, item, sourceContainer);
Hyunyoung Song1241e612018-05-15 21:46:51 -07001755 if (success && v instanceof BubbleTextView) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001756 // This is set to the view that launched the activity that navigated the user away
1757 // from launcher. Since there is no callback for when the activity has finished
1758 // launching, enable the press state and keep this reference to reset the press
1759 // state when we return to launcher.
1760 BubbleTextView btv = (BubbleTextView) v;
1761 btv.setStayPressed(true);
1762 setOnResumeCallback(btv);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001763 }
Hyunyoung Song1241e612018-05-15 21:46:51 -07001764 return success;
Michael Jurka86a720e2012-05-09 11:23:23 -07001765 }
1766
Winson Chung3d503fb2011-07-13 17:25:49 -07001767 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001768 // TODO: Remove this method
Sunny Goyal876e4622018-11-02 13:50:40 -07001769 return mHotseat != null && (layout == mHotseat);
Winson Chung3d503fb2011-07-13 17:25:49 -07001770 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001771
Winson Chung3d503fb2011-07-13 17:25:49 -07001772 /**
1773 * Returns the CellLayout of the specified container at the specified screen.
1774 */
Sunny Goyalefb7e842018-10-04 15:11:00 -07001775 public CellLayout getCellLayout(int container, int screenId) {
Sunny Goyal876e4622018-11-02 13:50:40 -07001776 return (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1777 ? mHotseat : mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08001778 }
1779
Michael Jurkae326f182011-11-21 14:05:46 -08001780 @Override
1781 public void onTrimMemory(int level) {
1782 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07001783 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
1784 // The widget preview db can result in holding onto over
1785 // 3MB of memory for caching which isn't necessary.
1786 SQLiteDatabase.releaseMemory();
1787
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08001788 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07001789 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08001790 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01001791 if (mLauncherCallbacks != null) {
1792 mLauncherCallbacks.onTrimMemory(level);
1793 }
Winson Chung62a70be2018-02-23 15:10:05 -08001794 UiFactory.onTrimMemory(this, level);
Michael Jurkae326f182011-11-21 14:05:46 -08001795 }
1796
Michael Jurkad7c28052012-04-27 15:43:36 -07001797 @Override
1798 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07001799 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07001800 final List<CharSequence> text = event.getText();
1801 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07001802 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001803 // TODO: When can workspace be null?
1804 text.add(mWorkspace == null
1805 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07001806 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07001807 return result;
1808 }
1809
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001810 public void setOnResumeCallback(OnResumeCallback callback) {
1811 if (mOnResumeCallback != null) {
1812 mOnResumeCallback.onLauncherResume();
1813 }
1814 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07001815 }
1816
Michael Jurka7607c2f2013-04-03 14:33:19 -07001817 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001818 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001819 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07001820 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001821 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07001822 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001823 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07001824 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07001825 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07001826 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001827 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001828
Joe Onorato9c1289c2009-08-17 11:03:03 -04001829 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07001830 * Clear any pending bind callbacks. This is called when is loader is planning to
1831 * perform a full rebind from scratch.
1832 */
1833 @Override
1834 public void clearPendingBinds() {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001835 if (mPendingExecutor != null) {
1836 mPendingExecutor.markCompleted();
1837 mPendingExecutor = null;
1838 }
1839 }
1840
1841 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001842 * Refreshes the shortcuts shown on the workspace.
1843 *
1844 * Implementation of the method from LauncherModel.Callbacks.
1845 */
1846 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001847 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001848 // Floating panels (except the full widget sheet) are associated with individual icons. If
1849 // we are starting a fresh bind, close all such panels as all the icons are about
1850 // to go away.
1851 AbstractFloatingView.closeOpenViews(this, true,
Sunny Goyal7ede6112017-12-05 15:11:21 -08001852 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_REBIND_SAFE);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08001853
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001854 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08001855
Winson Chungd64d1762013-08-20 14:37:16 -07001856 // Clear the workspace because it's going to be rebound
Sunny Goyal2db53422019-03-01 16:06:12 -08001857 mDragController.cancelDrag();
1858
Adam Cohendf035382011-04-11 17:22:04 -07001859 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07001860 mWorkspace.removeAllWorkspaceScreens();
Sunny Goyalc11fac32018-02-27 19:20:15 -08001861 mAppWidgetHost.clearViews();
Winson Chungd64d1762013-08-20 14:37:16 -07001862
Winson Chung3d503fb2011-07-13 17:25:49 -07001863 if (mHotseat != null) {
Sunny Goyalae6e3182019-04-30 12:04:37 -07001864 mHotseat.resetLayout(getWallpaperDeviceProfile().isVerticalBarLayout());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001865 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001866 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001867 }
1868
Adam Cohendcd297f2013-06-18 13:13:40 -07001869 @Override
Sunny Goyalefb7e842018-10-04 15:11:00 -07001870 public void bindScreens(IntArray orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001871 // Make sure the first screen is always at the start.
Jon Miranda7143ba62019-03-15 09:00:05 -07001872 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001873 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalefb7e842018-10-04 15:11:00 -07001874 orderedScreenIds.removeValue(Workspace.FIRST_SCREEN_ID);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001875 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Jon Miranda7143ba62019-03-15 09:00:05 -07001876 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001877 // If there are no screens, we need to have an empty screen
1878 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07001879 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001880 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07001881
Winsonc7d2e832016-07-28 12:24:55 -07001882 // After we have added all the screens, if the wallpaper was locked to the default state,
1883 // then notify to indicate that it can be released and a proper wallpaper offset can be
1884 // computed before the next layout
1885 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07001886 }
1887
Sunny Goyalefb7e842018-10-04 15:11:00 -07001888 private void bindAddScreens(IntArray orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001889 int count = orderedScreenIds.size();
1890 for (int i = 0; i < count; i++) {
Sunny Goyalefb7e842018-10-04 15:11:00 -07001891 int screenId = orderedScreenIds.get(i);
Jon Miranda7143ba62019-03-15 09:00:05 -07001892 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001893 // No need to bind the first screen, as its always bound.
1894 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
1895 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001896 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001897 }
1898
Sunny Goyalb23980c2017-08-17 07:45:25 -07001899 @Override
Tony Wickham6a71a5b2018-08-21 11:40:23 -07001900 public void preAddApps() {
1901 // If there's an undo snackbar, force it to complete to ensure empty screens are removed
1902 // before trying to add new items.
1903 mModelWriter.commitDelete();
1904 AbstractFloatingView snackbar = AbstractFloatingView.getOpenView(this, TYPE_SNACKBAR);
1905 if (snackbar != null) {
1906 snackbar.post(() -> snackbar.close(true));
1907 }
1908 }
1909
1910 @Override
Sunny Goyalefb7e842018-10-04 15:11:00 -07001911 public void bindAppsAdded(IntArray newScreens, ArrayList<ItemInfo> addNotAnimated,
Sunny Goyal29947f02017-12-18 13:49:44 -08001912 ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07001913 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08001914 if (newScreens != null) {
1915 bindAddScreens(newScreens);
1916 }
Winson Chungd64d1762013-08-20 14:37:16 -07001917
1918 // We add the items without animation on non-visible pages, and with
1919 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08001920 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001921 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001922 }
Adam Cohen76a47a12014-02-05 11:47:43 -08001923 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001924 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07001925 }
Winson Chungc58497e2013-09-03 17:48:37 -07001926
Winson Chung87412982013-10-03 18:34:14 -07001927 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07001928 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001929 }
1930
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001931 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001932 * Bind the items start-end from the list.
1933 *
1934 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001935 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07001936 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07001937 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001938 // Get the list of added items and intersect them with the set of items here
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001939 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07001940 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001941 Workspace workspace = mWorkspace;
Sunny Goyalefb7e842018-10-04 15:11:00 -07001942 int newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07001943 int end = items.size();
1944 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001945 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07001946
1947 // Short circuit if we are loading dock items for a configuration which has no dock
1948 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
1949 mHotseat == null) {
1950 continue;
1951 }
1952
Sunny Goyal639e9062015-08-19 19:17:06 -07001953 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001954 switch (item.itemType) {
1955 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1956 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08001957 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Sunny Goyal95899162019-03-27 16:03:06 -07001958 WorkspaceItemInfo info = (WorkspaceItemInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07001959 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001960 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001961 }
1962 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07001963 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001964 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001965 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001966 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001967 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07001968 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1969 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07001970 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07001971 if (view == null) {
1972 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001973 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08001974 break;
1975 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001976 default:
1977 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001978 }
Sunny Goyal639e9062015-08-19 19:17:06 -07001979
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001980 /*
1981 * Remove colliding items.
1982 */
1983 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1984 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
1985 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
1986 View v = cl.getChildAt(item.cellX, item.cellY);
1987 Object tag = v.getTag();
1988 String desc = "Collision while binding workspace item: " + item
1989 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08001990 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001991 throw (new RuntimeException(desc));
1992 } else {
1993 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001994 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001995 continue;
1996 }
1997 }
1998 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05301999 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07002000 if (animateIcons) {
2001 // Animate all the applications up now
2002 view.setAlpha(0f);
2003 view.setScaleX(0f);
2004 view.setScaleY(0f);
2005 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08002006 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07002007 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002008 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07002009
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002010 // Animate to the correct page
2011 if (animateIcons && newItemsScreenId > -1) {
2012 AnimatorSet anim = new AnimatorSet();
2013 anim.playTogether(bounceAnims);
Jon Miranda64eb8ea2018-03-29 11:43:58 -07002014
Sunny Goyalefb7e842018-10-04 15:11:00 -07002015 int currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002016 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
2017 final Runnable startBounceAnimRunnable = anim::start;
2018
2019 if (newItemsScreenId != currentScreenId) {
2020 // We post the animation slightly delayed to prevent slowdowns
2021 // when we are loading right after we return to launcher.
2022 mWorkspace.postDelayed(new Runnable() {
2023 public void run() {
2024 if (mWorkspace != null) {
2025 AbstractFloatingView.closeAllOpenViews(Launcher.this, false);
2026
2027 mWorkspace.snapToPage(newScreenIndex);
2028 mWorkspace.postDelayed(startBounceAnimRunnable,
2029 NEW_APPS_ANIMATION_DELAY);
Winson Chung94d67682013-09-25 16:29:40 -07002030 }
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002031 }
2032 }, NEW_APPS_PAGE_MOVE_DELAY);
2033 } else {
2034 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07002035 }
Winson Chung64359a52013-07-08 17:17:08 -07002036 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002037 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002038 }
2039
Joe Onorato9c1289c2009-08-17 11:03:03 -04002040 /**
2041 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002042 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07002043 public void bindAppWidget(LauncherAppWidgetInfo item) {
2044 View view = inflateAppWidget(item);
2045 if (view != null) {
2046 mWorkspace.addInScreen(view, item);
2047 mWorkspace.requestLayout();
2048 }
2049 }
2050
2051 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002052 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302053 PendingAppWidgetHostView view =
2054 new PendingAppWidgetHostView(this, item, mIconCache, true);
2055 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002056 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002057 }
2058
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002059 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002060
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002061 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08002062
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002063 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2064 // If the provider is not ready, bind as a pending widget.
2065 appWidgetInfo = null;
2066 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2067 // The widget id is not valid. Try to find the widget based on the provider info.
2068 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
2069 } else {
2070 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
2071 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002072
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002073 // If the provider is ready, but the width is not yet restored, try to restore it.
2074 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
2075 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07002076 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002077 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
2078 + " belongs to component " + item.providerName
2079 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002080 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002081 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002082 }
Sunny Goyalff572272014-07-23 13:58:07 -07002083
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002084 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002085 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07002086 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2087 // Id has not been allocated yet. Allocate a new id.
2088 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
2089 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07002090
Sunny Goyald478c832016-04-01 12:04:16 -07002091 // Also try to bind the widget. If the bind fails, the user will be shown
2092 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08002093 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07002094 pendingInfo.spanX = item.spanX;
2095 pendingInfo.spanY = item.spanY;
2096 pendingInfo.minSpanX = item.minSpanX;
2097 pendingInfo.minSpanY = item.minSpanY;
2098 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07002099
2100 boolean isDirectConfig =
2101 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2102 if (isDirectConfig && item.bindOptions != null) {
2103 Bundle newOptions = item.bindOptions.getExtras();
2104 if (options != null) {
2105 newOptions.putAll(options);
2106 }
2107 options = newOptions;
2108 }
Sunny Goyald478c832016-04-01 12:04:16 -07002109 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2110 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002111
Sunny Goyal86df1382016-08-10 15:03:22 -07002112 // We tried to bind once. If we were not able to bind, we would need to
2113 // go through the permission dialog, which means we cannot skip the config
2114 // activity.
2115 item.bindOptions = null;
2116 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2117
Sunny Goyald478c832016-04-01 12:04:16 -07002118 // Bind succeeded
2119 if (success) {
2120 // If the widget has a configure activity, it is still needs to set it up,
2121 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002122 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002123 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2124 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002125 }
Sunny Goyald478c832016-04-01 12:04:16 -07002126
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002127 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002128 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002129 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002130 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002131 // The widget was marked as UI not ready, but there is no configure activity to
2132 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002133 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002134 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002135 }
Sunny Goyalff572272014-07-23 13:58:07 -07002136 }
2137
Sunny Goyald5462aa2016-11-22 16:52:39 +05302138 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002139 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002140 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002141 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002142 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Tony Wickham6a71a5b2018-08-21 11:40:23 -07002143 getModelWriter().deleteWidgetInfo(item, getAppWidgetHost());
Sunny Goyalb23980c2017-08-17 07:45:25 -07002144 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002145 }
2146
Sunny Goyal233ee962015-08-03 13:05:01 -07002147 item.minSpanX = appWidgetInfo.minSpanX;
2148 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302149 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002150 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302151 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002152 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302153 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002154
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002155 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002156 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002157 }
2158
Sunny Goyalff572272014-07-23 13:58:07 -07002159 /**
2160 * Restores a pending widget.
2161 *
2162 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002163 */
Sunny Goyald478c832016-04-01 12:04:16 -07002164 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002165 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2166 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2167 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002168 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002169 }
2170
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002171 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002172 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002173 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2174 info.pendingItemInfo = null;
2175 }
Sunny Goyalff572272014-07-23 13:58:07 -07002176
Sunny Goyalba47faa2017-10-04 16:50:57 -07002177 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002178 view.reInflate();
Sunny Goyalba47faa2017-10-04 16:50:57 -07002179 }
2180
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002181 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002182 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002183 }
2184
Adam Cohen1462de32012-07-24 22:34:36 -07002185 public void onPageBoundSynchronously(int page) {
Sunny Goyalf4aceab2017-12-06 16:16:31 -08002186 mSynchronouslyBoundPage = page;
Adam Cohen1462de32012-07-24 22:34:36 -07002187 }
2188
Sunny Goyal527c7d32015-08-28 15:19:36 -07002189 @Override
2190 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2191 if (mPendingExecutor != null) {
2192 mPendingExecutor.markCompleted();
2193 }
2194 mPendingExecutor = executor;
Sunny Goyal60180b02018-02-07 12:56:30 -08002195 if (!isInState(ALL_APPS)) {
2196 mAppsView.getAppsStore().setDeferUpdates(true);
2197 mPendingExecutor.execute(() -> mAppsView.getAppsStore().setDeferUpdates(false));
2198 }
2199
Sunny Goyal527c7d32015-08-28 15:19:36 -07002200 executor.attachTo(this);
2201 }
2202
2203 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2204 if (mPendingExecutor == executor) {
2205 mPendingExecutor = null;
2206 }
2207 }
2208
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002209 @Override
2210 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
Sunny Goyal6001ea22018-05-10 16:31:00 -07002211 AlphaProperty property = mDragLayer.getAlphaProperty(ALPHA_INDEX_LAUNCHER_LOAD);
2212 if (property.getValue() < 1) {
2213 ObjectAnimator anim = ObjectAnimator.ofFloat(property, MultiValueAlpha.VALUE, 1);
2214 if (executor != null) {
2215 anim.addListener(new AnimatorListenerAdapter() {
2216 @Override
2217 public void onAnimationEnd(Animator animation) {
2218 executor.onLoadAnimationCompleted();
2219 }
2220 });
2221 }
2222 anim.start();
Sunny Goyal29947f02017-12-18 13:49:44 -08002223 } else if (executor != null) {
2224 executor.onLoadAnimationCompleted();
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002225 }
2226 }
2227
Joe Onorato9c1289c2009-08-17 11:03:03 -04002228 /**
2229 * Callback saying that there aren't any more items to bind.
2230 *
2231 * Implementation of the method from LauncherModel.Callbacks.
2232 */
Tonyf80e8932018-12-21 11:58:04 -08002233 public void finishBindingItems(int pageBoundFirst) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002234 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002235 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002236
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002237 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002238
Sunny Goyal2100c782016-08-22 16:00:03 -07002239 if (mPendingActivityResult != null) {
2240 handleActivityResult(mPendingActivityResult.requestCode,
2241 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2242 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002243 }
2244
Sunny Goyala474a9b2017-05-04 16:47:11 -07002245 InstallShortcutReceiver.disableAndFlushInstallQueue(
2246 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002247
Tonyf80e8932018-12-21 11:58:04 -08002248 // When undoing the removal of the last item on a page, return to that page.
Tony Wickham03f27012019-04-25 14:22:54 -07002249 // Since we are just resetting the current page without user interaction,
2250 // override the previous page so we don't log the page switch.
2251 mWorkspace.setCurrentPage(pageBoundFirst, pageBoundFirst /* overridePrevPage */);
Tony Wickham6a71a5b2018-08-21 11:40:23 -07002252
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002253 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002254 }
2255
Winson Chunga2413752012-04-03 14:22:34 -07002256 private boolean canRunNewAppsAnimation() {
2257 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002258 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002259 }
2260
Winson Chungc9168342013-06-26 14:54:55 -07002261 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002262 ValueAnimator bounceAnim = new PropertyListBuilder().alpha(1).scale(1).build(v)
2263 .setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
Winson Chungc9168342013-06-26 14:54:55 -07002264 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002265 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002266 return bounceAnim;
2267 }
2268
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002269 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002270 * Add the icons for all apps.
2271 *
2272 * Implementation of the method from LauncherModel.Callbacks.
2273 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002274 public void bindAllApplications(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002275 mAppsView.getAppsStore().setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002276 }
2277
2278 /**
Zak Cohen658c67a2018-10-19 14:21:05 -07002279 * Copies LauncherModel's map of activities to shortcut counts to Launcher's. This is necessary
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002280 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2281 */
2282 @Override
Zak Cohen658c67a2018-10-19 14:21:05 -07002283 public void bindDeepShortcutMap(HashMap<ComponentKey, Integer> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002284 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002285 }
2286
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002287 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002288 * A package was updated.
2289 *
2290 * Implementation of the method from LauncherModel.Callbacks.
2291 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002292 @Override
2293 public void bindAppsAddedOrUpdated(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002294 mAppsView.getAppsStore().addOrUpdateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002295 }
2296
Sunny Goyal4390ace2014-10-13 11:33:11 -07002297 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002298 public void bindPromiseAppProgressUpdated(PromiseAppInfo app) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002299 mAppsView.getAppsStore().updatePromiseAppProgress(app);
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002300 }
2301
2302 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002303 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002304 mWorkspace.widgetsRestored(widgets);
2305 }
2306
Joe Onorato9c1289c2009-08-17 11:03:03 -04002307 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002308 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002309 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002310 *
2311 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002312 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002313 @Override
Sunny Goyal95899162019-03-27 16:03:06 -07002314 public void bindWorkspaceItemsChanged(ArrayList<WorkspaceItemInfo> updated) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002315 if (!updated.isEmpty()) {
2316 mWorkspace.updateShortcuts(updated);
2317 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002318 }
2319
2320 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002321 * Update the state of a package, typically related to install state.
2322 *
2323 * Implementation of the method from LauncherModel.Callbacks.
2324 */
Sunny Goyale755d462014-07-22 13:48:29 -07002325 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002326 public void bindRestoreItemsChange(HashSet<ItemInfo> updates) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07002327 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07002328 }
2329
2330 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002331 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07002332 * in addition to specific applications to remove, the reason being that
2333 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002334 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07002335 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002336 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07002337 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002338 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002339 mWorkspace.removeItemsByMatcher(matcher);
2340 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002341 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002342
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002343 @Override
2344 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002345 mAppsView.getAppsStore().removeApps(appInfos);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002346 }
Joe Onoratobe386092009-11-17 17:32:16 -08002347
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002348 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002349 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
2350 mPopupDataProvider.setAllWidgets(allWidgets);
Tony Wickham26b17462017-03-20 17:12:24 -07002351 }
2352
Tony Wickham86222d22017-03-29 15:30:43 -07002353 /**
2354 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
2355 * refreshes the widgets and shortcuts associated with the given package/user
2356 */
2357 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07002358 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002359 }
2360
Winson Chung80baf5a2010-08-09 16:03:15 -07002361 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002362 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08002363 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002364 @Override
2365 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
2366 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002367
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002368 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
2369 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07002370 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002371 writer.println(prefix + " Homescreen " + i);
2372
2373 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
2374 for (int j = 0; j < layout.getChildCount(); j++) {
2375 Object tag = layout.getChildAt(j).getTag();
2376 if (tag != null) {
2377 writer.println(prefix + " " + tag.toString());
2378 }
2379 }
2380 }
2381
2382 writer.println(prefix + " Hotseat");
Sunny Goyal876e4622018-11-02 13:50:40 -07002383 ViewGroup layout = mHotseat.getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07002384 for (int j = 0; j < layout.getChildCount(); j++) {
2385 Object tag = layout.getChildAt(j).getTag();
2386 if (tag != null) {
2387 writer.println(prefix + " " + tag.toString());
2388 }
Adam Cohen4caf2982013-08-20 18:54:31 -07002389 }
Sunny Goyala1365452015-10-01 15:46:24 -07002390 }
2391
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002392 writer.println(prefix + "Misc:");
2393 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
2394 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
2395 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
Sunny Goyale43d00d2018-05-14 14:23:18 -07002396 writer.println(" mRotationHelper: " + mRotationHelper);
Sunny Goyalcb2c5052018-10-15 12:30:29 -07002397 // Extra logging for b/116853349
Hyunyoung Songa310a802019-04-25 13:57:16 -07002398 mDragLayer.dump(prefix, writer);
2399 mStateManager.dump(prefix, writer);
Sunny Goyale43d00d2018-05-14 14:23:18 -07002400 dumpMisc(writer);
Sunny Goyal2bba1902018-02-27 12:20:50 -08002401
2402 try {
2403 FileLog.flushAll(writer);
2404 } catch (Exception e) {
2405 // Ignore
2406 }
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002407
2408 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002409
Adam Cohen9211d422014-10-07 18:14:53 -07002410 if (mLauncherCallbacks != null) {
2411 mLauncherCallbacks.dump(prefix, fd, writer, args);
2412 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002413 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002414
Sunny Goyal66b24572016-09-21 15:57:55 -07002415 @Override
Sunny Goyal66b24572016-09-21 15:57:55 -07002416 public void onProvideKeyboardShortcuts(
2417 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
2418
2419 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002420 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002421 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
2422 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
Vadim Tryshev74c261c2018-06-12 13:44:43 -07002423 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.widget_button_text),
2424 KeyEvent.KEYCODE_W, KeyEvent.META_CTRL_ON));
Sunny Goyal66b24572016-09-21 15:57:55 -07002425 }
Vadim Tryshev3455f8d2018-05-11 20:02:20 -07002426 final View currentFocus = getCurrentFocus();
2427 if (currentFocus != null) {
2428 if (new CustomActionsPopup(this, currentFocus).canShow()) {
2429 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
2430 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
2431 }
2432 if (currentFocus.getTag() instanceof ItemInfo
2433 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
2434 shortcutInfos.add(new KeyboardShortcutInfo(
2435 getString(R.string.shortcuts_menu_with_notifications_description),
2436 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
2437 }
Sunny Goyal66b24572016-09-21 15:57:55 -07002438 }
2439 if (!shortcutInfos.isEmpty()) {
2440 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
2441 }
2442
2443 super.onProvideKeyboardShortcuts(data, menu, deviceId);
2444 }
2445
2446 @Override
2447 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
2448 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
2449 switch (keyCode) {
2450 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002451 if (isInState(NORMAL)) {
2452 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07002453 return true;
2454 }
2455 break;
2456 case KeyEvent.KEYCODE_S: {
2457 View focusedView = getCurrentFocus();
2458 if (focusedView instanceof BubbleTextView
2459 && focusedView.getTag() instanceof ItemInfo
2460 && mAccessibilityDelegate.performAction(focusedView,
2461 (ItemInfo) focusedView.getTag(),
2462 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal00ac9202017-11-09 15:24:06 -08002463 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07002464 return true;
2465 }
2466 break;
2467 }
2468 case KeyEvent.KEYCODE_O:
2469 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
2470 return true;
2471 }
2472 break;
Vadim Tryshev74c261c2018-06-12 13:44:43 -07002473 case KeyEvent.KEYCODE_W:
2474 if (isInState(NORMAL)) {
2475 OptionsPopupView.openWidgets(this);
2476 return true;
2477 }
2478 break;
Sunny Goyal66b24572016-09-21 15:57:55 -07002479 }
2480 }
2481 return super.onKeyShortcut(keyCode, event);
2482 }
2483
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002484 @Override
2485 public boolean onKeyUp(int keyCode, KeyEvent event) {
2486 if (keyCode == KeyEvent.KEYCODE_MENU) {
2487 // KEYCODE_MENU is sent by some tests, for example
2488 // LauncherJankTests#testWidgetsContainerFling. Don't just remove its handling.
2489 if (!mDragController.isDragging() && !mWorkspace.isSwitchingState() &&
2490 isInState(NORMAL)) {
2491 // Close any open floating views.
2492 AbstractFloatingView.closeAllOpenViews(this);
2493
2494 // Setting the touch point to (-1, -1) will show the options popup in the center of
2495 // the screen.
Sunny Goyal2fd7a8b2018-03-30 17:10:13 -07002496 OptionsPopupView.showDefaultOptions(this, -1, -1);
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002497 }
2498 return true;
2499 }
2500 return super.onKeyUp(keyCode, event);
2501 }
2502
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002503 public static Launcher getLauncher(Context context) {
Sunny Goyal87b5eb62018-07-03 15:53:39 -07002504 return (Launcher) fromContext(context);
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002505 }
2506
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002507 /**
Sunny Goyalab770a12018-11-14 15:17:26 -08002508 * Just a wrapper around the type cast to allow easier tracking of calls.
2509 */
2510 public static <T extends Launcher> T cast(ActivityContext activityContext) {
2511 return (T) activityContext;
2512 }
2513
2514 /**
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002515 * Callback for listening for onResume
2516 */
2517 public interface OnResumeCallback {
2518
2519 void onLauncherResume();
2520 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002521}