blob: c14512a7ece5dbc78eb64e57136327569669f1ba [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
Sunny Goyal651077b2014-06-30 14:15:31 -070077import com.android.launcher3.DropTarget.DragObject;
Sunny Goyalae502842016-06-17 08:43:56 -070078import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070079import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070080import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal7ede6112017-12-05 15:11:21 -080081import com.android.launcher3.allapps.DiscoveryBounce;
Sunny Goyalf0b6db72018-08-13 16:10:14 -070082import com.android.launcher3.anim.PropertyListBuilder;
Sunny Goyalffe83f12014-08-14 17:39:34 -070083import com.android.launcher3.compat.AppWidgetManagerCompat;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -070084import com.android.launcher3.compat.LauncherAppsCompatVO;
Tony Wickhame0c33232016-02-08 11:37:04 -080085import com.android.launcher3.config.FeatureFlags;
vadimt00d42552018-12-11 17:59:36 -080086import com.android.launcher3.dot.DotInfo;
Vadim Tryshevfedca432015-08-19 17:55:02 -070087import com.android.launcher3.dragndrop.DragController;
88import com.android.launcher3.dragndrop.DragLayer;
89import com.android.launcher3.dragndrop.DragView;
Jon Mirandaaeb4dd02018-07-11 14:16:23 -070090import com.android.launcher3.folder.Folder;
Sunny Goyal26119432016-02-18 22:09:23 +000091import com.android.launcher3.folder.FolderIcon;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070092import com.android.launcher3.folder.FolderIconPreviewVerifier;
Sunny Goyalf840f102018-09-21 14:41:05 -070093import com.android.launcher3.icons.IconCache;
Sunny Goyal66b24572016-09-21 15:57:55 -070094import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -070095import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -070096import com.android.launcher3.logging.FileLog;
Hyunyoung Songfc007472018-10-25 14:09:50 -070097import com.android.launcher3.logging.StatsLogUtils;
Hyunyoung Songaa953652016-04-19 18:30:24 -070098import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Song46d07f72018-05-22 15:41:25 -070099import com.android.launcher3.logging.UserEventDispatcher.UserEventDelegate;
Sunny Goyal369212a2019-03-26 15:03:57 -0700100import com.android.launcher3.model.AppLaunchTracker;
Sunny Goyala535ae42017-02-27 10:07:13 -0800101import com.android.launcher3.model.ModelWriter;
Sunny Goyala535ae42017-02-27 10:07:13 -0800102import com.android.launcher3.notification.NotificationListener;
Tony Wickham540913e2017-01-23 11:47:51 -0800103import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -0800104import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700105import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyalf8088ee2017-11-10 14:52:00 -0800106import com.android.launcher3.states.InternalStateHandler;
Sunny Goyal623eddd2018-03-02 12:24:41 -0800107import com.android.launcher3.states.RotationHelper;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800108import com.android.launcher3.touch.ItemClickHandler;
Sunny Goyala5ace712017-11-15 17:12:51 -0800109import com.android.launcher3.uioverrides.UiFactory;
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700110import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530111import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
112import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
Sunny Goyalaeb16432017-10-16 11:46:41 -0700113import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyal2100c782016-08-22 16:00:03 -0700114import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700115import com.android.launcher3.util.ComponentKey;
Sunny Goyalefb7e842018-10-04 15:11:00 -0700116import com.android.launcher3.util.IntArray;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700117import com.android.launcher3.util.ItemInfoMatcher;
Sunny Goyal6001ea22018-05-10 16:31:00 -0700118import com.android.launcher3.util.MultiValueAlpha;
119import com.android.launcher3.util.MultiValueAlpha.AlphaProperty;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700120import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800121import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700122import com.android.launcher3.util.PendingRequestArgs;
vadimt00d42552018-12-11 17:59:36 -0800123import com.android.launcher3.util.RaceConditionTracker;
Sunny Goyal8392c822017-06-20 10:03:56 -0700124import com.android.launcher3.util.SystemUiController;
Mario Bertschlera6936942017-05-31 14:48:19 -0700125import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700126import com.android.launcher3.util.Thunk;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700127import com.android.launcher3.util.TraceHelper;
Sunny Goyal326403e2017-10-02 12:45:10 -0700128import com.android.launcher3.util.UiThreadHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700129import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyalab770a12018-11-14 15:17:26 -0800130import com.android.launcher3.views.ActivityContext;
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800131import com.android.launcher3.views.OptionsPopupView;
Sunny Goyal29947f02017-12-18 13:49:44 -0800132import com.android.launcher3.widget.LauncherAppWidgetHostView;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800133import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700134import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal29947f02017-12-18 13:49:44 -0800135import com.android.launcher3.widget.PendingAppWidgetHostView;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800136import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700137import com.android.launcher3.widget.WidgetHostViewLoader;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700138import com.android.launcher3.widget.WidgetListRowEntry;
139import com.android.launcher3.widget.WidgetsFullSheet;
Sunny Goyal952e63d2017-08-16 04:59:08 -0700140import com.android.launcher3.widget.custom.CustomWidgetParser;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700141
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700142import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700143import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700144import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700145import java.util.Collection;
Zak Cohen658c67a2018-10-19 14:21:05 -0700146import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700147import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700148import java.util.List;
Sunny Goyalaae6fbb2019-01-31 16:05:58 -0800149import java.util.function.Predicate;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700150
Sunny Goyal8c48d8b2019-01-25 15:10:18 -0800151import androidx.annotation.Nullable;
152
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153/**
154 * Default launcher application.
155 */
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700156public class Launcher extends BaseDraggingActivity implements LauncherExterns,
Hyunyoung Song31971a32019-01-27 12:31:12 -0800157 LauncherModel.Callbacks, LauncherProviderChangeListener, UserEventDelegate,
158 InvariantDeviceProfile.OnIDPChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700159 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700160 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161
Winson Chunga2413752012-04-03 14:22:34 -0700162 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700163
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700165 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700166
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700167 private static final int REQUEST_PICK_APPWIDGET = 9;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168
Michael Jurka8b805b12012-04-18 14:23:14 -0700169 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800170 public static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
171 public static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700172
Jon Mirandac476d6e2017-05-04 16:30:01 -0700173 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700174
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700175 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
176
Mathew Inwood876a8462013-06-14 14:12:41 +0100177 /**
178 * IntentStarter uses request codes starting with this. This must be greater than all activity
179 * request codes used internally.
180 */
181 protected static final int REQUEST_LAST = 100;
182
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 // Type: int
184 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700185 // Type: int
186 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700187 // Type: PendingRequestArgs
188 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
189 // Type: ActivityResultInfo
190 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700191 // Type: SparseArray<Parcelable>
192 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
vadimt00d42552018-12-11 17:59:36 -0800193 public static final String ON_CREATE_EVT = "Launcher.onCreate";
vadimtcb863752018-12-19 17:44:57 -0800194 private static final String ON_START_EVT = "Launcher.onStart";
195 private static final String ON_RESUME_EVT = "Launcher.onResume";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700197 private LauncherStateManager mStateManager;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700198
Adam Cohenad4e15c2013-10-17 16:21:35 -0700199 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200
Winson Chunga2413752012-04-03 14:22:34 -0700201 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700202 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
203 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
204 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700205
Jon Miranda54441f52018-01-24 15:38:25 -0800206 private LauncherAppTransitionManager mAppTransitionManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800207 private Configuration mOldConfig;
Jon Miranda54441f52018-01-24 15:38:25 -0800208
Adam Cohen091440a2015-03-18 14:16:05 -0700209 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700210 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700211 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800212 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700213
Sunny Goyalffe83f12014-08-14 17:39:34 -0700214 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700215 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700216
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700217 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700218
Sunny Goyal316490e2015-06-02 09:38:28 -0700219 @Thunk Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700220
Sunny Goyal47328fd2016-05-25 18:56:41 -0700221 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700222
223 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700224 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700225 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700226
Winson Chung8ae41982017-11-10 11:42:13 -0800227 // UI and state for the overview panel
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800228 private View mOverviewPanel;
Winson Chung8ae41982017-11-10 11:42:13 -0800229
Adam Cohen091440a2015-03-18 14:16:05 -0700230 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400231
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700232 private OnResumeCallback mOnResumeCallback;
233
Sunny Goyal527c7d32015-08-28 15:19:36 -0700234 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700235
Joe Onorato9c1289c2009-08-17 11:03:03 -0400236 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800237 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800238 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700239 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400240
Tony Wickham010d2552017-01-20 08:15:28 -0800241 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700242
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800243 private int mSynchronouslyBoundPage = PagedView.INVALID_PAGE;
Adam Cohen1462de32012-07-24 22:34:36 -0700244
Winson Chung46353de2012-02-16 14:05:10 -0800245 // We only want to get the SharedPreferences once since it does an FS stat each time we get
246 // it from the context.
247 private SharedPreferences mSharedPrefs;
248
Sunny Goyal2100c782016-08-22 16:00:03 -0700249 // Activity result which needs to be processed after workspace has loaded.
250 private ActivityResultInfo mPendingActivityResult;
251 /**
252 * Holds extra information required to handle a result from an external call, like
253 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
254 */
255 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800256
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700257 public ViewGroupFocusHelper mFocusHandler;
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700258
Sunny Goyal623eddd2018-03-02 12:24:41 -0800259 private RotationHelper mRotationHelper;
Sunny Goyal5743f862019-03-28 15:35:32 -0700260 private Runnable mCancelTouchController;
Sunny Goyal7779d622015-06-11 16:18:39 -0700261
Sunny Goyal2db53422019-03-01 16:06:12 -0800262 final Handler mHandler = new Handler();
Sunny Goyala002c6c2019-02-12 16:20:10 -0800263 private final Runnable mHandleDeferredResume = this::handleDeferredResume;
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700264
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800265 @Override
266 protected void onCreate(Bundle savedInstanceState) {
vadimt00d42552018-12-11 17:59:36 -0800267 RaceConditionTracker.onEvent(ON_CREATE_EVT, ENTER);
Winson Chunga2413752012-04-03 14:22:34 -0700268 if (DEBUG_STRICT_MODE) {
269 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
270 .detectDiskReads()
271 .detectDiskWrites()
272 .detectNetwork() // or .detectAll() for all detectable problems
273 .penaltyLog()
274 .build());
275 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
276 .detectLeakedSqlLiteObjects()
277 .detectLeakedClosableObjects()
278 .penaltyLog()
279 .penaltyDeath()
280 .build());
281 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700282 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700283
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800284 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700285 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400286
Sunny Goyal87f784c2017-01-11 10:48:34 -0800287 LauncherAppState app = LauncherAppState.getInstance(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800288 mOldConfig = new Configuration(getResources().getConfiguration());
Sunny Goyal0e7724c2018-02-22 13:22:21 -0800289 mModel = app.setLauncher(this);
Hyunyoung Song31971a32019-01-27 12:31:12 -0800290 InvariantDeviceProfile idp = app.getInvariantDeviceProfile();
291 initDeviceProfile(idp);
292 idp.addOnChangeListener(this);
Sunny Goyalf7258242015-10-19 16:59:07 -0700293 mSharedPrefs = Utilities.getPrefs(this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800294 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700295 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700296
Joe Onorato41a12d22009-10-31 18:30:00 -0400297 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700298 mAllAppsController = new AllAppsTransitionController(this);
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800299 mStateManager = new LauncherStateManager(this);
Tracy Zhou8b23c6f2018-04-30 17:13:27 -0700300 UiFactory.onCreate(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700301
Sunny Goyalffe83f12014-08-14 17:39:34 -0700302 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700303
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700304 mAppWidgetHost = new LauncherAppWidgetHost(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700305 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700306
Sunny Goyal60820d72017-05-09 12:40:11 -0700307 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700308
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800309 setupViews();
Tony Wickham010d2552017-01-20 08:15:28 -0800310 mPopupDataProvider = new PopupDataProvider(this);
311
Sunny Goyal623eddd2018-03-02 12:24:41 -0800312 mRotationHelper = new RotationHelper(this);
Sunny Goyal90548432018-04-04 17:17:00 -0700313 mAppTransitionManager = LauncherAppTransitionManager.newInstance(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800314
Sunny Goyald3864fa2017-11-14 15:06:36 -0800315 boolean internalStateHandled = InternalStateHandler.handleCreate(this, getIntent());
316 if (internalStateHandled) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800317 if (savedInstanceState != null) {
318 // InternalStateHandler has already set the appropriate state.
319 // We dont need to do anything.
320 savedInstanceState.remove(RUNTIME_STATE);
321 }
322 }
323 restoreState(savedInstanceState);
Sunny Goyalbbece862019-03-01 13:13:52 -0800324 mStateManager.reapplyState();
Winson Chungb63b44c2017-11-10 17:54:44 -0800325
Sunny Goyal2100c782016-08-22 16:00:03 -0700326 // We only load the page synchronously if the user rotates (or triggers a
327 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700328 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
329 if (savedInstanceState != null) {
330 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
331 }
Sunny Goyald3864fa2017-11-14 15:06:36 -0800332
Sunny Goyalfe770c92016-09-27 14:38:58 -0700333 if (!mModel.startLoader(currentScreen)) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800334 if (!internalStateHandled) {
335 // If we are not binding synchronously, show a fade in animation when
336 // the first page bind completes.
Sunny Goyal6001ea22018-05-10 16:31:00 -0700337 mDragLayer.getAlphaProperty(ALPHA_INDEX_LAUNCHER_LOAD).setValue(0);
Sunny Goyald3864fa2017-11-14 15:06:36 -0800338 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700339 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700340 // Pages bound synchronously.
341 mWorkspace.setCurrentPage(currentScreen);
342
Sunny Goyal2100c782016-08-22 16:00:03 -0700343 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800344 }
345
346 // For handling default keys
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800347 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800348
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800349 setContentView(mLauncherView);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800350 getRootView().dispatchInsets();
Mario Bertschlera6936942017-05-31 14:48:19 -0700351
Jon Miranda7fda2852017-07-19 16:07:01 -0700352 // Listen for broadcasts
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700353 registerReceiver(mScreenOffReceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));
Sunny Goyala616d2b2017-06-12 13:54:01 -0700354
Sunny Goyal8392c822017-06-20 10:03:56 -0700355 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
356 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700357
358 if (mLauncherCallbacks != null) {
359 mLauncherCallbacks.onCreate(savedInstanceState);
360 }
Sunny Goyal623eddd2018-03-02 12:24:41 -0800361 mRotationHelper.initialize();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700362
363 TraceHelper.endSection("Launcher-onCreate");
vadimt00d42552018-12-11 17:59:36 -0800364 RaceConditionTracker.onEvent(ON_CREATE_EVT, EXIT);
Adam Cohen9211d422014-10-07 18:14:53 -0700365 }
366
Sunny Goyal7779d622015-06-11 16:18:39 -0700367 @Override
Winson Chung8eb49e02018-07-16 13:33:31 -0700368 public void onEnterAnimationComplete() {
369 super.onEnterAnimationComplete();
370 UiFactory.onEnterAnimationComplete(this);
arangelovad783052018-08-17 14:46:45 +0100371 mAllAppsController.highlightWorkTabIfNecessary();
Winson Chung8eb49e02018-07-16 13:33:31 -0700372 }
373
374 @Override
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800375 public void onConfigurationChanged(Configuration newConfig) {
376 int diff = newConfig.diff(mOldConfig);
Jon Mirandaaeb4dd02018-07-11 14:16:23 -0700377
378 if ((diff & CONFIG_LOCALE) != 0) {
379 Folder.setLocaleDependentFields(getResources(), true /* force */);
380 }
381
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800382 if ((diff & (CONFIG_ORIENTATION | CONFIG_SCREEN_SIZE)) != 0) {
Sunny Goyal371ea052019-03-06 15:38:32 -0800383 onIdpChanged(mDeviceProfile.inv);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800384 }
385
386 mOldConfig.setTo(newConfig);
Tracy Zhoua706f002018-03-28 13:55:19 -0700387 UiFactory.onLauncherStateOrResumeChanged(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800388 super.onConfigurationChanged(newConfig);
389 }
390
Sunny Goyal605bcf32018-03-02 15:16:12 -0800391 @Override
Sunny Goyal59d086c2018-05-07 17:31:40 -0700392 protected void reapplyUi() {
393 getRootView().dispatchInsets();
394 getStateManager().reapplyState(true /* cancelCurrentAnimation */);
395 }
396
397 @Override
Sunny Goyal605bcf32018-03-02 15:16:12 -0800398 public void rebindModel() {
399 int currentPage = mWorkspace.getNextPage();
400 if (mModel.startLoader(currentPage)) {
401 mWorkspace.setCurrentPage(currentPage);
402 setWorkspaceLoading(true);
403 }
404 }
405
Hyunyoung Song31971a32019-01-27 12:31:12 -0800406 @Override
407 public void onIdpChanged(int changeFlags, InvariantDeviceProfile idp) {
Sunny Goyal371ea052019-03-06 15:38:32 -0800408 onIdpChanged(idp);
409 }
410
Govinda Wasserman24e489e2019-03-28 12:23:28 -0400411 public void setQuickSearchBarAlpha(float alpha) {
412 View qsbAllApps = findViewById(R.id.search_container_all_apps);
413 if (qsbAllApps != null) {
414 qsbAllApps.setAlpha(alpha);
415 }
416 }
417
Sunny Goyal371ea052019-03-06 15:38:32 -0800418 private void onIdpChanged(InvariantDeviceProfile idp) {
419 mUserEventDispatcher = null;
420
Hyunyoung Song31971a32019-01-27 12:31:12 -0800421 initDeviceProfile(idp);
Sunny Goyal371ea052019-03-06 15:38:32 -0800422 dispatchDeviceProfileChanged();
423 reapplyUi();
424 mDragLayer.recreateControllers();
425
426 // TODO: We can probably avoid rebind when only screen size changed.
427 rebindModel();
Hyunyoung Song31971a32019-01-27 12:31:12 -0800428 }
429
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800430 private void initDeviceProfile(InvariantDeviceProfile idp) {
431 // Load configuration-specific DeviceProfile
Sunny Goyal59d086c2018-05-07 17:31:40 -0700432 mDeviceProfile = idp.getDeviceProfile(this);
Sunny Goyal8c48d8b2019-01-25 15:10:18 -0800433 if (isInMultiWindowMode()) {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700434 Display display = getWindowManager().getDefaultDisplay();
435 Point mwSize = new Point();
436 display.getSize(mwSize);
437 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
438 }
439 onDeviceProfileInitiated();
Sunny Goyal605bcf32018-03-02 15:16:12 -0800440 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout(), true);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800441 }
442
Sunny Goyal623eddd2018-03-02 12:24:41 -0800443 public RotationHelper getRotationHelper() {
444 return mRotationHelper;
445 }
446
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700447 public LauncherStateManager getStateManager() {
448 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700449 }
450
Mario Bertschler27288382017-05-24 15:35:09 -0700451 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700452 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800453 return mLauncherView.findViewById(id);
454 }
455
456 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700457 public void onAppWidgetHostReset() {
458 if (mAppWidgetHost != null) {
459 mAppWidgetHost.startListening();
460 }
461 }
462
Adam Cohen9211d422014-10-07 18:14:53 -0700463 private LauncherCallbacks mLauncherCallbacks;
464
Sunny Goyal32554d12015-12-03 15:31:25 -0800465 /**
466 * Call this after onCreate to set or clear overlay.
467 */
468 public void setLauncherOverlay(LauncherOverlay overlay) {
469 if (overlay != null) {
470 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
471 }
472 mWorkspace.setLauncherOverlay(overlay);
473 }
474
Adam Cohen9211d422014-10-07 18:14:53 -0700475 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
476 mLauncherCallbacks = callbacks;
477 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700478 }
479
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700480 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700481 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700482 if (mLauncherCallbacks != null) {
483 mLauncherCallbacks.onLauncherProviderChange();
484 }
485 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700486
Hyunyoung Song3f471442015-04-08 19:01:34 -0700487 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700488 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
489 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700490 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700491 }
492
Tony Wickham010d2552017-01-20 08:15:28 -0800493 public PopupDataProvider getPopupDataProvider() {
494 return mPopupDataProvider;
495 }
496
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700497 @Override
Tony Wickhamf34bee82018-12-03 18:11:39 -0800498 public DotInfo getDotInfoForItem(ItemInfo info) {
499 return mPopupDataProvider.getDotInfoForItem(info);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700500 }
501
502 @Override
503 public void invalidateParent(ItemInfo info) {
Jon Mirandae6f3fa42019-03-04 09:14:40 -0800504 if (info.container >= 0) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700505 View folderIcon = getWorkspace().getHomescreenIconByItemId(info.container);
Jon Mirandae6f3fa42019-03-04 09:14:40 -0800506 if (folderIcon instanceof FolderIcon && folderIcon.getTag() instanceof FolderInfo) {
507 FolderIconPreviewVerifier verifier =
508 new FolderIconPreviewVerifier(getDeviceProfile().inv);
509 verifier.setFolderInfo((FolderInfo) folderIcon.getTag());
510 if (verifier.isItemInPreview(info.rank)) {
511 folderIcon.invalidate();
512 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700513 }
514 }
515 }
516
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000517 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700518 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
519 * a configuration step, this allows the proper animations to run after other transitions.
520 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700521 private int completeAdd(
Sunny Goyal2100c782016-08-22 16:00:03 -0700522 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
Sunny Goyalefb7e842018-10-04 15:11:00 -0700523 int screenId = info.screenId;
Sunny Goyal2100c782016-08-22 16:00:03 -0700524 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700525 // When the screen id represents an actual screen (as opposed to a rank) we make sure
526 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700527 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700528 }
Adam Cohendb364c32014-05-20 14:23:40 -0700529
Sunny Goyal2100c782016-08-22 16:00:03 -0700530 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800531 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700532 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700533 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800534 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700535 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700536 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700537 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700538 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700539 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700540 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700541 int widgetId = appWidgetId;
542 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700543 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700544 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700545 // Since the view was just bound, also launch the configure activity if needed
546 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
547 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800548 if (provider != null) {
549 new WidgetAddFlowHandler(provider)
550 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700551 }
552 }
553 break;
554 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800555 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700556
Adam Cohendb364c32014-05-20 14:23:40 -0700557 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800558 }
559
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800560 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700561 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700562 if (isWorkspaceLoading()) {
563 // process the result once the workspace has loaded.
564 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
565 return;
566 }
567 mPendingActivityResult = null;
568
Winson Chunge341d302013-08-16 14:31:00 -0700569 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700570 final PendingRequestArgs requestArgs = mPendingRequestArgs;
571 setWaitingForResult(null);
572 if (requestArgs == null) {
573 return;
574 }
575
576 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700577
Adam Cohenad4e15c2013-10-17 16:21:35 -0700578 Runnable exitSpringLoaded = new Runnable() {
579 @Override
580 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700581 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700582 }
583 };
584
Michael Jurka8b805b12012-04-18 14:23:14 -0700585 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700586 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700587 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700588 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
589 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700590 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700591 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700592 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700593 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700594 addAppWidgetImpl(
595 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800596 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700597 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700598 }
599 return;
600 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200601
Adam Cohened66b2b2012-01-23 17:28:51 -0800602 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700603 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700604
Adam Cohened66b2b2012-01-23 17:28:51 -0800605 // We have special handling for widgets
606 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800607 final int appWidgetId;
608 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
609 : -1;
610 if (widgetId < 0) {
611 appWidgetId = pendingAddWidgetId;
612 } else {
613 appWidgetId = widgetId;
614 }
615
Adam Cohenad4e15c2013-10-17 16:21:35 -0700616 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800617 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700618 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
619 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700620 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700621 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700622 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700623 @Override
624 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700625 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700626 }
627 };
Adam Cohendb364c32014-05-20 14:23:40 -0700628
Sunny Goyal2100c782016-08-22 16:00:03 -0700629 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
630 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
631 } else {
632 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
633 // When the screen id represents an actual screen (as opposed to a rank)
634 // we make sure that the drop page actually exists.
635 requestArgs.screenId =
636 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700637 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700638 final CellLayout dropLayout =
639 mWorkspace.getScreenWithId(requestArgs.screenId);
640
641 dropLayout.setDropPending(true);
642 final Runnable onComplete = new Runnable() {
643 @Override
644 public void run() {
645 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
646 dropLayout.setDropPending(false);
647 }
648 };
649 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
650 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700651 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800652 return;
653 }
654
Sunny Goyald478c832016-04-01 12:04:16 -0700655 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
656 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700657 if (resultCode == RESULT_OK) {
658 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700659 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700660 }
661 // Leave the widget in the pending state if the user canceled the configure.
662 return;
663 }
664
Sunny Goyalaad90582015-07-09 14:22:50 -0700665 if (requestCode == REQUEST_CREATE_SHORTCUT) {
666 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700667 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
668 completeAdd(requestCode, data, -1, requestArgs);
669 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
670 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
671
Sunny Goyalaad90582015-07-09 14:22:50 -0700672 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700673 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
674 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800675 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800676 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800677 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800678 }
679
680 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700681 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800682 final int requestCode, final int resultCode, final Intent data) {
683 handleActivityResult(requestCode, resultCode, data);
684 if (mLauncherCallbacks != null) {
685 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
686 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800687 }
688
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700689 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700690 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600691 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700692 PendingRequestArgs pendingArgs = mPendingRequestArgs;
693 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
694 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
695 setWaitingForResult(null);
696
Sunny Goyal28c6b962015-10-12 11:42:05 -0700697 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700698 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700699 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700700 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700701 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700702 Intent intent = pendingArgs.getPendingIntent();
703
Sunny Goyal28c6b962015-10-12 11:42:05 -0700704 if (grantResults.length > 0
705 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal369212a2019-03-26 15:03:57 -0700706 startActivitySafely(v, intent, null, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700707 } else {
708 // TODO: Show a snack bar with link to settings
709 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700710 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700711 }
712 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600713 if (mLauncherCallbacks != null) {
714 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
715 grantResults);
716 }
717 }
718
Adam Cohendb364c32014-05-20 14:23:40 -0700719 /**
720 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
721 *
722 * @param screenId the screen id to check
723 * @return the new screen, or screenId if it exists
724 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700725 private int ensurePendingDropLayoutExists(int screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800726 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700727 if (dropLayout == null) {
728 // it's possible that the add screen was removed because it was
729 // empty and a re-bind occurred
730 mWorkspace.addExtraEmptyScreen();
731 return mWorkspace.commitExtraEmptyScreen();
732 } else {
733 return screenId;
734 }
735 }
736
Sunny Goyal2100c782016-08-22 16:00:03 -0700737 @Thunk void completeTwoStageWidgetDrop(
738 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
739 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800740 Runnable onCompleteRunnable = null;
741 int animationType = 0;
742
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700743 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800744 if (resultCode == RESULT_OK) {
745 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
746 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800747 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700748 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800749 onCompleteRunnable = new Runnable() {
750 @Override
751 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700752 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700753 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800754 }
755 };
756 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800757 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800758 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800759 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700760 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700761 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700762 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
763 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700764 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700765 // The animated view may be null in the case of a rotation during widget configuration
766 onCompleteRunnable.run();
767 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800768 }
769
770 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700771 protected void onStop() {
772 super.onStop();
Adam Cohen9211d422014-10-07 18:14:53 -0700773 if (mLauncherCallbacks != null) {
774 mLauncherCallbacks.onStop();
775 }
Miranda Kephart1a359a22019-02-11 13:07:04 -0500776
Hyunyoung Song1241e612018-05-15 21:46:51 -0700777 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
778 mStateManager.getState().containerType, -1);
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700779
780 mAppWidgetHost.setListenIfResumed(false);
781
Tony Wickham010d2552017-01-20 08:15:28 -0800782 NotificationListener.removeNotificationsChangedListener();
Sunny Goyalaf161fd2018-02-05 16:34:57 -0800783 getStateManager().moveToRestState();
Winson Chungedb0d6b2018-03-21 17:32:55 -0700784
Tracy Zhou62646712018-06-26 14:19:02 -0700785 UiFactory.onLauncherStateOrResumeChanged(this);
786
Winson Chungdea51352018-04-24 13:02:10 -0700787 // Workaround for b/78520668, explicitly trim memory once UI is hidden
Winson Chung473730f2018-04-26 11:54:22 -0700788 onTrimMemory(TRIM_MEMORY_UI_HIDDEN);
Michael Jurkacd496d72013-04-11 11:32:45 -0700789 }
790
791 @Override
792 protected void onStart() {
vadimtcb863752018-12-19 17:44:57 -0800793 RaceConditionTracker.onEvent(ON_START_EVT, ENTER);
Michael Jurkacd496d72013-04-11 11:32:45 -0700794 super.onStart();
Adam Cohen9211d422014-10-07 18:14:53 -0700795 if (mLauncherCallbacks != null) {
796 mLauncherCallbacks.onStart();
797 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800798 mAppWidgetHost.setListenIfResumed(true);
Sunny Goyalcac002d2018-01-04 09:51:45 -0800799 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
vadimtcb863752018-12-19 17:44:57 -0800800 RaceConditionTracker.onEvent(ON_START_EVT, EXIT);
Michael Jurkacd496d72013-04-11 11:32:45 -0700801 }
802
Sunny Goyala002c6c2019-02-12 16:20:10 -0800803 private void handleDeferredResume() {
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700804 if (hasBeenResumed()) {
805 getUserEventDispatcher().logActionCommand(Action.Command.RESUME,
806 mStateManager.getState().containerType, -1);
807 getUserEventDispatcher().startSession();
Sunny Goyala002c6c2019-02-12 16:20:10 -0800808
809 UiFactory.onLauncherStateOrResumeChanged(this);
Sunny Goyal369212a2019-03-26 15:03:57 -0700810 AppLaunchTracker.INSTANCE.get(this).onReturnedToHome();
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700811 }
812 }
813
Michael Jurkacd496d72013-04-11 11:32:45 -0700814 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800815 protected void onResume() {
vadimtcb863752018-12-19 17:44:57 -0800816 RaceConditionTracker.onEvent(ON_RESUME_EVT, ENTER);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700817 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800818 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700819 TraceHelper.partitionSection("ON_RESUME", "superCall");
820
Sunny Goyala002c6c2019-02-12 16:20:10 -0800821 mHandler.removeCallbacks(mHandleDeferredResume);
822 Utilities.postAsyncCallback(mHandler, mHandleDeferredResume);
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700823
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700824 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700825 // Process any items that were added while Launcher was away.
826 InstallShortcutReceiver.disableAndFlushInstallQueue(
827 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700828
Sunny Goyala474a9b2017-05-04 16:47:11 -0700829 // Refresh shortcuts if the permission changed.
830 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700831
Tracy Zhoue5575f92018-04-19 11:12:53 -0700832 DiscoveryBounce.showForHomeIfNeeded(this);
Adam Cohen9211d422014-10-07 18:14:53 -0700833 if (mLauncherCallbacks != null) {
834 mLauncherCallbacks.onResume();
835 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800836
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700837 TraceHelper.endSection("ON_RESUME");
vadimtcb863752018-12-19 17:44:57 -0800838 RaceConditionTracker.onEvent(ON_RESUME_EVT, EXIT);
Adam Cohen06dff352012-06-01 17:17:08 -0700839 }
840
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800841 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700842 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700843 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700844 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700845
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700846 super.onPause();
Joe Onorato24b6fd82009-11-12 13:47:09 -0800847 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700848 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700849
Adam Cohen9211d422014-10-07 18:14:53 -0700850 if (mLauncherCallbacks != null) {
851 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700852 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700853 }
854
Winson Chungd7823942018-02-16 03:23:47 +0000855 @Override
Tracy Zhoua706f002018-03-28 13:55:19 -0700856 protected void onUserLeaveHint() {
857 super.onUserLeaveHint();
858 UiFactory.onLauncherStateOrResumeChanged(this);
859 }
860
861 @Override
Winson Chungd7823942018-02-16 03:23:47 +0000862 public void onWindowFocusChanged(boolean hasFocus) {
863 super.onWindowFocusChanged(hasFocus);
864 mStateManager.onWindowFocusChanged();
865 }
866
Adam Cohenc2d6e892014-10-16 09:49:24 -0700867 public interface LauncherOverlay {
868
869 /**
870 * Touch interaction leading to overscroll has begun
871 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700872 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700873
874 /**
875 * Touch interaction related to overscroll has ended
876 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700877 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700878
879 /**
880 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
881 * screen (or in the case of RTL, the rightmost screen).
882 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700883 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700884
885 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800886 * Called when the launcher is ready to use the overlay
887 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700888 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700889 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700890 }
891
892 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700893 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700894 }
895
896 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
897
Sunny Goyalc86df472016-02-25 09:19:38 -0800898 public void onScrollChanged(float progress) {
899 if (mWorkspace != null) {
900 mWorkspace.onOverlayScrollChanged(progress);
901 }
902 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700903 }
904
Sunny Goyalf9403d92017-10-18 10:55:56 -0700905 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -0700906 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700907 }
908
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800909 /**
910 * Restores the previous state, if it exists.
911 *
912 * @param savedState The previous state.
913 */
914 private void restoreState(Bundle savedState) {
915 if (savedState == null) {
916 return;
917 }
918
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700919 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -0700920 LauncherState[] stateValues = LauncherState.values();
921 LauncherState state = stateValues[stateOrdinal];
Sunny Goyalbc683e92017-12-06 10:25:07 -0800922 if (!state.disableRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700923 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800924 }
925
Sunny Goyal2100c782016-08-22 16:00:03 -0700926 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
927 if (requestArgs != null) {
928 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800929 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700930
931 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700932
933 SparseArray<Parcelable> widgetsState =
934 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
935 if (widgetsState != null) {
936 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
937 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800938 }
939
940 /**
941 * Finds all the views we need and configure them properly.
942 */
943 private void setupViews() {
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800944 mDragLayer = findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700945 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -0700946 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700947 mWorkspace.initParentViews(mDragLayer);
Sunny Goyal16764582017-11-06 16:00:34 -0800948 mOverviewPanel = findViewById(R.id.overview_panel);
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800949 mHotseat = findViewById(R.id.hotseat);
Craig Mautner360310b2012-10-26 15:13:08 -0700950
Sunny Goyal784f9c32016-03-23 16:56:54 -0700951 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
952 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
953 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800954
Winson Chung4c98d922011-05-31 16:50:48 -0700955 // Setup the drag layer
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700956 mDragLayer.setup(mDragController, mWorkspace);
Sunny Goyal5743f862019-03-28 15:35:32 -0700957 mCancelTouchController = UiFactory.enableLiveTouchControllerChanges(mDragLayer);
Winson Chung4c98d922011-05-31 16:50:48 -0700958
Hyunyoung Song645764e2016-06-06 14:19:02 -0700959 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -0700960 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
961 // default state, otherwise we will update to the wrong offsets in RTL
962 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700963 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700964 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700965
Tony Wickham34d2c912015-09-11 09:27:58 -0700966 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700967 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700968
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700969 // Setup Apps
970 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -0700971
Winson Chung3d503fb2011-07-13 17:25:49 -0700972 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -0700973 mDragController.setMoveTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700974 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -0400975
Sunny Goyal7185dd62018-03-14 17:51:49 -0700976 mAllAppsController.setupViews(mAppsView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800977 }
978
979 /**
980 * Creates a view representing a shortcut.
981 *
982 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800983 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800984 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700985 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800986 }
987
988 /**
989 * Creates a view representing a shortcut inflated from the specified resource.
990 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800991 * @param parent The group the shortcut belongs to.
992 * @param info The data structure describing the shortcut.
993 *
994 * @return A View inflated from layoutResId.
995 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700996 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -0800997 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
998 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800999 favorite.applyFromShortcutInfo(info);
Sunny Goyal8a2a63b2018-03-06 22:15:18 -08001000 favorite.setOnClickListener(ItemClickHandler.INSTANCE);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001001 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001002 return favorite;
1003 }
1004
1005 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -07001006 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001007 *
1008 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001009 */
Sunny Goyalefb7e842018-10-04 15:11:00 -07001010 private void completeAddShortcut(Intent data, int container, int screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001011 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001012 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
1013 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001014 return;
1015 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001016
Jon Mirandac476d6e2017-05-04 16:30:01 -07001017 int[] cellXY = mTmpAddItemCellCoordinates;
1018 CellLayout layout = getCellLayout(container, screenId);
1019
Sunny Goyal782f0c92017-01-19 10:27:54 -08001020 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001021 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001022 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
1023 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001024 }
1025
1026 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001027 // Legacy shortcuts are only supported for primary profile.
1028 info = Process.myUserHandle().equals(args.user)
1029 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001030
Sunny Goyalb57645f2017-03-20 13:57:28 -07001031 if (info == null) {
1032 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1033 return;
1034 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001035 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001036 // The app is trying to add a shortcut without sufficient permissions
1037 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1038 return;
1039 }
1040 }
1041
Jon Mirandac476d6e2017-05-04 16:30:01 -07001042 if (container < 0) {
1043 // Adding a shortcut to the Workspace.
1044 final View view = createShortcut(info);
1045 boolean foundCellSpan = false;
1046 // First we check if we already know the exact location where we want to add this item.
1047 if (cellX >= 0 && cellY >= 0) {
1048 cellXY[0] = cellX;
1049 cellXY[1] = cellY;
1050 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001051
Jon Mirandac476d6e2017-05-04 16:30:01 -07001052 // If appropriate, either create a folder or add to an existing folder
1053 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001054 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001055 return;
1056 }
1057 DragObject dragObject = new DragObject();
1058 dragObject.dragInfo = info;
1059 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1060 true)) {
1061 return;
1062 }
1063 } else {
1064 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1065 }
1066
1067 if (!foundCellSpan) {
1068 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001069 return;
1070 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001071
1072 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1073 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001074 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001075 // Adding a shortcut to a Folder.
Sunny Goyale29897f52017-07-20 10:09:42 -07001076 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001077 if (folderIcon != null) {
1078 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1079 folderInfo.add(info, args.rank, false);
1080 } else {
Sunny Goyale29897f52017-07-20 10:09:42 -07001081 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001082 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001083 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001084 }
1085
Sunny Goyalefb7e842018-10-04 15:11:00 -07001086 public FolderIcon findFolderIcon(final int folderIconId) {
Sunny Goyal83fd25e2018-07-09 16:47:01 -07001087 return (FolderIcon) mWorkspace.getHomescreenIconByItemId(folderIconId);
Sunny Goyale29897f52017-07-20 10:09:42 -07001088 }
1089
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001091 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001092 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001093 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001094 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001095 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001096 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1097
Adam Cohened66b2b2012-01-23 17:28:51 -08001098 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001099 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001100 }
Romain Guycbb89e42009-06-08 15:52:54 -07001101
Adam Cohen59400422014-03-05 18:07:04 -08001102 LauncherAppWidgetInfo launcherInfo;
1103 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001104 launcherInfo.spanX = itemInfo.spanX;
1105 launcherInfo.spanY = itemInfo.spanY;
1106 launcherInfo.minSpanX = itemInfo.minSpanX;
1107 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001108 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001109
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001110 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001111 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001112
Sunny Goyal2100c782016-08-22 16:00:03 -07001113 if (hostView == null) {
1114 // Perform actual inflation because we're live
1115 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001116 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001117 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301118 prepareAppWidget(hostView, launcherInfo);
1119 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001120 }
Romain Guycbb89e42009-06-08 15:52:54 -07001121
Sunny Goyald5462aa2016-11-22 16:52:39 +05301122 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001123 hostView.setTag(item);
1124 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001125 hostView.setFocusable(true);
1126 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001127 }
1128
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001129 private final BroadcastReceiver mScreenOffReceiver = new BroadcastReceiver() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001130 @Override
1131 public void onReceive(Context context, Intent intent) {
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001132 // Reset AllApps to its initial state only if we are not in the middle of
1133 // processing a multi-step drop
1134 if (mPendingRequestArgs == null) {
1135 mStateManager.goToState(NORMAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001136 }
1137 }
1138 };
1139
Sunny Goyalaae6fbb2019-01-31 16:05:58 -08001140 public void updateNotificationDots(Predicate<PackageUserKey> updatedDots) {
Tony Wickhamf34bee82018-12-03 18:11:39 -08001141 mWorkspace.updateNotificationDots(updatedDots);
1142 mAppsView.getAppsStore().updateNotificationDots(updatedDots);
Tony Wickham010d2552017-01-20 08:15:28 -08001143 }
1144
Adam Cohended9f8d2010-11-03 13:25:16 -07001145 @Override
1146 public void onAttachedToWindow() {
1147 super.onAttachedToWindow();
1148
Sunny Goyalc86df472016-02-25 09:19:38 -08001149 if (mLauncherCallbacks != null) {
1150 mLauncherCallbacks.onAttachedToWindow();
1151 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001152 }
1153
1154 @Override
1155 public void onDetachedFromWindow() {
1156 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001157
1158 if (mLauncherCallbacks != null) {
1159 mLauncherCallbacks.onDetachedFromWindow();
1160 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001161 }
1162
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001163 public AllAppsTransitionController getAllAppsController() {
1164 return mAllAppsController;
1165 }
1166
Sunny Goyal9d69c8d2018-03-19 13:41:31 -07001167 @Override
Winson Chung1a341002018-01-17 10:00:23 -08001168 public LauncherRootView getRootView() {
1169 return (LauncherRootView) mLauncherView;
1170 }
1171
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001172 @Override
Winson Chunga6945242014-01-08 14:04:34 -08001173 public DragLayer getDragLayer() {
1174 return mDragLayer;
1175 }
1176
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001177 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001178 return mAppsView;
1179 }
1180
Winson Chunga6945242014-01-08 14:04:34 -08001181 public Workspace getWorkspace() {
1182 return mWorkspace;
1183 }
1184
1185 public Hotseat getHotseat() {
1186 return mHotseat;
1187 }
1188
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001189 public <T extends View> T getOverviewPanel() {
Winson Chung8ae41982017-11-10 11:42:13 -08001190 return (T) mOverviewPanel;
Winson Chunga6945242014-01-08 14:04:34 -08001191 }
1192
Sunny Goyal47328fd2016-05-25 18:56:41 -07001193 public DropTargetBar getDropTargetBar() {
1194 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001195 }
1196
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001197 public LauncherAppWidgetHost getAppWidgetHost() {
1198 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001199 }
Romain Guycbb89e42009-06-08 15:52:54 -07001200
Winson Chunga9abd0e2010-10-27 17:18:37 -07001201 public LauncherModel getModel() {
1202 return mModel;
1203 }
1204
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001205 public ModelWriter getModelWriter() {
1206 return mModelWriter;
1207 }
1208
Adam Cohen79d90c52016-04-22 13:29:20 -07001209 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001210 return mSharedPrefs;
1211 }
1212
Sunny Goyalf8d56fc2018-01-31 15:18:11 -08001213 public int getOrientation() { return mOldConfig.orientation; }
Jon Miranda6bed3502017-09-19 14:43:17 -07001214
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001215 @Override
1216 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001217 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001218 super.onNewIntent(intent);
1219
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001220 boolean alreadyOnHome = hasWindowFocus() && ((intent.getFlags() &
Winson15f8b172015-08-19 11:02:39 -07001221 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1222 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001223
1224 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001225 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001226 && AbstractFloatingView.getTopOpenView(this) == null;
Winson15f8b172015-08-19 11:02:39 -07001227 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001228 boolean internalStateHandled = InternalStateHandler
Sunny Goyal8b264562018-02-03 23:00:13 -08001229 .handleNewIntent(this, intent, isStarted());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001230
Winson15f8b172015-08-19 11:02:39 -07001231 if (isActionMain) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001232 if (!internalStateHandled) {
1233 // Note: There should be at most one log per method call. This is enforced
1234 // implicitly by using if-else statements.
1235 UserEventDispatcher ued = getUserEventDispatcher();
1236 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
1237 if (topOpenView != null) {
1238 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
1239 } else if (alreadyOnHome) {
1240 Target target = newContainerTarget(mStateManager.getState().containerType);
1241 target.pageIndex = mWorkspace.getCurrentPage();
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001242 ued.logActionCommand(Action.Command.HOME_INTENT, target,
1243 newContainerTarget(ContainerType.WORKSPACE));
Sunny Goyald3864fa2017-11-14 15:06:36 -08001244 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001245
Sunny Goyald3864fa2017-11-14 15:06:36 -08001246 // In all these cases, only animate if we're already on home
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001247 AbstractFloatingView.closeAllOpenViews(this, isStarted());
Jon Mirandafeba90f2016-10-06 10:53:29 -07001248
Sunny Goyala2467272018-03-16 14:53:05 -07001249 if (!isInState(NORMAL)) {
1250 // Only change state, if not already the same. This prevents cancelling any
1251 // animations running as part of resume
1252 mStateManager.goToState(NORMAL);
1253 }
Sunny Goyald3864fa2017-11-14 15:06:36 -08001254
1255 // Reset the apps view
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001256 if (!alreadyOnHome) {
Sunny Goyal7185dd62018-03-14 17:51:49 -07001257 mAppsView.reset(isStarted() /* animate */);
Sunny Goyald3864fa2017-11-14 15:06:36 -08001258 }
1259
Sunny Goyalaad33592018-08-07 17:20:35 -07001260 if (shouldMoveToDefaultScreen && !mWorkspace.isHandlingTouch()) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001261 mWorkspace.post(mWorkspace::moveToDefaultScreen);
1262 }
1263 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001264
1265 final View v = getWindow().peekDecorView();
1266 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001267 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001268 }
1269
Adam Cohen9211d422014-10-07 18:14:53 -07001270 if (mLauncherCallbacks != null) {
Sunny Goyal85462132018-04-24 11:39:37 -07001271 mLauncherCallbacks.onHomeIntent(internalStateHandled);
Adam Cohen9211d422014-10-07 18:14:53 -07001272 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001273 }
Winson15f8b172015-08-19 11:02:39 -07001274
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001275 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001276 }
1277
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001278 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001279 public void onRestoreInstanceState(Bundle state) {
1280 super.onRestoreInstanceState(state);
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001281 mWorkspace.restoreInstanceStateForChild(mSynchronouslyBoundPage);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001282 }
1283
1284 @Override
1285 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001286 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001287 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001288
Adam Cohen21cd0022013-10-09 18:57:02 -07001289 }
Sunny Goyalea609262017-10-25 15:47:38 -07001290 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001291
1292
1293 AbstractFloatingView widgets = AbstractFloatingView
1294 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1295 if (widgets != null) {
1296 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1297 widgets.saveHierarchyState(widgetsState);
1298 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1299 } else {
1300 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1301 }
1302
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001303 // We close any open folders and shortcut containers since they will not be re-opened,
1304 // and we need to make sure this state is reflected.
1305 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001306
Sunny Goyal2100c782016-08-22 16:00:03 -07001307 if (mPendingRequestArgs != null) {
1308 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1309 }
1310 if (mPendingActivityResult != null) {
1311 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001312 }
1313
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001314 super.onSaveInstanceState(outState);
1315
Adam Cohen9211d422014-10-07 18:14:53 -07001316 if (mLauncherCallbacks != null) {
1317 mLauncherCallbacks.onSaveInstanceState(outState);
1318 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001319 }
1320
1321 @Override
1322 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001323 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001324
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001325 unregisterReceiver(mScreenOffReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001326 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001327
Sunny Goyal5743f862019-03-28 15:35:32 -07001328 if (mCancelTouchController != null) {
1329 mCancelTouchController.run();
1330 mCancelTouchController = null;
1331 }
Tony Wickhama0068302018-04-11 16:16:11 -07001332
Winson Chungcd2b0142011-06-08 16:02:26 -07001333 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001334 // It's possible to receive onDestroy after a new Launcher activity has
1335 // been created. In this case, don't interfere with the new Launcher.
1336 if (mModel.isCurrentCallbacks(this)) {
1337 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001338 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001339 }
Sunny Goyal623eddd2018-03-02 12:24:41 -08001340 mRotationHelper.destroy();
Sunny Goyal745bad92016-05-02 10:54:12 -07001341
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001342 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001343 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001344 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001345 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001346 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001347
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001348 TextKeyListener.getInstance().release();
Tony2917a8b2017-07-31 23:29:42 -07001349 clearPendingBinds();
Hyunyoung Song31971a32019-01-27 12:31:12 -08001350 LauncherAppState.getIDP(this).removeOnChangeListener(this);
Adam Cohen9211d422014-10-07 18:14:53 -07001351 if (mLauncherCallbacks != null) {
1352 mLauncherCallbacks.onDestroy();
1353 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001354 }
1355
Sunny Goyalae502842016-06-17 08:43:56 -07001356 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1357 return mAccessibilityDelegate;
1358 }
1359
Adam Cohena9cf38f2011-05-02 15:36:58 -07001360 public DragController getDragController() {
1361 return mDragController;
1362 }
1363
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001364 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001365 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001366 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001367 }
1368
1369 @Override
1370 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1371 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001372 try {
1373 super.startIntentSenderForResult(intent, requestCode,
1374 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1375 } catch (IntentSender.SendIntentException e) {
1376 throw new ActivityNotFoundException();
1377 }
1378 }
1379
Winson Chung70d72102011-08-12 11:24:35 -07001380 /**
1381 * Indicates that we want global search for this activity by setting the globalSearch
1382 * argument for {@link #startSearch} to true.
1383 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001384 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001385 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001386 Bundle appSearchData, boolean globalSearch) {
1387 if (appSearchData == null) {
1388 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001389 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001390 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001391
Sunny Goyal6f28e712016-09-13 14:19:29 -07001392 if (mLauncherCallbacks == null ||
1393 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1394 // Starting search from the callbacks failed. Start the default global search.
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001395 super.startSearch(initialQuery, selectInitialQuery, appSearchData, true);
Ian Parkinson047036e2014-09-01 15:40:53 +01001396 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001397
1398 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001399 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001400 }
1401
Joe Onorato9c1289c2009-08-17 11:03:03 -04001402 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001403 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001404 }
1405
Adam Cohen517a7f52014-03-01 12:12:59 -08001406 public boolean isWorkspaceLoading() {
1407 return mWorkspaceLoading;
1408 }
1409
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001410 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001411 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001412 }
1413
Sunny Goyal04a324a2017-01-20 21:08:59 -08001414 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001415 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001416 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001417
Sunny Goyal2100c782016-08-22 16:00:03 -07001418 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001419 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001420 if (LOGD) {
1421 Log.d(TAG, "Adding widget from drop");
1422 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001423 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001424 }
1425
Sunny Goyal2100c782016-08-22 16:00:03 -07001426 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001427 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1428 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1429 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001430
Adam Cohenad4e15c2013-10-17 16:21:35 -07001431 Runnable onComplete = new Runnable() {
1432 @Override
1433 public void run() {
1434 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001435 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001436 }
1437 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001438 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001439 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001440 }
1441 }
Romain Guycbb89e42009-06-08 15:52:54 -07001442
Sunny Goyalefb7e842018-10-04 15:11:00 -07001443 public void addPendingItem(PendingAddItemInfo info, int container, int screenId,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001444 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001445 info.container = container;
1446 info.screenId = screenId;
1447 if (cell != null) {
1448 info.cellX = cell[0];
1449 info.cellY = cell[1];
1450 }
1451 info.spanX = spanX;
1452 info.spanY = spanY;
1453
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001454 switch (info.itemType) {
1455 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1456 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001457 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001458 break;
1459 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001460 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001461 break;
1462 default:
1463 throw new IllegalStateException("Unknown item type: " + info.itemType);
1464 }
1465 }
1466
Adam Cohenfbba09b2011-07-18 21:43:05 -07001467 /**
1468 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001469 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001470 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001471 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1472 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001473 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1474 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1475 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001476 }
1477
Adam Cohenfbba09b2011-07-18 21:43:05 -07001478 /**
1479 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001480 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001481 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001482 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001483 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001484 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001485 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001486 // In the case where we've prebound the widget, we remove it from the DragLayer
1487 if (LOGD) {
1488 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1489 }
1490 getDragLayer().removeView(hostView);
1491
Adam Cohened66b2b2012-01-23 17:28:51 -08001492 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001493 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001494
1495 // Clear the boundWidget so that it doesn't get destroyed.
1496 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001497 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001498 // In this case, we either need to start an activity to get permission to bind
1499 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001500 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1501 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1502 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1503 this, info.componentName);
1504 } else {
1505 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1506 }
Adam Cohendd70d662012-10-04 16:53:44 -07001507 Bundle options = info.bindOptions;
1508
Sunny Goyalffe83f12014-08-14 17:39:34 -07001509 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1510 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001511 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001512 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001513 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001514 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001515 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001516 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001517 }
1518
Sunny Goyalefb7e842018-10-04 15:11:00 -07001519 FolderIcon addFolder(CellLayout layout, int container, final int screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001520 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001521 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001522 folderInfo.title = getText(R.string.folder_name);
1523
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001524 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001525 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001526
1527 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001528 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301529 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001530 // Force measure the new folder icon
1531 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1532 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001533 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001534 }
Romain Guycbb89e42009-06-08 15:52:54 -07001535
Winsonc0b52fe2015-09-09 16:38:15 -07001536 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001537 * Unbinds the view for the specified item, and removes the item and all its children.
1538 *
1539 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001540 * @param itemInfo the {@link ItemInfo} for this view.
1541 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001542 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001543 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001544 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001545 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001546 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1547 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001548 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001549 } else {
1550 mWorkspace.removeWorkspaceItem(v);
1551 }
Winsonc0b52fe2015-09-09 16:38:15 -07001552 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001553 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001554 }
1555 } else if (itemInfo instanceof FolderInfo) {
1556 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001557 if (v instanceof FolderIcon) {
1558 ((FolderIcon) v).removeListeners();
1559 }
Winsonc0b52fe2015-09-09 16:38:15 -07001560 mWorkspace.removeWorkspaceItem(v);
1561 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001562 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001563 }
1564 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1565 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001566 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001567 if (deleteFromDb) {
Tony Wickham6a71a5b2018-08-21 11:40:23 -07001568 getModelWriter().deleteWidgetInfo(widgetInfo, getAppWidgetHost());
Winsonc0b52fe2015-09-09 16:38:15 -07001569 }
1570 } else {
1571 return false;
1572 }
1573 return true;
1574 }
1575
Tony Wickham6a71a5b2018-08-21 11:40:23 -07001576
Winsonc0b52fe2015-09-09 16:38:15 -07001577
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001578 @Override
1579 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001580 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001581 }
1582
Joe Onorato88ec0992009-11-19 13:16:06 -08001583 @Override
1584 public void onBackPressed() {
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08001585 if (finishAutoCancelActionMode()) {
1586 return;
1587 }
Adam Cohen9211d422014-10-07 18:14:53 -07001588 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1589 return;
1590 }
1591
Sunny Goyal45478022015-06-08 16:52:41 -07001592 if (mDragController.isDragging()) {
1593 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001594 return;
1595 }
1596
Jon Mirandafeba90f2016-10-06 10:53:29 -07001597 // Note: There should be at most one log per method call. This is enforced implicitly
1598 // by using if-else statements.
1599 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001600 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
Tony Wickham52c1b662018-05-21 13:13:58 -07001601 if (topView != null && topView.onBackPressed()) {
1602 // Handled by the floating view.
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001603 } else {
Sunny Goyale39690b2018-08-02 13:35:07 -07001604 mStateManager.getState().onBackPressed(this);
Michael Jurkaaf442092010-06-10 17:01:57 -07001605 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001606 }
1607
Sunny Goyalfe770c92016-09-27 14:38:58 -07001608 @TargetApi(Build.VERSION_CODES.M)
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001609 @Override
Jon Miranda73c27ec2018-05-16 18:06:23 -07001610 public ActivityOptions getActivityLaunchOptions(View v) {
1611 return mAppTransitionManager.getActivityLaunchOptions(this, v);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001612 }
1613
Tony Wickham3a6746a2018-03-29 09:39:56 -07001614 public LauncherAppTransitionManager getAppTransitionManager() {
1615 return mAppTransitionManager;
1616 }
1617
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001618 @TargetApi(Build.VERSION_CODES.M)
1619 @Override
1620 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
1621 // Due to legacy reasons, direct call shortcuts require Launchers to have the
1622 // corresponding permission. Show the appropriate permission prompt if that
1623 // is the case.
1624 if (intent.getComponent() == null
1625 && Intent.ACTION_CALL.equals(intent.getAction())
1626 && checkSelfPermission(android.Manifest.permission.CALL_PHONE) !=
1627 PackageManager.PERMISSION_GRANTED) {
1628
1629 setWaitingForResult(PendingRequestArgs
1630 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
1631 requestPermissions(new String[]{android.Manifest.permission.CALL_PHONE},
1632 REQUEST_PERMISSION_CALL_PHONE);
1633 return true;
1634 } else {
1635 return false;
1636 }
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001637 }
1638
Hyunyoung Song46d07f72018-05-22 15:41:25 -07001639 @Override
Hyunyoung Songfc007472018-10-25 14:09:50 -07001640 public int getCurrentState() {
1641 if(mStateManager.getState() == LauncherState.ALL_APPS) {
1642 return StatsLogUtils.LAUNCHER_STATE_ALLAPPS;
1643 } else if (mStateManager.getState() == LauncherState.OVERVIEW) {
1644 return StatsLogUtils.LAUNCHER_STATE_OVERVIEW;
1645 }
Hyunyoung Song0ae38882018-11-05 14:45:19 -08001646 return StatsLogUtils.LAUNCHER_STATE_HOME;
Hyunyoung Songfc007472018-10-25 14:09:50 -07001647 }
1648
1649 @Override
Hyunyoung Song46d07f72018-05-22 15:41:25 -07001650 public void modifyUserEvent(LauncherLogProto.LauncherEvent event) {
1651 if (event.srcTarget != null && event.srcTarget.length > 0 &&
1652 event.srcTarget[1].containerType == ContainerType.PREDICTION) {
1653 Target[] targets = new Target[3];
1654 targets[0] = event.srcTarget[0];
1655 targets[1] = event.srcTarget[1];
1656 targets[2] = newTarget(Target.Type.CONTAINER);
1657 event.srcTarget = targets;
1658 LauncherState state = mStateManager.getState();
1659 if (state == LauncherState.ALL_APPS) {
1660 event.srcTarget[2].containerType = ContainerType.ALLAPPS;
1661 } else if (state == LauncherState.OVERVIEW) {
1662 event.srcTarget[2].containerType = ContainerType.TASKSWITCHER;
1663 }
1664 }
1665 }
1666
Sunny Goyal369212a2019-03-26 15:03:57 -07001667 public boolean startActivitySafely(View v, Intent intent, ItemInfo item,
1668 @Nullable String sourceContainer) {
1669 boolean success = super.startActivitySafely(v, intent, item, sourceContainer);
Hyunyoung Song1241e612018-05-15 21:46:51 -07001670 if (success && v instanceof BubbleTextView) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001671 // This is set to the view that launched the activity that navigated the user away
1672 // from launcher. Since there is no callback for when the activity has finished
1673 // launching, enable the press state and keep this reference to reset the press
1674 // state when we return to launcher.
1675 BubbleTextView btv = (BubbleTextView) v;
1676 btv.setStayPressed(true);
1677 setOnResumeCallback(btv);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001678 }
Hyunyoung Song1241e612018-05-15 21:46:51 -07001679 return success;
Michael Jurka86a720e2012-05-09 11:23:23 -07001680 }
1681
Winson Chung3d503fb2011-07-13 17:25:49 -07001682 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001683 // TODO: Remove this method
Sunny Goyal876e4622018-11-02 13:50:40 -07001684 return mHotseat != null && (layout == mHotseat);
Winson Chung3d503fb2011-07-13 17:25:49 -07001685 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001686
Winson Chung3d503fb2011-07-13 17:25:49 -07001687 /**
1688 * Returns the CellLayout of the specified container at the specified screen.
1689 */
Sunny Goyalefb7e842018-10-04 15:11:00 -07001690 public CellLayout getCellLayout(int container, int screenId) {
Sunny Goyal876e4622018-11-02 13:50:40 -07001691 return (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1692 ? mHotseat : mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08001693 }
1694
Michael Jurkae326f182011-11-21 14:05:46 -08001695 @Override
1696 public void onTrimMemory(int level) {
1697 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07001698 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
1699 // The widget preview db can result in holding onto over
1700 // 3MB of memory for caching which isn't necessary.
1701 SQLiteDatabase.releaseMemory();
1702
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08001703 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07001704 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08001705 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01001706 if (mLauncherCallbacks != null) {
1707 mLauncherCallbacks.onTrimMemory(level);
1708 }
Winson Chung62a70be2018-02-23 15:10:05 -08001709 UiFactory.onTrimMemory(this, level);
Michael Jurkae326f182011-11-21 14:05:46 -08001710 }
1711
Michael Jurkad7c28052012-04-27 15:43:36 -07001712 @Override
1713 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07001714 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07001715 final List<CharSequence> text = event.getText();
1716 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07001717 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001718 // TODO: When can workspace be null?
1719 text.add(mWorkspace == null
1720 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07001721 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07001722 return result;
1723 }
1724
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001725 public void setOnResumeCallback(OnResumeCallback callback) {
1726 if (mOnResumeCallback != null) {
1727 mOnResumeCallback.onLauncherResume();
1728 }
1729 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07001730 }
1731
Michael Jurka7607c2f2013-04-03 14:33:19 -07001732 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001733 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001734 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07001735 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001736 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07001737 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001738 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07001739 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07001740 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07001741 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001742 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001743
Joe Onorato9c1289c2009-08-17 11:03:03 -04001744 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07001745 * Clear any pending bind callbacks. This is called when is loader is planning to
1746 * perform a full rebind from scratch.
1747 */
1748 @Override
1749 public void clearPendingBinds() {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001750 if (mPendingExecutor != null) {
1751 mPendingExecutor.markCompleted();
1752 mPendingExecutor = null;
1753 }
1754 }
1755
1756 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001757 * Refreshes the shortcuts shown on the workspace.
1758 *
1759 * Implementation of the method from LauncherModel.Callbacks.
1760 */
1761 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001762 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001763 // Floating panels (except the full widget sheet) are associated with individual icons. If
1764 // we are starting a fresh bind, close all such panels as all the icons are about
1765 // to go away.
1766 AbstractFloatingView.closeOpenViews(this, true,
Sunny Goyal7ede6112017-12-05 15:11:21 -08001767 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_REBIND_SAFE);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08001768
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001769 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08001770
Winson Chungd64d1762013-08-20 14:37:16 -07001771 // Clear the workspace because it's going to be rebound
Sunny Goyal2db53422019-03-01 16:06:12 -08001772 mDragController.cancelDrag();
1773
Adam Cohendf035382011-04-11 17:22:04 -07001774 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07001775 mWorkspace.removeAllWorkspaceScreens();
Sunny Goyalc11fac32018-02-27 19:20:15 -08001776 mAppWidgetHost.clearViews();
Winson Chungd64d1762013-08-20 14:37:16 -07001777
Winson Chung3d503fb2011-07-13 17:25:49 -07001778 if (mHotseat != null) {
Sunny Goyal11e96492018-03-23 17:06:00 -07001779 mHotseat.resetLayout(mDeviceProfile.isVerticalBarLayout());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001780 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001781 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001782 }
1783
Adam Cohendcd297f2013-06-18 13:13:40 -07001784 @Override
Sunny Goyalefb7e842018-10-04 15:11:00 -07001785 public void bindScreens(IntArray orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001786 // Make sure the first screen is always at the start.
Jon Miranda7143ba62019-03-15 09:00:05 -07001787 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001788 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalefb7e842018-10-04 15:11:00 -07001789 orderedScreenIds.removeValue(Workspace.FIRST_SCREEN_ID);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001790 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Jon Miranda7143ba62019-03-15 09:00:05 -07001791 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001792 // If there are no screens, we need to have an empty screen
1793 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07001794 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001795 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07001796
Winsonc7d2e832016-07-28 12:24:55 -07001797 // After we have added all the screens, if the wallpaper was locked to the default state,
1798 // then notify to indicate that it can be released and a proper wallpaper offset can be
1799 // computed before the next layout
1800 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07001801 }
1802
Sunny Goyalefb7e842018-10-04 15:11:00 -07001803 private void bindAddScreens(IntArray orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001804 int count = orderedScreenIds.size();
1805 for (int i = 0; i < count; i++) {
Sunny Goyalefb7e842018-10-04 15:11:00 -07001806 int screenId = orderedScreenIds.get(i);
Jon Miranda7143ba62019-03-15 09:00:05 -07001807 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001808 // No need to bind the first screen, as its always bound.
1809 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
1810 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001811 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001812 }
1813
Sunny Goyalb23980c2017-08-17 07:45:25 -07001814 @Override
Tony Wickham6a71a5b2018-08-21 11:40:23 -07001815 public void preAddApps() {
1816 // If there's an undo snackbar, force it to complete to ensure empty screens are removed
1817 // before trying to add new items.
1818 mModelWriter.commitDelete();
1819 AbstractFloatingView snackbar = AbstractFloatingView.getOpenView(this, TYPE_SNACKBAR);
1820 if (snackbar != null) {
1821 snackbar.post(() -> snackbar.close(true));
1822 }
1823 }
1824
1825 @Override
Sunny Goyalefb7e842018-10-04 15:11:00 -07001826 public void bindAppsAdded(IntArray newScreens, ArrayList<ItemInfo> addNotAnimated,
Sunny Goyal29947f02017-12-18 13:49:44 -08001827 ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07001828 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08001829 if (newScreens != null) {
1830 bindAddScreens(newScreens);
1831 }
Winson Chungd64d1762013-08-20 14:37:16 -07001832
1833 // We add the items without animation on non-visible pages, and with
1834 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08001835 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001836 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001837 }
Adam Cohen76a47a12014-02-05 11:47:43 -08001838 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001839 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07001840 }
Winson Chungc58497e2013-09-03 17:48:37 -07001841
Winson Chung87412982013-10-03 18:34:14 -07001842 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07001843 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001844 }
1845
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001846 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001847 * Bind the items start-end from the list.
1848 *
1849 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001850 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07001851 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07001852 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001853 // Get the list of added items and intersect them with the set of items here
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001854 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07001855 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001856 Workspace workspace = mWorkspace;
Sunny Goyalefb7e842018-10-04 15:11:00 -07001857 int newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07001858 int end = items.size();
1859 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001860 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07001861
1862 // Short circuit if we are loading dock items for a configuration which has no dock
1863 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
1864 mHotseat == null) {
1865 continue;
1866 }
1867
Sunny Goyal639e9062015-08-19 19:17:06 -07001868 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001869 switch (item.itemType) {
1870 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1871 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08001872 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001873 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07001874 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001875 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001876 }
1877 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07001878 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001879 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001880 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001881 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001882 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07001883 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1884 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07001885 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07001886 if (view == null) {
1887 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001888 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08001889 break;
1890 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001891 default:
1892 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001893 }
Sunny Goyal639e9062015-08-19 19:17:06 -07001894
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001895 /*
1896 * Remove colliding items.
1897 */
1898 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1899 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
1900 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
1901 View v = cl.getChildAt(item.cellX, item.cellY);
1902 Object tag = v.getTag();
1903 String desc = "Collision while binding workspace item: " + item
1904 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08001905 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001906 throw (new RuntimeException(desc));
1907 } else {
1908 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001909 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001910 continue;
1911 }
1912 }
1913 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05301914 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07001915 if (animateIcons) {
1916 // Animate all the applications up now
1917 view.setAlpha(0f);
1918 view.setScaleX(0f);
1919 view.setScaleY(0f);
1920 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08001921 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07001922 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001923 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07001924
Sunny Goyalf0b6db72018-08-13 16:10:14 -07001925 // Animate to the correct page
1926 if (animateIcons && newItemsScreenId > -1) {
1927 AnimatorSet anim = new AnimatorSet();
1928 anim.playTogether(bounceAnims);
Jon Miranda64eb8ea2018-03-29 11:43:58 -07001929
Sunny Goyalefb7e842018-10-04 15:11:00 -07001930 int currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyalf0b6db72018-08-13 16:10:14 -07001931 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
1932 final Runnable startBounceAnimRunnable = anim::start;
1933
1934 if (newItemsScreenId != currentScreenId) {
1935 // We post the animation slightly delayed to prevent slowdowns
1936 // when we are loading right after we return to launcher.
1937 mWorkspace.postDelayed(new Runnable() {
1938 public void run() {
1939 if (mWorkspace != null) {
1940 AbstractFloatingView.closeAllOpenViews(Launcher.this, false);
1941
1942 mWorkspace.snapToPage(newScreenIndex);
1943 mWorkspace.postDelayed(startBounceAnimRunnable,
1944 NEW_APPS_ANIMATION_DELAY);
Winson Chung94d67682013-09-25 16:29:40 -07001945 }
Sunny Goyalf0b6db72018-08-13 16:10:14 -07001946 }
1947 }, NEW_APPS_PAGE_MOVE_DELAY);
1948 } else {
1949 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07001950 }
Winson Chung64359a52013-07-08 17:17:08 -07001951 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001952 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001953 }
1954
Joe Onorato9c1289c2009-08-17 11:03:03 -04001955 /**
1956 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001957 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07001958 public void bindAppWidget(LauncherAppWidgetInfo item) {
1959 View view = inflateAppWidget(item);
1960 if (view != null) {
1961 mWorkspace.addInScreen(view, item);
1962 mWorkspace.requestLayout();
1963 }
1964 }
1965
1966 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001967 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05301968 PendingAppWidgetHostView view =
1969 new PendingAppWidgetHostView(this, item, mIconCache, true);
1970 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07001971 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001972 }
1973
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001974 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001975
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001976 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08001977
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001978 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
1979 // If the provider is not ready, bind as a pending widget.
1980 appWidgetInfo = null;
1981 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
1982 // The widget id is not valid. Try to find the widget based on the provider info.
1983 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
1984 } else {
1985 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
1986 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07001987
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001988 // If the provider is ready, but the width is not yet restored, try to restore it.
1989 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
1990 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07001991 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001992 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
1993 + " belongs to component " + item.providerName
1994 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001995 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07001996 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07001997 }
Sunny Goyalff572272014-07-23 13:58:07 -07001998
Sunny Goyal84b4adc2015-08-12 15:12:16 -07001999 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002000 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07002001 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2002 // Id has not been allocated yet. Allocate a new id.
2003 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
2004 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07002005
Sunny Goyald478c832016-04-01 12:04:16 -07002006 // Also try to bind the widget. If the bind fails, the user will be shown
2007 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08002008 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07002009 pendingInfo.spanX = item.spanX;
2010 pendingInfo.spanY = item.spanY;
2011 pendingInfo.minSpanX = item.minSpanX;
2012 pendingInfo.minSpanY = item.minSpanY;
2013 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07002014
2015 boolean isDirectConfig =
2016 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2017 if (isDirectConfig && item.bindOptions != null) {
2018 Bundle newOptions = item.bindOptions.getExtras();
2019 if (options != null) {
2020 newOptions.putAll(options);
2021 }
2022 options = newOptions;
2023 }
Sunny Goyald478c832016-04-01 12:04:16 -07002024 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2025 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002026
Sunny Goyal86df1382016-08-10 15:03:22 -07002027 // We tried to bind once. If we were not able to bind, we would need to
2028 // go through the permission dialog, which means we cannot skip the config
2029 // activity.
2030 item.bindOptions = null;
2031 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2032
Sunny Goyald478c832016-04-01 12:04:16 -07002033 // Bind succeeded
2034 if (success) {
2035 // If the widget has a configure activity, it is still needs to set it up,
2036 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002037 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002038 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2039 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002040 }
Sunny Goyald478c832016-04-01 12:04:16 -07002041
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002042 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002043 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002044 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002045 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002046 // The widget was marked as UI not ready, but there is no configure activity to
2047 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002048 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002049 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002050 }
Sunny Goyalff572272014-07-23 13:58:07 -07002051 }
2052
Sunny Goyald5462aa2016-11-22 16:52:39 +05302053 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002054 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002055 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002056 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002057 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Tony Wickham6a71a5b2018-08-21 11:40:23 -07002058 getModelWriter().deleteWidgetInfo(item, getAppWidgetHost());
Sunny Goyalb23980c2017-08-17 07:45:25 -07002059 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002060 }
2061
Sunny Goyal233ee962015-08-03 13:05:01 -07002062 item.minSpanX = appWidgetInfo.minSpanX;
2063 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302064 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002065 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302066 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002067 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302068 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002069
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002070 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002071 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002072 }
2073
Sunny Goyalff572272014-07-23 13:58:07 -07002074 /**
2075 * Restores a pending widget.
2076 *
2077 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002078 */
Sunny Goyald478c832016-04-01 12:04:16 -07002079 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002080 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2081 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2082 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002083 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002084 }
2085
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002086 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002087 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002088 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2089 info.pendingItemInfo = null;
2090 }
Sunny Goyalff572272014-07-23 13:58:07 -07002091
Sunny Goyalba47faa2017-10-04 16:50:57 -07002092 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002093 view.reInflate();
Sunny Goyalba47faa2017-10-04 16:50:57 -07002094 }
2095
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002096 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002097 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002098 }
2099
Adam Cohen1462de32012-07-24 22:34:36 -07002100 public void onPageBoundSynchronously(int page) {
Sunny Goyalf4aceab2017-12-06 16:16:31 -08002101 mSynchronouslyBoundPage = page;
Adam Cohen1462de32012-07-24 22:34:36 -07002102 }
2103
Sunny Goyal527c7d32015-08-28 15:19:36 -07002104 @Override
2105 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2106 if (mPendingExecutor != null) {
2107 mPendingExecutor.markCompleted();
2108 }
2109 mPendingExecutor = executor;
Sunny Goyal60180b02018-02-07 12:56:30 -08002110 if (!isInState(ALL_APPS)) {
2111 mAppsView.getAppsStore().setDeferUpdates(true);
2112 mPendingExecutor.execute(() -> mAppsView.getAppsStore().setDeferUpdates(false));
2113 }
2114
Sunny Goyal527c7d32015-08-28 15:19:36 -07002115 executor.attachTo(this);
2116 }
2117
2118 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2119 if (mPendingExecutor == executor) {
2120 mPendingExecutor = null;
2121 }
2122 }
2123
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002124 @Override
2125 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
Sunny Goyal6001ea22018-05-10 16:31:00 -07002126 AlphaProperty property = mDragLayer.getAlphaProperty(ALPHA_INDEX_LAUNCHER_LOAD);
2127 if (property.getValue() < 1) {
2128 ObjectAnimator anim = ObjectAnimator.ofFloat(property, MultiValueAlpha.VALUE, 1);
2129 if (executor != null) {
2130 anim.addListener(new AnimatorListenerAdapter() {
2131 @Override
2132 public void onAnimationEnd(Animator animation) {
2133 executor.onLoadAnimationCompleted();
2134 }
2135 });
2136 }
2137 anim.start();
Sunny Goyal29947f02017-12-18 13:49:44 -08002138 } else if (executor != null) {
2139 executor.onLoadAnimationCompleted();
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002140 }
2141 }
2142
Joe Onorato9c1289c2009-08-17 11:03:03 -04002143 /**
2144 * Callback saying that there aren't any more items to bind.
2145 *
2146 * Implementation of the method from LauncherModel.Callbacks.
2147 */
Tonyf80e8932018-12-21 11:58:04 -08002148 public void finishBindingItems(int pageBoundFirst) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002149 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002150 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002151
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002152 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002153
Sunny Goyal2100c782016-08-22 16:00:03 -07002154 if (mPendingActivityResult != null) {
2155 handleActivityResult(mPendingActivityResult.requestCode,
2156 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2157 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002158 }
2159
Sunny Goyala474a9b2017-05-04 16:47:11 -07002160 InstallShortcutReceiver.disableAndFlushInstallQueue(
2161 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002162
Tonyf80e8932018-12-21 11:58:04 -08002163 // When undoing the removal of the last item on a page, return to that page.
2164 mWorkspace.setCurrentPage(pageBoundFirst);
Tony Wickham6a71a5b2018-08-21 11:40:23 -07002165
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002166 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002167 }
2168
Winson Chunga2413752012-04-03 14:22:34 -07002169 private boolean canRunNewAppsAnimation() {
2170 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002171 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002172 }
2173
Winson Chungc9168342013-06-26 14:54:55 -07002174 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002175 ValueAnimator bounceAnim = new PropertyListBuilder().alpha(1).scale(1).build(v)
2176 .setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
Winson Chungc9168342013-06-26 14:54:55 -07002177 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002178 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002179 return bounceAnim;
2180 }
2181
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002182 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002183 * Add the icons for all apps.
2184 *
2185 * Implementation of the method from LauncherModel.Callbacks.
2186 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002187 public void bindAllApplications(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002188 mAppsView.getAppsStore().setApps(apps);
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002189
Adam Cohen9211d422014-10-07 18:14:53 -07002190 if (mLauncherCallbacks != null) {
2191 mLauncherCallbacks.bindAllApplications(apps);
2192 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002193 }
2194
2195 /**
Zak Cohen658c67a2018-10-19 14:21:05 -07002196 * Copies LauncherModel's map of activities to shortcut counts to Launcher's. This is necessary
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002197 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2198 */
2199 @Override
Zak Cohen658c67a2018-10-19 14:21:05 -07002200 public void bindDeepShortcutMap(HashMap<ComponentKey, Integer> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002201 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002202 }
2203
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002204 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002205 * A package was updated.
2206 *
2207 * Implementation of the method from LauncherModel.Callbacks.
2208 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002209 @Override
2210 public void bindAppsAddedOrUpdated(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002211 mAppsView.getAppsStore().addOrUpdateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002212 }
2213
Sunny Goyal4390ace2014-10-13 11:33:11 -07002214 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002215 public void bindPromiseAppProgressUpdated(PromiseAppInfo app) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002216 mAppsView.getAppsStore().updatePromiseAppProgress(app);
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002217 }
2218
2219 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002220 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002221 mWorkspace.widgetsRestored(widgets);
2222 }
2223
Joe Onorato9c1289c2009-08-17 11:03:03 -04002224 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002225 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002226 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002227 *
2228 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002229 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002230 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002231 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002232 if (!updated.isEmpty()) {
2233 mWorkspace.updateShortcuts(updated);
2234 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002235 }
2236
2237 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002238 * Update the state of a package, typically related to install state.
2239 *
2240 * Implementation of the method from LauncherModel.Callbacks.
2241 */
Sunny Goyale755d462014-07-22 13:48:29 -07002242 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002243 public void bindRestoreItemsChange(HashSet<ItemInfo> updates) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07002244 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07002245 }
2246
2247 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002248 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07002249 * in addition to specific applications to remove, the reason being that
2250 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002251 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07002252 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002253 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07002254 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002255 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002256 mWorkspace.removeItemsByMatcher(matcher);
2257 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002258 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002259
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002260 @Override
2261 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002262 mAppsView.getAppsStore().removeApps(appInfos);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002263 }
Joe Onoratobe386092009-11-17 17:32:16 -08002264
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002265 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002266 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
2267 mPopupDataProvider.setAllWidgets(allWidgets);
Tony Wickham26b17462017-03-20 17:12:24 -07002268 }
2269
Tony Wickham86222d22017-03-29 15:30:43 -07002270 /**
2271 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
2272 * refreshes the widgets and shortcuts associated with the given package/user
2273 */
2274 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07002275 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002276 }
2277
Winson Chung80baf5a2010-08-09 16:03:15 -07002278 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002279 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08002280 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002281 @Override
2282 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
2283 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002284
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002285 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
2286 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07002287 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002288 writer.println(prefix + " Homescreen " + i);
2289
2290 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
2291 for (int j = 0; j < layout.getChildCount(); j++) {
2292 Object tag = layout.getChildAt(j).getTag();
2293 if (tag != null) {
2294 writer.println(prefix + " " + tag.toString());
2295 }
2296 }
2297 }
2298
2299 writer.println(prefix + " Hotseat");
Sunny Goyal876e4622018-11-02 13:50:40 -07002300 ViewGroup layout = mHotseat.getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07002301 for (int j = 0; j < layout.getChildCount(); j++) {
2302 Object tag = layout.getChildAt(j).getTag();
2303 if (tag != null) {
2304 writer.println(prefix + " " + tag.toString());
2305 }
Adam Cohen4caf2982013-08-20 18:54:31 -07002306 }
Sunny Goyala1365452015-10-01 15:46:24 -07002307 }
2308
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002309 writer.println(prefix + "Misc:");
2310 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
2311 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
2312 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
Sunny Goyale43d00d2018-05-14 14:23:18 -07002313 writer.println(" mRotationHelper: " + mRotationHelper);
Sunny Goyalcb2c5052018-10-15 12:30:29 -07002314 // Extra logging for b/116853349
2315 mDragLayer.dumpAlpha(writer);
Sunny Goyale43d00d2018-05-14 14:23:18 -07002316 dumpMisc(writer);
Sunny Goyal2bba1902018-02-27 12:20:50 -08002317
2318 try {
2319 FileLog.flushAll(writer);
2320 } catch (Exception e) {
2321 // Ignore
2322 }
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002323
2324 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002325
Adam Cohen9211d422014-10-07 18:14:53 -07002326 if (mLauncherCallbacks != null) {
2327 mLauncherCallbacks.dump(prefix, fd, writer, args);
2328 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002329 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002330
Sunny Goyal66b24572016-09-21 15:57:55 -07002331 @Override
Sunny Goyal66b24572016-09-21 15:57:55 -07002332 public void onProvideKeyboardShortcuts(
2333 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
2334
2335 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002336 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002337 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
2338 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
Vadim Tryshev74c261c2018-06-12 13:44:43 -07002339 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.widget_button_text),
2340 KeyEvent.KEYCODE_W, KeyEvent.META_CTRL_ON));
Sunny Goyal66b24572016-09-21 15:57:55 -07002341 }
Vadim Tryshev3455f8d2018-05-11 20:02:20 -07002342 final View currentFocus = getCurrentFocus();
2343 if (currentFocus != null) {
2344 if (new CustomActionsPopup(this, currentFocus).canShow()) {
2345 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
2346 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
2347 }
2348 if (currentFocus.getTag() instanceof ItemInfo
2349 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
2350 shortcutInfos.add(new KeyboardShortcutInfo(
2351 getString(R.string.shortcuts_menu_with_notifications_description),
2352 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
2353 }
Sunny Goyal66b24572016-09-21 15:57:55 -07002354 }
2355 if (!shortcutInfos.isEmpty()) {
2356 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
2357 }
2358
2359 super.onProvideKeyboardShortcuts(data, menu, deviceId);
2360 }
2361
2362 @Override
2363 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
2364 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
2365 switch (keyCode) {
2366 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002367 if (isInState(NORMAL)) {
2368 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07002369 return true;
2370 }
2371 break;
2372 case KeyEvent.KEYCODE_S: {
2373 View focusedView = getCurrentFocus();
2374 if (focusedView instanceof BubbleTextView
2375 && focusedView.getTag() instanceof ItemInfo
2376 && mAccessibilityDelegate.performAction(focusedView,
2377 (ItemInfo) focusedView.getTag(),
2378 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal00ac9202017-11-09 15:24:06 -08002379 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07002380 return true;
2381 }
2382 break;
2383 }
2384 case KeyEvent.KEYCODE_O:
2385 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
2386 return true;
2387 }
2388 break;
Vadim Tryshev74c261c2018-06-12 13:44:43 -07002389 case KeyEvent.KEYCODE_W:
2390 if (isInState(NORMAL)) {
2391 OptionsPopupView.openWidgets(this);
2392 return true;
2393 }
2394 break;
Sunny Goyal66b24572016-09-21 15:57:55 -07002395 }
2396 }
2397 return super.onKeyShortcut(keyCode, event);
2398 }
2399
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002400 @Override
2401 public boolean onKeyUp(int keyCode, KeyEvent event) {
2402 if (keyCode == KeyEvent.KEYCODE_MENU) {
2403 // KEYCODE_MENU is sent by some tests, for example
2404 // LauncherJankTests#testWidgetsContainerFling. Don't just remove its handling.
2405 if (!mDragController.isDragging() && !mWorkspace.isSwitchingState() &&
2406 isInState(NORMAL)) {
2407 // Close any open floating views.
2408 AbstractFloatingView.closeAllOpenViews(this);
2409
2410 // Setting the touch point to (-1, -1) will show the options popup in the center of
2411 // the screen.
Sunny Goyal2fd7a8b2018-03-30 17:10:13 -07002412 OptionsPopupView.showDefaultOptions(this, -1, -1);
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002413 }
2414 return true;
2415 }
2416 return super.onKeyUp(keyCode, event);
2417 }
2418
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002419 public static Launcher getLauncher(Context context) {
Sunny Goyal87b5eb62018-07-03 15:53:39 -07002420 return (Launcher) fromContext(context);
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002421 }
2422
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002423 /**
Sunny Goyalab770a12018-11-14 15:17:26 -08002424 * Just a wrapper around the type cast to allow easier tracking of calls.
2425 */
2426 public static <T extends Launcher> T cast(ActivityContext activityContext) {
2427 return (T) activityContext;
2428 }
2429
2430 /**
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002431 * Callback for listening for onResume
2432 */
2433 public interface OnResumeCallback {
2434
2435 void onLauncherResume();
2436 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002437}