blob: 177e7801650bd057cf46e87338af8ab658b939fa [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Jon Mirandaaeb4dd02018-07-11 14:16:23 -070019import static android.content.pm.ActivityInfo.CONFIG_LOCALE;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080020import static android.content.pm.ActivityInfo.CONFIG_ORIENTATION;
21import static android.content.pm.ActivityInfo.CONFIG_SCREEN_SIZE;
vadimt00d42552018-12-11 17:59:36 -080022
Tony Wickham6a71a5b2018-08-21 11:40:23 -070023import static com.android.launcher3.AbstractFloatingView.TYPE_SNACKBAR;
Sunny Goyal4c7f2152017-10-17 17:17:16 -070024import static com.android.launcher3.LauncherAnimUtils.SPRING_LOADED_EXIT_DELAY;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -070025import static com.android.launcher3.LauncherState.ALL_APPS;
26import static com.android.launcher3.LauncherState.NORMAL;
Sunny Goyal6001ea22018-05-10 16:31:00 -070027import static com.android.launcher3.dragndrop.DragLayer.ALPHA_INDEX_LAUNCHER_LOAD;
Sunny Goyalaeb16432017-10-16 11:46:41 -070028import static com.android.launcher3.logging.LoggerUtils.newContainerTarget;
Hyunyoung Song46d07f72018-05-22 15:41:25 -070029import static com.android.launcher3.logging.LoggerUtils.newTarget;
vadimt00d42552018-12-11 17:59:36 -080030import static com.android.launcher3.util.RaceConditionTracker.ENTER;
31import static com.android.launcher3.util.RaceConditionTracker.EXIT;
Sunny Goyal326403e2017-10-02 12:45:10 -070032
Gilles Debunnedd6c9922010-10-25 11:23:41 -070033import android.animation.Animator;
Sunny Goyal6001ea22018-05-10 16:31:00 -070034import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070035import android.animation.AnimatorSet;
Sunny Goyal6001ea22018-05-10 16:31:00 -070036import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070037import android.animation.ValueAnimator;
Adam Cohen0b395352014-06-09 22:54:36 +000038import android.annotation.TargetApi;
Tony Wickham005df0b2018-02-13 11:28:12 -080039import android.app.ActivityOptions;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080040import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070041import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080043import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080044import android.content.ComponentCallbacks2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070047import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070048import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070049import android.content.SharedPreferences;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.content.pm.PackageManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080051import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070052import android.database.sqlite.SQLiteDatabase;
Sunny Goyal59d086c2018-05-07 17:31:40 -070053import android.graphics.Point;
Adam Cohen0f668f32014-09-08 19:54:17 +020054import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055import android.os.Bundle;
Hyunyoung Songbd6fba92018-05-16 15:54:31 -070056import android.os.Handler;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070057import android.os.Parcelable;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080058import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070059import android.os.StrictMode;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070060import android.os.UserHandle;
Michael Jurkaa33411c2012-06-14 16:18:21 -070061import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070063import android.util.Log;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070064import android.util.SparseArray;
Sunny Goyal59d086c2018-05-07 17:31:40 -070065import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070066import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070067import android.view.KeyboardShortcutGroup;
68import android.view.KeyboardShortcutInfo;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080069import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -070070import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070071import android.view.View;
Adam Cohen96d30a12013-07-16 18:13:21 -070072import android.view.ViewGroup;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080073import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070074import android.view.animation.OvershootInterpolator;
Adam Cohen96d30a12013-07-16 18:13:21 -070075import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070076
vadimt00d42552018-12-11 17:59:36 -080077import androidx.annotation.Nullable;
78
Sunny Goyal651077b2014-06-30 14:15:31 -070079import com.android.launcher3.DropTarget.DragObject;
Sunny Goyalae502842016-06-17 08:43:56 -070080import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070081import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070082import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal7ede6112017-12-05 15:11:21 -080083import com.android.launcher3.allapps.DiscoveryBounce;
Sunny Goyalf0b6db72018-08-13 16:10:14 -070084import com.android.launcher3.anim.PropertyListBuilder;
Sunny Goyalffe83f12014-08-14 17:39:34 -070085import com.android.launcher3.compat.AppWidgetManagerCompat;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -070086import com.android.launcher3.compat.LauncherAppsCompatVO;
Tony Wickhame0c33232016-02-08 11:37:04 -080087import com.android.launcher3.config.FeatureFlags;
vadimt00d42552018-12-11 17:59:36 -080088import com.android.launcher3.dot.DotInfo;
Vadim Tryshevfedca432015-08-19 17:55:02 -070089import com.android.launcher3.dragndrop.DragController;
90import com.android.launcher3.dragndrop.DragLayer;
91import com.android.launcher3.dragndrop.DragView;
Jon Mirandaaeb4dd02018-07-11 14:16:23 -070092import com.android.launcher3.folder.Folder;
Sunny Goyal26119432016-02-18 22:09:23 +000093import com.android.launcher3.folder.FolderIcon;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070094import com.android.launcher3.folder.FolderIconPreviewVerifier;
Sunny Goyalf840f102018-09-21 14:41:05 -070095import com.android.launcher3.icons.IconCache;
Sunny Goyal66b24572016-09-21 15:57:55 -070096import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -070097import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -070098import com.android.launcher3.logging.FileLog;
Hyunyoung Songfc007472018-10-25 14:09:50 -070099import com.android.launcher3.logging.StatsLogUtils;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700100import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700101import com.android.launcher3.logging.UserEventDispatcher.UserEventDelegate;
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;
Sunny Goyal29947f02017-12-18 13:49:44 -0800133import com.android.launcher3.widget.LauncherAppWidgetHostView;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800134import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700135import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal29947f02017-12-18 13:49:44 -0800136import com.android.launcher3.widget.PendingAppWidgetHostView;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800137import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700138import com.android.launcher3.widget.WidgetHostViewLoader;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700139import com.android.launcher3.widget.WidgetListRowEntry;
140import com.android.launcher3.widget.WidgetsFullSheet;
Sunny Goyal952e63d2017-08-16 04:59:08 -0700141import com.android.launcher3.widget.custom.CustomWidgetParser;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700142
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700143import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700144import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700145import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700146import java.util.Collection;
Zak Cohen658c67a2018-10-19 14:21:05 -0700147import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700148import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700149import java.util.List;
Tony Wickham010d2552017-01-20 08:15:28 -0800150import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700151
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152/**
153 * Default launcher application.
154 */
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700155public class Launcher extends BaseDraggingActivity implements LauncherExterns,
Hyunyoung Song31971a32019-01-27 12:31:12 -0800156 LauncherModel.Callbacks, LauncherProviderChangeListener, UserEventDelegate,
157 InvariantDeviceProfile.OnIDPChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700158 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700159 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160
Winson Chunga2413752012-04-03 14:22:34 -0700161 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700162
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700164 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700165
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700166 private static final int REQUEST_PICK_APPWIDGET = 9;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167
Michael Jurka8b805b12012-04-18 14:23:14 -0700168 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800169 public static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
170 public static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700171
Jon Mirandac476d6e2017-05-04 16:30:01 -0700172 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700173
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700174 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
175
Mathew Inwood876a8462013-06-14 14:12:41 +0100176 /**
177 * IntentStarter uses request codes starting with this. This must be greater than all activity
178 * request codes used internally.
179 */
180 protected static final int REQUEST_LAST = 100;
181
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 // Type: int
183 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700184 // Type: int
185 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700186 // Type: PendingRequestArgs
187 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
188 // Type: ActivityResultInfo
189 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700190 // Type: SparseArray<Parcelable>
191 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
vadimt00d42552018-12-11 17:59:36 -0800192 public static final String ON_CREATE_EVT = "Launcher.onCreate";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700194 private LauncherStateManager mStateManager;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700195
Adam Cohenad4e15c2013-10-17 16:21:35 -0700196 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800197
Winson Chunga2413752012-04-03 14:22:34 -0700198 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700199 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
200 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
201 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700202
Jon Miranda54441f52018-01-24 15:38:25 -0800203 private LauncherAppTransitionManager mAppTransitionManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800204 private Configuration mOldConfig;
Jon Miranda54441f52018-01-24 15:38:25 -0800205
Adam Cohen091440a2015-03-18 14:16:05 -0700206 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700207 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700208 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800209 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700210
Sunny Goyalffe83f12014-08-14 17:39:34 -0700211 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700212 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700213
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700214 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700215
Sunny Goyal316490e2015-06-02 09:38:28 -0700216 @Thunk Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700217
Sunny Goyal47328fd2016-05-25 18:56:41 -0700218 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700219
220 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700221 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700222 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700223
Winson Chung8ae41982017-11-10 11:42:13 -0800224 // UI and state for the overview panel
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800225 private View mOverviewPanel;
Winson Chung8ae41982017-11-10 11:42:13 -0800226
Adam Cohen091440a2015-03-18 14:16:05 -0700227 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400228
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700229 private OnResumeCallback mOnResumeCallback;
230
Sunny Goyal527c7d32015-08-28 15:19:36 -0700231 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700232
Joe Onorato9c1289c2009-08-17 11:03:03 -0400233 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800234 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800235 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700236 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400237
Tony Wickham010d2552017-01-20 08:15:28 -0800238 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700239
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800240 private int mSynchronouslyBoundPage = PagedView.INVALID_PAGE;
Adam Cohen1462de32012-07-24 22:34:36 -0700241
Winson Chung46353de2012-02-16 14:05:10 -0800242 // We only want to get the SharedPreferences once since it does an FS stat each time we get
243 // it from the context.
244 private SharedPreferences mSharedPrefs;
245
Sunny Goyal2100c782016-08-22 16:00:03 -0700246 // Activity result which needs to be processed after workspace has loaded.
247 private ActivityResultInfo mPendingActivityResult;
248 /**
249 * Holds extra information required to handle a result from an external call, like
250 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
251 */
252 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800253
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700254 public ViewGroupFocusHelper mFocusHandler;
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700255
Sunny Goyal623eddd2018-03-02 12:24:41 -0800256 private RotationHelper mRotationHelper;
Sunny Goyal7779d622015-06-11 16:18:39 -0700257
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700258
259 private final Handler mHandler = new Handler();
260 private final Runnable mLogOnDelayedResume = this::logOnDelayedResume;
261
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800262 @Override
263 protected void onCreate(Bundle savedInstanceState) {
vadimt00d42552018-12-11 17:59:36 -0800264 RaceConditionTracker.onEvent(ON_CREATE_EVT, ENTER);
Winson Chunga2413752012-04-03 14:22:34 -0700265 if (DEBUG_STRICT_MODE) {
266 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
267 .detectDiskReads()
268 .detectDiskWrites()
269 .detectNetwork() // or .detectAll() for all detectable problems
270 .penaltyLog()
271 .build());
272 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
273 .detectLeakedSqlLiteObjects()
274 .detectLeakedClosableObjects()
275 .penaltyLog()
276 .penaltyDeath()
277 .build());
278 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700279 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700280
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800281 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700282 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400283
Sunny Goyal87f784c2017-01-11 10:48:34 -0800284 LauncherAppState app = LauncherAppState.getInstance(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800285 mOldConfig = new Configuration(getResources().getConfiguration());
Sunny Goyal0e7724c2018-02-22 13:22:21 -0800286 mModel = app.setLauncher(this);
Hyunyoung Song31971a32019-01-27 12:31:12 -0800287 InvariantDeviceProfile idp = app.getInvariantDeviceProfile();
288 initDeviceProfile(idp);
289 idp.addOnChangeListener(this);
Sunny Goyalf7258242015-10-19 16:59:07 -0700290 mSharedPrefs = Utilities.getPrefs(this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800291 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700292 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700293
Joe Onorato41a12d22009-10-31 18:30:00 -0400294 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700295 mAllAppsController = new AllAppsTransitionController(this);
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800296 mStateManager = new LauncherStateManager(this);
Tracy Zhou8b23c6f2018-04-30 17:13:27 -0700297 UiFactory.onCreate(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700298
Sunny Goyalffe83f12014-08-14 17:39:34 -0700299 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700300
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700301 mAppWidgetHost = new LauncherAppWidgetHost(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700302 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700303
Sunny Goyal60820d72017-05-09 12:40:11 -0700304 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700305
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800306 setupViews();
Tony Wickham010d2552017-01-20 08:15:28 -0800307 mPopupDataProvider = new PopupDataProvider(this);
308
Sunny Goyal623eddd2018-03-02 12:24:41 -0800309 mRotationHelper = new RotationHelper(this);
Sunny Goyal90548432018-04-04 17:17:00 -0700310 mAppTransitionManager = LauncherAppTransitionManager.newInstance(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800311
Sunny Goyald3864fa2017-11-14 15:06:36 -0800312 boolean internalStateHandled = InternalStateHandler.handleCreate(this, getIntent());
313 if (internalStateHandled) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800314 if (savedInstanceState != null) {
315 // InternalStateHandler has already set the appropriate state.
316 // We dont need to do anything.
317 savedInstanceState.remove(RUNTIME_STATE);
318 }
319 }
320 restoreState(savedInstanceState);
Winson Chungb63b44c2017-11-10 17:54:44 -0800321
Sunny Goyal2100c782016-08-22 16:00:03 -0700322 // We only load the page synchronously if the user rotates (or triggers a
323 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700324 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
325 if (savedInstanceState != null) {
326 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
327 }
Sunny Goyald3864fa2017-11-14 15:06:36 -0800328
Sunny Goyalfe770c92016-09-27 14:38:58 -0700329 if (!mModel.startLoader(currentScreen)) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800330 if (!internalStateHandled) {
331 // If we are not binding synchronously, show a fade in animation when
332 // the first page bind completes.
Sunny Goyal6001ea22018-05-10 16:31:00 -0700333 mDragLayer.getAlphaProperty(ALPHA_INDEX_LAUNCHER_LOAD).setValue(0);
Sunny Goyald3864fa2017-11-14 15:06:36 -0800334 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700335 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700336 // Pages bound synchronously.
337 mWorkspace.setCurrentPage(currentScreen);
338
Sunny Goyal2100c782016-08-22 16:00:03 -0700339 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800340 }
341
342 // For handling default keys
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800343 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800344
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800345 setContentView(mLauncherView);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800346 getRootView().dispatchInsets();
Mario Bertschlera6936942017-05-31 14:48:19 -0700347
Jon Miranda7fda2852017-07-19 16:07:01 -0700348 // Listen for broadcasts
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700349 registerReceiver(mScreenOffReceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));
Sunny Goyala616d2b2017-06-12 13:54:01 -0700350
Sunny Goyal8392c822017-06-20 10:03:56 -0700351 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
352 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700353
354 if (mLauncherCallbacks != null) {
355 mLauncherCallbacks.onCreate(savedInstanceState);
356 }
Sunny Goyal623eddd2018-03-02 12:24:41 -0800357 mRotationHelper.initialize();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700358
359 TraceHelper.endSection("Launcher-onCreate");
vadimt00d42552018-12-11 17:59:36 -0800360 RaceConditionTracker.onEvent(ON_CREATE_EVT, EXIT);
Adam Cohen9211d422014-10-07 18:14:53 -0700361 }
362
Sunny Goyal7779d622015-06-11 16:18:39 -0700363 @Override
Winson Chung8eb49e02018-07-16 13:33:31 -0700364 public void onEnterAnimationComplete() {
365 super.onEnterAnimationComplete();
366 UiFactory.onEnterAnimationComplete(this);
arangelovad783052018-08-17 14:46:45 +0100367 mAllAppsController.highlightWorkTabIfNecessary();
Winson Chung8eb49e02018-07-16 13:33:31 -0700368 }
369
370 @Override
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800371 public void onConfigurationChanged(Configuration newConfig) {
372 int diff = newConfig.diff(mOldConfig);
Jon Mirandaaeb4dd02018-07-11 14:16:23 -0700373
374 if ((diff & CONFIG_LOCALE) != 0) {
375 Folder.setLocaleDependentFields(getResources(), true /* force */);
376 }
377
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800378 if ((diff & (CONFIG_ORIENTATION | CONFIG_SCREEN_SIZE)) != 0) {
379 mUserEventDispatcher = null;
380 initDeviceProfile(mDeviceProfile.inv);
Sunny Goyalfde55052018-02-01 14:46:13 -0800381 dispatchDeviceProfileChanged();
Sunny Goyal59d086c2018-05-07 17:31:40 -0700382 reapplyUi();
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700383 mDragLayer.recreateControllers();
Sunny Goyal7185dd62018-03-14 17:51:49 -0700384
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800385 // TODO: We can probably avoid rebind when only screen size changed.
Sunny Goyal605bcf32018-03-02 15:16:12 -0800386 rebindModel();
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800387 }
388
389 mOldConfig.setTo(newConfig);
Tracy Zhoua706f002018-03-28 13:55:19 -0700390 UiFactory.onLauncherStateOrResumeChanged(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800391 super.onConfigurationChanged(newConfig);
392 }
393
Sunny Goyal605bcf32018-03-02 15:16:12 -0800394 @Override
Sunny Goyal59d086c2018-05-07 17:31:40 -0700395 protected void reapplyUi() {
396 getRootView().dispatchInsets();
397 getStateManager().reapplyState(true /* cancelCurrentAnimation */);
398 }
399
400 @Override
Sunny Goyal605bcf32018-03-02 15:16:12 -0800401 public void rebindModel() {
402 int currentPage = mWorkspace.getNextPage();
403 if (mModel.startLoader(currentPage)) {
404 mWorkspace.setCurrentPage(currentPage);
405 setWorkspaceLoading(true);
406 }
407 }
408
Hyunyoung Song31971a32019-01-27 12:31:12 -0800409 @Override
410 public void onIdpChanged(int changeFlags, InvariantDeviceProfile idp) {
411 initDeviceProfile(idp);
412 getRootView().dispatchInsets();
413 }
414
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800415 private void initDeviceProfile(InvariantDeviceProfile idp) {
416 // Load configuration-specific DeviceProfile
Sunny Goyal59d086c2018-05-07 17:31:40 -0700417 mDeviceProfile = idp.getDeviceProfile(this);
418 if (isInMultiWindowModeCompat()) {
419 Display display = getWindowManager().getDefaultDisplay();
420 Point mwSize = new Point();
421 display.getSize(mwSize);
422 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
423 }
424 onDeviceProfileInitiated();
Sunny Goyal605bcf32018-03-02 15:16:12 -0800425 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout(), true);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800426 }
427
Sunny Goyal623eddd2018-03-02 12:24:41 -0800428 public RotationHelper getRotationHelper() {
429 return mRotationHelper;
430 }
431
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700432 public LauncherStateManager getStateManager() {
433 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700434 }
435
Mario Bertschler27288382017-05-24 15:35:09 -0700436 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700437 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800438 return mLauncherView.findViewById(id);
439 }
440
441 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700442 public void onAppWidgetHostReset() {
443 if (mAppWidgetHost != null) {
444 mAppWidgetHost.startListening();
445 }
446 }
447
Adam Cohen9211d422014-10-07 18:14:53 -0700448 private LauncherCallbacks mLauncherCallbacks;
449
Sunny Goyal32554d12015-12-03 15:31:25 -0800450 /**
451 * Call this after onCreate to set or clear overlay.
452 */
453 public void setLauncherOverlay(LauncherOverlay overlay) {
454 if (overlay != null) {
455 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
456 }
457 mWorkspace.setLauncherOverlay(overlay);
458 }
459
Adam Cohen9211d422014-10-07 18:14:53 -0700460 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
461 mLauncherCallbacks = callbacks;
462 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700463 }
464
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700465 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700466 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700467 if (mLauncherCallbacks != null) {
468 mLauncherCallbacks.onLauncherProviderChange();
469 }
470 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700471
Hyunyoung Song3f471442015-04-08 19:01:34 -0700472 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700473 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
474 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700475 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700476 }
477
Tony Wickham010d2552017-01-20 08:15:28 -0800478 public PopupDataProvider getPopupDataProvider() {
479 return mPopupDataProvider;
480 }
481
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700482 @Override
Tony Wickhamf34bee82018-12-03 18:11:39 -0800483 public DotInfo getDotInfoForItem(ItemInfo info) {
484 return mPopupDataProvider.getDotInfoForItem(info);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700485 }
486
487 @Override
488 public void invalidateParent(ItemInfo info) {
489 FolderIconPreviewVerifier verifier = new FolderIconPreviewVerifier(getDeviceProfile().inv);
490 if (verifier.isItemInPreview(info.rank) && (info.container >= 0)) {
491 View folderIcon = getWorkspace().getHomescreenIconByItemId(info.container);
492 if (folderIcon != null) {
493 folderIcon.invalidate();
494 }
495 }
496 }
497
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000498 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700499 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
500 * a configuration step, this allows the proper animations to run after other transitions.
501 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700502 private int completeAdd(
Sunny Goyal2100c782016-08-22 16:00:03 -0700503 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
Sunny Goyalefb7e842018-10-04 15:11:00 -0700504 int screenId = info.screenId;
Sunny Goyal2100c782016-08-22 16:00:03 -0700505 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700506 // When the screen id represents an actual screen (as opposed to a rank) we make sure
507 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700508 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700509 }
Adam Cohendb364c32014-05-20 14:23:40 -0700510
Sunny Goyal2100c782016-08-22 16:00:03 -0700511 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800512 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700513 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700514 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800515 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700516 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700517 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700518 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700519 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700520 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700521 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700522 int widgetId = appWidgetId;
523 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700524 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700525 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700526 // Since the view was just bound, also launch the configure activity if needed
527 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
528 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800529 if (provider != null) {
530 new WidgetAddFlowHandler(provider)
531 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700532 }
533 }
534 break;
535 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800536 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700537
Adam Cohendb364c32014-05-20 14:23:40 -0700538 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800539 }
540
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800541 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700542 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700543 if (isWorkspaceLoading()) {
544 // process the result once the workspace has loaded.
545 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
546 return;
547 }
548 mPendingActivityResult = null;
549
Winson Chunge341d302013-08-16 14:31:00 -0700550 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700551 final PendingRequestArgs requestArgs = mPendingRequestArgs;
552 setWaitingForResult(null);
553 if (requestArgs == null) {
554 return;
555 }
556
557 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700558
Adam Cohenad4e15c2013-10-17 16:21:35 -0700559 Runnable exitSpringLoaded = new Runnable() {
560 @Override
561 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700562 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700563 }
564 };
565
Michael Jurka8b805b12012-04-18 14:23:14 -0700566 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700567 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700568 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700569 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
570 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700571 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700572 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700573 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700574 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700575 addAppWidgetImpl(
576 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800577 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700578 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700579 }
580 return;
581 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200582
Adam Cohened66b2b2012-01-23 17:28:51 -0800583 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700584 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700585
Adam Cohened66b2b2012-01-23 17:28:51 -0800586 // We have special handling for widgets
587 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800588 final int appWidgetId;
589 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
590 : -1;
591 if (widgetId < 0) {
592 appWidgetId = pendingAddWidgetId;
593 } else {
594 appWidgetId = widgetId;
595 }
596
Adam Cohenad4e15c2013-10-17 16:21:35 -0700597 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800598 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700599 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
600 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700601 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700602 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700603 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700604 @Override
605 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700606 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700607 }
608 };
Adam Cohendb364c32014-05-20 14:23:40 -0700609
Sunny Goyal2100c782016-08-22 16:00:03 -0700610 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
611 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
612 } else {
613 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
614 // When the screen id represents an actual screen (as opposed to a rank)
615 // we make sure that the drop page actually exists.
616 requestArgs.screenId =
617 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700618 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700619 final CellLayout dropLayout =
620 mWorkspace.getScreenWithId(requestArgs.screenId);
621
622 dropLayout.setDropPending(true);
623 final Runnable onComplete = new Runnable() {
624 @Override
625 public void run() {
626 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
627 dropLayout.setDropPending(false);
628 }
629 };
630 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
631 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700632 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800633 return;
634 }
635
Sunny Goyald478c832016-04-01 12:04:16 -0700636 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
637 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700638 if (resultCode == RESULT_OK) {
639 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700640 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700641 }
642 // Leave the widget in the pending state if the user canceled the configure.
643 return;
644 }
645
Sunny Goyalaad90582015-07-09 14:22:50 -0700646 if (requestCode == REQUEST_CREATE_SHORTCUT) {
647 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700648 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
649 completeAdd(requestCode, data, -1, requestArgs);
650 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
651 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
652
Sunny Goyalaad90582015-07-09 14:22:50 -0700653 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700654 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
655 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800656 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800657 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800658 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800659 }
660
661 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700662 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800663 final int requestCode, final int resultCode, final Intent data) {
664 handleActivityResult(requestCode, resultCode, data);
665 if (mLauncherCallbacks != null) {
666 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
667 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800668 }
669
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700670 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700671 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600672 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700673 PendingRequestArgs pendingArgs = mPendingRequestArgs;
674 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
675 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
676 setWaitingForResult(null);
677
Sunny Goyal28c6b962015-10-12 11:42:05 -0700678 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700679 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700680 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700681 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700682 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700683 Intent intent = pendingArgs.getPendingIntent();
684
Sunny Goyal28c6b962015-10-12 11:42:05 -0700685 if (grantResults.length > 0
686 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700687 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700688 } else {
689 // TODO: Show a snack bar with link to settings
690 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700691 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700692 }
693 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600694 if (mLauncherCallbacks != null) {
695 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
696 grantResults);
697 }
698 }
699
Adam Cohendb364c32014-05-20 14:23:40 -0700700 /**
701 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
702 *
703 * @param screenId the screen id to check
704 * @return the new screen, or screenId if it exists
705 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700706 private int ensurePendingDropLayoutExists(int screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800707 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700708 if (dropLayout == null) {
709 // it's possible that the add screen was removed because it was
710 // empty and a re-bind occurred
711 mWorkspace.addExtraEmptyScreen();
712 return mWorkspace.commitExtraEmptyScreen();
713 } else {
714 return screenId;
715 }
716 }
717
Sunny Goyal2100c782016-08-22 16:00:03 -0700718 @Thunk void completeTwoStageWidgetDrop(
719 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
720 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800721 Runnable onCompleteRunnable = null;
722 int animationType = 0;
723
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700724 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800725 if (resultCode == RESULT_OK) {
726 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
727 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800728 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700729 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800730 onCompleteRunnable = new Runnable() {
731 @Override
732 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700733 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700734 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800735 }
736 };
737 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800738 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800739 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800740 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700741 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700742 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700743 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
744 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700745 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700746 // The animated view may be null in the case of a rotation during widget configuration
747 onCompleteRunnable.run();
748 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800749 }
750
751 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700752 protected void onStop() {
753 super.onStop();
Adam Cohen9211d422014-10-07 18:14:53 -0700754 if (mLauncherCallbacks != null) {
755 mLauncherCallbacks.onStop();
756 }
Hyunyoung Song1241e612018-05-15 21:46:51 -0700757 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
758 mStateManager.getState().containerType, -1);
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700759
760 mAppWidgetHost.setListenIfResumed(false);
761
Tony Wickham010d2552017-01-20 08:15:28 -0800762 NotificationListener.removeNotificationsChangedListener();
Sunny Goyalaf161fd2018-02-05 16:34:57 -0800763 getStateManager().moveToRestState();
Winson Chungedb0d6b2018-03-21 17:32:55 -0700764
Tracy Zhou62646712018-06-26 14:19:02 -0700765 UiFactory.onLauncherStateOrResumeChanged(this);
766
Winson Chungdea51352018-04-24 13:02:10 -0700767 // Workaround for b/78520668, explicitly trim memory once UI is hidden
Winson Chung473730f2018-04-26 11:54:22 -0700768 onTrimMemory(TRIM_MEMORY_UI_HIDDEN);
Michael Jurkacd496d72013-04-11 11:32:45 -0700769 }
770
771 @Override
772 protected void onStart() {
773 super.onStart();
Adam Cohen9211d422014-10-07 18:14:53 -0700774 if (mLauncherCallbacks != null) {
775 mLauncherCallbacks.onStart();
776 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800777 mAppWidgetHost.setListenIfResumed(true);
Sunny Goyalcac002d2018-01-04 09:51:45 -0800778 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
Michael Jurkacd496d72013-04-11 11:32:45 -0700779 }
780
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700781 private void logOnDelayedResume() {
782 if (hasBeenResumed()) {
783 getUserEventDispatcher().logActionCommand(Action.Command.RESUME,
784 mStateManager.getState().containerType, -1);
785 getUserEventDispatcher().startSession();
786 }
787 }
788
Michael Jurkacd496d72013-04-11 11:32:45 -0700789 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800790 protected void onResume() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700791 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800792 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700793 TraceHelper.partitionSection("ON_RESUME", "superCall");
794
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700795 mHandler.removeCallbacks(mLogOnDelayedResume);
796 Utilities.postAsyncCallback(mHandler, mLogOnDelayedResume);
797
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700798 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700799 // Process any items that were added while Launcher was away.
800 InstallShortcutReceiver.disableAndFlushInstallQueue(
801 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700802
Sunny Goyala474a9b2017-05-04 16:47:11 -0700803 // Refresh shortcuts if the permission changed.
804 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700805
Tracy Zhoue5575f92018-04-19 11:12:53 -0700806 DiscoveryBounce.showForHomeIfNeeded(this);
Adam Cohen9211d422014-10-07 18:14:53 -0700807 if (mLauncherCallbacks != null) {
808 mLauncherCallbacks.onResume();
809 }
Tracy Zhoua706f002018-03-28 13:55:19 -0700810 UiFactory.onLauncherStateOrResumeChanged(this);
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800811
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700812 TraceHelper.endSection("ON_RESUME");
Adam Cohen06dff352012-06-01 17:17:08 -0700813 }
814
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800815 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700816 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700817 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700818 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700819
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700820 super.onPause();
Joe Onorato24b6fd82009-11-12 13:47:09 -0800821 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700822 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700823
Adam Cohen9211d422014-10-07 18:14:53 -0700824 if (mLauncherCallbacks != null) {
825 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700826 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700827 }
828
Winson Chungd7823942018-02-16 03:23:47 +0000829 @Override
Tracy Zhoua706f002018-03-28 13:55:19 -0700830 protected void onUserLeaveHint() {
831 super.onUserLeaveHint();
832 UiFactory.onLauncherStateOrResumeChanged(this);
833 }
834
835 @Override
Winson Chungd7823942018-02-16 03:23:47 +0000836 public void onWindowFocusChanged(boolean hasFocus) {
837 super.onWindowFocusChanged(hasFocus);
838 mStateManager.onWindowFocusChanged();
839 }
840
Adam Cohenc2d6e892014-10-16 09:49:24 -0700841 public interface LauncherOverlay {
842
843 /**
844 * Touch interaction leading to overscroll has begun
845 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700846 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700847
848 /**
849 * Touch interaction related to overscroll has ended
850 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700851 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700852
853 /**
854 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
855 * screen (or in the case of RTL, the rightmost screen).
856 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700857 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700858
859 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800860 * Called when the launcher is ready to use the overlay
861 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700862 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700863 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700864 }
865
866 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700867 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700868 }
869
870 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
871
Sunny Goyalc86df472016-02-25 09:19:38 -0800872 public void onScrollChanged(float progress) {
873 if (mWorkspace != null) {
874 mWorkspace.onOverlayScrollChanged(progress);
875 }
876 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700877 }
878
Sunny Goyalf9403d92017-10-18 10:55:56 -0700879 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -0700880 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700881 }
882
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800883 /**
884 * Restores the previous state, if it exists.
885 *
886 * @param savedState The previous state.
887 */
888 private void restoreState(Bundle savedState) {
889 if (savedState == null) {
890 return;
891 }
892
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700893 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -0700894 LauncherState[] stateValues = LauncherState.values();
895 LauncherState state = stateValues[stateOrdinal];
Sunny Goyalbc683e92017-12-06 10:25:07 -0800896 if (!state.disableRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700897 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800898 }
899
Sunny Goyal2100c782016-08-22 16:00:03 -0700900 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
901 if (requestArgs != null) {
902 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800903 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700904
905 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700906
907 SparseArray<Parcelable> widgetsState =
908 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
909 if (widgetsState != null) {
910 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
911 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800912 }
913
914 /**
915 * Finds all the views we need and configure them properly.
916 */
917 private void setupViews() {
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800918 mDragLayer = findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700919 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -0700920 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700921 mWorkspace.initParentViews(mDragLayer);
Sunny Goyal16764582017-11-06 16:00:34 -0800922 mOverviewPanel = findViewById(R.id.overview_panel);
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800923 mHotseat = findViewById(R.id.hotseat);
Craig Mautner360310b2012-10-26 15:13:08 -0700924
Sunny Goyal784f9c32016-03-23 16:56:54 -0700925 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
926 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
927 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800928
Winson Chung4c98d922011-05-31 16:50:48 -0700929 // Setup the drag layer
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700930 mDragLayer.setup(mDragController, mWorkspace);
931 UiFactory.setOnTouchControllersChangedListener(this, mDragLayer::recreateControllers);
Winson Chung4c98d922011-05-31 16:50:48 -0700932
Hyunyoung Song645764e2016-06-06 14:19:02 -0700933 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -0700934 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
935 // default state, otherwise we will update to the wrong offsets in RTL
936 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700937 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700938 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700939
Tony Wickham34d2c912015-09-11 09:27:58 -0700940 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700941 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700942
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700943 // Setup Apps
944 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -0700945
Winson Chung3d503fb2011-07-13 17:25:49 -0700946 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -0700947 mDragController.setMoveTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700948 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -0400949
Sunny Goyal7185dd62018-03-14 17:51:49 -0700950 mAllAppsController.setupViews(mAppsView);
Sunny Goyalab770a12018-11-14 15:17:26 -0800951 mHotseat.setOnInterceptTouchListener(mWorkspace::onInterceptHotseatTouch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800952 }
953
954 /**
955 * Creates a view representing a shortcut.
956 *
957 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800958 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800959 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700960 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800961 }
962
963 /**
964 * Creates a view representing a shortcut inflated from the specified resource.
965 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800966 * @param parent The group the shortcut belongs to.
967 * @param info The data structure describing the shortcut.
968 *
969 * @return A View inflated from layoutResId.
970 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700971 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -0800972 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
973 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800974 favorite.applyFromShortcutInfo(info);
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800975 favorite.setOnClickListener(ItemClickHandler.INSTANCE);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700976 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800977 return favorite;
978 }
979
980 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -0700981 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800982 *
983 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800984 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700985 private void completeAddShortcut(Intent data, int container, int screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700986 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -0700987 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
988 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700989 return;
990 }
Adam Cohen558baaf2011-08-15 15:22:57 -0700991
Jon Mirandac476d6e2017-05-04 16:30:01 -0700992 int[] cellXY = mTmpAddItemCellCoordinates;
993 CellLayout layout = getCellLayout(container, screenId);
994
Sunny Goyal782f0c92017-01-19 10:27:54 -0800995 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700996 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -0700997 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
998 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -0800999 }
1000
1001 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001002 // Legacy shortcuts are only supported for primary profile.
1003 info = Process.myUserHandle().equals(args.user)
1004 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001005
Sunny Goyalb57645f2017-03-20 13:57:28 -07001006 if (info == null) {
1007 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1008 return;
1009 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001010 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001011 // The app is trying to add a shortcut without sufficient permissions
1012 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1013 return;
1014 }
1015 }
1016
Jon Mirandac476d6e2017-05-04 16:30:01 -07001017 if (container < 0) {
1018 // Adding a shortcut to the Workspace.
1019 final View view = createShortcut(info);
1020 boolean foundCellSpan = false;
1021 // First we check if we already know the exact location where we want to add this item.
1022 if (cellX >= 0 && cellY >= 0) {
1023 cellXY[0] = cellX;
1024 cellXY[1] = cellY;
1025 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001026
Jon Mirandac476d6e2017-05-04 16:30:01 -07001027 // If appropriate, either create a folder or add to an existing folder
1028 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001029 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001030 return;
1031 }
1032 DragObject dragObject = new DragObject();
1033 dragObject.dragInfo = info;
1034 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1035 true)) {
1036 return;
1037 }
1038 } else {
1039 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1040 }
1041
1042 if (!foundCellSpan) {
1043 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001044 return;
1045 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001046
1047 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1048 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001049 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001050 // Adding a shortcut to a Folder.
Sunny Goyale29897f52017-07-20 10:09:42 -07001051 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001052 if (folderIcon != null) {
1053 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1054 folderInfo.add(info, args.rank, false);
1055 } else {
Sunny Goyale29897f52017-07-20 10:09:42 -07001056 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001057 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001058 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001059 }
1060
Sunny Goyalefb7e842018-10-04 15:11:00 -07001061 public FolderIcon findFolderIcon(final int folderIconId) {
Sunny Goyal83fd25e2018-07-09 16:47:01 -07001062 return (FolderIcon) mWorkspace.getHomescreenIconByItemId(folderIconId);
Sunny Goyale29897f52017-07-20 10:09:42 -07001063 }
1064
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001065 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001066 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001067 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001068 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001069 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001070 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001071 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1072
Adam Cohened66b2b2012-01-23 17:28:51 -08001073 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001074 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001075 }
Romain Guycbb89e42009-06-08 15:52:54 -07001076
Adam Cohen59400422014-03-05 18:07:04 -08001077 LauncherAppWidgetInfo launcherInfo;
1078 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001079 launcherInfo.spanX = itemInfo.spanX;
1080 launcherInfo.spanY = itemInfo.spanY;
1081 launcherInfo.minSpanX = itemInfo.minSpanX;
1082 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001083 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001084
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001085 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001086 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001087
Sunny Goyal2100c782016-08-22 16:00:03 -07001088 if (hostView == null) {
1089 // Perform actual inflation because we're live
1090 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001091 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001092 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301093 prepareAppWidget(hostView, launcherInfo);
1094 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001095 }
Romain Guycbb89e42009-06-08 15:52:54 -07001096
Sunny Goyald5462aa2016-11-22 16:52:39 +05301097 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001098 hostView.setTag(item);
1099 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001100 hostView.setFocusable(true);
1101 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001102 }
1103
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001104 private final BroadcastReceiver mScreenOffReceiver = new BroadcastReceiver() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001105 @Override
1106 public void onReceive(Context context, Intent intent) {
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001107 // Reset AllApps to its initial state only if we are not in the middle of
1108 // processing a multi-step drop
1109 if (mPendingRequestArgs == null) {
1110 mStateManager.goToState(NORMAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001111 }
1112 }
1113 };
1114
Tony Wickhamf34bee82018-12-03 18:11:39 -08001115 public void updateNotificationDots(final Set<PackageUserKey> updatedDots) {
1116 mWorkspace.updateNotificationDots(updatedDots);
1117 mAppsView.getAppsStore().updateNotificationDots(updatedDots);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001118
Sunny Goyal29947f02017-12-18 13:49:44 -08001119 PopupContainerWithArrow popup = PopupContainerWithArrow.getOpen(Launcher.this);
1120 if (popup != null) {
Tony Wickhamf34bee82018-12-03 18:11:39 -08001121 popup.updateNotificationHeader(updatedDots);
Tony Wickham010d2552017-01-20 08:15:28 -08001122 }
1123 }
1124
Adam Cohended9f8d2010-11-03 13:25:16 -07001125 @Override
1126 public void onAttachedToWindow() {
1127 super.onAttachedToWindow();
1128
Sunny Goyalc86df472016-02-25 09:19:38 -08001129 if (mLauncherCallbacks != null) {
1130 mLauncherCallbacks.onAttachedToWindow();
1131 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001132 }
1133
1134 @Override
1135 public void onDetachedFromWindow() {
1136 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001137
1138 if (mLauncherCallbacks != null) {
1139 mLauncherCallbacks.onDetachedFromWindow();
1140 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001141 }
1142
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001143 public AllAppsTransitionController getAllAppsController() {
1144 return mAllAppsController;
1145 }
1146
Sunny Goyal9d69c8d2018-03-19 13:41:31 -07001147 @Override
Winson Chung1a341002018-01-17 10:00:23 -08001148 public LauncherRootView getRootView() {
1149 return (LauncherRootView) mLauncherView;
1150 }
1151
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001152 @Override
Winson Chunga6945242014-01-08 14:04:34 -08001153 public DragLayer getDragLayer() {
1154 return mDragLayer;
1155 }
1156
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001157 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001158 return mAppsView;
1159 }
1160
Winson Chunga6945242014-01-08 14:04:34 -08001161 public Workspace getWorkspace() {
1162 return mWorkspace;
1163 }
1164
1165 public Hotseat getHotseat() {
1166 return mHotseat;
1167 }
1168
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001169 public <T extends View> T getOverviewPanel() {
Winson Chung8ae41982017-11-10 11:42:13 -08001170 return (T) mOverviewPanel;
Winson Chunga6945242014-01-08 14:04:34 -08001171 }
1172
Sunny Goyal47328fd2016-05-25 18:56:41 -07001173 public DropTargetBar getDropTargetBar() {
1174 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001175 }
1176
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001177 public LauncherAppWidgetHost getAppWidgetHost() {
1178 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001179 }
Romain Guycbb89e42009-06-08 15:52:54 -07001180
Winson Chunga9abd0e2010-10-27 17:18:37 -07001181 public LauncherModel getModel() {
1182 return mModel;
1183 }
1184
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001185 public ModelWriter getModelWriter() {
1186 return mModelWriter;
1187 }
1188
Adam Cohen79d90c52016-04-22 13:29:20 -07001189 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001190 return mSharedPrefs;
1191 }
1192
Sunny Goyalf8d56fc2018-01-31 15:18:11 -08001193 public int getOrientation() { return mOldConfig.orientation; }
Jon Miranda6bed3502017-09-19 14:43:17 -07001194
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001195 @Override
1196 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001197 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001198 super.onNewIntent(intent);
1199
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001200 boolean alreadyOnHome = hasWindowFocus() && ((intent.getFlags() &
Winson15f8b172015-08-19 11:02:39 -07001201 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1202 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001203
1204 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001205 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001206 && AbstractFloatingView.getTopOpenView(this) == null;
Winson15f8b172015-08-19 11:02:39 -07001207 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001208 boolean internalStateHandled = InternalStateHandler
Sunny Goyal8b264562018-02-03 23:00:13 -08001209 .handleNewIntent(this, intent, isStarted());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001210
Winson15f8b172015-08-19 11:02:39 -07001211 if (isActionMain) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001212 if (!internalStateHandled) {
1213 // Note: There should be at most one log per method call. This is enforced
1214 // implicitly by using if-else statements.
1215 UserEventDispatcher ued = getUserEventDispatcher();
1216 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
1217 if (topOpenView != null) {
1218 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
1219 } else if (alreadyOnHome) {
1220 Target target = newContainerTarget(mStateManager.getState().containerType);
1221 target.pageIndex = mWorkspace.getCurrentPage();
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001222 ued.logActionCommand(Action.Command.HOME_INTENT, target,
1223 newContainerTarget(ContainerType.WORKSPACE));
Sunny Goyald3864fa2017-11-14 15:06:36 -08001224 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001225
Sunny Goyald3864fa2017-11-14 15:06:36 -08001226 // In all these cases, only animate if we're already on home
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001227 AbstractFloatingView.closeAllOpenViews(this, isStarted());
Jon Mirandafeba90f2016-10-06 10:53:29 -07001228
Sunny Goyala2467272018-03-16 14:53:05 -07001229 if (!isInState(NORMAL)) {
1230 // Only change state, if not already the same. This prevents cancelling any
1231 // animations running as part of resume
1232 mStateManager.goToState(NORMAL);
1233 }
Sunny Goyald3864fa2017-11-14 15:06:36 -08001234
1235 // Reset the apps view
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001236 if (!alreadyOnHome) {
Sunny Goyal7185dd62018-03-14 17:51:49 -07001237 mAppsView.reset(isStarted() /* animate */);
Sunny Goyald3864fa2017-11-14 15:06:36 -08001238 }
1239
Sunny Goyalaad33592018-08-07 17:20:35 -07001240 if (shouldMoveToDefaultScreen && !mWorkspace.isHandlingTouch()) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001241 mWorkspace.post(mWorkspace::moveToDefaultScreen);
1242 }
1243 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001244
1245 final View v = getWindow().peekDecorView();
1246 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001247 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001248 }
1249
Adam Cohen9211d422014-10-07 18:14:53 -07001250 if (mLauncherCallbacks != null) {
Sunny Goyal85462132018-04-24 11:39:37 -07001251 mLauncherCallbacks.onHomeIntent(internalStateHandled);
Adam Cohen9211d422014-10-07 18:14:53 -07001252 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001253 }
Winson15f8b172015-08-19 11:02:39 -07001254
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001255 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001256 }
1257
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001258 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001259 public void onRestoreInstanceState(Bundle state) {
1260 super.onRestoreInstanceState(state);
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001261 mWorkspace.restoreInstanceStateForChild(mSynchronouslyBoundPage);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001262 }
1263
1264 @Override
1265 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001266 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001267 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001268
Adam Cohen21cd0022013-10-09 18:57:02 -07001269 }
Sunny Goyalea609262017-10-25 15:47:38 -07001270 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001271
1272
1273 AbstractFloatingView widgets = AbstractFloatingView
1274 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1275 if (widgets != null) {
1276 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1277 widgets.saveHierarchyState(widgetsState);
1278 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1279 } else {
1280 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1281 }
1282
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001283 // We close any open folders and shortcut containers since they will not be re-opened,
1284 // and we need to make sure this state is reflected.
1285 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001286
Sunny Goyal2100c782016-08-22 16:00:03 -07001287 if (mPendingRequestArgs != null) {
1288 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1289 }
1290 if (mPendingActivityResult != null) {
1291 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001292 }
1293
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001294 super.onSaveInstanceState(outState);
1295
Adam Cohen9211d422014-10-07 18:14:53 -07001296 if (mLauncherCallbacks != null) {
1297 mLauncherCallbacks.onSaveInstanceState(outState);
1298 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001299 }
1300
1301 @Override
1302 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001303 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001304
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001305 unregisterReceiver(mScreenOffReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001306 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001307
Tony Wickhama0068302018-04-11 16:16:11 -07001308 UiFactory.setOnTouchControllersChangedListener(this, null);
1309
Winson Chungcd2b0142011-06-08 16:02:26 -07001310 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001311 // It's possible to receive onDestroy after a new Launcher activity has
1312 // been created. In this case, don't interfere with the new Launcher.
1313 if (mModel.isCurrentCallbacks(this)) {
1314 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001315 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001316 }
Sunny Goyal623eddd2018-03-02 12:24:41 -08001317 mRotationHelper.destroy();
Sunny Goyal745bad92016-05-02 10:54:12 -07001318
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001319 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001320 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001321 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001322 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001323 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001324
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001325 TextKeyListener.getInstance().release();
Tony2917a8b2017-07-31 23:29:42 -07001326 clearPendingBinds();
Hyunyoung Song31971a32019-01-27 12:31:12 -08001327 LauncherAppState.getIDP(this).removeOnChangeListener(this);
Adam Cohen9211d422014-10-07 18:14:53 -07001328 if (mLauncherCallbacks != null) {
1329 mLauncherCallbacks.onDestroy();
1330 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001331 }
1332
Sunny Goyalae502842016-06-17 08:43:56 -07001333 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1334 return mAccessibilityDelegate;
1335 }
1336
Adam Cohena9cf38f2011-05-02 15:36:58 -07001337 public DragController getDragController() {
1338 return mDragController;
1339 }
1340
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001341 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001342 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001343 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001344 }
1345
1346 @Override
1347 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1348 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001349 try {
1350 super.startIntentSenderForResult(intent, requestCode,
1351 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1352 } catch (IntentSender.SendIntentException e) {
1353 throw new ActivityNotFoundException();
1354 }
1355 }
1356
Winson Chung70d72102011-08-12 11:24:35 -07001357 /**
1358 * Indicates that we want global search for this activity by setting the globalSearch
1359 * argument for {@link #startSearch} to true.
1360 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001361 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001362 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001363 Bundle appSearchData, boolean globalSearch) {
1364 if (appSearchData == null) {
1365 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001366 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001367 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001368
Sunny Goyal6f28e712016-09-13 14:19:29 -07001369 if (mLauncherCallbacks == null ||
1370 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1371 // Starting search from the callbacks failed. Start the default global search.
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001372 super.startSearch(initialQuery, selectInitialQuery, appSearchData, true);
Ian Parkinson047036e2014-09-01 15:40:53 +01001373 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001374
1375 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001376 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001377 }
1378
Joe Onorato9c1289c2009-08-17 11:03:03 -04001379 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001380 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001381 }
1382
Adam Cohen517a7f52014-03-01 12:12:59 -08001383 public boolean isWorkspaceLoading() {
1384 return mWorkspaceLoading;
1385 }
1386
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001387 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001388 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001389 }
1390
Sunny Goyal04a324a2017-01-20 21:08:59 -08001391 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001392 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001393 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001394
Sunny Goyal2100c782016-08-22 16:00:03 -07001395 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001396 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001397 if (LOGD) {
1398 Log.d(TAG, "Adding widget from drop");
1399 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001400 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001401 }
1402
Sunny Goyal2100c782016-08-22 16:00:03 -07001403 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001404 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1405 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1406 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001407
Adam Cohenad4e15c2013-10-17 16:21:35 -07001408 Runnable onComplete = new Runnable() {
1409 @Override
1410 public void run() {
1411 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001412 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001413 }
1414 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001415 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001416 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001417 }
1418 }
Romain Guycbb89e42009-06-08 15:52:54 -07001419
Sunny Goyalefb7e842018-10-04 15:11:00 -07001420 public void addPendingItem(PendingAddItemInfo info, int container, int screenId,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001421 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001422 info.container = container;
1423 info.screenId = screenId;
1424 if (cell != null) {
1425 info.cellX = cell[0];
1426 info.cellY = cell[1];
1427 }
1428 info.spanX = spanX;
1429 info.spanY = spanY;
1430
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001431 switch (info.itemType) {
1432 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1433 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001434 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001435 break;
1436 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001437 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001438 break;
1439 default:
1440 throw new IllegalStateException("Unknown item type: " + info.itemType);
1441 }
1442 }
1443
Adam Cohenfbba09b2011-07-18 21:43:05 -07001444 /**
1445 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001446 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001447 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001448 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1449 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001450 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1451 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1452 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001453 }
1454
Adam Cohenfbba09b2011-07-18 21:43:05 -07001455 /**
1456 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001457 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001458 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001459 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001460 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001461 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001462 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001463 // In the case where we've prebound the widget, we remove it from the DragLayer
1464 if (LOGD) {
1465 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1466 }
1467 getDragLayer().removeView(hostView);
1468
Adam Cohened66b2b2012-01-23 17:28:51 -08001469 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001470 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001471
1472 // Clear the boundWidget so that it doesn't get destroyed.
1473 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001474 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001475 // In this case, we either need to start an activity to get permission to bind
1476 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001477 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1478 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1479 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1480 this, info.componentName);
1481 } else {
1482 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1483 }
Adam Cohendd70d662012-10-04 16:53:44 -07001484 Bundle options = info.bindOptions;
1485
Sunny Goyalffe83f12014-08-14 17:39:34 -07001486 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1487 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001488 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001489 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001490 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001491 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001492 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001493 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001494 }
1495
Sunny Goyalefb7e842018-10-04 15:11:00 -07001496 FolderIcon addFolder(CellLayout layout, int container, final int screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001497 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001498 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001499 folderInfo.title = getText(R.string.folder_name);
1500
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001501 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001502 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001503
1504 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001505 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301506 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001507 // Force measure the new folder icon
1508 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1509 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001510 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511 }
Romain Guycbb89e42009-06-08 15:52:54 -07001512
Winsonc0b52fe2015-09-09 16:38:15 -07001513 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001514 * Unbinds the view for the specified item, and removes the item and all its children.
1515 *
1516 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001517 * @param itemInfo the {@link ItemInfo} for this view.
1518 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001519 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001520 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001521 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001522 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001523 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1524 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001525 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001526 } else {
1527 mWorkspace.removeWorkspaceItem(v);
1528 }
Winsonc0b52fe2015-09-09 16:38:15 -07001529 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001530 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001531 }
1532 } else if (itemInfo instanceof FolderInfo) {
1533 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001534 if (v instanceof FolderIcon) {
1535 ((FolderIcon) v).removeListeners();
1536 }
Winsonc0b52fe2015-09-09 16:38:15 -07001537 mWorkspace.removeWorkspaceItem(v);
1538 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001539 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001540 }
1541 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1542 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001543 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001544 if (deleteFromDb) {
Tony Wickham6a71a5b2018-08-21 11:40:23 -07001545 getModelWriter().deleteWidgetInfo(widgetInfo, getAppWidgetHost());
Winsonc0b52fe2015-09-09 16:38:15 -07001546 }
1547 } else {
1548 return false;
1549 }
1550 return true;
1551 }
1552
Tony Wickham6a71a5b2018-08-21 11:40:23 -07001553
Winsonc0b52fe2015-09-09 16:38:15 -07001554
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001555 @Override
1556 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001557 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001558 }
1559
Joe Onorato88ec0992009-11-19 13:16:06 -08001560 @Override
1561 public void onBackPressed() {
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08001562 if (finishAutoCancelActionMode()) {
1563 return;
1564 }
Adam Cohen9211d422014-10-07 18:14:53 -07001565 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1566 return;
1567 }
1568
Sunny Goyal45478022015-06-08 16:52:41 -07001569 if (mDragController.isDragging()) {
1570 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001571 return;
1572 }
1573
Jon Mirandafeba90f2016-10-06 10:53:29 -07001574 // Note: There should be at most one log per method call. This is enforced implicitly
1575 // by using if-else statements.
1576 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001577 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
Tony Wickham52c1b662018-05-21 13:13:58 -07001578 if (topView != null && topView.onBackPressed()) {
1579 // Handled by the floating view.
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001580 } else {
Sunny Goyale39690b2018-08-02 13:35:07 -07001581 mStateManager.getState().onBackPressed(this);
Michael Jurkaaf442092010-06-10 17:01:57 -07001582 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001583 }
1584
Sunny Goyalfe770c92016-09-27 14:38:58 -07001585 @TargetApi(Build.VERSION_CODES.M)
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001586 @Override
Jon Miranda73c27ec2018-05-16 18:06:23 -07001587 public ActivityOptions getActivityLaunchOptions(View v) {
1588 return mAppTransitionManager.getActivityLaunchOptions(this, v);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001589 }
1590
Tony Wickham3a6746a2018-03-29 09:39:56 -07001591 public LauncherAppTransitionManager getAppTransitionManager() {
1592 return mAppTransitionManager;
1593 }
1594
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001595 @TargetApi(Build.VERSION_CODES.M)
1596 @Override
1597 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
1598 // Due to legacy reasons, direct call shortcuts require Launchers to have the
1599 // corresponding permission. Show the appropriate permission prompt if that
1600 // is the case.
1601 if (intent.getComponent() == null
1602 && Intent.ACTION_CALL.equals(intent.getAction())
1603 && checkSelfPermission(android.Manifest.permission.CALL_PHONE) !=
1604 PackageManager.PERMISSION_GRANTED) {
1605
1606 setWaitingForResult(PendingRequestArgs
1607 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
1608 requestPermissions(new String[]{android.Manifest.permission.CALL_PHONE},
1609 REQUEST_PERMISSION_CALL_PHONE);
1610 return true;
1611 } else {
1612 return false;
1613 }
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001614 }
1615
Hyunyoung Song46d07f72018-05-22 15:41:25 -07001616 @Override
Hyunyoung Songfc007472018-10-25 14:09:50 -07001617 public int getCurrentState() {
1618 if(mStateManager.getState() == LauncherState.ALL_APPS) {
1619 return StatsLogUtils.LAUNCHER_STATE_ALLAPPS;
1620 } else if (mStateManager.getState() == LauncherState.OVERVIEW) {
1621 return StatsLogUtils.LAUNCHER_STATE_OVERVIEW;
1622 }
Hyunyoung Song0ae38882018-11-05 14:45:19 -08001623 return StatsLogUtils.LAUNCHER_STATE_HOME;
Hyunyoung Songfc007472018-10-25 14:09:50 -07001624 }
1625
1626 @Override
Hyunyoung Song46d07f72018-05-22 15:41:25 -07001627 public void modifyUserEvent(LauncherLogProto.LauncherEvent event) {
1628 if (event.srcTarget != null && event.srcTarget.length > 0 &&
1629 event.srcTarget[1].containerType == ContainerType.PREDICTION) {
1630 Target[] targets = new Target[3];
1631 targets[0] = event.srcTarget[0];
1632 targets[1] = event.srcTarget[1];
1633 targets[2] = newTarget(Target.Type.CONTAINER);
1634 event.srcTarget = targets;
1635 LauncherState state = mStateManager.getState();
1636 if (state == LauncherState.ALL_APPS) {
1637 event.srcTarget[2].containerType = ContainerType.ALLAPPS;
1638 } else if (state == LauncherState.OVERVIEW) {
1639 event.srcTarget[2].containerType = ContainerType.TASKSWITCHER;
1640 }
1641 }
1642 }
1643
Sunny Goyala7ce1662016-05-31 15:01:35 -07001644 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Hyunyoung Song1241e612018-05-15 21:46:51 -07001645 boolean success = super.startActivitySafely(v, intent, item);
1646 if (success && v instanceof BubbleTextView) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001647 // This is set to the view that launched the activity that navigated the user away
1648 // from launcher. Since there is no callback for when the activity has finished
1649 // launching, enable the press state and keep this reference to reset the press
1650 // state when we return to launcher.
1651 BubbleTextView btv = (BubbleTextView) v;
1652 btv.setStayPressed(true);
1653 setOnResumeCallback(btv);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001654 }
Hyunyoung Song1241e612018-05-15 21:46:51 -07001655 return success;
Michael Jurka86a720e2012-05-09 11:23:23 -07001656 }
1657
Winson Chung3d503fb2011-07-13 17:25:49 -07001658 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001659 // TODO: Remove this method
Sunny Goyal876e4622018-11-02 13:50:40 -07001660 return mHotseat != null && (layout == mHotseat);
Winson Chung3d503fb2011-07-13 17:25:49 -07001661 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001662
Winson Chung3d503fb2011-07-13 17:25:49 -07001663 /**
1664 * Returns the CellLayout of the specified container at the specified screen.
1665 */
Sunny Goyalefb7e842018-10-04 15:11:00 -07001666 public CellLayout getCellLayout(int container, int screenId) {
Sunny Goyal876e4622018-11-02 13:50:40 -07001667 return (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1668 ? mHotseat : mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08001669 }
1670
Michael Jurkae326f182011-11-21 14:05:46 -08001671 @Override
1672 public void onTrimMemory(int level) {
1673 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07001674 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
1675 // The widget preview db can result in holding onto over
1676 // 3MB of memory for caching which isn't necessary.
1677 SQLiteDatabase.releaseMemory();
1678
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08001679 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07001680 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08001681 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01001682 if (mLauncherCallbacks != null) {
1683 mLauncherCallbacks.onTrimMemory(level);
1684 }
Winson Chung62a70be2018-02-23 15:10:05 -08001685 UiFactory.onTrimMemory(this, level);
Michael Jurkae326f182011-11-21 14:05:46 -08001686 }
1687
Michael Jurkad7c28052012-04-27 15:43:36 -07001688 @Override
1689 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07001690 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07001691 final List<CharSequence> text = event.getText();
1692 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07001693 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001694 // TODO: When can workspace be null?
1695 text.add(mWorkspace == null
1696 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07001697 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07001698 return result;
1699 }
1700
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001701 public void setOnResumeCallback(OnResumeCallback callback) {
1702 if (mOnResumeCallback != null) {
1703 mOnResumeCallback.onLauncherResume();
1704 }
1705 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07001706 }
1707
Michael Jurka7607c2f2013-04-03 14:33:19 -07001708 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001709 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001710 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07001711 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001712 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07001713 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001714 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07001715 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07001716 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07001717 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001718 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001719
Joe Onorato9c1289c2009-08-17 11:03:03 -04001720 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07001721 * Clear any pending bind callbacks. This is called when is loader is planning to
1722 * perform a full rebind from scratch.
1723 */
1724 @Override
1725 public void clearPendingBinds() {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001726 if (mPendingExecutor != null) {
1727 mPendingExecutor.markCompleted();
1728 mPendingExecutor = null;
1729 }
1730 }
1731
1732 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001733 * Refreshes the shortcuts shown on the workspace.
1734 *
1735 * Implementation of the method from LauncherModel.Callbacks.
1736 */
1737 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001738 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001739 // Floating panels (except the full widget sheet) are associated with individual icons. If
1740 // we are starting a fresh bind, close all such panels as all the icons are about
1741 // to go away.
1742 AbstractFloatingView.closeOpenViews(this, true,
Sunny Goyal7ede6112017-12-05 15:11:21 -08001743 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_REBIND_SAFE);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08001744
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001745 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08001746
Winson Chungd64d1762013-08-20 14:37:16 -07001747 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07001748 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07001749 mWorkspace.removeAllWorkspaceScreens();
Sunny Goyalc11fac32018-02-27 19:20:15 -08001750 mAppWidgetHost.clearViews();
Winson Chungd64d1762013-08-20 14:37:16 -07001751
Winson Chung3d503fb2011-07-13 17:25:49 -07001752 if (mHotseat != null) {
Sunny Goyal11e96492018-03-23 17:06:00 -07001753 mHotseat.resetLayout(mDeviceProfile.isVerticalBarLayout());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001754 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001755 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001756 }
1757
Adam Cohendcd297f2013-06-18 13:13:40 -07001758 @Override
Sunny Goyalefb7e842018-10-04 15:11:00 -07001759 public void bindScreens(IntArray orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001760 // Make sure the first screen is always at the start.
Ryan Lothianfa530cd2018-10-12 14:14:16 -04001761 if (FeatureFlags.QSB_ON_FIRST_SCREEN.get() &&
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001762 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalefb7e842018-10-04 15:11:00 -07001763 orderedScreenIds.removeValue(Workspace.FIRST_SCREEN_ID);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001764 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Ryan Lothianfa530cd2018-10-12 14:14:16 -04001765 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN.get()
Ryan Lothian688e9892018-10-03 13:32:01 -04001766 && orderedScreenIds.isEmpty()) {
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001767 // If there are no screens, we need to have an empty screen
1768 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07001769 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001770 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07001771
Winsonc7d2e832016-07-28 12:24:55 -07001772 // After we have added all the screens, if the wallpaper was locked to the default state,
1773 // then notify to indicate that it can be released and a proper wallpaper offset can be
1774 // computed before the next layout
1775 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07001776 }
1777
Sunny Goyalefb7e842018-10-04 15:11:00 -07001778 private void bindAddScreens(IntArray orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001779 int count = orderedScreenIds.size();
1780 for (int i = 0; i < count; i++) {
Sunny Goyalefb7e842018-10-04 15:11:00 -07001781 int screenId = orderedScreenIds.get(i);
Ryan Lothianfa530cd2018-10-12 14:14:16 -04001782 if (!FeatureFlags.QSB_ON_FIRST_SCREEN.get() || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001783 // No need to bind the first screen, as its always bound.
1784 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
1785 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001786 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001787 }
1788
Sunny Goyalb23980c2017-08-17 07:45:25 -07001789 @Override
Tony Wickham6a71a5b2018-08-21 11:40:23 -07001790 public void preAddApps() {
1791 // If there's an undo snackbar, force it to complete to ensure empty screens are removed
1792 // before trying to add new items.
1793 mModelWriter.commitDelete();
1794 AbstractFloatingView snackbar = AbstractFloatingView.getOpenView(this, TYPE_SNACKBAR);
1795 if (snackbar != null) {
1796 snackbar.post(() -> snackbar.close(true));
1797 }
1798 }
1799
1800 @Override
Sunny Goyalefb7e842018-10-04 15:11:00 -07001801 public void bindAppsAdded(IntArray newScreens, ArrayList<ItemInfo> addNotAnimated,
Sunny Goyal29947f02017-12-18 13:49:44 -08001802 ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07001803 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08001804 if (newScreens != null) {
1805 bindAddScreens(newScreens);
1806 }
Winson Chungd64d1762013-08-20 14:37:16 -07001807
1808 // We add the items without animation on non-visible pages, and with
1809 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08001810 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001811 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001812 }
Adam Cohen76a47a12014-02-05 11:47:43 -08001813 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001814 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07001815 }
Winson Chungc58497e2013-09-03 17:48:37 -07001816
Winson Chung87412982013-10-03 18:34:14 -07001817 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07001818 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001819 }
1820
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001821 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001822 * Bind the items start-end from the list.
1823 *
1824 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001825 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07001826 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07001827 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001828 // Get the list of added items and intersect them with the set of items here
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001829 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07001830 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001831 Workspace workspace = mWorkspace;
Sunny Goyalefb7e842018-10-04 15:11:00 -07001832 int newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07001833 int end = items.size();
1834 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001835 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07001836
1837 // Short circuit if we are loading dock items for a configuration which has no dock
1838 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
1839 mHotseat == null) {
1840 continue;
1841 }
1842
Sunny Goyal639e9062015-08-19 19:17:06 -07001843 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001844 switch (item.itemType) {
1845 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1846 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08001847 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001848 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07001849 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001850 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001851 }
1852 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07001853 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001854 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001855 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001856 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001857 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07001858 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1859 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07001860 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07001861 if (view == null) {
1862 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001863 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08001864 break;
1865 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001866 default:
1867 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001868 }
Sunny Goyal639e9062015-08-19 19:17:06 -07001869
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001870 /*
1871 * Remove colliding items.
1872 */
1873 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1874 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
1875 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
1876 View v = cl.getChildAt(item.cellX, item.cellY);
1877 Object tag = v.getTag();
1878 String desc = "Collision while binding workspace item: " + item
1879 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08001880 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001881 throw (new RuntimeException(desc));
1882 } else {
1883 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001884 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001885 continue;
1886 }
1887 }
1888 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05301889 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07001890 if (animateIcons) {
1891 // Animate all the applications up now
1892 view.setAlpha(0f);
1893 view.setScaleX(0f);
1894 view.setScaleY(0f);
1895 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08001896 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07001897 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001898 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07001899
Sunny Goyalf0b6db72018-08-13 16:10:14 -07001900 // Animate to the correct page
1901 if (animateIcons && newItemsScreenId > -1) {
1902 AnimatorSet anim = new AnimatorSet();
1903 anim.playTogether(bounceAnims);
Jon Miranda64eb8ea2018-03-29 11:43:58 -07001904
Sunny Goyalefb7e842018-10-04 15:11:00 -07001905 int currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyalf0b6db72018-08-13 16:10:14 -07001906 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
1907 final Runnable startBounceAnimRunnable = anim::start;
1908
1909 if (newItemsScreenId != currentScreenId) {
1910 // We post the animation slightly delayed to prevent slowdowns
1911 // when we are loading right after we return to launcher.
1912 mWorkspace.postDelayed(new Runnable() {
1913 public void run() {
1914 if (mWorkspace != null) {
1915 AbstractFloatingView.closeAllOpenViews(Launcher.this, false);
1916
1917 mWorkspace.snapToPage(newScreenIndex);
1918 mWorkspace.postDelayed(startBounceAnimRunnable,
1919 NEW_APPS_ANIMATION_DELAY);
Winson Chung94d67682013-09-25 16:29:40 -07001920 }
Sunny Goyalf0b6db72018-08-13 16:10:14 -07001921 }
1922 }, NEW_APPS_PAGE_MOVE_DELAY);
1923 } else {
1924 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07001925 }
Winson Chung64359a52013-07-08 17:17:08 -07001926 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001927 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001928 }
1929
Joe Onorato9c1289c2009-08-17 11:03:03 -04001930 /**
1931 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001932 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07001933 public void bindAppWidget(LauncherAppWidgetInfo item) {
1934 View view = inflateAppWidget(item);
1935 if (view != null) {
1936 mWorkspace.addInScreen(view, item);
1937 mWorkspace.requestLayout();
1938 }
1939 }
1940
1941 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001942 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05301943 PendingAppWidgetHostView view =
1944 new PendingAppWidgetHostView(this, item, mIconCache, true);
1945 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07001946 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001947 }
1948
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001949 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001950
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001951 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08001952
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001953 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
1954 // If the provider is not ready, bind as a pending widget.
1955 appWidgetInfo = null;
1956 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
1957 // The widget id is not valid. Try to find the widget based on the provider info.
1958 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
1959 } else {
1960 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
1961 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07001962
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001963 // If the provider is ready, but the width is not yet restored, try to restore it.
1964 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
1965 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07001966 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001967 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
1968 + " belongs to component " + item.providerName
1969 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001970 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07001971 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07001972 }
Sunny Goyalff572272014-07-23 13:58:07 -07001973
Sunny Goyal84b4adc2015-08-12 15:12:16 -07001974 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001975 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07001976 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
1977 // Id has not been allocated yet. Allocate a new id.
1978 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
1979 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07001980
Sunny Goyald478c832016-04-01 12:04:16 -07001981 // Also try to bind the widget. If the bind fails, the user will be shown
1982 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08001983 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07001984 pendingInfo.spanX = item.spanX;
1985 pendingInfo.spanY = item.spanY;
1986 pendingInfo.minSpanX = item.minSpanX;
1987 pendingInfo.minSpanY = item.minSpanY;
1988 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07001989
1990 boolean isDirectConfig =
1991 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
1992 if (isDirectConfig && item.bindOptions != null) {
1993 Bundle newOptions = item.bindOptions.getExtras();
1994 if (options != null) {
1995 newOptions.putAll(options);
1996 }
1997 options = newOptions;
1998 }
Sunny Goyald478c832016-04-01 12:04:16 -07001999 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2000 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002001
Sunny Goyal86df1382016-08-10 15:03:22 -07002002 // We tried to bind once. If we were not able to bind, we would need to
2003 // go through the permission dialog, which means we cannot skip the config
2004 // activity.
2005 item.bindOptions = null;
2006 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2007
Sunny Goyald478c832016-04-01 12:04:16 -07002008 // Bind succeeded
2009 if (success) {
2010 // If the widget has a configure activity, it is still needs to set it up,
2011 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002012 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002013 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2014 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002015 }
Sunny Goyald478c832016-04-01 12:04:16 -07002016
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002017 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002018 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002019 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002020 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002021 // The widget was marked as UI not ready, but there is no configure activity to
2022 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002023 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002024 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002025 }
Sunny Goyalff572272014-07-23 13:58:07 -07002026 }
2027
Sunny Goyald5462aa2016-11-22 16:52:39 +05302028 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002029 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002030 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002031 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002032 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Tony Wickham6a71a5b2018-08-21 11:40:23 -07002033 getModelWriter().deleteWidgetInfo(item, getAppWidgetHost());
Sunny Goyalb23980c2017-08-17 07:45:25 -07002034 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002035 }
2036
Sunny Goyal233ee962015-08-03 13:05:01 -07002037 item.minSpanX = appWidgetInfo.minSpanX;
2038 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302039 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002040 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302041 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002042 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302043 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002044
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002045 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002046 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002047 }
2048
Sunny Goyalff572272014-07-23 13:58:07 -07002049 /**
2050 * Restores a pending widget.
2051 *
2052 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002053 */
Sunny Goyald478c832016-04-01 12:04:16 -07002054 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002055 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2056 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2057 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002058 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002059 }
2060
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002061 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002062 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002063 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2064 info.pendingItemInfo = null;
2065 }
Sunny Goyalff572272014-07-23 13:58:07 -07002066
Sunny Goyalba47faa2017-10-04 16:50:57 -07002067 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002068 view.reInflate();
Sunny Goyalba47faa2017-10-04 16:50:57 -07002069 }
2070
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002071 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002072 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002073 }
2074
Adam Cohen1462de32012-07-24 22:34:36 -07002075 public void onPageBoundSynchronously(int page) {
Sunny Goyalf4aceab2017-12-06 16:16:31 -08002076 mSynchronouslyBoundPage = page;
Adam Cohen1462de32012-07-24 22:34:36 -07002077 }
2078
Sunny Goyal527c7d32015-08-28 15:19:36 -07002079 @Override
2080 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2081 if (mPendingExecutor != null) {
2082 mPendingExecutor.markCompleted();
2083 }
2084 mPendingExecutor = executor;
Sunny Goyal60180b02018-02-07 12:56:30 -08002085 if (!isInState(ALL_APPS)) {
2086 mAppsView.getAppsStore().setDeferUpdates(true);
2087 mPendingExecutor.execute(() -> mAppsView.getAppsStore().setDeferUpdates(false));
2088 }
2089
Sunny Goyal527c7d32015-08-28 15:19:36 -07002090 executor.attachTo(this);
2091 }
2092
2093 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2094 if (mPendingExecutor == executor) {
2095 mPendingExecutor = null;
2096 }
2097 }
2098
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002099 @Override
2100 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
Sunny Goyal6001ea22018-05-10 16:31:00 -07002101 AlphaProperty property = mDragLayer.getAlphaProperty(ALPHA_INDEX_LAUNCHER_LOAD);
2102 if (property.getValue() < 1) {
2103 ObjectAnimator anim = ObjectAnimator.ofFloat(property, MultiValueAlpha.VALUE, 1);
2104 if (executor != null) {
2105 anim.addListener(new AnimatorListenerAdapter() {
2106 @Override
2107 public void onAnimationEnd(Animator animation) {
2108 executor.onLoadAnimationCompleted();
2109 }
2110 });
2111 }
2112 anim.start();
Sunny Goyal29947f02017-12-18 13:49:44 -08002113 } else if (executor != null) {
2114 executor.onLoadAnimationCompleted();
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002115 }
2116 }
2117
Joe Onorato9c1289c2009-08-17 11:03:03 -04002118 /**
2119 * Callback saying that there aren't any more items to bind.
2120 *
2121 * Implementation of the method from LauncherModel.Callbacks.
2122 */
Tonyf80e8932018-12-21 11:58:04 -08002123 public void finishBindingItems(int pageBoundFirst) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002124 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002125 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002126
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002127 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002128
Sunny Goyal2100c782016-08-22 16:00:03 -07002129 if (mPendingActivityResult != null) {
2130 handleActivityResult(mPendingActivityResult.requestCode,
2131 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2132 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002133 }
2134
Sunny Goyala474a9b2017-05-04 16:47:11 -07002135 InstallShortcutReceiver.disableAndFlushInstallQueue(
2136 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002137
Tonyf80e8932018-12-21 11:58:04 -08002138 // When undoing the removal of the last item on a page, return to that page.
2139 mWorkspace.setCurrentPage(pageBoundFirst);
Tony Wickham6a71a5b2018-08-21 11:40:23 -07002140
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002141 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002142 }
2143
Winson Chunga2413752012-04-03 14:22:34 -07002144 private boolean canRunNewAppsAnimation() {
2145 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002146 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002147 }
2148
Winson Chungc9168342013-06-26 14:54:55 -07002149 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002150 ValueAnimator bounceAnim = new PropertyListBuilder().alpha(1).scale(1).build(v)
2151 .setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
Winson Chungc9168342013-06-26 14:54:55 -07002152 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002153 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002154 return bounceAnim;
2155 }
2156
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002157 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002158 * Add the icons for all apps.
2159 *
2160 * Implementation of the method from LauncherModel.Callbacks.
2161 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002162 public void bindAllApplications(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002163 mAppsView.getAppsStore().setApps(apps);
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002164
Adam Cohen9211d422014-10-07 18:14:53 -07002165 if (mLauncherCallbacks != null) {
2166 mLauncherCallbacks.bindAllApplications(apps);
2167 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002168 }
2169
2170 /**
Zak Cohen658c67a2018-10-19 14:21:05 -07002171 * Copies LauncherModel's map of activities to shortcut counts to Launcher's. This is necessary
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002172 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2173 */
2174 @Override
Zak Cohen658c67a2018-10-19 14:21:05 -07002175 public void bindDeepShortcutMap(HashMap<ComponentKey, Integer> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002176 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002177 }
2178
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002179 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002180 * A package was updated.
2181 *
2182 * Implementation of the method from LauncherModel.Callbacks.
2183 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002184 @Override
2185 public void bindAppsAddedOrUpdated(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002186 mAppsView.getAppsStore().addOrUpdateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002187 }
2188
Sunny Goyal4390ace2014-10-13 11:33:11 -07002189 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002190 public void bindPromiseAppProgressUpdated(PromiseAppInfo app) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002191 mAppsView.getAppsStore().updatePromiseAppProgress(app);
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002192 }
2193
2194 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002195 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002196 mWorkspace.widgetsRestored(widgets);
2197 }
2198
Joe Onorato9c1289c2009-08-17 11:03:03 -04002199 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002200 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002201 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002202 *
2203 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002204 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002205 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002206 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002207 if (!updated.isEmpty()) {
2208 mWorkspace.updateShortcuts(updated);
2209 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002210 }
2211
2212 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002213 * Update the state of a package, typically related to install state.
2214 *
2215 * Implementation of the method from LauncherModel.Callbacks.
2216 */
Sunny Goyale755d462014-07-22 13:48:29 -07002217 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002218 public void bindRestoreItemsChange(HashSet<ItemInfo> updates) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07002219 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07002220 }
2221
2222 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002223 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07002224 * in addition to specific applications to remove, the reason being that
2225 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002226 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07002227 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002228 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07002229 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002230 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002231 mWorkspace.removeItemsByMatcher(matcher);
2232 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002233 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002234
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002235 @Override
2236 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002237 mAppsView.getAppsStore().removeApps(appInfos);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002238 }
Joe Onoratobe386092009-11-17 17:32:16 -08002239
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002240 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002241 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
2242 mPopupDataProvider.setAllWidgets(allWidgets);
Tony Wickham26b17462017-03-20 17:12:24 -07002243 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
2244 if (topView != null) {
2245 topView.onWidgetsBound();
2246 }
2247 }
2248
Tony Wickham86222d22017-03-29 15:30:43 -07002249 /**
2250 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
2251 * refreshes the widgets and shortcuts associated with the given package/user
2252 */
2253 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07002254 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002255 }
2256
Winson Chung80baf5a2010-08-09 16:03:15 -07002257 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002258 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08002259 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002260 @Override
2261 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
2262 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002263
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002264 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
2265 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07002266 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002267 writer.println(prefix + " Homescreen " + i);
2268
2269 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
2270 for (int j = 0; j < layout.getChildCount(); j++) {
2271 Object tag = layout.getChildAt(j).getTag();
2272 if (tag != null) {
2273 writer.println(prefix + " " + tag.toString());
2274 }
2275 }
2276 }
2277
2278 writer.println(prefix + " Hotseat");
Sunny Goyal876e4622018-11-02 13:50:40 -07002279 ViewGroup layout = mHotseat.getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07002280 for (int j = 0; j < layout.getChildCount(); j++) {
2281 Object tag = layout.getChildAt(j).getTag();
2282 if (tag != null) {
2283 writer.println(prefix + " " + tag.toString());
2284 }
Adam Cohen4caf2982013-08-20 18:54:31 -07002285 }
Sunny Goyala1365452015-10-01 15:46:24 -07002286 }
2287
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002288 writer.println(prefix + "Misc:");
2289 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
2290 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
2291 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
Sunny Goyale43d00d2018-05-14 14:23:18 -07002292 writer.println(" mRotationHelper: " + mRotationHelper);
Sunny Goyalcb2c5052018-10-15 12:30:29 -07002293 // Extra logging for b/116853349
2294 mDragLayer.dumpAlpha(writer);
Sunny Goyale43d00d2018-05-14 14:23:18 -07002295 dumpMisc(writer);
Sunny Goyal2bba1902018-02-27 12:20:50 -08002296
2297 try {
2298 FileLog.flushAll(writer);
2299 } catch (Exception e) {
2300 // Ignore
2301 }
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002302
2303 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002304
Adam Cohen9211d422014-10-07 18:14:53 -07002305 if (mLauncherCallbacks != null) {
2306 mLauncherCallbacks.dump(prefix, fd, writer, args);
2307 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002308 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002309
Sunny Goyal66b24572016-09-21 15:57:55 -07002310 @Override
2311 @TargetApi(Build.VERSION_CODES.N)
2312 public void onProvideKeyboardShortcuts(
2313 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
2314
2315 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002316 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002317 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
2318 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
Vadim Tryshev74c261c2018-06-12 13:44:43 -07002319 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.widget_button_text),
2320 KeyEvent.KEYCODE_W, KeyEvent.META_CTRL_ON));
Sunny Goyal66b24572016-09-21 15:57:55 -07002321 }
Vadim Tryshev3455f8d2018-05-11 20:02:20 -07002322 final View currentFocus = getCurrentFocus();
2323 if (currentFocus != null) {
2324 if (new CustomActionsPopup(this, currentFocus).canShow()) {
2325 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
2326 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
2327 }
2328 if (currentFocus.getTag() instanceof ItemInfo
2329 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
2330 shortcutInfos.add(new KeyboardShortcutInfo(
2331 getString(R.string.shortcuts_menu_with_notifications_description),
2332 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
2333 }
Sunny Goyal66b24572016-09-21 15:57:55 -07002334 }
2335 if (!shortcutInfos.isEmpty()) {
2336 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
2337 }
2338
2339 super.onProvideKeyboardShortcuts(data, menu, deviceId);
2340 }
2341
2342 @Override
2343 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
2344 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
2345 switch (keyCode) {
2346 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002347 if (isInState(NORMAL)) {
2348 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07002349 return true;
2350 }
2351 break;
2352 case KeyEvent.KEYCODE_S: {
2353 View focusedView = getCurrentFocus();
2354 if (focusedView instanceof BubbleTextView
2355 && focusedView.getTag() instanceof ItemInfo
2356 && mAccessibilityDelegate.performAction(focusedView,
2357 (ItemInfo) focusedView.getTag(),
2358 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal00ac9202017-11-09 15:24:06 -08002359 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07002360 return true;
2361 }
2362 break;
2363 }
2364 case KeyEvent.KEYCODE_O:
2365 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
2366 return true;
2367 }
2368 break;
Vadim Tryshev74c261c2018-06-12 13:44:43 -07002369 case KeyEvent.KEYCODE_W:
2370 if (isInState(NORMAL)) {
2371 OptionsPopupView.openWidgets(this);
2372 return true;
2373 }
2374 break;
Sunny Goyal66b24572016-09-21 15:57:55 -07002375 }
2376 }
2377 return super.onKeyShortcut(keyCode, event);
2378 }
2379
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002380 @Override
2381 public boolean onKeyUp(int keyCode, KeyEvent event) {
2382 if (keyCode == KeyEvent.KEYCODE_MENU) {
2383 // KEYCODE_MENU is sent by some tests, for example
2384 // LauncherJankTests#testWidgetsContainerFling. Don't just remove its handling.
2385 if (!mDragController.isDragging() && !mWorkspace.isSwitchingState() &&
2386 isInState(NORMAL)) {
2387 // Close any open floating views.
2388 AbstractFloatingView.closeAllOpenViews(this);
2389
2390 // Setting the touch point to (-1, -1) will show the options popup in the center of
2391 // the screen.
Sunny Goyal2fd7a8b2018-03-30 17:10:13 -07002392 OptionsPopupView.showDefaultOptions(this, -1, -1);
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002393 }
2394 return true;
2395 }
2396 return super.onKeyUp(keyCode, event);
2397 }
2398
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002399 public static Launcher getLauncher(Context context) {
Sunny Goyal87b5eb62018-07-03 15:53:39 -07002400 return (Launcher) fromContext(context);
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002401 }
2402
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002403 /**
Sunny Goyalab770a12018-11-14 15:17:26 -08002404 * Just a wrapper around the type cast to allow easier tracking of calls.
2405 */
2406 public static <T extends Launcher> T cast(ActivityContext activityContext) {
2407 return (T) activityContext;
2408 }
2409
2410 /**
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002411 * Callback for listening for onResume
2412 */
2413 public interface OnResumeCallback {
2414
2415 void onLauncherResume();
2416 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002417}