blob: fff8f02fb943a4df7444f591f86fdfc62801099c [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;
Adam Cohen0f668f32014-09-08 19:54:17 +020056import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057import android.os.Bundle;
Hyunyoung Songbd6fba92018-05-16 15:54:31 -070058import android.os.Handler;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070059import android.os.Parcelable;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080060import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070061import android.os.StrictMode;
Michael Jurkaa33411c2012-06-14 16:18:21 -070062import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070064import android.util.Log;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070065import android.util.SparseArray;
Sunny Goyal59d086c2018-05-07 17:31:40 -070066import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070067import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070068import android.view.KeyboardShortcutGroup;
69import android.view.KeyboardShortcutInfo;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080070import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -070071import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070072import android.view.View;
Adam Cohen96d30a12013-07-16 18:13:21 -070073import android.view.ViewGroup;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080074import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070075import android.view.animation.OvershootInterpolator;
Adam Cohen96d30a12013-07-16 18:13:21 -070076import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070077
Sunny Goyal651077b2014-06-30 14:15:31 -070078import com.android.launcher3.DropTarget.DragObject;
Sunny Goyalae502842016-06-17 08:43:56 -070079import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070080import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070081import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal7ede6112017-12-05 15:11:21 -080082import com.android.launcher3.allapps.DiscoveryBounce;
Sunny Goyalf0b6db72018-08-13 16:10:14 -070083import com.android.launcher3.anim.PropertyListBuilder;
Sunny Goyalffe83f12014-08-14 17:39:34 -070084import com.android.launcher3.compat.AppWidgetManagerCompat;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -070085import com.android.launcher3.compat.LauncherAppsCompatVO;
Tony Wickhame0c33232016-02-08 11:37:04 -080086import com.android.launcher3.config.FeatureFlags;
vadimt00d42552018-12-11 17:59:36 -080087import com.android.launcher3.dot.DotInfo;
Vadim Tryshevfedca432015-08-19 17:55:02 -070088import com.android.launcher3.dragndrop.DragController;
89import com.android.launcher3.dragndrop.DragLayer;
90import com.android.launcher3.dragndrop.DragView;
Jon Mirandaaeb4dd02018-07-11 14:16:23 -070091import com.android.launcher3.folder.Folder;
Sunny Goyal26119432016-02-18 22:09:23 +000092import com.android.launcher3.folder.FolderIcon;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070093import com.android.launcher3.folder.FolderIconPreviewVerifier;
Sunny Goyalf840f102018-09-21 14:41:05 -070094import com.android.launcher3.icons.IconCache;
Sunny Goyal66b24572016-09-21 15:57:55 -070095import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -070096import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -070097import com.android.launcher3.logging.FileLog;
Hyunyoung Songfc007472018-10-25 14:09:50 -070098import com.android.launcher3.logging.StatsLogUtils;
Hyunyoung Songaa953652016-04-19 18:30:24 -070099import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700100import com.android.launcher3.logging.UserEventDispatcher.UserEventDelegate;
Sunny Goyal369212a2019-03-26 15:03:57 -0700101import com.android.launcher3.model.AppLaunchTracker;
Sunny Goyala535ae42017-02-27 10:07:13 -0800102import com.android.launcher3.model.ModelWriter;
Sunny Goyala535ae42017-02-27 10:07:13 -0800103import com.android.launcher3.notification.NotificationListener;
Tony Wickham540913e2017-01-23 11:47:51 -0800104import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -0800105import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700106import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyalf8088ee2017-11-10 14:52:00 -0800107import com.android.launcher3.states.InternalStateHandler;
Sunny Goyal623eddd2018-03-02 12:24:41 -0800108import com.android.launcher3.states.RotationHelper;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800109import com.android.launcher3.touch.ItemClickHandler;
Sunny Goyala5ace712017-11-15 17:12:51 -0800110import com.android.launcher3.uioverrides.UiFactory;
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700111import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530112import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
113import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
Sunny Goyalaeb16432017-10-16 11:46:41 -0700114import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyal2100c782016-08-22 16:00:03 -0700115import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700116import com.android.launcher3.util.ComponentKey;
Sunny Goyalefb7e842018-10-04 15:11:00 -0700117import com.android.launcher3.util.IntArray;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700118import com.android.launcher3.util.ItemInfoMatcher;
Sunny Goyal6001ea22018-05-10 16:31:00 -0700119import com.android.launcher3.util.MultiValueAlpha;
120import com.android.launcher3.util.MultiValueAlpha.AlphaProperty;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700121import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800122import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700123import com.android.launcher3.util.PendingRequestArgs;
vadimt00d42552018-12-11 17:59:36 -0800124import com.android.launcher3.util.RaceConditionTracker;
Sunny Goyal8392c822017-06-20 10:03:56 -0700125import com.android.launcher3.util.SystemUiController;
Mario Bertschlera6936942017-05-31 14:48:19 -0700126import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700127import com.android.launcher3.util.Thunk;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700128import com.android.launcher3.util.TraceHelper;
Sunny Goyal326403e2017-10-02 12:45:10 -0700129import com.android.launcher3.util.UiThreadHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700130import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyalab770a12018-11-14 15:17:26 -0800131import com.android.launcher3.views.ActivityContext;
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800132import com.android.launcher3.views.OptionsPopupView;
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400133import com.android.launcher3.views.ScrimView;
Sunny Goyal29947f02017-12-18 13:49:44 -0800134import com.android.launcher3.widget.LauncherAppWidgetHostView;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800135import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700136import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal29947f02017-12-18 13:49:44 -0800137import com.android.launcher3.widget.PendingAppWidgetHostView;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800138import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700139import com.android.launcher3.widget.WidgetHostViewLoader;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700140import com.android.launcher3.widget.WidgetListRowEntry;
141import com.android.launcher3.widget.WidgetsFullSheet;
Sunny Goyal952e63d2017-08-16 04:59:08 -0700142import com.android.launcher3.widget.custom.CustomWidgetParser;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700143
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700144import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700145import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700146import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700147import java.util.Collection;
Zak Cohen658c67a2018-10-19 14:21:05 -0700148import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700149import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700150import java.util.List;
Sunny Goyalaae6fbb2019-01-31 16:05:58 -0800151import java.util.function.Predicate;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700152
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400153import androidx.annotation.IdRes;
Sunny Goyal8c48d8b2019-01-25 15:10:18 -0800154import androidx.annotation.Nullable;
155
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156/**
157 * Default launcher application.
158 */
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700159public class Launcher extends BaseDraggingActivity implements LauncherExterns,
Hyunyoung Song31971a32019-01-27 12:31:12 -0800160 LauncherModel.Callbacks, LauncherProviderChangeListener, UserEventDelegate,
161 InvariantDeviceProfile.OnIDPChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700162 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700163 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164
Winson Chunga2413752012-04-03 14:22:34 -0700165 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700166
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700168 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700169
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700170 private static final int REQUEST_PICK_APPWIDGET = 9;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171
Michael Jurka8b805b12012-04-18 14:23:14 -0700172 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800173 public static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
174 public static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700175
Jon Mirandac476d6e2017-05-04 16:30:01 -0700176 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700177
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700178 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
179
Mathew Inwood876a8462013-06-14 14:12:41 +0100180 /**
181 * IntentStarter uses request codes starting with this. This must be greater than all activity
182 * request codes used internally.
183 */
184 protected static final int REQUEST_LAST = 100;
185
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186 // Type: int
187 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700188 // Type: int
189 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700190 // Type: PendingRequestArgs
191 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
192 // Type: ActivityResultInfo
193 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700194 // Type: SparseArray<Parcelable>
195 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
vadimt00d42552018-12-11 17:59:36 -0800196 public static final String ON_CREATE_EVT = "Launcher.onCreate";
vadimtcb863752018-12-19 17:44:57 -0800197 private static final String ON_START_EVT = "Launcher.onStart";
198 private static final String ON_RESUME_EVT = "Launcher.onResume";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700200 private LauncherStateManager mStateManager;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700201
Adam Cohenad4e15c2013-10-17 16:21:35 -0700202 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800203
Winson Chunga2413752012-04-03 14:22:34 -0700204 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700205 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
206 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
207 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700208
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400209 private static final int APPS_VIEW_ALPHA_CHANNEL_INDEX = 1;
210 private static final int SCRIM_VIEW_ALPHA_CHANNEL_INDEX = 0;
211
Jon Miranda54441f52018-01-24 15:38:25 -0800212 private LauncherAppTransitionManager mAppTransitionManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800213 private Configuration mOldConfig;
Jon Miranda54441f52018-01-24 15:38:25 -0800214
Adam Cohen091440a2015-03-18 14:16:05 -0700215 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700216 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700217 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800218 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700219
Sunny Goyalffe83f12014-08-14 17:39:34 -0700220 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700221 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700222
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700223 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700224
Sunny Goyal316490e2015-06-02 09:38:28 -0700225 @Thunk Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700226
Sunny Goyal47328fd2016-05-25 18:56:41 -0700227 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700228
229 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700230 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700231 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700232
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400233 // Scrim view for the all apps and overview state.
234 @Thunk ScrimView mScrimView;
235
Winson Chung8ae41982017-11-10 11:42:13 -0800236 // UI and state for the overview panel
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800237 private View mOverviewPanel;
Winson Chung8ae41982017-11-10 11:42:13 -0800238
Adam Cohen091440a2015-03-18 14:16:05 -0700239 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400240
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700241 private OnResumeCallback mOnResumeCallback;
242
Sunny Goyal527c7d32015-08-28 15:19:36 -0700243 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700244
Joe Onorato9c1289c2009-08-17 11:03:03 -0400245 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800246 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800247 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700248 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400249
Tony Wickham010d2552017-01-20 08:15:28 -0800250 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700251
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800252 private int mSynchronouslyBoundPage = PagedView.INVALID_PAGE;
Adam Cohen1462de32012-07-24 22:34:36 -0700253
Winson Chung46353de2012-02-16 14:05:10 -0800254 // We only want to get the SharedPreferences once since it does an FS stat each time we get
255 // it from the context.
256 private SharedPreferences mSharedPrefs;
257
Sunny Goyal2100c782016-08-22 16:00:03 -0700258 // Activity result which needs to be processed after workspace has loaded.
259 private ActivityResultInfo mPendingActivityResult;
260 /**
261 * Holds extra information required to handle a result from an external call, like
262 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
263 */
264 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800265
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700266 public ViewGroupFocusHelper mFocusHandler;
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700267
Sunny Goyal623eddd2018-03-02 12:24:41 -0800268 private RotationHelper mRotationHelper;
Sunny Goyal5743f862019-03-28 15:35:32 -0700269 private Runnable mCancelTouchController;
Sunny Goyal7779d622015-06-11 16:18:39 -0700270
Sunny Goyal2db53422019-03-01 16:06:12 -0800271 final Handler mHandler = new Handler();
Sunny Goyala002c6c2019-02-12 16:20:10 -0800272 private final Runnable mHandleDeferredResume = this::handleDeferredResume;
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700273
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400274 private float mCurrentAssistantVisibility = 0f;
275
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800276 @Override
277 protected void onCreate(Bundle savedInstanceState) {
vadimt00d42552018-12-11 17:59:36 -0800278 RaceConditionTracker.onEvent(ON_CREATE_EVT, ENTER);
Winson Chunga2413752012-04-03 14:22:34 -0700279 if (DEBUG_STRICT_MODE) {
280 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
281 .detectDiskReads()
282 .detectDiskWrites()
283 .detectNetwork() // or .detectAll() for all detectable problems
284 .penaltyLog()
285 .build());
286 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
287 .detectLeakedSqlLiteObjects()
288 .detectLeakedClosableObjects()
289 .penaltyLog()
290 .penaltyDeath()
291 .build());
292 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700293 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700294
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800295 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700296 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400297
Sunny Goyal87f784c2017-01-11 10:48:34 -0800298 LauncherAppState app = LauncherAppState.getInstance(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800299 mOldConfig = new Configuration(getResources().getConfiguration());
Sunny Goyal0e7724c2018-02-22 13:22:21 -0800300 mModel = app.setLauncher(this);
Hyunyoung Song31971a32019-01-27 12:31:12 -0800301 InvariantDeviceProfile idp = app.getInvariantDeviceProfile();
302 initDeviceProfile(idp);
303 idp.addOnChangeListener(this);
Sunny Goyalf7258242015-10-19 16:59:07 -0700304 mSharedPrefs = Utilities.getPrefs(this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800305 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700306 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700307
Joe Onorato41a12d22009-10-31 18:30:00 -0400308 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700309 mAllAppsController = new AllAppsTransitionController(this);
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800310 mStateManager = new LauncherStateManager(this);
Tracy Zhou8b23c6f2018-04-30 17:13:27 -0700311 UiFactory.onCreate(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700312
Sunny Goyalffe83f12014-08-14 17:39:34 -0700313 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700314
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700315 mAppWidgetHost = new LauncherAppWidgetHost(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700316 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700317
Sunny Goyal60820d72017-05-09 12:40:11 -0700318 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700319
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800320 setupViews();
Tony Wickham010d2552017-01-20 08:15:28 -0800321 mPopupDataProvider = new PopupDataProvider(this);
322
Sunny Goyal623eddd2018-03-02 12:24:41 -0800323 mRotationHelper = new RotationHelper(this);
Sunny Goyal90548432018-04-04 17:17:00 -0700324 mAppTransitionManager = LauncherAppTransitionManager.newInstance(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800325
Sunny Goyald3864fa2017-11-14 15:06:36 -0800326 boolean internalStateHandled = InternalStateHandler.handleCreate(this, getIntent());
327 if (internalStateHandled) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800328 if (savedInstanceState != null) {
329 // InternalStateHandler has already set the appropriate state.
330 // We dont need to do anything.
331 savedInstanceState.remove(RUNTIME_STATE);
332 }
333 }
334 restoreState(savedInstanceState);
Sunny Goyalbbece862019-03-01 13:13:52 -0800335 mStateManager.reapplyState();
Winson Chungb63b44c2017-11-10 17:54:44 -0800336
Sunny Goyal2100c782016-08-22 16:00:03 -0700337 // We only load the page synchronously if the user rotates (or triggers a
338 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700339 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
340 if (savedInstanceState != null) {
341 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
342 }
Sunny Goyald3864fa2017-11-14 15:06:36 -0800343
Sunny Goyalfe770c92016-09-27 14:38:58 -0700344 if (!mModel.startLoader(currentScreen)) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800345 if (!internalStateHandled) {
346 // If we are not binding synchronously, show a fade in animation when
347 // the first page bind completes.
Sunny Goyal6001ea22018-05-10 16:31:00 -0700348 mDragLayer.getAlphaProperty(ALPHA_INDEX_LAUNCHER_LOAD).setValue(0);
Sunny Goyald3864fa2017-11-14 15:06:36 -0800349 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700350 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700351 // Pages bound synchronously.
352 mWorkspace.setCurrentPage(currentScreen);
353
Sunny Goyal2100c782016-08-22 16:00:03 -0700354 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800355 }
356
357 // For handling default keys
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800358 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800359
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800360 setContentView(mLauncherView);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800361 getRootView().dispatchInsets();
Mario Bertschlera6936942017-05-31 14:48:19 -0700362
Jon Miranda7fda2852017-07-19 16:07:01 -0700363 // Listen for broadcasts
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700364 registerReceiver(mScreenOffReceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));
Sunny Goyala616d2b2017-06-12 13:54:01 -0700365
Sunny Goyal8392c822017-06-20 10:03:56 -0700366 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
367 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700368
369 if (mLauncherCallbacks != null) {
370 mLauncherCallbacks.onCreate(savedInstanceState);
371 }
Sunny Goyal623eddd2018-03-02 12:24:41 -0800372 mRotationHelper.initialize();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700373
374 TraceHelper.endSection("Launcher-onCreate");
vadimt00d42552018-12-11 17:59:36 -0800375 RaceConditionTracker.onEvent(ON_CREATE_EVT, EXIT);
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400376 mStateManager.addStateListener(new LauncherStateManager.StateListener() {
377 @Override
378 public void onStateTransitionStart(LauncherState toState) {}
379
380 @Override
381 public void onStateTransitionComplete(LauncherState finalState) {
382 float alpha = 1f - mCurrentAssistantVisibility;
383 if (finalState == NORMAL) {
384 mAppsView.getAlphaProperty(APPS_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
385 } else if (finalState == OVERVIEW || finalState == OVERVIEW_PEEK) {
386 mAppsView.getAlphaProperty(APPS_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
387 mScrimView.getAlphaProperty(SCRIM_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
388 } else {
389 mAppsView.getAlphaProperty(APPS_VIEW_ALPHA_CHANNEL_INDEX).setValue(1f);
390 mScrimView.getAlphaProperty(SCRIM_VIEW_ALPHA_CHANNEL_INDEX).setValue(1f);
391 }
392 }
393 });
Adam Cohen9211d422014-10-07 18:14:53 -0700394 }
395
Sunny Goyal7779d622015-06-11 16:18:39 -0700396 @Override
Winson Chung8eb49e02018-07-16 13:33:31 -0700397 public void onEnterAnimationComplete() {
398 super.onEnterAnimationComplete();
399 UiFactory.onEnterAnimationComplete(this);
arangelovad783052018-08-17 14:46:45 +0100400 mAllAppsController.highlightWorkTabIfNecessary();
Winson Chung8eb49e02018-07-16 13:33:31 -0700401 }
402
403 @Override
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800404 public void onConfigurationChanged(Configuration newConfig) {
405 int diff = newConfig.diff(mOldConfig);
Jon Mirandaaeb4dd02018-07-11 14:16:23 -0700406
407 if ((diff & CONFIG_LOCALE) != 0) {
408 Folder.setLocaleDependentFields(getResources(), true /* force */);
409 }
410
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800411 if ((diff & (CONFIG_ORIENTATION | CONFIG_SCREEN_SIZE)) != 0) {
Sunny Goyal371ea052019-03-06 15:38:32 -0800412 onIdpChanged(mDeviceProfile.inv);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800413 }
414
415 mOldConfig.setTo(newConfig);
Tracy Zhoua706f002018-03-28 13:55:19 -0700416 UiFactory.onLauncherStateOrResumeChanged(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800417 super.onConfigurationChanged(newConfig);
418 }
419
Sunny Goyal605bcf32018-03-02 15:16:12 -0800420 @Override
Sunny Goyal59d086c2018-05-07 17:31:40 -0700421 protected void reapplyUi() {
422 getRootView().dispatchInsets();
423 getStateManager().reapplyState(true /* cancelCurrentAnimation */);
424 }
425
426 @Override
Sunny Goyal605bcf32018-03-02 15:16:12 -0800427 public void rebindModel() {
428 int currentPage = mWorkspace.getNextPage();
429 if (mModel.startLoader(currentPage)) {
430 mWorkspace.setCurrentPage(currentPage);
431 setWorkspaceLoading(true);
432 }
433 }
434
Hyunyoung Song31971a32019-01-27 12:31:12 -0800435 @Override
436 public void onIdpChanged(int changeFlags, InvariantDeviceProfile idp) {
Sunny Goyal371ea052019-03-06 15:38:32 -0800437 onIdpChanged(idp);
438 }
439
440 private void onIdpChanged(InvariantDeviceProfile idp) {
441 mUserEventDispatcher = null;
442
Hyunyoung Song31971a32019-01-27 12:31:12 -0800443 initDeviceProfile(idp);
Sunny Goyal371ea052019-03-06 15:38:32 -0800444 dispatchDeviceProfileChanged();
445 reapplyUi();
446 mDragLayer.recreateControllers();
447
448 // TODO: We can probably avoid rebind when only screen size changed.
449 rebindModel();
Hyunyoung Song31971a32019-01-27 12:31:12 -0800450 }
451
Govinda Wassermanc06e1512019-04-09 09:56:53 -0400452 public void onAssistantVisibilityChanged(float visibility) {
453 mCurrentAssistantVisibility = visibility;
454 float alpha = 1f - visibility;
455 LauncherState state = mStateManager.getState();
456 if (state == NORMAL) {
457 mAppsView.getAlphaProperty(APPS_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
458 } else if (state == OVERVIEW || state == OVERVIEW_PEEK) {
459 mAppsView.getAlphaProperty(APPS_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
460 mScrimView.getAlphaProperty(SCRIM_VIEW_ALPHA_CHANNEL_INDEX).setValue(alpha);
461 }
462 }
463
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800464 private void initDeviceProfile(InvariantDeviceProfile idp) {
465 // Load configuration-specific DeviceProfile
Sunny Goyal59d086c2018-05-07 17:31:40 -0700466 mDeviceProfile = idp.getDeviceProfile(this);
Sunny Goyal8c48d8b2019-01-25 15:10:18 -0800467 if (isInMultiWindowMode()) {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700468 Display display = getWindowManager().getDefaultDisplay();
469 Point mwSize = new Point();
470 display.getSize(mwSize);
471 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
472 }
473 onDeviceProfileInitiated();
Sunny Goyal605bcf32018-03-02 15:16:12 -0800474 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout(), true);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800475 }
476
Sunny Goyal623eddd2018-03-02 12:24:41 -0800477 public RotationHelper getRotationHelper() {
478 return mRotationHelper;
479 }
480
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700481 public LauncherStateManager getStateManager() {
482 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700483 }
484
Mario Bertschler27288382017-05-24 15:35:09 -0700485 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700486 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800487 return mLauncherView.findViewById(id);
488 }
489
490 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700491 public void onAppWidgetHostReset() {
492 if (mAppWidgetHost != null) {
493 mAppWidgetHost.startListening();
494 }
495 }
496
Adam Cohen9211d422014-10-07 18:14:53 -0700497 private LauncherCallbacks mLauncherCallbacks;
498
Sunny Goyal32554d12015-12-03 15:31:25 -0800499 /**
500 * Call this after onCreate to set or clear overlay.
501 */
502 public void setLauncherOverlay(LauncherOverlay overlay) {
503 if (overlay != null) {
504 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
505 }
506 mWorkspace.setLauncherOverlay(overlay);
507 }
508
Adam Cohen9211d422014-10-07 18:14:53 -0700509 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
510 mLauncherCallbacks = callbacks;
511 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700512 }
513
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700514 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700515 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700516 if (mLauncherCallbacks != null) {
517 mLauncherCallbacks.onLauncherProviderChange();
518 }
519 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700520
Hyunyoung Song3f471442015-04-08 19:01:34 -0700521 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700522 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
523 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700524 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700525 }
526
Tony Wickham010d2552017-01-20 08:15:28 -0800527 public PopupDataProvider getPopupDataProvider() {
528 return mPopupDataProvider;
529 }
530
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700531 @Override
Tony Wickhamf34bee82018-12-03 18:11:39 -0800532 public DotInfo getDotInfoForItem(ItemInfo info) {
533 return mPopupDataProvider.getDotInfoForItem(info);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700534 }
535
536 @Override
537 public void invalidateParent(ItemInfo info) {
Jon Mirandae6f3fa42019-03-04 09:14:40 -0800538 if (info.container >= 0) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700539 View folderIcon = getWorkspace().getHomescreenIconByItemId(info.container);
Jon Mirandae6f3fa42019-03-04 09:14:40 -0800540 if (folderIcon instanceof FolderIcon && folderIcon.getTag() instanceof FolderInfo) {
541 FolderIconPreviewVerifier verifier =
542 new FolderIconPreviewVerifier(getDeviceProfile().inv);
543 verifier.setFolderInfo((FolderInfo) folderIcon.getTag());
544 if (verifier.isItemInPreview(info.rank)) {
545 folderIcon.invalidate();
546 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700547 }
548 }
549 }
550
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000551 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700552 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
553 * a configuration step, this allows the proper animations to run after other transitions.
554 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700555 private int completeAdd(
Sunny Goyal2100c782016-08-22 16:00:03 -0700556 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
Sunny Goyalefb7e842018-10-04 15:11:00 -0700557 int screenId = info.screenId;
Sunny Goyal2100c782016-08-22 16:00:03 -0700558 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700559 // When the screen id represents an actual screen (as opposed to a rank) we make sure
560 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700561 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700562 }
Adam Cohendb364c32014-05-20 14:23:40 -0700563
Sunny Goyal2100c782016-08-22 16:00:03 -0700564 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800565 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700566 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700567 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800568 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700569 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700570 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700571 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700572 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700573 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700574 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700575 int widgetId = appWidgetId;
576 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700577 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700578 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700579 // Since the view was just bound, also launch the configure activity if needed
580 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
581 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800582 if (provider != null) {
583 new WidgetAddFlowHandler(provider)
584 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700585 }
586 }
587 break;
588 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800589 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700590
Adam Cohendb364c32014-05-20 14:23:40 -0700591 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800592 }
593
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800594 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700595 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700596 if (isWorkspaceLoading()) {
597 // process the result once the workspace has loaded.
598 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
599 return;
600 }
601 mPendingActivityResult = null;
602
Winson Chunge341d302013-08-16 14:31:00 -0700603 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700604 final PendingRequestArgs requestArgs = mPendingRequestArgs;
605 setWaitingForResult(null);
606 if (requestArgs == null) {
607 return;
608 }
609
610 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700611
Adam Cohenad4e15c2013-10-17 16:21:35 -0700612 Runnable exitSpringLoaded = new Runnable() {
613 @Override
614 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700615 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700616 }
617 };
618
Michael Jurka8b805b12012-04-18 14:23:14 -0700619 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700620 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700621 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700622 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
623 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700624 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700625 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700626 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700627 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700628 addAppWidgetImpl(
629 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800630 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700631 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700632 }
633 return;
634 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200635
Adam Cohened66b2b2012-01-23 17:28:51 -0800636 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700637 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700638
Adam Cohened66b2b2012-01-23 17:28:51 -0800639 // We have special handling for widgets
640 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800641 final int appWidgetId;
642 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
643 : -1;
644 if (widgetId < 0) {
645 appWidgetId = pendingAddWidgetId;
646 } else {
647 appWidgetId = widgetId;
648 }
649
Adam Cohenad4e15c2013-10-17 16:21:35 -0700650 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800651 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700652 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
653 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700654 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700655 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700656 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700657 @Override
658 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700659 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700660 }
661 };
Adam Cohendb364c32014-05-20 14:23:40 -0700662
Sunny Goyal2100c782016-08-22 16:00:03 -0700663 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
664 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
665 } else {
666 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
667 // When the screen id represents an actual screen (as opposed to a rank)
668 // we make sure that the drop page actually exists.
669 requestArgs.screenId =
670 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700671 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700672 final CellLayout dropLayout =
673 mWorkspace.getScreenWithId(requestArgs.screenId);
674
675 dropLayout.setDropPending(true);
676 final Runnable onComplete = new Runnable() {
677 @Override
678 public void run() {
679 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
680 dropLayout.setDropPending(false);
681 }
682 };
683 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
684 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700685 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800686 return;
687 }
688
Sunny Goyald478c832016-04-01 12:04:16 -0700689 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
690 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700691 if (resultCode == RESULT_OK) {
692 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700693 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700694 }
695 // Leave the widget in the pending state if the user canceled the configure.
696 return;
697 }
698
Sunny Goyalaad90582015-07-09 14:22:50 -0700699 if (requestCode == REQUEST_CREATE_SHORTCUT) {
700 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700701 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
702 completeAdd(requestCode, data, -1, requestArgs);
703 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
704 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
705
Sunny Goyalaad90582015-07-09 14:22:50 -0700706 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700707 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
708 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800709 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800710 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800711 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800712 }
713
714 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700715 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800716 final int requestCode, final int resultCode, final Intent data) {
717 handleActivityResult(requestCode, resultCode, data);
718 if (mLauncherCallbacks != null) {
719 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
720 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800721 }
722
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700723 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700724 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600725 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700726 PendingRequestArgs pendingArgs = mPendingRequestArgs;
727 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
728 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
729 setWaitingForResult(null);
730
Sunny Goyal28c6b962015-10-12 11:42:05 -0700731 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700732 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700733 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700734 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700735 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700736 Intent intent = pendingArgs.getPendingIntent();
737
Sunny Goyal28c6b962015-10-12 11:42:05 -0700738 if (grantResults.length > 0
739 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal369212a2019-03-26 15:03:57 -0700740 startActivitySafely(v, intent, null, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700741 } else {
742 // TODO: Show a snack bar with link to settings
743 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700744 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700745 }
746 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600747 if (mLauncherCallbacks != null) {
748 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
749 grantResults);
750 }
751 }
752
Adam Cohendb364c32014-05-20 14:23:40 -0700753 /**
754 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
755 *
756 * @param screenId the screen id to check
757 * @return the new screen, or screenId if it exists
758 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700759 private int ensurePendingDropLayoutExists(int screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800760 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700761 if (dropLayout == null) {
762 // it's possible that the add screen was removed because it was
763 // empty and a re-bind occurred
764 mWorkspace.addExtraEmptyScreen();
765 return mWorkspace.commitExtraEmptyScreen();
766 } else {
767 return screenId;
768 }
769 }
770
Sunny Goyal2100c782016-08-22 16:00:03 -0700771 @Thunk void completeTwoStageWidgetDrop(
772 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
773 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800774 Runnable onCompleteRunnable = null;
775 int animationType = 0;
776
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700777 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800778 if (resultCode == RESULT_OK) {
779 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
780 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800781 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700782 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800783 onCompleteRunnable = new Runnable() {
784 @Override
785 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700786 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700787 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800788 }
789 };
790 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800791 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800792 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800793 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700794 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700795 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700796 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
797 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700798 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700799 // The animated view may be null in the case of a rotation during widget configuration
800 onCompleteRunnable.run();
801 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800802 }
803
804 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700805 protected void onStop() {
806 super.onStop();
Adam Cohen9211d422014-10-07 18:14:53 -0700807 if (mLauncherCallbacks != null) {
808 mLauncherCallbacks.onStop();
809 }
Miranda Kephart1a359a22019-02-11 13:07:04 -0500810
Hyunyoung Song1241e612018-05-15 21:46:51 -0700811 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
812 mStateManager.getState().containerType, -1);
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700813
814 mAppWidgetHost.setListenIfResumed(false);
815
Tony Wickham010d2552017-01-20 08:15:28 -0800816 NotificationListener.removeNotificationsChangedListener();
Sunny Goyalaf161fd2018-02-05 16:34:57 -0800817 getStateManager().moveToRestState();
Winson Chungedb0d6b2018-03-21 17:32:55 -0700818
Tracy Zhou62646712018-06-26 14:19:02 -0700819 UiFactory.onLauncherStateOrResumeChanged(this);
820
Winson Chungdea51352018-04-24 13:02:10 -0700821 // Workaround for b/78520668, explicitly trim memory once UI is hidden
Winson Chung473730f2018-04-26 11:54:22 -0700822 onTrimMemory(TRIM_MEMORY_UI_HIDDEN);
Michael Jurkacd496d72013-04-11 11:32:45 -0700823 }
824
825 @Override
826 protected void onStart() {
vadimtcb863752018-12-19 17:44:57 -0800827 RaceConditionTracker.onEvent(ON_START_EVT, ENTER);
Michael Jurkacd496d72013-04-11 11:32:45 -0700828 super.onStart();
Adam Cohen9211d422014-10-07 18:14:53 -0700829 if (mLauncherCallbacks != null) {
830 mLauncherCallbacks.onStart();
831 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800832 mAppWidgetHost.setListenIfResumed(true);
Sunny Goyalcac002d2018-01-04 09:51:45 -0800833 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
vadimtcb863752018-12-19 17:44:57 -0800834 RaceConditionTracker.onEvent(ON_START_EVT, EXIT);
Michael Jurkacd496d72013-04-11 11:32:45 -0700835 }
836
Sunny Goyala002c6c2019-02-12 16:20:10 -0800837 private void handleDeferredResume() {
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700838 if (hasBeenResumed()) {
839 getUserEventDispatcher().logActionCommand(Action.Command.RESUME,
840 mStateManager.getState().containerType, -1);
841 getUserEventDispatcher().startSession();
Sunny Goyala002c6c2019-02-12 16:20:10 -0800842
843 UiFactory.onLauncherStateOrResumeChanged(this);
Sunny Goyal369212a2019-03-26 15:03:57 -0700844 AppLaunchTracker.INSTANCE.get(this).onReturnedToHome();
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700845 }
846 }
847
Michael Jurkacd496d72013-04-11 11:32:45 -0700848 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800849 protected void onResume() {
vadimtcb863752018-12-19 17:44:57 -0800850 RaceConditionTracker.onEvent(ON_RESUME_EVT, ENTER);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700851 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800852 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700853 TraceHelper.partitionSection("ON_RESUME", "superCall");
854
Sunny Goyala002c6c2019-02-12 16:20:10 -0800855 mHandler.removeCallbacks(mHandleDeferredResume);
856 Utilities.postAsyncCallback(mHandler, mHandleDeferredResume);
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700857
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700858 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700859 // Process any items that were added while Launcher was away.
860 InstallShortcutReceiver.disableAndFlushInstallQueue(
861 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700862
Sunny Goyala474a9b2017-05-04 16:47:11 -0700863 // Refresh shortcuts if the permission changed.
864 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700865
Tracy Zhoue5575f92018-04-19 11:12:53 -0700866 DiscoveryBounce.showForHomeIfNeeded(this);
Adam Cohen9211d422014-10-07 18:14:53 -0700867 if (mLauncherCallbacks != null) {
868 mLauncherCallbacks.onResume();
869 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800870
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700871 TraceHelper.endSection("ON_RESUME");
vadimtcb863752018-12-19 17:44:57 -0800872 RaceConditionTracker.onEvent(ON_RESUME_EVT, EXIT);
Adam Cohen06dff352012-06-01 17:17:08 -0700873 }
874
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800875 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700876 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700877 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700878 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700879
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700880 super.onPause();
Joe Onorato24b6fd82009-11-12 13:47:09 -0800881 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700882 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700883
Adam Cohen9211d422014-10-07 18:14:53 -0700884 if (mLauncherCallbacks != null) {
885 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700886 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700887 }
888
Winson Chungd7823942018-02-16 03:23:47 +0000889 @Override
Tracy Zhoua706f002018-03-28 13:55:19 -0700890 protected void onUserLeaveHint() {
891 super.onUserLeaveHint();
892 UiFactory.onLauncherStateOrResumeChanged(this);
893 }
894
895 @Override
Winson Chungd7823942018-02-16 03:23:47 +0000896 public void onWindowFocusChanged(boolean hasFocus) {
897 super.onWindowFocusChanged(hasFocus);
898 mStateManager.onWindowFocusChanged();
899 }
900
Adam Cohenc2d6e892014-10-16 09:49:24 -0700901 public interface LauncherOverlay {
902
903 /**
904 * Touch interaction leading to overscroll has begun
905 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700906 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700907
908 /**
909 * Touch interaction related to overscroll has ended
910 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700911 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700912
913 /**
914 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
915 * screen (or in the case of RTL, the rightmost screen).
916 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700917 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700918
919 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800920 * Called when the launcher is ready to use the overlay
921 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700922 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700923 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700924 }
925
926 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700927 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700928 }
929
930 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
931
Sunny Goyalc86df472016-02-25 09:19:38 -0800932 public void onScrollChanged(float progress) {
933 if (mWorkspace != null) {
934 mWorkspace.onOverlayScrollChanged(progress);
935 }
936 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700937 }
938
Sunny Goyalf9403d92017-10-18 10:55:56 -0700939 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -0700940 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700941 }
942
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800943 /**
944 * Restores the previous state, if it exists.
945 *
946 * @param savedState The previous state.
947 */
948 private void restoreState(Bundle savedState) {
949 if (savedState == null) {
950 return;
951 }
952
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700953 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -0700954 LauncherState[] stateValues = LauncherState.values();
955 LauncherState state = stateValues[stateOrdinal];
Sunny Goyalbc683e92017-12-06 10:25:07 -0800956 if (!state.disableRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700957 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800958 }
959
Sunny Goyal2100c782016-08-22 16:00:03 -0700960 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
961 if (requestArgs != null) {
962 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800963 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700964
965 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700966
967 SparseArray<Parcelable> widgetsState =
968 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
969 if (widgetsState != null) {
970 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
971 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800972 }
973
974 /**
975 * Finds all the views we need and configure them properly.
976 */
977 private void setupViews() {
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800978 mDragLayer = findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700979 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -0700980 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700981 mWorkspace.initParentViews(mDragLayer);
Sunny Goyal16764582017-11-06 16:00:34 -0800982 mOverviewPanel = findViewById(R.id.overview_panel);
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800983 mHotseat = findViewById(R.id.hotseat);
Craig Mautner360310b2012-10-26 15:13:08 -0700984
Sunny Goyal784f9c32016-03-23 16:56:54 -0700985 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
986 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
987 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800988
Winson Chung4c98d922011-05-31 16:50:48 -0700989 // Setup the drag layer
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700990 mDragLayer.setup(mDragController, mWorkspace);
Sunny Goyal5743f862019-03-28 15:35:32 -0700991 mCancelTouchController = UiFactory.enableLiveTouchControllerChanges(mDragLayer);
Winson Chung4c98d922011-05-31 16:50:48 -0700992
Hyunyoung Song645764e2016-06-06 14:19:02 -0700993 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -0700994 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
995 // default state, otherwise we will update to the wrong offsets in RTL
996 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700997 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700998 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700999
Tony Wickham34d2c912015-09-11 09:27:58 -07001000 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001001 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001002
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001003 // Setup Apps
1004 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001005
Govinda Wassermanc06e1512019-04-09 09:56:53 -04001006 // Setup Scrim
1007 mScrimView = findViewById(R.id.scrim_view);
1008
Winson Chung3d503fb2011-07-13 17:25:49 -07001009 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001010 mDragController.setMoveTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001011 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001012
Sunny Goyal7185dd62018-03-14 17:51:49 -07001013 mAllAppsController.setupViews(mAppsView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001014 }
1015
1016 /**
1017 * Creates a view representing a shortcut.
1018 *
1019 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001020 */
Sunny Goyal95899162019-03-27 16:03:06 -07001021 View createShortcut(WorkspaceItemInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001022 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001023 }
1024
1025 /**
1026 * Creates a view representing a shortcut inflated from the specified resource.
1027 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001028 * @param parent The group the shortcut belongs to.
1029 * @param info The data structure describing the shortcut.
1030 *
1031 * @return A View inflated from layoutResId.
1032 */
Sunny Goyal95899162019-03-27 16:03:06 -07001033 public View createShortcut(ViewGroup parent, WorkspaceItemInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -08001034 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
1035 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal95899162019-03-27 16:03:06 -07001036 favorite.applyFromWorkspaceItem(info);
Sunny Goyal8a2a63b2018-03-06 22:15:18 -08001037 favorite.setOnClickListener(ItemClickHandler.INSTANCE);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001038 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001039 return favorite;
1040 }
1041
1042 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -07001043 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001044 *
1045 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001046 */
Sunny Goyalefb7e842018-10-04 15:11:00 -07001047 private void completeAddShortcut(Intent data, int container, int screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001048 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001049 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
1050 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001051 return;
1052 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001053
Jon Mirandac476d6e2017-05-04 16:30:01 -07001054 int[] cellXY = mTmpAddItemCellCoordinates;
1055 CellLayout layout = getCellLayout(container, screenId);
1056
Sunny Goyal95899162019-03-27 16:03:06 -07001057 WorkspaceItemInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001058 if (Utilities.ATLEAST_OREO) {
Sunny Goyal95899162019-03-27 16:03:06 -07001059 info = LauncherAppsCompatVO.createWorkspaceItemFromPinItemRequest(
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001060 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001061 }
1062
1063 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001064 // Legacy shortcuts are only supported for primary profile.
1065 info = Process.myUserHandle().equals(args.user)
1066 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001067
Sunny Goyalb57645f2017-03-20 13:57:28 -07001068 if (info == null) {
1069 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1070 return;
1071 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001072 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001073 // The app is trying to add a shortcut without sufficient permissions
1074 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1075 return;
1076 }
1077 }
1078
Jon Mirandac476d6e2017-05-04 16:30:01 -07001079 if (container < 0) {
1080 // Adding a shortcut to the Workspace.
1081 final View view = createShortcut(info);
1082 boolean foundCellSpan = false;
1083 // First we check if we already know the exact location where we want to add this item.
1084 if (cellX >= 0 && cellY >= 0) {
1085 cellXY[0] = cellX;
1086 cellXY[1] = cellY;
1087 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001088
Jon Mirandac476d6e2017-05-04 16:30:01 -07001089 // If appropriate, either create a folder or add to an existing folder
1090 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001091 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001092 return;
1093 }
1094 DragObject dragObject = new DragObject();
1095 dragObject.dragInfo = info;
1096 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1097 true)) {
1098 return;
1099 }
1100 } else {
1101 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1102 }
1103
1104 if (!foundCellSpan) {
1105 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001106 return;
1107 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001108
1109 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1110 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001111 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001112 // Adding a shortcut to a Folder.
Sunny Goyale29897f52017-07-20 10:09:42 -07001113 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001114 if (folderIcon != null) {
1115 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1116 folderInfo.add(info, args.rank, false);
1117 } else {
Sunny Goyale29897f52017-07-20 10:09:42 -07001118 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001119 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001120 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001121 }
1122
Sunny Goyalefb7e842018-10-04 15:11:00 -07001123 public FolderIcon findFolderIcon(final int folderIconId) {
Sunny Goyal83fd25e2018-07-09 16:47:01 -07001124 return (FolderIcon) mWorkspace.getHomescreenIconByItemId(folderIconId);
Sunny Goyale29897f52017-07-20 10:09:42 -07001125 }
1126
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001127 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001128 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001129 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001130 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001131 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001132 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001133 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1134
Adam Cohened66b2b2012-01-23 17:28:51 -08001135 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001136 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001137 }
Romain Guycbb89e42009-06-08 15:52:54 -07001138
Adam Cohen59400422014-03-05 18:07:04 -08001139 LauncherAppWidgetInfo launcherInfo;
1140 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001141 launcherInfo.spanX = itemInfo.spanX;
1142 launcherInfo.spanY = itemInfo.spanY;
1143 launcherInfo.minSpanX = itemInfo.minSpanX;
1144 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001145 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001146
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001147 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001148 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001149
Sunny Goyal2100c782016-08-22 16:00:03 -07001150 if (hostView == null) {
1151 // Perform actual inflation because we're live
1152 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001153 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001154 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301155 prepareAppWidget(hostView, launcherInfo);
1156 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001157 }
Romain Guycbb89e42009-06-08 15:52:54 -07001158
Sunny Goyald5462aa2016-11-22 16:52:39 +05301159 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001160 hostView.setTag(item);
1161 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001162 hostView.setFocusable(true);
1163 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001164 }
1165
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001166 private final BroadcastReceiver mScreenOffReceiver = new BroadcastReceiver() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001167 @Override
1168 public void onReceive(Context context, Intent intent) {
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001169 // Reset AllApps to its initial state only if we are not in the middle of
1170 // processing a multi-step drop
1171 if (mPendingRequestArgs == null) {
1172 mStateManager.goToState(NORMAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001173 }
1174 }
1175 };
1176
Sunny Goyalaae6fbb2019-01-31 16:05:58 -08001177 public void updateNotificationDots(Predicate<PackageUserKey> updatedDots) {
Tony Wickhamf34bee82018-12-03 18:11:39 -08001178 mWorkspace.updateNotificationDots(updatedDots);
1179 mAppsView.getAppsStore().updateNotificationDots(updatedDots);
Tony Wickham010d2552017-01-20 08:15:28 -08001180 }
1181
Adam Cohended9f8d2010-11-03 13:25:16 -07001182 @Override
1183 public void onAttachedToWindow() {
1184 super.onAttachedToWindow();
1185
Sunny Goyalc86df472016-02-25 09:19:38 -08001186 if (mLauncherCallbacks != null) {
1187 mLauncherCallbacks.onAttachedToWindow();
1188 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001189 }
1190
1191 @Override
1192 public void onDetachedFromWindow() {
1193 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001194
1195 if (mLauncherCallbacks != null) {
1196 mLauncherCallbacks.onDetachedFromWindow();
1197 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001198 }
1199
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001200 public AllAppsTransitionController getAllAppsController() {
1201 return mAllAppsController;
1202 }
1203
Sunny Goyal9d69c8d2018-03-19 13:41:31 -07001204 @Override
Winson Chung1a341002018-01-17 10:00:23 -08001205 public LauncherRootView getRootView() {
1206 return (LauncherRootView) mLauncherView;
1207 }
1208
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001209 @Override
Winson Chunga6945242014-01-08 14:04:34 -08001210 public DragLayer getDragLayer() {
1211 return mDragLayer;
1212 }
1213
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001214 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001215 return mAppsView;
1216 }
1217
Winson Chunga6945242014-01-08 14:04:34 -08001218 public Workspace getWorkspace() {
1219 return mWorkspace;
1220 }
1221
1222 public Hotseat getHotseat() {
1223 return mHotseat;
1224 }
1225
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001226 public <T extends View> T getOverviewPanel() {
Winson Chung8ae41982017-11-10 11:42:13 -08001227 return (T) mOverviewPanel;
Winson Chunga6945242014-01-08 14:04:34 -08001228 }
1229
Sunny Goyal47328fd2016-05-25 18:56:41 -07001230 public DropTargetBar getDropTargetBar() {
1231 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001232 }
1233
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001234 public LauncherAppWidgetHost getAppWidgetHost() {
1235 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001236 }
Romain Guycbb89e42009-06-08 15:52:54 -07001237
Winson Chunga9abd0e2010-10-27 17:18:37 -07001238 public LauncherModel getModel() {
1239 return mModel;
1240 }
1241
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001242 public ModelWriter getModelWriter() {
1243 return mModelWriter;
1244 }
1245
Adam Cohen79d90c52016-04-22 13:29:20 -07001246 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001247 return mSharedPrefs;
1248 }
1249
Sunny Goyalf8d56fc2018-01-31 15:18:11 -08001250 public int getOrientation() { return mOldConfig.orientation; }
Jon Miranda6bed3502017-09-19 14:43:17 -07001251
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001252 @Override
1253 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001254 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001255 super.onNewIntent(intent);
1256
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001257 boolean alreadyOnHome = hasWindowFocus() && ((intent.getFlags() &
Winson15f8b172015-08-19 11:02:39 -07001258 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1259 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001260
1261 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001262 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001263 && AbstractFloatingView.getTopOpenView(this) == null;
Winson15f8b172015-08-19 11:02:39 -07001264 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001265 boolean internalStateHandled = InternalStateHandler
Sunny Goyal8b264562018-02-03 23:00:13 -08001266 .handleNewIntent(this, intent, isStarted());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001267
Winson15f8b172015-08-19 11:02:39 -07001268 if (isActionMain) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001269 if (!internalStateHandled) {
1270 // Note: There should be at most one log per method call. This is enforced
1271 // implicitly by using if-else statements.
1272 UserEventDispatcher ued = getUserEventDispatcher();
1273 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
1274 if (topOpenView != null) {
1275 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
1276 } else if (alreadyOnHome) {
1277 Target target = newContainerTarget(mStateManager.getState().containerType);
1278 target.pageIndex = mWorkspace.getCurrentPage();
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001279 ued.logActionCommand(Action.Command.HOME_INTENT, target,
1280 newContainerTarget(ContainerType.WORKSPACE));
Sunny Goyald3864fa2017-11-14 15:06:36 -08001281 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001282
Sunny Goyald3864fa2017-11-14 15:06:36 -08001283 // In all these cases, only animate if we're already on home
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001284 AbstractFloatingView.closeAllOpenViews(this, isStarted());
Jon Mirandafeba90f2016-10-06 10:53:29 -07001285
Sunny Goyala2467272018-03-16 14:53:05 -07001286 if (!isInState(NORMAL)) {
1287 // Only change state, if not already the same. This prevents cancelling any
1288 // animations running as part of resume
1289 mStateManager.goToState(NORMAL);
1290 }
Sunny Goyald3864fa2017-11-14 15:06:36 -08001291
1292 // Reset the apps view
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001293 if (!alreadyOnHome) {
Sunny Goyal7185dd62018-03-14 17:51:49 -07001294 mAppsView.reset(isStarted() /* animate */);
Sunny Goyald3864fa2017-11-14 15:06:36 -08001295 }
1296
Sunny Goyalaad33592018-08-07 17:20:35 -07001297 if (shouldMoveToDefaultScreen && !mWorkspace.isHandlingTouch()) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001298 mWorkspace.post(mWorkspace::moveToDefaultScreen);
1299 }
1300 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001301
1302 final View v = getWindow().peekDecorView();
1303 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001304 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001305 }
1306
Adam Cohen9211d422014-10-07 18:14:53 -07001307 if (mLauncherCallbacks != null) {
Sunny Goyal85462132018-04-24 11:39:37 -07001308 mLauncherCallbacks.onHomeIntent(internalStateHandled);
Adam Cohen9211d422014-10-07 18:14:53 -07001309 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001310 }
Winson15f8b172015-08-19 11:02:39 -07001311
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001312 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001313 }
1314
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001315 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001316 public void onRestoreInstanceState(Bundle state) {
1317 super.onRestoreInstanceState(state);
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001318 mWorkspace.restoreInstanceStateForChild(mSynchronouslyBoundPage);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001319 }
1320
1321 @Override
1322 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001323 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001324 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001325
Adam Cohen21cd0022013-10-09 18:57:02 -07001326 }
Sunny Goyalea609262017-10-25 15:47:38 -07001327 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001328
1329
1330 AbstractFloatingView widgets = AbstractFloatingView
1331 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1332 if (widgets != null) {
1333 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1334 widgets.saveHierarchyState(widgetsState);
1335 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1336 } else {
1337 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1338 }
1339
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001340 // We close any open folders and shortcut containers since they will not be re-opened,
1341 // and we need to make sure this state is reflected.
1342 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001343
Sunny Goyal2100c782016-08-22 16:00:03 -07001344 if (mPendingRequestArgs != null) {
1345 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1346 }
1347 if (mPendingActivityResult != null) {
1348 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001349 }
1350
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001351 super.onSaveInstanceState(outState);
1352
Adam Cohen9211d422014-10-07 18:14:53 -07001353 if (mLauncherCallbacks != null) {
1354 mLauncherCallbacks.onSaveInstanceState(outState);
1355 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001356 }
1357
1358 @Override
1359 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001360 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001361
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001362 unregisterReceiver(mScreenOffReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001363 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001364
Sunny Goyal5743f862019-03-28 15:35:32 -07001365 if (mCancelTouchController != null) {
1366 mCancelTouchController.run();
1367 mCancelTouchController = null;
1368 }
Tony Wickhama0068302018-04-11 16:16:11 -07001369
Winson Chungcd2b0142011-06-08 16:02:26 -07001370 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001371 // It's possible to receive onDestroy after a new Launcher activity has
1372 // been created. In this case, don't interfere with the new Launcher.
1373 if (mModel.isCurrentCallbacks(this)) {
1374 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001375 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001376 }
Sunny Goyal623eddd2018-03-02 12:24:41 -08001377 mRotationHelper.destroy();
Sunny Goyal745bad92016-05-02 10:54:12 -07001378
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001379 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001380 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001381 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001382 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001383 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001384
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001385 TextKeyListener.getInstance().release();
Tony2917a8b2017-07-31 23:29:42 -07001386 clearPendingBinds();
Hyunyoung Song31971a32019-01-27 12:31:12 -08001387 LauncherAppState.getIDP(this).removeOnChangeListener(this);
Adam Cohen9211d422014-10-07 18:14:53 -07001388 if (mLauncherCallbacks != null) {
1389 mLauncherCallbacks.onDestroy();
1390 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001391 }
1392
Sunny Goyalae502842016-06-17 08:43:56 -07001393 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1394 return mAccessibilityDelegate;
1395 }
1396
Adam Cohena9cf38f2011-05-02 15:36:58 -07001397 public DragController getDragController() {
1398 return mDragController;
1399 }
1400
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001401 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001402 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001403 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001404 }
1405
1406 @Override
1407 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1408 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001409 try {
1410 super.startIntentSenderForResult(intent, requestCode,
1411 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1412 } catch (IntentSender.SendIntentException e) {
1413 throw new ActivityNotFoundException();
1414 }
1415 }
1416
Winson Chung70d72102011-08-12 11:24:35 -07001417 /**
1418 * Indicates that we want global search for this activity by setting the globalSearch
1419 * argument for {@link #startSearch} to true.
1420 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001421 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001422 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001423 Bundle appSearchData, boolean globalSearch) {
1424 if (appSearchData == null) {
1425 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001426 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001427 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001428
Sunny Goyal6f28e712016-09-13 14:19:29 -07001429 if (mLauncherCallbacks == null ||
1430 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1431 // Starting search from the callbacks failed. Start the default global search.
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001432 super.startSearch(initialQuery, selectInitialQuery, appSearchData, true);
Ian Parkinson047036e2014-09-01 15:40:53 +01001433 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001434
1435 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001436 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001437 }
1438
Joe Onorato9c1289c2009-08-17 11:03:03 -04001439 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001440 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001441 }
1442
Adam Cohen517a7f52014-03-01 12:12:59 -08001443 public boolean isWorkspaceLoading() {
1444 return mWorkspaceLoading;
1445 }
1446
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001447 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001448 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001449 }
1450
Sunny Goyal04a324a2017-01-20 21:08:59 -08001451 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001452 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001453 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001454
Sunny Goyal2100c782016-08-22 16:00:03 -07001455 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001456 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001457 if (LOGD) {
1458 Log.d(TAG, "Adding widget from drop");
1459 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001460 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001461 }
1462
Sunny Goyal2100c782016-08-22 16:00:03 -07001463 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001464 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1465 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1466 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001467
Adam Cohenad4e15c2013-10-17 16:21:35 -07001468 Runnable onComplete = new Runnable() {
1469 @Override
1470 public void run() {
1471 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001472 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001473 }
1474 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001475 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001476 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001477 }
1478 }
Romain Guycbb89e42009-06-08 15:52:54 -07001479
Sunny Goyalefb7e842018-10-04 15:11:00 -07001480 public void addPendingItem(PendingAddItemInfo info, int container, int screenId,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001481 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001482 info.container = container;
1483 info.screenId = screenId;
1484 if (cell != null) {
1485 info.cellX = cell[0];
1486 info.cellY = cell[1];
1487 }
1488 info.spanX = spanX;
1489 info.spanY = spanY;
1490
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001491 switch (info.itemType) {
1492 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1493 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001494 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001495 break;
1496 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001497 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001498 break;
1499 default:
1500 throw new IllegalStateException("Unknown item type: " + info.itemType);
1501 }
1502 }
1503
Adam Cohenfbba09b2011-07-18 21:43:05 -07001504 /**
1505 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001506 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001507 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001508 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1509 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001510 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1511 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1512 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001513 }
1514
Adam Cohenfbba09b2011-07-18 21:43:05 -07001515 /**
1516 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001517 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001518 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001519 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001520 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001521 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001522 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001523 // In the case where we've prebound the widget, we remove it from the DragLayer
1524 if (LOGD) {
1525 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1526 }
1527 getDragLayer().removeView(hostView);
1528
Adam Cohened66b2b2012-01-23 17:28:51 -08001529 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001530 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001531
1532 // Clear the boundWidget so that it doesn't get destroyed.
1533 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001534 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001535 // In this case, we either need to start an activity to get permission to bind
1536 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001537 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1538 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1539 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1540 this, info.componentName);
1541 } else {
1542 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1543 }
Adam Cohendd70d662012-10-04 16:53:44 -07001544 Bundle options = info.bindOptions;
1545
Sunny Goyalffe83f12014-08-14 17:39:34 -07001546 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1547 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001548 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001549 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001550 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001551 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001552 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001553 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001554 }
1555
Sunny Goyalefb7e842018-10-04 15:11:00 -07001556 FolderIcon addFolder(CellLayout layout, int container, final int screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001557 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001558 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001559 folderInfo.title = getText(R.string.folder_name);
1560
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001561 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001562 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001563
1564 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001565 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301566 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001567 // Force measure the new folder icon
1568 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1569 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001570 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001571 }
Romain Guycbb89e42009-06-08 15:52:54 -07001572
Winsonc0b52fe2015-09-09 16:38:15 -07001573 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001574 * Unbinds the view for the specified item, and removes the item and all its children.
1575 *
1576 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001577 * @param itemInfo the {@link ItemInfo} for this view.
1578 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001579 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001580 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Sunny Goyal95899162019-03-27 16:03:06 -07001581 if (itemInfo instanceof WorkspaceItemInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001582 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001583 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1584 if (folderIcon instanceof FolderIcon) {
Sunny Goyal95899162019-03-27 16:03:06 -07001585 ((FolderInfo) folderIcon.getTag()).remove((WorkspaceItemInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001586 } else {
1587 mWorkspace.removeWorkspaceItem(v);
1588 }
Winsonc0b52fe2015-09-09 16:38:15 -07001589 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001590 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001591 }
1592 } else if (itemInfo instanceof FolderInfo) {
1593 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001594 if (v instanceof FolderIcon) {
1595 ((FolderIcon) v).removeListeners();
1596 }
Winsonc0b52fe2015-09-09 16:38:15 -07001597 mWorkspace.removeWorkspaceItem(v);
1598 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001599 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001600 }
1601 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1602 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001603 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001604 if (deleteFromDb) {
Tony Wickham6a71a5b2018-08-21 11:40:23 -07001605 getModelWriter().deleteWidgetInfo(widgetInfo, getAppWidgetHost());
Winsonc0b52fe2015-09-09 16:38:15 -07001606 }
1607 } else {
1608 return false;
1609 }
1610 return true;
1611 }
1612
Tony Wickham6a71a5b2018-08-21 11:40:23 -07001613
Winsonc0b52fe2015-09-09 16:38:15 -07001614
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001615 @Override
1616 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001617 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001618 }
1619
Joe Onorato88ec0992009-11-19 13:16:06 -08001620 @Override
1621 public void onBackPressed() {
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08001622 if (finishAutoCancelActionMode()) {
1623 return;
1624 }
Adam Cohen9211d422014-10-07 18:14:53 -07001625 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1626 return;
1627 }
1628
Sunny Goyal45478022015-06-08 16:52:41 -07001629 if (mDragController.isDragging()) {
1630 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001631 return;
1632 }
1633
Jon Mirandafeba90f2016-10-06 10:53:29 -07001634 // Note: There should be at most one log per method call. This is enforced implicitly
1635 // by using if-else statements.
1636 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001637 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
Tony Wickham52c1b662018-05-21 13:13:58 -07001638 if (topView != null && topView.onBackPressed()) {
1639 // Handled by the floating view.
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001640 } else {
Sunny Goyale39690b2018-08-02 13:35:07 -07001641 mStateManager.getState().onBackPressed(this);
Michael Jurkaaf442092010-06-10 17:01:57 -07001642 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001643 }
1644
Sunny Goyalfe770c92016-09-27 14:38:58 -07001645 @TargetApi(Build.VERSION_CODES.M)
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001646 @Override
Jon Miranda73c27ec2018-05-16 18:06:23 -07001647 public ActivityOptions getActivityLaunchOptions(View v) {
1648 return mAppTransitionManager.getActivityLaunchOptions(this, v);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001649 }
1650
Tony Wickham3a6746a2018-03-29 09:39:56 -07001651 public LauncherAppTransitionManager getAppTransitionManager() {
1652 return mAppTransitionManager;
1653 }
1654
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001655 @TargetApi(Build.VERSION_CODES.M)
1656 @Override
1657 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
1658 // Due to legacy reasons, direct call shortcuts require Launchers to have the
1659 // corresponding permission. Show the appropriate permission prompt if that
1660 // is the case.
1661 if (intent.getComponent() == null
1662 && Intent.ACTION_CALL.equals(intent.getAction())
1663 && checkSelfPermission(android.Manifest.permission.CALL_PHONE) !=
1664 PackageManager.PERMISSION_GRANTED) {
1665
1666 setWaitingForResult(PendingRequestArgs
1667 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
1668 requestPermissions(new String[]{android.Manifest.permission.CALL_PHONE},
1669 REQUEST_PERMISSION_CALL_PHONE);
1670 return true;
1671 } else {
1672 return false;
1673 }
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001674 }
1675
Hyunyoung Song46d07f72018-05-22 15:41:25 -07001676 @Override
Hyunyoung Songfc007472018-10-25 14:09:50 -07001677 public int getCurrentState() {
1678 if(mStateManager.getState() == LauncherState.ALL_APPS) {
1679 return StatsLogUtils.LAUNCHER_STATE_ALLAPPS;
Govinda Wassermanc06e1512019-04-09 09:56:53 -04001680 } else if (mStateManager.getState() == OVERVIEW) {
Hyunyoung Songfc007472018-10-25 14:09:50 -07001681 return StatsLogUtils.LAUNCHER_STATE_OVERVIEW;
1682 }
Hyunyoung Song0ae38882018-11-05 14:45:19 -08001683 return StatsLogUtils.LAUNCHER_STATE_HOME;
Hyunyoung Songfc007472018-10-25 14:09:50 -07001684 }
1685
1686 @Override
Hyunyoung Song46d07f72018-05-22 15:41:25 -07001687 public void modifyUserEvent(LauncherLogProto.LauncherEvent event) {
1688 if (event.srcTarget != null && event.srcTarget.length > 0 &&
1689 event.srcTarget[1].containerType == ContainerType.PREDICTION) {
1690 Target[] targets = new Target[3];
1691 targets[0] = event.srcTarget[0];
1692 targets[1] = event.srcTarget[1];
1693 targets[2] = newTarget(Target.Type.CONTAINER);
1694 event.srcTarget = targets;
1695 LauncherState state = mStateManager.getState();
1696 if (state == LauncherState.ALL_APPS) {
1697 event.srcTarget[2].containerType = ContainerType.ALLAPPS;
Govinda Wassermanc06e1512019-04-09 09:56:53 -04001698 } else if (state == OVERVIEW) {
Hyunyoung Song46d07f72018-05-22 15:41:25 -07001699 event.srcTarget[2].containerType = ContainerType.TASKSWITCHER;
1700 }
1701 }
1702 }
1703
Sunny Goyal369212a2019-03-26 15:03:57 -07001704 public boolean startActivitySafely(View v, Intent intent, ItemInfo item,
1705 @Nullable String sourceContainer) {
1706 boolean success = super.startActivitySafely(v, intent, item, sourceContainer);
Hyunyoung Song1241e612018-05-15 21:46:51 -07001707 if (success && v instanceof BubbleTextView) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001708 // This is set to the view that launched the activity that navigated the user away
1709 // from launcher. Since there is no callback for when the activity has finished
1710 // launching, enable the press state and keep this reference to reset the press
1711 // state when we return to launcher.
1712 BubbleTextView btv = (BubbleTextView) v;
1713 btv.setStayPressed(true);
1714 setOnResumeCallback(btv);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001715 }
Hyunyoung Song1241e612018-05-15 21:46:51 -07001716 return success;
Michael Jurka86a720e2012-05-09 11:23:23 -07001717 }
1718
Winson Chung3d503fb2011-07-13 17:25:49 -07001719 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001720 // TODO: Remove this method
Sunny Goyal876e4622018-11-02 13:50:40 -07001721 return mHotseat != null && (layout == mHotseat);
Winson Chung3d503fb2011-07-13 17:25:49 -07001722 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001723
Winson Chung3d503fb2011-07-13 17:25:49 -07001724 /**
1725 * Returns the CellLayout of the specified container at the specified screen.
1726 */
Sunny Goyalefb7e842018-10-04 15:11:00 -07001727 public CellLayout getCellLayout(int container, int screenId) {
Sunny Goyal876e4622018-11-02 13:50:40 -07001728 return (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1729 ? mHotseat : mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08001730 }
1731
Michael Jurkae326f182011-11-21 14:05:46 -08001732 @Override
1733 public void onTrimMemory(int level) {
1734 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07001735 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
1736 // The widget preview db can result in holding onto over
1737 // 3MB of memory for caching which isn't necessary.
1738 SQLiteDatabase.releaseMemory();
1739
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08001740 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07001741 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08001742 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01001743 if (mLauncherCallbacks != null) {
1744 mLauncherCallbacks.onTrimMemory(level);
1745 }
Winson Chung62a70be2018-02-23 15:10:05 -08001746 UiFactory.onTrimMemory(this, level);
Michael Jurkae326f182011-11-21 14:05:46 -08001747 }
1748
Michael Jurkad7c28052012-04-27 15:43:36 -07001749 @Override
1750 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07001751 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07001752 final List<CharSequence> text = event.getText();
1753 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07001754 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001755 // TODO: When can workspace be null?
1756 text.add(mWorkspace == null
1757 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07001758 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07001759 return result;
1760 }
1761
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001762 public void setOnResumeCallback(OnResumeCallback callback) {
1763 if (mOnResumeCallback != null) {
1764 mOnResumeCallback.onLauncherResume();
1765 }
1766 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07001767 }
1768
Michael Jurka7607c2f2013-04-03 14:33:19 -07001769 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001770 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001771 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07001772 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001773 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07001774 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001775 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07001776 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07001777 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07001778 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001779 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001780
Joe Onorato9c1289c2009-08-17 11:03:03 -04001781 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07001782 * Clear any pending bind callbacks. This is called when is loader is planning to
1783 * perform a full rebind from scratch.
1784 */
1785 @Override
1786 public void clearPendingBinds() {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001787 if (mPendingExecutor != null) {
1788 mPendingExecutor.markCompleted();
1789 mPendingExecutor = null;
1790 }
1791 }
1792
1793 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001794 * Refreshes the shortcuts shown on the workspace.
1795 *
1796 * Implementation of the method from LauncherModel.Callbacks.
1797 */
1798 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001799 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001800 // Floating panels (except the full widget sheet) are associated with individual icons. If
1801 // we are starting a fresh bind, close all such panels as all the icons are about
1802 // to go away.
1803 AbstractFloatingView.closeOpenViews(this, true,
Sunny Goyal7ede6112017-12-05 15:11:21 -08001804 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_REBIND_SAFE);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08001805
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001806 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08001807
Winson Chungd64d1762013-08-20 14:37:16 -07001808 // Clear the workspace because it's going to be rebound
Sunny Goyal2db53422019-03-01 16:06:12 -08001809 mDragController.cancelDrag();
1810
Adam Cohendf035382011-04-11 17:22:04 -07001811 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07001812 mWorkspace.removeAllWorkspaceScreens();
Sunny Goyalc11fac32018-02-27 19:20:15 -08001813 mAppWidgetHost.clearViews();
Winson Chungd64d1762013-08-20 14:37:16 -07001814
Winson Chung3d503fb2011-07-13 17:25:49 -07001815 if (mHotseat != null) {
Sunny Goyal11e96492018-03-23 17:06:00 -07001816 mHotseat.resetLayout(mDeviceProfile.isVerticalBarLayout());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001817 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001818 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001819 }
1820
Adam Cohendcd297f2013-06-18 13:13:40 -07001821 @Override
Sunny Goyalefb7e842018-10-04 15:11:00 -07001822 public void bindScreens(IntArray orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001823 // Make sure the first screen is always at the start.
Jon Miranda7143ba62019-03-15 09:00:05 -07001824 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001825 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalefb7e842018-10-04 15:11:00 -07001826 orderedScreenIds.removeValue(Workspace.FIRST_SCREEN_ID);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001827 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Jon Miranda7143ba62019-03-15 09:00:05 -07001828 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001829 // If there are no screens, we need to have an empty screen
1830 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07001831 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001832 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07001833
Winsonc7d2e832016-07-28 12:24:55 -07001834 // After we have added all the screens, if the wallpaper was locked to the default state,
1835 // then notify to indicate that it can be released and a proper wallpaper offset can be
1836 // computed before the next layout
1837 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07001838 }
1839
Sunny Goyalefb7e842018-10-04 15:11:00 -07001840 private void bindAddScreens(IntArray orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001841 int count = orderedScreenIds.size();
1842 for (int i = 0; i < count; i++) {
Sunny Goyalefb7e842018-10-04 15:11:00 -07001843 int screenId = orderedScreenIds.get(i);
Jon Miranda7143ba62019-03-15 09:00:05 -07001844 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001845 // No need to bind the first screen, as its always bound.
1846 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
1847 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001848 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001849 }
1850
Sunny Goyalb23980c2017-08-17 07:45:25 -07001851 @Override
Tony Wickham6a71a5b2018-08-21 11:40:23 -07001852 public void preAddApps() {
1853 // If there's an undo snackbar, force it to complete to ensure empty screens are removed
1854 // before trying to add new items.
1855 mModelWriter.commitDelete();
1856 AbstractFloatingView snackbar = AbstractFloatingView.getOpenView(this, TYPE_SNACKBAR);
1857 if (snackbar != null) {
1858 snackbar.post(() -> snackbar.close(true));
1859 }
1860 }
1861
1862 @Override
Sunny Goyalefb7e842018-10-04 15:11:00 -07001863 public void bindAppsAdded(IntArray newScreens, ArrayList<ItemInfo> addNotAnimated,
Sunny Goyal29947f02017-12-18 13:49:44 -08001864 ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07001865 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08001866 if (newScreens != null) {
1867 bindAddScreens(newScreens);
1868 }
Winson Chungd64d1762013-08-20 14:37:16 -07001869
1870 // We add the items without animation on non-visible pages, and with
1871 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08001872 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001873 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001874 }
Adam Cohen76a47a12014-02-05 11:47:43 -08001875 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001876 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07001877 }
Winson Chungc58497e2013-09-03 17:48:37 -07001878
Winson Chung87412982013-10-03 18:34:14 -07001879 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07001880 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001881 }
1882
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001883 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001884 * Bind the items start-end from the list.
1885 *
1886 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001887 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07001888 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07001889 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001890 // Get the list of added items and intersect them with the set of items here
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001891 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07001892 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001893 Workspace workspace = mWorkspace;
Sunny Goyalefb7e842018-10-04 15:11:00 -07001894 int newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07001895 int end = items.size();
1896 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001897 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07001898
1899 // Short circuit if we are loading dock items for a configuration which has no dock
1900 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
1901 mHotseat == null) {
1902 continue;
1903 }
1904
Sunny Goyal639e9062015-08-19 19:17:06 -07001905 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001906 switch (item.itemType) {
1907 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1908 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08001909 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Sunny Goyal95899162019-03-27 16:03:06 -07001910 WorkspaceItemInfo info = (WorkspaceItemInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07001911 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001912 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001913 }
1914 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07001915 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001916 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001917 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001918 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001919 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07001920 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1921 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07001922 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07001923 if (view == null) {
1924 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001925 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08001926 break;
1927 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001928 default:
1929 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001930 }
Sunny Goyal639e9062015-08-19 19:17:06 -07001931
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001932 /*
1933 * Remove colliding items.
1934 */
1935 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1936 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
1937 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
1938 View v = cl.getChildAt(item.cellX, item.cellY);
1939 Object tag = v.getTag();
1940 String desc = "Collision while binding workspace item: " + item
1941 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08001942 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001943 throw (new RuntimeException(desc));
1944 } else {
1945 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001946 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001947 continue;
1948 }
1949 }
1950 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05301951 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07001952 if (animateIcons) {
1953 // Animate all the applications up now
1954 view.setAlpha(0f);
1955 view.setScaleX(0f);
1956 view.setScaleY(0f);
1957 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08001958 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07001959 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001960 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07001961
Sunny Goyalf0b6db72018-08-13 16:10:14 -07001962 // Animate to the correct page
1963 if (animateIcons && newItemsScreenId > -1) {
1964 AnimatorSet anim = new AnimatorSet();
1965 anim.playTogether(bounceAnims);
Jon Miranda64eb8ea2018-03-29 11:43:58 -07001966
Sunny Goyalefb7e842018-10-04 15:11:00 -07001967 int currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyalf0b6db72018-08-13 16:10:14 -07001968 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
1969 final Runnable startBounceAnimRunnable = anim::start;
1970
1971 if (newItemsScreenId != currentScreenId) {
1972 // We post the animation slightly delayed to prevent slowdowns
1973 // when we are loading right after we return to launcher.
1974 mWorkspace.postDelayed(new Runnable() {
1975 public void run() {
1976 if (mWorkspace != null) {
1977 AbstractFloatingView.closeAllOpenViews(Launcher.this, false);
1978
1979 mWorkspace.snapToPage(newScreenIndex);
1980 mWorkspace.postDelayed(startBounceAnimRunnable,
1981 NEW_APPS_ANIMATION_DELAY);
Winson Chung94d67682013-09-25 16:29:40 -07001982 }
Sunny Goyalf0b6db72018-08-13 16:10:14 -07001983 }
1984 }, NEW_APPS_PAGE_MOVE_DELAY);
1985 } else {
1986 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07001987 }
Winson Chung64359a52013-07-08 17:17:08 -07001988 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001989 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001990 }
1991
Joe Onorato9c1289c2009-08-17 11:03:03 -04001992 /**
1993 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001994 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07001995 public void bindAppWidget(LauncherAppWidgetInfo item) {
1996 View view = inflateAppWidget(item);
1997 if (view != null) {
1998 mWorkspace.addInScreen(view, item);
1999 mWorkspace.requestLayout();
2000 }
2001 }
2002
2003 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002004 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302005 PendingAppWidgetHostView view =
2006 new PendingAppWidgetHostView(this, item, mIconCache, true);
2007 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002008 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002009 }
2010
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002011 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002012
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002013 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08002014
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002015 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2016 // If the provider is not ready, bind as a pending widget.
2017 appWidgetInfo = null;
2018 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2019 // The widget id is not valid. Try to find the widget based on the provider info.
2020 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
2021 } else {
2022 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
2023 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002024
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002025 // If the provider is ready, but the width is not yet restored, try to restore it.
2026 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
2027 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07002028 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002029 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
2030 + " belongs to component " + item.providerName
2031 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002032 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002033 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002034 }
Sunny Goyalff572272014-07-23 13:58:07 -07002035
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002036 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002037 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07002038 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2039 // Id has not been allocated yet. Allocate a new id.
2040 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
2041 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07002042
Sunny Goyald478c832016-04-01 12:04:16 -07002043 // Also try to bind the widget. If the bind fails, the user will be shown
2044 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08002045 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07002046 pendingInfo.spanX = item.spanX;
2047 pendingInfo.spanY = item.spanY;
2048 pendingInfo.minSpanX = item.minSpanX;
2049 pendingInfo.minSpanY = item.minSpanY;
2050 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07002051
2052 boolean isDirectConfig =
2053 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2054 if (isDirectConfig && item.bindOptions != null) {
2055 Bundle newOptions = item.bindOptions.getExtras();
2056 if (options != null) {
2057 newOptions.putAll(options);
2058 }
2059 options = newOptions;
2060 }
Sunny Goyald478c832016-04-01 12:04:16 -07002061 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2062 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002063
Sunny Goyal86df1382016-08-10 15:03:22 -07002064 // We tried to bind once. If we were not able to bind, we would need to
2065 // go through the permission dialog, which means we cannot skip the config
2066 // activity.
2067 item.bindOptions = null;
2068 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2069
Sunny Goyald478c832016-04-01 12:04:16 -07002070 // Bind succeeded
2071 if (success) {
2072 // If the widget has a configure activity, it is still needs to set it up,
2073 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002074 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002075 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2076 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002077 }
Sunny Goyald478c832016-04-01 12:04:16 -07002078
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002079 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002080 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002081 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002082 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002083 // The widget was marked as UI not ready, but there is no configure activity to
2084 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002085 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002086 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002087 }
Sunny Goyalff572272014-07-23 13:58:07 -07002088 }
2089
Sunny Goyald5462aa2016-11-22 16:52:39 +05302090 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002091 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002092 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002093 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002094 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Tony Wickham6a71a5b2018-08-21 11:40:23 -07002095 getModelWriter().deleteWidgetInfo(item, getAppWidgetHost());
Sunny Goyalb23980c2017-08-17 07:45:25 -07002096 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002097 }
2098
Sunny Goyal233ee962015-08-03 13:05:01 -07002099 item.minSpanX = appWidgetInfo.minSpanX;
2100 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302101 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002102 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302103 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002104 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302105 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002106
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002107 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002108 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002109 }
2110
Sunny Goyalff572272014-07-23 13:58:07 -07002111 /**
2112 * Restores a pending widget.
2113 *
2114 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002115 */
Sunny Goyald478c832016-04-01 12:04:16 -07002116 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002117 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2118 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2119 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002120 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002121 }
2122
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002123 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002124 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002125 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2126 info.pendingItemInfo = null;
2127 }
Sunny Goyalff572272014-07-23 13:58:07 -07002128
Sunny Goyalba47faa2017-10-04 16:50:57 -07002129 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002130 view.reInflate();
Sunny Goyalba47faa2017-10-04 16:50:57 -07002131 }
2132
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002133 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002134 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002135 }
2136
Adam Cohen1462de32012-07-24 22:34:36 -07002137 public void onPageBoundSynchronously(int page) {
Sunny Goyalf4aceab2017-12-06 16:16:31 -08002138 mSynchronouslyBoundPage = page;
Adam Cohen1462de32012-07-24 22:34:36 -07002139 }
2140
Sunny Goyal527c7d32015-08-28 15:19:36 -07002141 @Override
2142 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2143 if (mPendingExecutor != null) {
2144 mPendingExecutor.markCompleted();
2145 }
2146 mPendingExecutor = executor;
Sunny Goyal60180b02018-02-07 12:56:30 -08002147 if (!isInState(ALL_APPS)) {
2148 mAppsView.getAppsStore().setDeferUpdates(true);
2149 mPendingExecutor.execute(() -> mAppsView.getAppsStore().setDeferUpdates(false));
2150 }
2151
Sunny Goyal527c7d32015-08-28 15:19:36 -07002152 executor.attachTo(this);
2153 }
2154
2155 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2156 if (mPendingExecutor == executor) {
2157 mPendingExecutor = null;
2158 }
2159 }
2160
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002161 @Override
2162 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
Sunny Goyal6001ea22018-05-10 16:31:00 -07002163 AlphaProperty property = mDragLayer.getAlphaProperty(ALPHA_INDEX_LAUNCHER_LOAD);
2164 if (property.getValue() < 1) {
2165 ObjectAnimator anim = ObjectAnimator.ofFloat(property, MultiValueAlpha.VALUE, 1);
2166 if (executor != null) {
2167 anim.addListener(new AnimatorListenerAdapter() {
2168 @Override
2169 public void onAnimationEnd(Animator animation) {
2170 executor.onLoadAnimationCompleted();
2171 }
2172 });
2173 }
2174 anim.start();
Sunny Goyal29947f02017-12-18 13:49:44 -08002175 } else if (executor != null) {
2176 executor.onLoadAnimationCompleted();
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002177 }
2178 }
2179
Joe Onorato9c1289c2009-08-17 11:03:03 -04002180 /**
2181 * Callback saying that there aren't any more items to bind.
2182 *
2183 * Implementation of the method from LauncherModel.Callbacks.
2184 */
Tonyf80e8932018-12-21 11:58:04 -08002185 public void finishBindingItems(int pageBoundFirst) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002186 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002187 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002188
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002189 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002190
Sunny Goyal2100c782016-08-22 16:00:03 -07002191 if (mPendingActivityResult != null) {
2192 handleActivityResult(mPendingActivityResult.requestCode,
2193 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2194 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002195 }
2196
Sunny Goyala474a9b2017-05-04 16:47:11 -07002197 InstallShortcutReceiver.disableAndFlushInstallQueue(
2198 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002199
Tonyf80e8932018-12-21 11:58:04 -08002200 // When undoing the removal of the last item on a page, return to that page.
2201 mWorkspace.setCurrentPage(pageBoundFirst);
Tony Wickham6a71a5b2018-08-21 11:40:23 -07002202
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002203 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002204 }
2205
Winson Chunga2413752012-04-03 14:22:34 -07002206 private boolean canRunNewAppsAnimation() {
2207 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002208 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002209 }
2210
Winson Chungc9168342013-06-26 14:54:55 -07002211 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002212 ValueAnimator bounceAnim = new PropertyListBuilder().alpha(1).scale(1).build(v)
2213 .setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
Winson Chungc9168342013-06-26 14:54:55 -07002214 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002215 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002216 return bounceAnim;
2217 }
2218
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002219 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002220 * Add the icons for all apps.
2221 *
2222 * Implementation of the method from LauncherModel.Callbacks.
2223 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002224 public void bindAllApplications(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002225 mAppsView.getAppsStore().setApps(apps);
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002226
Adam Cohen9211d422014-10-07 18:14:53 -07002227 if (mLauncherCallbacks != null) {
2228 mLauncherCallbacks.bindAllApplications(apps);
2229 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002230 }
2231
2232 /**
Zak Cohen658c67a2018-10-19 14:21:05 -07002233 * Copies LauncherModel's map of activities to shortcut counts to Launcher's. This is necessary
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002234 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2235 */
2236 @Override
Zak Cohen658c67a2018-10-19 14:21:05 -07002237 public void bindDeepShortcutMap(HashMap<ComponentKey, Integer> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002238 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002239 }
2240
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002241 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002242 * A package was updated.
2243 *
2244 * Implementation of the method from LauncherModel.Callbacks.
2245 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002246 @Override
2247 public void bindAppsAddedOrUpdated(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002248 mAppsView.getAppsStore().addOrUpdateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002249 }
2250
Sunny Goyal4390ace2014-10-13 11:33:11 -07002251 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002252 public void bindPromiseAppProgressUpdated(PromiseAppInfo app) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002253 mAppsView.getAppsStore().updatePromiseAppProgress(app);
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002254 }
2255
2256 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002257 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002258 mWorkspace.widgetsRestored(widgets);
2259 }
2260
Joe Onorato9c1289c2009-08-17 11:03:03 -04002261 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002262 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002263 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002264 *
2265 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002266 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002267 @Override
Sunny Goyal95899162019-03-27 16:03:06 -07002268 public void bindWorkspaceItemsChanged(ArrayList<WorkspaceItemInfo> updated) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002269 if (!updated.isEmpty()) {
2270 mWorkspace.updateShortcuts(updated);
2271 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002272 }
2273
2274 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002275 * Update the state of a package, typically related to install state.
2276 *
2277 * Implementation of the method from LauncherModel.Callbacks.
2278 */
Sunny Goyale755d462014-07-22 13:48:29 -07002279 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002280 public void bindRestoreItemsChange(HashSet<ItemInfo> updates) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07002281 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07002282 }
2283
2284 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002285 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07002286 * in addition to specific applications to remove, the reason being that
2287 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002288 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07002289 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002290 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07002291 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002292 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002293 mWorkspace.removeItemsByMatcher(matcher);
2294 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002295 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002296
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002297 @Override
2298 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002299 mAppsView.getAppsStore().removeApps(appInfos);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002300 }
Joe Onoratobe386092009-11-17 17:32:16 -08002301
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002302 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002303 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
2304 mPopupDataProvider.setAllWidgets(allWidgets);
Tony Wickham26b17462017-03-20 17:12:24 -07002305 }
2306
Tony Wickham86222d22017-03-29 15:30:43 -07002307 /**
2308 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
2309 * refreshes the widgets and shortcuts associated with the given package/user
2310 */
2311 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07002312 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002313 }
2314
Winson Chung80baf5a2010-08-09 16:03:15 -07002315 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002316 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08002317 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002318 @Override
2319 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
2320 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002321
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002322 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
2323 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07002324 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002325 writer.println(prefix + " Homescreen " + i);
2326
2327 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
2328 for (int j = 0; j < layout.getChildCount(); j++) {
2329 Object tag = layout.getChildAt(j).getTag();
2330 if (tag != null) {
2331 writer.println(prefix + " " + tag.toString());
2332 }
2333 }
2334 }
2335
2336 writer.println(prefix + " Hotseat");
Sunny Goyal876e4622018-11-02 13:50:40 -07002337 ViewGroup layout = mHotseat.getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07002338 for (int j = 0; j < layout.getChildCount(); j++) {
2339 Object tag = layout.getChildAt(j).getTag();
2340 if (tag != null) {
2341 writer.println(prefix + " " + tag.toString());
2342 }
Adam Cohen4caf2982013-08-20 18:54:31 -07002343 }
Sunny Goyala1365452015-10-01 15:46:24 -07002344 }
2345
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002346 writer.println(prefix + "Misc:");
2347 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
2348 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
2349 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
Sunny Goyale43d00d2018-05-14 14:23:18 -07002350 writer.println(" mRotationHelper: " + mRotationHelper);
Sunny Goyalcb2c5052018-10-15 12:30:29 -07002351 // Extra logging for b/116853349
2352 mDragLayer.dumpAlpha(writer);
Sunny Goyale43d00d2018-05-14 14:23:18 -07002353 dumpMisc(writer);
Sunny Goyal2bba1902018-02-27 12:20:50 -08002354
2355 try {
2356 FileLog.flushAll(writer);
2357 } catch (Exception e) {
2358 // Ignore
2359 }
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002360
2361 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002362
Adam Cohen9211d422014-10-07 18:14:53 -07002363 if (mLauncherCallbacks != null) {
2364 mLauncherCallbacks.dump(prefix, fd, writer, args);
2365 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002366 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002367
Sunny Goyal66b24572016-09-21 15:57:55 -07002368 @Override
Sunny Goyal66b24572016-09-21 15:57:55 -07002369 public void onProvideKeyboardShortcuts(
2370 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
2371
2372 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002373 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002374 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
2375 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
Vadim Tryshev74c261c2018-06-12 13:44:43 -07002376 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.widget_button_text),
2377 KeyEvent.KEYCODE_W, KeyEvent.META_CTRL_ON));
Sunny Goyal66b24572016-09-21 15:57:55 -07002378 }
Vadim Tryshev3455f8d2018-05-11 20:02:20 -07002379 final View currentFocus = getCurrentFocus();
2380 if (currentFocus != null) {
2381 if (new CustomActionsPopup(this, currentFocus).canShow()) {
2382 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
2383 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
2384 }
2385 if (currentFocus.getTag() instanceof ItemInfo
2386 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
2387 shortcutInfos.add(new KeyboardShortcutInfo(
2388 getString(R.string.shortcuts_menu_with_notifications_description),
2389 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
2390 }
Sunny Goyal66b24572016-09-21 15:57:55 -07002391 }
2392 if (!shortcutInfos.isEmpty()) {
2393 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
2394 }
2395
2396 super.onProvideKeyboardShortcuts(data, menu, deviceId);
2397 }
2398
2399 @Override
2400 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
2401 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
2402 switch (keyCode) {
2403 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002404 if (isInState(NORMAL)) {
2405 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07002406 return true;
2407 }
2408 break;
2409 case KeyEvent.KEYCODE_S: {
2410 View focusedView = getCurrentFocus();
2411 if (focusedView instanceof BubbleTextView
2412 && focusedView.getTag() instanceof ItemInfo
2413 && mAccessibilityDelegate.performAction(focusedView,
2414 (ItemInfo) focusedView.getTag(),
2415 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal00ac9202017-11-09 15:24:06 -08002416 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07002417 return true;
2418 }
2419 break;
2420 }
2421 case KeyEvent.KEYCODE_O:
2422 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
2423 return true;
2424 }
2425 break;
Vadim Tryshev74c261c2018-06-12 13:44:43 -07002426 case KeyEvent.KEYCODE_W:
2427 if (isInState(NORMAL)) {
2428 OptionsPopupView.openWidgets(this);
2429 return true;
2430 }
2431 break;
Sunny Goyal66b24572016-09-21 15:57:55 -07002432 }
2433 }
2434 return super.onKeyShortcut(keyCode, event);
2435 }
2436
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002437 @Override
2438 public boolean onKeyUp(int keyCode, KeyEvent event) {
2439 if (keyCode == KeyEvent.KEYCODE_MENU) {
2440 // KEYCODE_MENU is sent by some tests, for example
2441 // LauncherJankTests#testWidgetsContainerFling. Don't just remove its handling.
2442 if (!mDragController.isDragging() && !mWorkspace.isSwitchingState() &&
2443 isInState(NORMAL)) {
2444 // Close any open floating views.
2445 AbstractFloatingView.closeAllOpenViews(this);
2446
2447 // Setting the touch point to (-1, -1) will show the options popup in the center of
2448 // the screen.
Sunny Goyal2fd7a8b2018-03-30 17:10:13 -07002449 OptionsPopupView.showDefaultOptions(this, -1, -1);
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002450 }
2451 return true;
2452 }
2453 return super.onKeyUp(keyCode, event);
2454 }
2455
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002456 public static Launcher getLauncher(Context context) {
Sunny Goyal87b5eb62018-07-03 15:53:39 -07002457 return (Launcher) fromContext(context);
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002458 }
2459
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002460 /**
Sunny Goyalab770a12018-11-14 15:17:26 -08002461 * Just a wrapper around the type cast to allow easier tracking of calls.
2462 */
2463 public static <T extends Launcher> T cast(ActivityContext activityContext) {
2464 return (T) activityContext;
2465 }
2466
2467 /**
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002468 * Callback for listening for onResume
2469 */
2470 public interface OnResumeCallback {
2471
2472 void onLauncherResume();
2473 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002474}