blob: c8bee2ea5f710ebfb9ef5cd9fb10f8d3444bb575 [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;
Vadim Tryshev6d2321c2018-04-11 17:19:08 -070022
Sunny Goyal4c7f2152017-10-17 17:17:16 -070023import static com.android.launcher3.LauncherAnimUtils.SPRING_LOADED_EXIT_DELAY;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -070024import static com.android.launcher3.LauncherState.ALL_APPS;
25import static com.android.launcher3.LauncherState.NORMAL;
Sunny Goyal6001ea22018-05-10 16:31:00 -070026import static com.android.launcher3.dragndrop.DragLayer.ALPHA_INDEX_LAUNCHER_LOAD;
Sunny Goyalaeb16432017-10-16 11:46:41 -070027import static com.android.launcher3.logging.LoggerUtils.newContainerTarget;
Hyunyoung Song46d07f72018-05-22 15:41:25 -070028import static com.android.launcher3.logging.LoggerUtils.newTarget;
Sunny Goyal326403e2017-10-02 12:45:10 -070029
Gilles Debunnedd6c9922010-10-25 11:23:41 -070030import android.animation.Animator;
Sunny Goyal6001ea22018-05-10 16:31:00 -070031import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070032import android.animation.AnimatorSet;
Sunny Goyal6001ea22018-05-10 16:31:00 -070033import android.animation.ObjectAnimator;
Sunny Goyal849c6a22018-08-08 16:33:46 -070034import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070035import android.animation.ValueAnimator;
Adam Cohen0b395352014-06-09 22:54:36 +000036import android.annotation.TargetApi;
Tony Wickham005df0b2018-02-13 11:28:12 -080037import android.app.ActivityOptions;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080038import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070039import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080041import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080042import android.content.ComponentCallbacks2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070045import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070046import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070047import android.content.SharedPreferences;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.pm.PackageManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080049import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070050import android.database.sqlite.SQLiteDatabase;
Sunny Goyal59d086c2018-05-07 17:31:40 -070051import android.graphics.Point;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070052import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020053import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054import android.os.Bundle;
Hyunyoung Songbd6fba92018-05-16 15:54:31 -070055import android.os.Handler;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070056import android.os.Parcelable;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080057import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070058import android.os.StrictMode;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070059import android.os.UserHandle;
Michael Jurkaa33411c2012-06-14 16:18:21 -070060import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070062import android.util.Log;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070063import android.util.SparseArray;
Sunny Goyal59d086c2018-05-07 17:31:40 -070064import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070065import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070066import android.view.KeyboardShortcutGroup;
67import android.view.KeyboardShortcutInfo;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080068import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -070069import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070070import android.view.View;
Adam Cohen96d30a12013-07-16 18:13:21 -070071import android.view.ViewGroup;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080072import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070073import android.view.animation.OvershootInterpolator;
Adam Cohen96d30a12013-07-16 18:13:21 -070074import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070075
Sunny Goyal651077b2014-06-30 14:15:31 -070076import com.android.launcher3.DropTarget.DragObject;
Sunny Goyalae502842016-06-17 08:43:56 -070077import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070078import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070079import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal7ede6112017-12-05 15:11:21 -080080import com.android.launcher3.allapps.DiscoveryBounce;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070081import com.android.launcher3.badge.BadgeInfo;
Sunny Goyalffe83f12014-08-14 17:39:34 -070082import com.android.launcher3.compat.AppWidgetManagerCompat;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -070083import com.android.launcher3.compat.LauncherAppsCompatVO;
Tony Wickhame0c33232016-02-08 11:37:04 -080084import com.android.launcher3.config.FeatureFlags;
Vadim Tryshevfedca432015-08-19 17:55:02 -070085import com.android.launcher3.dragndrop.DragController;
86import com.android.launcher3.dragndrop.DragLayer;
87import com.android.launcher3.dragndrop.DragView;
Jon Mirandaaeb4dd02018-07-11 14:16:23 -070088import com.android.launcher3.folder.Folder;
Sunny Goyal26119432016-02-18 22:09:23 +000089import com.android.launcher3.folder.FolderIcon;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070090import com.android.launcher3.folder.FolderIconPreviewVerifier;
Sunny Goyal66b24572016-09-21 15:57:55 -070091import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -070092import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -070093import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -070094import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Song46d07f72018-05-22 15:41:25 -070095import com.android.launcher3.logging.UserEventDispatcher.UserEventDelegate;
Sunny Goyala535ae42017-02-27 10:07:13 -080096import com.android.launcher3.model.ModelWriter;
Sunny Goyala535ae42017-02-27 10:07:13 -080097import com.android.launcher3.notification.NotificationListener;
Tony Wickham540913e2017-01-23 11:47:51 -080098import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -080099import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700100import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyalf8088ee2017-11-10 14:52:00 -0800101import com.android.launcher3.states.InternalStateHandler;
Sunny Goyal623eddd2018-03-02 12:24:41 -0800102import com.android.launcher3.states.RotationHelper;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800103import com.android.launcher3.touch.ItemClickHandler;
Sunny Goyala5ace712017-11-15 17:12:51 -0800104import com.android.launcher3.uioverrides.UiFactory;
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700105import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530106import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
107import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
Sunny Goyalaeb16432017-10-16 11:46:41 -0700108import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyal2100c782016-08-22 16:00:03 -0700109import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700110import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700111import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700112import com.android.launcher3.util.MultiHashMap;
Sunny Goyal6001ea22018-05-10 16:31:00 -0700113import com.android.launcher3.util.MultiValueAlpha;
114import com.android.launcher3.util.MultiValueAlpha.AlphaProperty;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700115import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800116import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700117import com.android.launcher3.util.PendingRequestArgs;
Sunny Goyal8392c822017-06-20 10:03:56 -0700118import com.android.launcher3.util.SystemUiController;
Mario Bertschlera6936942017-05-31 14:48:19 -0700119import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700120import com.android.launcher3.util.Thunk;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700121import com.android.launcher3.util.TraceHelper;
Sunny Goyal326403e2017-10-02 12:45:10 -0700122import com.android.launcher3.util.UiThreadHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700123import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800124import com.android.launcher3.views.OptionsPopupView;
Sunny Goyal29947f02017-12-18 13:49:44 -0800125import com.android.launcher3.widget.LauncherAppWidgetHostView;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800126import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700127import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal29947f02017-12-18 13:49:44 -0800128import com.android.launcher3.widget.PendingAppWidgetHostView;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800129import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700130import com.android.launcher3.widget.WidgetHostViewLoader;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700131import com.android.launcher3.widget.WidgetListRowEntry;
132import com.android.launcher3.widget.WidgetsFullSheet;
Sunny Goyal952e63d2017-08-16 04:59:08 -0700133import com.android.launcher3.widget.custom.CustomWidgetParser;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700134
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700135import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700136import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700137import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700138import java.util.Collection;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700139import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700140import java.util.List;
Tony Wickham010d2552017-01-20 08:15:28 -0800141import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700142
Sunny Goyald2303072018-08-14 15:21:45 -0700143import androidx.annotation.Nullable;
144
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145/**
146 * Default launcher application.
147 */
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700148public class Launcher extends BaseDraggingActivity implements LauncherExterns,
149 LauncherModel.Callbacks, LauncherProviderChangeListener, UserEventDelegate{
Vadim Tryshevfedca432015-08-19 17:55:02 -0700150 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700151 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152
Winson Chunga2413752012-04-03 14:22:34 -0700153 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700154
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700156 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700157
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700158 private static final int REQUEST_PICK_APPWIDGET = 9;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159
Michael Jurka8b805b12012-04-18 14:23:14 -0700160 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800161 public static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
162 public static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700163
Jon Mirandac476d6e2017-05-04 16:30:01 -0700164 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700165
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700166 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
167
Mathew Inwood876a8462013-06-14 14:12:41 +0100168 /**
169 * IntentStarter uses request codes starting with this. This must be greater than all activity
170 * request codes used internally.
171 */
172 protected static final int REQUEST_LAST = 100;
173
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174 // Type: int
175 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700176 // Type: int
177 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700178 // Type: PendingRequestArgs
179 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
180 // Type: ActivityResultInfo
181 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700182 // Type: SparseArray<Parcelable>
183 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700185 private LauncherStateManager mStateManager;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700186
Adam Cohenad4e15c2013-10-17 16:21:35 -0700187 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188
Winson Chunga2413752012-04-03 14:22:34 -0700189 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700190 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
191 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
192 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700193
Jon Miranda54441f52018-01-24 15:38:25 -0800194 private LauncherAppTransitionManager mAppTransitionManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800195 private Configuration mOldConfig;
Jon Miranda54441f52018-01-24 15:38:25 -0800196
Adam Cohen091440a2015-03-18 14:16:05 -0700197 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700198 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700199 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800200 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700201
Sunny Goyalffe83f12014-08-14 17:39:34 -0700202 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700203 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700204
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700205 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700206
Sunny Goyal316490e2015-06-02 09:38:28 -0700207 @Thunk Hotseat mHotseat;
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700208 @Nullable private View mHotseatSearchBox;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700209
Sunny Goyal47328fd2016-05-25 18:56:41 -0700210 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700211
212 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700213 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700214 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700215
Winson Chung8ae41982017-11-10 11:42:13 -0800216 // UI and state for the overview panel
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800217 private View mOverviewPanel;
Winson Chung8ae41982017-11-10 11:42:13 -0800218
Adam Cohen091440a2015-03-18 14:16:05 -0700219 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400220
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700221 private OnResumeCallback mOnResumeCallback;
222
Sunny Goyal527c7d32015-08-28 15:19:36 -0700223 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700224
Joe Onorato9c1289c2009-08-17 11:03:03 -0400225 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800226 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800227 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700228 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400229
Tony Wickham010d2552017-01-20 08:15:28 -0800230 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700231
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800232 private int mSynchronouslyBoundPage = PagedView.INVALID_PAGE;
Adam Cohen1462de32012-07-24 22:34:36 -0700233
Winson Chung46353de2012-02-16 14:05:10 -0800234 // We only want to get the SharedPreferences once since it does an FS stat each time we get
235 // it from the context.
236 private SharedPreferences mSharedPrefs;
237
Sunny Goyal2100c782016-08-22 16:00:03 -0700238 // Activity result which needs to be processed after workspace has loaded.
239 private ActivityResultInfo mPendingActivityResult;
240 /**
241 * Holds extra information required to handle a result from an external call, like
242 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
243 */
244 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800245
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700246 public ViewGroupFocusHelper mFocusHandler;
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700247
Sunny Goyal623eddd2018-03-02 12:24:41 -0800248 private RotationHelper mRotationHelper;
Sunny Goyal7779d622015-06-11 16:18:39 -0700249
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700250
251 private final Handler mHandler = new Handler();
252 private final Runnable mLogOnDelayedResume = this::logOnDelayedResume;
253
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800254 @Override
255 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700256 if (DEBUG_STRICT_MODE) {
257 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
258 .detectDiskReads()
259 .detectDiskWrites()
260 .detectNetwork() // or .detectAll() for all detectable problems
261 .penaltyLog()
262 .build());
263 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
264 .detectLeakedSqlLiteObjects()
265 .detectLeakedClosableObjects()
266 .penaltyLog()
267 .penaltyDeath()
268 .build());
269 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700270 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700271
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800272 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700273 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400274
Sunny Goyal87f784c2017-01-11 10:48:34 -0800275 LauncherAppState app = LauncherAppState.getInstance(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800276 mOldConfig = new Configuration(getResources().getConfiguration());
Sunny Goyal0e7724c2018-02-22 13:22:21 -0800277 mModel = app.setLauncher(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800278 initDeviceProfile(app.getInvariantDeviceProfile());
Winson Chung6e1c0d32013-10-25 15:24:24 -0700279
Sunny Goyalf7258242015-10-19 16:59:07 -0700280 mSharedPrefs = Utilities.getPrefs(this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800281 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700282 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700283
Joe Onorato41a12d22009-10-31 18:30:00 -0400284 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700285 mAllAppsController = new AllAppsTransitionController(this);
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800286 mStateManager = new LauncherStateManager(this);
Tracy Zhou8b23c6f2018-04-30 17:13:27 -0700287 UiFactory.onCreate(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700288
Sunny Goyalffe83f12014-08-14 17:39:34 -0700289 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700290
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700291 mAppWidgetHost = new LauncherAppWidgetHost(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700292 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700293
Sunny Goyal60820d72017-05-09 12:40:11 -0700294 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700295
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800296 setupViews();
Tony Wickham010d2552017-01-20 08:15:28 -0800297 mPopupDataProvider = new PopupDataProvider(this);
298
Sunny Goyal623eddd2018-03-02 12:24:41 -0800299 mRotationHelper = new RotationHelper(this);
Sunny Goyal90548432018-04-04 17:17:00 -0700300 mAppTransitionManager = LauncherAppTransitionManager.newInstance(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800301
Sunny Goyald3864fa2017-11-14 15:06:36 -0800302 boolean internalStateHandled = InternalStateHandler.handleCreate(this, getIntent());
303 if (internalStateHandled) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800304 if (savedInstanceState != null) {
305 // InternalStateHandler has already set the appropriate state.
306 // We dont need to do anything.
307 savedInstanceState.remove(RUNTIME_STATE);
308 }
309 }
310 restoreState(savedInstanceState);
Winson Chungb63b44c2017-11-10 17:54:44 -0800311
Sunny Goyal2100c782016-08-22 16:00:03 -0700312 // We only load the page synchronously if the user rotates (or triggers a
313 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700314 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
315 if (savedInstanceState != null) {
316 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
317 }
Sunny Goyald3864fa2017-11-14 15:06:36 -0800318
Sunny Goyalfe770c92016-09-27 14:38:58 -0700319 if (!mModel.startLoader(currentScreen)) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800320 if (!internalStateHandled) {
321 // If we are not binding synchronously, show a fade in animation when
322 // the first page bind completes.
Sunny Goyal6001ea22018-05-10 16:31:00 -0700323 mDragLayer.getAlphaProperty(ALPHA_INDEX_LAUNCHER_LOAD).setValue(0);
Sunny Goyald3864fa2017-11-14 15:06:36 -0800324 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700325 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700326 // Pages bound synchronously.
327 mWorkspace.setCurrentPage(currentScreen);
328
Sunny Goyal2100c782016-08-22 16:00:03 -0700329 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800330 }
331
332 // For handling default keys
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800333 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800334
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800335 setContentView(mLauncherView);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800336 getRootView().dispatchInsets();
Mario Bertschlera6936942017-05-31 14:48:19 -0700337
Jon Miranda7fda2852017-07-19 16:07:01 -0700338 // Listen for broadcasts
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700339 registerReceiver(mScreenOffReceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));
Sunny Goyala616d2b2017-06-12 13:54:01 -0700340
Sunny Goyal8392c822017-06-20 10:03:56 -0700341 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
342 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700343
344 if (mLauncherCallbacks != null) {
345 mLauncherCallbacks.onCreate(savedInstanceState);
346 }
Sunny Goyal623eddd2018-03-02 12:24:41 -0800347 mRotationHelper.initialize();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700348
349 TraceHelper.endSection("Launcher-onCreate");
Adam Cohen9211d422014-10-07 18:14:53 -0700350 }
351
Sunny Goyal7779d622015-06-11 16:18:39 -0700352 @Override
Winson Chung8eb49e02018-07-16 13:33:31 -0700353 public void onEnterAnimationComplete() {
354 super.onEnterAnimationComplete();
355 UiFactory.onEnterAnimationComplete(this);
356 }
357
358 @Override
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800359 public void onConfigurationChanged(Configuration newConfig) {
360 int diff = newConfig.diff(mOldConfig);
Jon Mirandaaeb4dd02018-07-11 14:16:23 -0700361
362 if ((diff & CONFIG_LOCALE) != 0) {
363 Folder.setLocaleDependentFields(getResources(), true /* force */);
364 }
365
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800366 if ((diff & (CONFIG_ORIENTATION | CONFIG_SCREEN_SIZE)) != 0) {
367 mUserEventDispatcher = null;
368 initDeviceProfile(mDeviceProfile.inv);
Sunny Goyalfde55052018-02-01 14:46:13 -0800369 dispatchDeviceProfileChanged();
Sunny Goyal59d086c2018-05-07 17:31:40 -0700370 reapplyUi();
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700371 mDragLayer.recreateControllers();
Sunny Goyal7185dd62018-03-14 17:51:49 -0700372
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800373 // TODO: We can probably avoid rebind when only screen size changed.
Sunny Goyal605bcf32018-03-02 15:16:12 -0800374 rebindModel();
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800375 }
376
377 mOldConfig.setTo(newConfig);
Tracy Zhoua706f002018-03-28 13:55:19 -0700378 UiFactory.onLauncherStateOrResumeChanged(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800379 super.onConfigurationChanged(newConfig);
380 }
381
Sunny Goyal605bcf32018-03-02 15:16:12 -0800382 @Override
Sunny Goyal59d086c2018-05-07 17:31:40 -0700383 protected void reapplyUi() {
384 getRootView().dispatchInsets();
385 getStateManager().reapplyState(true /* cancelCurrentAnimation */);
386 }
387
388 @Override
Sunny Goyal605bcf32018-03-02 15:16:12 -0800389 public void rebindModel() {
390 int currentPage = mWorkspace.getNextPage();
391 if (mModel.startLoader(currentPage)) {
392 mWorkspace.setCurrentPage(currentPage);
393 setWorkspaceLoading(true);
394 }
395 }
396
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800397 private void initDeviceProfile(InvariantDeviceProfile idp) {
398 // Load configuration-specific DeviceProfile
Sunny Goyal59d086c2018-05-07 17:31:40 -0700399 mDeviceProfile = idp.getDeviceProfile(this);
400 if (isInMultiWindowModeCompat()) {
401 Display display = getWindowManager().getDefaultDisplay();
402 Point mwSize = new Point();
403 display.getSize(mwSize);
404 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
405 }
406 onDeviceProfileInitiated();
Sunny Goyal605bcf32018-03-02 15:16:12 -0800407 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout(), true);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800408 }
409
Sunny Goyal623eddd2018-03-02 12:24:41 -0800410 public RotationHelper getRotationHelper() {
411 return mRotationHelper;
412 }
413
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700414 public LauncherStateManager getStateManager() {
415 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700416 }
417
Mario Bertschler27288382017-05-24 15:35:09 -0700418 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700419 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800420 return mLauncherView.findViewById(id);
421 }
422
423 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700424 public void onAppWidgetHostReset() {
425 if (mAppWidgetHost != null) {
426 mAppWidgetHost.startListening();
427 }
428 }
429
Adam Cohen9211d422014-10-07 18:14:53 -0700430 private LauncherCallbacks mLauncherCallbacks;
431
Sunny Goyal32554d12015-12-03 15:31:25 -0800432 /**
433 * Call this after onCreate to set or clear overlay.
434 */
435 public void setLauncherOverlay(LauncherOverlay overlay) {
436 if (overlay != null) {
437 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
438 }
439 mWorkspace.setLauncherOverlay(overlay);
440 }
441
Adam Cohen9211d422014-10-07 18:14:53 -0700442 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
443 mLauncherCallbacks = callbacks;
444 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700445 }
446
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700447 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700448 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700449 if (mLauncherCallbacks != null) {
450 mLauncherCallbacks.onLauncherProviderChange();
451 }
452 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700453
Hyunyoung Song3f471442015-04-08 19:01:34 -0700454 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700455 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
456 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700457 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700458 }
459
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000460 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700461 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
462 // This cast is safe as long as the id < 0x00FFFFFF
463 // Since we jail all the dynamically generated views, there should be no clashes
464 // with any other views.
465 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000466 }
467
Tony Wickham010d2552017-01-20 08:15:28 -0800468 public PopupDataProvider getPopupDataProvider() {
469 return mPopupDataProvider;
470 }
471
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700472 @Override
473 public BadgeInfo getBadgeInfoForItem(ItemInfo info) {
474 return mPopupDataProvider.getBadgeInfoForItem(info);
475 }
476
477 @Override
478 public void invalidateParent(ItemInfo info) {
479 FolderIconPreviewVerifier verifier = new FolderIconPreviewVerifier(getDeviceProfile().inv);
480 if (verifier.isItemInPreview(info.rank) && (info.container >= 0)) {
481 View folderIcon = getWorkspace().getHomescreenIconByItemId(info.container);
482 if (folderIcon != null) {
483 folderIcon.invalidate();
484 }
485 }
486 }
487
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000488 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700489 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
490 * a configuration step, this allows the proper animations to run after other transitions.
491 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700492 private long completeAdd(
493 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
494 long screenId = info.screenId;
495 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700496 // When the screen id represents an actual screen (as opposed to a rank) we make sure
497 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700498 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700499 }
Adam Cohendb364c32014-05-20 14:23:40 -0700500
Sunny Goyal2100c782016-08-22 16:00:03 -0700501 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800502 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700503 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700504 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800505 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700506 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700507 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700508 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700509 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700510 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700511 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700512 int widgetId = appWidgetId;
513 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700514 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700515 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700516 // Since the view was just bound, also launch the configure activity if needed
517 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
518 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800519 if (provider != null) {
520 new WidgetAddFlowHandler(provider)
521 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700522 }
523 }
524 break;
525 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800526 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700527
Adam Cohendb364c32014-05-20 14:23:40 -0700528 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800529 }
530
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800531 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700532 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700533 if (isWorkspaceLoading()) {
534 // process the result once the workspace has loaded.
535 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
536 return;
537 }
538 mPendingActivityResult = null;
539
Winson Chunge341d302013-08-16 14:31:00 -0700540 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700541 final PendingRequestArgs requestArgs = mPendingRequestArgs;
542 setWaitingForResult(null);
543 if (requestArgs == null) {
544 return;
545 }
546
547 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700548
Adam Cohenad4e15c2013-10-17 16:21:35 -0700549 Runnable exitSpringLoaded = new Runnable() {
550 @Override
551 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700552 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700553 }
554 };
555
Michael Jurka8b805b12012-04-18 14:23:14 -0700556 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700557 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700558 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700559 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
560 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700561 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700562 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700563 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700564 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700565 addAppWidgetImpl(
566 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800567 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700568 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700569 }
570 return;
571 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200572
Adam Cohened66b2b2012-01-23 17:28:51 -0800573 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700574 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700575
Adam Cohened66b2b2012-01-23 17:28:51 -0800576 // We have special handling for widgets
577 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800578 final int appWidgetId;
579 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
580 : -1;
581 if (widgetId < 0) {
582 appWidgetId = pendingAddWidgetId;
583 } else {
584 appWidgetId = widgetId;
585 }
586
Adam Cohenad4e15c2013-10-17 16:21:35 -0700587 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800588 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700589 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
590 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700591 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700592 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700593 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700594 @Override
595 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700596 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700597 }
598 };
Adam Cohendb364c32014-05-20 14:23:40 -0700599
Sunny Goyal2100c782016-08-22 16:00:03 -0700600 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
601 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
602 } else {
603 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
604 // When the screen id represents an actual screen (as opposed to a rank)
605 // we make sure that the drop page actually exists.
606 requestArgs.screenId =
607 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700608 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700609 final CellLayout dropLayout =
610 mWorkspace.getScreenWithId(requestArgs.screenId);
611
612 dropLayout.setDropPending(true);
613 final Runnable onComplete = new Runnable() {
614 @Override
615 public void run() {
616 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
617 dropLayout.setDropPending(false);
618 }
619 };
620 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
621 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700622 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800623 return;
624 }
625
Sunny Goyald478c832016-04-01 12:04:16 -0700626 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
627 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700628 if (resultCode == RESULT_OK) {
629 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700630 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700631 }
632 // Leave the widget in the pending state if the user canceled the configure.
633 return;
634 }
635
Sunny Goyalaad90582015-07-09 14:22:50 -0700636 if (requestCode == REQUEST_CREATE_SHORTCUT) {
637 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700638 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
639 completeAdd(requestCode, data, -1, requestArgs);
640 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
641 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
642
Sunny Goyalaad90582015-07-09 14:22:50 -0700643 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700644 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
645 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800646 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800647 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800648 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800649 }
650
651 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700652 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800653 final int requestCode, final int resultCode, final Intent data) {
654 handleActivityResult(requestCode, resultCode, data);
655 if (mLauncherCallbacks != null) {
656 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
657 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800658 }
659
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700660 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700661 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600662 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700663 PendingRequestArgs pendingArgs = mPendingRequestArgs;
664 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
665 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
666 setWaitingForResult(null);
667
Sunny Goyal28c6b962015-10-12 11:42:05 -0700668 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700669 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700670 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700671 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700672 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700673 Intent intent = pendingArgs.getPendingIntent();
674
Sunny Goyal28c6b962015-10-12 11:42:05 -0700675 if (grantResults.length > 0
676 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700677 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700678 } else {
679 // TODO: Show a snack bar with link to settings
680 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700681 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700682 }
683 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600684 if (mLauncherCallbacks != null) {
685 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
686 grantResults);
687 }
688 }
689
Adam Cohendb364c32014-05-20 14:23:40 -0700690 /**
691 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
692 *
693 * @param screenId the screen id to check
694 * @return the new screen, or screenId if it exists
695 */
696 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800697 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700698 if (dropLayout == null) {
699 // it's possible that the add screen was removed because it was
700 // empty and a re-bind occurred
701 mWorkspace.addExtraEmptyScreen();
702 return mWorkspace.commitExtraEmptyScreen();
703 } else {
704 return screenId;
705 }
706 }
707
Sunny Goyal2100c782016-08-22 16:00:03 -0700708 @Thunk void completeTwoStageWidgetDrop(
709 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
710 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800711 Runnable onCompleteRunnable = null;
712 int animationType = 0;
713
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700714 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800715 if (resultCode == RESULT_OK) {
716 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
717 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800718 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700719 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800720 onCompleteRunnable = new Runnable() {
721 @Override
722 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700723 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700724 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800725 }
726 };
727 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800728 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800729 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800730 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700731 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700732 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700733 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
734 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700735 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700736 // The animated view may be null in the case of a rotation during widget configuration
737 onCompleteRunnable.run();
738 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800739 }
740
741 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700742 protected void onStop() {
743 super.onStop();
744 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700745
746 if (mLauncherCallbacks != null) {
747 mLauncherCallbacks.onStop();
748 }
Hyunyoung Song1241e612018-05-15 21:46:51 -0700749 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
750 mStateManager.getState().containerType, -1);
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700751
752 mAppWidgetHost.setListenIfResumed(false);
753
Tony Wickham010d2552017-01-20 08:15:28 -0800754 NotificationListener.removeNotificationsChangedListener();
Sunny Goyalaf161fd2018-02-05 16:34:57 -0800755 getStateManager().moveToRestState();
Winson Chungedb0d6b2018-03-21 17:32:55 -0700756
Tracy Zhou62646712018-06-26 14:19:02 -0700757 UiFactory.onLauncherStateOrResumeChanged(this);
758
Winson Chungdea51352018-04-24 13:02:10 -0700759 // Workaround for b/78520668, explicitly trim memory once UI is hidden
Winson Chung473730f2018-04-26 11:54:22 -0700760 onTrimMemory(TRIM_MEMORY_UI_HIDDEN);
Michael Jurkacd496d72013-04-11 11:32:45 -0700761 }
762
763 @Override
764 protected void onStart() {
765 super.onStart();
766 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700767
768 if (mLauncherCallbacks != null) {
769 mLauncherCallbacks.onStart();
770 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800771 mAppWidgetHost.setListenIfResumed(true);
Sunny Goyalcac002d2018-01-04 09:51:45 -0800772 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
Winson Chung62a70be2018-02-23 15:10:05 -0800773 UiFactory.onStart(this);
Michael Jurkacd496d72013-04-11 11:32:45 -0700774 }
775
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700776 private void logOnDelayedResume() {
777 if (hasBeenResumed()) {
778 getUserEventDispatcher().logActionCommand(Action.Command.RESUME,
779 mStateManager.getState().containerType, -1);
780 getUserEventDispatcher().startSession();
781 }
782 }
783
Michael Jurkacd496d72013-04-11 11:32:45 -0700784 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800785 protected void onResume() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700786 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800787 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700788 TraceHelper.partitionSection("ON_RESUME", "superCall");
789
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700790 mHandler.removeCallbacks(mLogOnDelayedResume);
791 Utilities.postAsyncCallback(mHandler, mLogOnDelayedResume);
792
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700793 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700794 // Process any items that were added while Launcher was away.
795 InstallShortcutReceiver.disableAndFlushInstallQueue(
796 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700797
Sunny Goyala474a9b2017-05-04 16:47:11 -0700798 // Refresh shortcuts if the permission changed.
799 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700800
Tracy Zhoue5575f92018-04-19 11:12:53 -0700801 DiscoveryBounce.showForHomeIfNeeded(this);
Adam Cohen9211d422014-10-07 18:14:53 -0700802 if (mLauncherCallbacks != null) {
803 mLauncherCallbacks.onResume();
804 }
Tracy Zhoua706f002018-03-28 13:55:19 -0700805 UiFactory.onLauncherStateOrResumeChanged(this);
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800806
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700807 TraceHelper.endSection("ON_RESUME");
Adam Cohen06dff352012-06-01 17:17:08 -0700808 }
809
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800810 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700811 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700812 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700813 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700814
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700815 super.onPause();
Joe Onorato24b6fd82009-11-12 13:47:09 -0800816 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700817 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700818
Adam Cohen9211d422014-10-07 18:14:53 -0700819 if (mLauncherCallbacks != null) {
820 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700821 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700822 }
823
Winson Chungd7823942018-02-16 03:23:47 +0000824 @Override
Tracy Zhoua706f002018-03-28 13:55:19 -0700825 protected void onUserLeaveHint() {
826 super.onUserLeaveHint();
827 UiFactory.onLauncherStateOrResumeChanged(this);
828 }
829
830 @Override
Winson Chungd7823942018-02-16 03:23:47 +0000831 public void onWindowFocusChanged(boolean hasFocus) {
832 super.onWindowFocusChanged(hasFocus);
833 mStateManager.onWindowFocusChanged();
834 }
835
Adam Cohenc2d6e892014-10-16 09:49:24 -0700836 public interface LauncherOverlay {
837
838 /**
839 * Touch interaction leading to overscroll has begun
840 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700841 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700842
843 /**
844 * Touch interaction related to overscroll has ended
845 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700846 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700847
848 /**
849 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
850 * screen (or in the case of RTL, the rightmost screen).
851 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700852 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700853
854 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800855 * Called when the launcher is ready to use the overlay
856 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700857 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700858 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700859 }
860
861 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700862 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700863 }
864
865 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
866
Sunny Goyalc86df472016-02-25 09:19:38 -0800867 public void onScrollChanged(float progress) {
868 if (mWorkspace != null) {
869 mWorkspace.onOverlayScrollChanged(progress);
870 }
871 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700872 }
873
Sunny Goyal16764582017-11-06 16:00:34 -0800874 public boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -0700875 if (mLauncherCallbacks != null) {
876 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700877 } else {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800878 // On O and above we there is always some setting present settings (add icon to
879 // home screen or icon badging). On earlier APIs we will have the allow rotation
880 // setting, on devices with a locked orientation,
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700881 return Utilities.ATLEAST_OREO || !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -0700882 }
Jorim Jaggid017f882014-01-14 17:08:48 -0800883 }
884
Sunny Goyalf9403d92017-10-18 10:55:56 -0700885 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -0700886 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700887 }
888
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800889 /**
890 * Restores the previous state, if it exists.
891 *
892 * @param savedState The previous state.
893 */
894 private void restoreState(Bundle savedState) {
895 if (savedState == null) {
896 return;
897 }
898
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700899 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -0700900 LauncherState[] stateValues = LauncherState.values();
901 LauncherState state = stateValues[stateOrdinal];
Sunny Goyalbc683e92017-12-06 10:25:07 -0800902 if (!state.disableRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700903 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800904 }
905
Sunny Goyal2100c782016-08-22 16:00:03 -0700906 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
907 if (requestArgs != null) {
908 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800909 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700910
911 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700912
913 SparseArray<Parcelable> widgetsState =
914 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
915 if (widgetsState != null) {
916 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
917 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800918 }
919
920 /**
921 * Finds all the views we need and configure them properly.
922 */
923 private void setupViews() {
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800924 mDragLayer = findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700925 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -0700926 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700927 mWorkspace.initParentViews(mDragLayer);
Sunny Goyal16764582017-11-06 16:00:34 -0800928 mOverviewPanel = findViewById(R.id.overview_panel);
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800929 mHotseat = findViewById(R.id.hotseat);
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700930 mHotseatSearchBox = findViewById(R.id.search_container_hotseat);
Craig Mautner360310b2012-10-26 15:13:08 -0700931
Sunny Goyal784f9c32016-03-23 16:56:54 -0700932 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
933 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
934 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800935
Winson Chung4c98d922011-05-31 16:50:48 -0700936 // Setup the drag layer
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700937 mDragLayer.setup(mDragController, mWorkspace);
938 UiFactory.setOnTouchControllersChangedListener(this, mDragLayer::recreateControllers);
Winson Chung4c98d922011-05-31 16:50:48 -0700939
Hyunyoung Song645764e2016-06-06 14:19:02 -0700940 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -0700941 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
942 // default state, otherwise we will update to the wrong offsets in RTL
943 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700944 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700945 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700946
Tony Wickham34d2c912015-09-11 09:27:58 -0700947 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700948 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700949
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700950 // Setup Apps
951 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -0700952
Winson Chung3d503fb2011-07-13 17:25:49 -0700953 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -0700954 mDragController.setMoveTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700955 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -0400956
Sunny Goyal7185dd62018-03-14 17:51:49 -0700957 mAllAppsController.setupViews(mAppsView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800958 }
959
960 /**
961 * Creates a view representing a shortcut.
962 *
963 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800964 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800965 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700966 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800967 }
968
969 /**
970 * Creates a view representing a shortcut inflated from the specified resource.
971 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800972 * @param parent The group the shortcut belongs to.
973 * @param info The data structure describing the shortcut.
974 *
975 * @return A View inflated from layoutResId.
976 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700977 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -0800978 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
979 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800980 favorite.applyFromShortcutInfo(info);
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800981 favorite.setOnClickListener(ItemClickHandler.INSTANCE);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700982 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800983 return favorite;
984 }
985
986 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -0700987 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800988 *
989 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800990 */
Adam Cohendcd297f2013-06-18 13:13:40 -0700991 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700992 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -0700993 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
994 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700995 return;
996 }
Adam Cohen558baaf2011-08-15 15:22:57 -0700997
Jon Mirandac476d6e2017-05-04 16:30:01 -0700998 int[] cellXY = mTmpAddItemCellCoordinates;
999 CellLayout layout = getCellLayout(container, screenId);
1000
Sunny Goyal782f0c92017-01-19 10:27:54 -08001001 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001002 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001003 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
1004 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001005 }
1006
1007 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001008 // Legacy shortcuts are only supported for primary profile.
1009 info = Process.myUserHandle().equals(args.user)
1010 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001011
Sunny Goyalb57645f2017-03-20 13:57:28 -07001012 if (info == null) {
1013 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1014 return;
1015 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001016 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001017 // The app is trying to add a shortcut without sufficient permissions
1018 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1019 return;
1020 }
1021 }
1022
Jon Mirandac476d6e2017-05-04 16:30:01 -07001023 if (container < 0) {
1024 // Adding a shortcut to the Workspace.
1025 final View view = createShortcut(info);
1026 boolean foundCellSpan = false;
1027 // First we check if we already know the exact location where we want to add this item.
1028 if (cellX >= 0 && cellY >= 0) {
1029 cellXY[0] = cellX;
1030 cellXY[1] = cellY;
1031 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001032
Jon Mirandac476d6e2017-05-04 16:30:01 -07001033 // If appropriate, either create a folder or add to an existing folder
1034 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001035 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001036 return;
1037 }
1038 DragObject dragObject = new DragObject();
1039 dragObject.dragInfo = info;
1040 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1041 true)) {
1042 return;
1043 }
1044 } else {
1045 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1046 }
1047
1048 if (!foundCellSpan) {
1049 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001050 return;
1051 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001052
1053 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1054 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001055 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001056 // Adding a shortcut to a Folder.
Sunny Goyale29897f2017-07-20 10:09:42 -07001057 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001058 if (folderIcon != null) {
1059 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1060 folderInfo.add(info, args.rank, false);
1061 } else {
Sunny Goyale29897f2017-07-20 10:09:42 -07001062 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001063 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001064 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001065 }
1066
Sunny Goyale29897f2017-07-20 10:09:42 -07001067 public FolderIcon findFolderIcon(final long folderIconId) {
Sunny Goyal83fd25e2018-07-09 16:47:01 -07001068 return (FolderIcon) mWorkspace.getHomescreenIconByItemId(folderIconId);
Sunny Goyale29897f2017-07-20 10:09:42 -07001069 }
1070
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001071 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001072 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001073 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001074 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001075 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001076 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001077 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1078
Adam Cohened66b2b2012-01-23 17:28:51 -08001079 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001080 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001081 }
Romain Guycbb89e42009-06-08 15:52:54 -07001082
Adam Cohen59400422014-03-05 18:07:04 -08001083 LauncherAppWidgetInfo launcherInfo;
1084 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001085 launcherInfo.spanX = itemInfo.spanX;
1086 launcherInfo.spanY = itemInfo.spanY;
1087 launcherInfo.minSpanX = itemInfo.minSpanX;
1088 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001089 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001090
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001091 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001092 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001093
Sunny Goyal2100c782016-08-22 16:00:03 -07001094 if (hostView == null) {
1095 // Perform actual inflation because we're live
1096 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001097 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001098 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301099 prepareAppWidget(hostView, launcherInfo);
1100 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001101 }
Romain Guycbb89e42009-06-08 15:52:54 -07001102
Sunny Goyald5462aa2016-11-22 16:52:39 +05301103 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001104 hostView.setTag(item);
1105 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001106 hostView.setFocusable(true);
1107 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001108 }
1109
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001110 private final BroadcastReceiver mScreenOffReceiver = new BroadcastReceiver() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001111 @Override
1112 public void onReceive(Context context, Intent intent) {
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001113 // Reset AllApps to its initial state only if we are not in the middle of
1114 // processing a multi-step drop
1115 if (mPendingRequestArgs == null) {
1116 mStateManager.goToState(NORMAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001117 }
1118 }
1119 };
1120
Tony Wickham010d2552017-01-20 08:15:28 -08001121 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
Sunny Goyal29947f02017-12-18 13:49:44 -08001122 mWorkspace.updateIconBadges(updatedBadges);
Sunny Goyal60180b02018-02-07 12:56:30 -08001123 mAppsView.getAppsStore().updateIconBadges(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001124
Sunny Goyal29947f02017-12-18 13:49:44 -08001125 PopupContainerWithArrow popup = PopupContainerWithArrow.getOpen(Launcher.this);
1126 if (popup != null) {
1127 popup.updateNotificationHeader(updatedBadges);
Tony Wickham010d2552017-01-20 08:15:28 -08001128 }
1129 }
1130
Adam Cohended9f8d2010-11-03 13:25:16 -07001131 @Override
1132 public void onAttachedToWindow() {
1133 super.onAttachedToWindow();
1134
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001135 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyalc86df472016-02-25 09:19:38 -08001136 if (mLauncherCallbacks != null) {
1137 mLauncherCallbacks.onAttachedToWindow();
1138 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001139 }
1140
1141 @Override
1142 public void onDetachedFromWindow() {
1143 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001144
1145 if (mLauncherCallbacks != null) {
1146 mLauncherCallbacks.onDetachedFromWindow();
1147 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001148 }
1149
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001150 public AllAppsTransitionController getAllAppsController() {
1151 return mAllAppsController;
1152 }
1153
Sunny Goyal9d69c8d2018-03-19 13:41:31 -07001154 @Override
Winson Chung1a341002018-01-17 10:00:23 -08001155 public LauncherRootView getRootView() {
1156 return (LauncherRootView) mLauncherView;
1157 }
1158
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001159 @Override
Winson Chunga6945242014-01-08 14:04:34 -08001160 public DragLayer getDragLayer() {
1161 return mDragLayer;
1162 }
1163
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001164 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001165 return mAppsView;
1166 }
1167
Winson Chunga6945242014-01-08 14:04:34 -08001168 public Workspace getWorkspace() {
1169 return mWorkspace;
1170 }
1171
1172 public Hotseat getHotseat() {
1173 return mHotseat;
1174 }
1175
Sunny Goyal81b4c7b2018-03-26 12:10:31 -07001176 public View getHotseatSearchBox() {
1177 return mHotseatSearchBox;
1178 }
1179
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001180 public <T extends View> T getOverviewPanel() {
Winson Chung8ae41982017-11-10 11:42:13 -08001181 return (T) mOverviewPanel;
Winson Chunga6945242014-01-08 14:04:34 -08001182 }
1183
Sunny Goyal47328fd2016-05-25 18:56:41 -07001184 public DropTargetBar getDropTargetBar() {
1185 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001186 }
1187
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001188 public LauncherAppWidgetHost getAppWidgetHost() {
1189 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001190 }
Romain Guycbb89e42009-06-08 15:52:54 -07001191
Winson Chunga9abd0e2010-10-27 17:18:37 -07001192 public LauncherModel getModel() {
1193 return mModel;
1194 }
1195
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001196 public ModelWriter getModelWriter() {
1197 return mModelWriter;
1198 }
1199
Adam Cohen79d90c52016-04-22 13:29:20 -07001200 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001201 return mSharedPrefs;
1202 }
1203
Sunny Goyalf8d56fc2018-01-31 15:18:11 -08001204 public int getOrientation() { return mOldConfig.orientation; }
Jon Miranda6bed3502017-09-19 14:43:17 -07001205
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001206 @Override
1207 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001208 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001209 super.onNewIntent(intent);
1210
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001211 boolean alreadyOnHome = hasWindowFocus() && ((intent.getFlags() &
Winson15f8b172015-08-19 11:02:39 -07001212 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1213 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001214
1215 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001216 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001217 && AbstractFloatingView.getTopOpenView(this) == null;
Winson15f8b172015-08-19 11:02:39 -07001218 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001219 boolean internalStateHandled = InternalStateHandler
Sunny Goyal8b264562018-02-03 23:00:13 -08001220 .handleNewIntent(this, intent, isStarted());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001221
Winson15f8b172015-08-19 11:02:39 -07001222 if (isActionMain) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001223 if (!internalStateHandled) {
1224 // Note: There should be at most one log per method call. This is enforced
1225 // implicitly by using if-else statements.
1226 UserEventDispatcher ued = getUserEventDispatcher();
1227 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
1228 if (topOpenView != null) {
1229 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
1230 } else if (alreadyOnHome) {
1231 Target target = newContainerTarget(mStateManager.getState().containerType);
1232 target.pageIndex = mWorkspace.getCurrentPage();
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001233 ued.logActionCommand(Action.Command.HOME_INTENT, target,
1234 newContainerTarget(ContainerType.WORKSPACE));
Sunny Goyald3864fa2017-11-14 15:06:36 -08001235 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001236
Sunny Goyald3864fa2017-11-14 15:06:36 -08001237 // In all these cases, only animate if we're already on home
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001238 AbstractFloatingView.closeAllOpenViews(this, isStarted());
Jon Mirandafeba90f2016-10-06 10:53:29 -07001239
Sunny Goyala2467272018-03-16 14:53:05 -07001240 if (!isInState(NORMAL)) {
1241 // Only change state, if not already the same. This prevents cancelling any
1242 // animations running as part of resume
1243 mStateManager.goToState(NORMAL);
1244 }
Sunny Goyald3864fa2017-11-14 15:06:36 -08001245
1246 // Reset the apps view
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001247 if (!alreadyOnHome) {
Sunny Goyal7185dd62018-03-14 17:51:49 -07001248 mAppsView.reset(isStarted() /* animate */);
Sunny Goyald3864fa2017-11-14 15:06:36 -08001249 }
1250
Sunny Goyalaad33592018-08-07 17:20:35 -07001251 if (shouldMoveToDefaultScreen && !mWorkspace.isHandlingTouch()) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001252 mWorkspace.post(mWorkspace::moveToDefaultScreen);
1253 }
1254 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001255
1256 final View v = getWindow().peekDecorView();
1257 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001258 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001259 }
1260
Adam Cohen9211d422014-10-07 18:14:53 -07001261 if (mLauncherCallbacks != null) {
Sunny Goyal85462132018-04-24 11:39:37 -07001262 mLauncherCallbacks.onHomeIntent(internalStateHandled);
Adam Cohen9211d422014-10-07 18:14:53 -07001263 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001264 }
Winson15f8b172015-08-19 11:02:39 -07001265
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001266 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001267 }
1268
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001269 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001270 public void onRestoreInstanceState(Bundle state) {
1271 super.onRestoreInstanceState(state);
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001272 mWorkspace.restoreInstanceStateForChild(mSynchronouslyBoundPage);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001273 }
1274
1275 @Override
1276 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001277 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001278 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001279
Adam Cohen21cd0022013-10-09 18:57:02 -07001280 }
Sunny Goyalea609262017-10-25 15:47:38 -07001281 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001282
1283
1284 AbstractFloatingView widgets = AbstractFloatingView
1285 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1286 if (widgets != null) {
1287 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1288 widgets.saveHierarchyState(widgetsState);
1289 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1290 } else {
1291 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1292 }
1293
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001294 // We close any open folders and shortcut containers since they will not be re-opened,
1295 // and we need to make sure this state is reflected.
1296 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001297
Sunny Goyal2100c782016-08-22 16:00:03 -07001298 if (mPendingRequestArgs != null) {
1299 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1300 }
1301 if (mPendingActivityResult != null) {
1302 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001303 }
1304
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001305 super.onSaveInstanceState(outState);
1306
Adam Cohen9211d422014-10-07 18:14:53 -07001307 if (mLauncherCallbacks != null) {
1308 mLauncherCallbacks.onSaveInstanceState(outState);
1309 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001310 }
1311
1312 @Override
1313 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001314 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001315
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001316 unregisterReceiver(mScreenOffReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001317 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001318
Tony Wickhama0068302018-04-11 16:16:11 -07001319 UiFactory.setOnTouchControllersChangedListener(this, null);
1320
Winson Chungcd2b0142011-06-08 16:02:26 -07001321 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001322 // It's possible to receive onDestroy after a new Launcher activity has
1323 // been created. In this case, don't interfere with the new Launcher.
1324 if (mModel.isCurrentCallbacks(this)) {
1325 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001326 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001327 }
Sunny Goyal623eddd2018-03-02 12:24:41 -08001328 mRotationHelper.destroy();
Sunny Goyal745bad92016-05-02 10:54:12 -07001329
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001330 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001331 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001332 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001333 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001334 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001335
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001336 TextKeyListener.getInstance().release();
Tony2917a8b2017-07-31 23:29:42 -07001337 clearPendingBinds();
1338
Adam Cohen9211d422014-10-07 18:14:53 -07001339 if (mLauncherCallbacks != null) {
1340 mLauncherCallbacks.onDestroy();
1341 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001342 }
1343
Sunny Goyalae502842016-06-17 08:43:56 -07001344 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1345 return mAccessibilityDelegate;
1346 }
1347
Adam Cohena9cf38f2011-05-02 15:36:58 -07001348 public DragController getDragController() {
1349 return mDragController;
1350 }
1351
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001352 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001353 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001354 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001355 }
1356
1357 @Override
1358 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1359 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001360 try {
1361 super.startIntentSenderForResult(intent, requestCode,
1362 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1363 } catch (IntentSender.SendIntentException e) {
1364 throw new ActivityNotFoundException();
1365 }
1366 }
1367
Winson Chung70d72102011-08-12 11:24:35 -07001368 /**
1369 * Indicates that we want global search for this activity by setting the globalSearch
1370 * argument for {@link #startSearch} to true.
1371 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001372 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001373 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001374 Bundle appSearchData, boolean globalSearch) {
1375 if (appSearchData == null) {
1376 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001377 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001378 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001379
Sunny Goyal6f28e712016-09-13 14:19:29 -07001380 if (mLauncherCallbacks == null ||
1381 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1382 // Starting search from the callbacks failed. Start the default global search.
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001383 super.startSearch(initialQuery, selectInitialQuery, appSearchData, true);
Ian Parkinson047036e2014-09-01 15:40:53 +01001384 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001385
1386 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001387 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001388 }
1389
Joe Onorato9c1289c2009-08-17 11:03:03 -04001390 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001391 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001392 }
1393
Adam Cohen517a7f52014-03-01 12:12:59 -08001394 public boolean isWorkspaceLoading() {
1395 return mWorkspaceLoading;
1396 }
1397
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001398 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001399 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001400 }
1401
Sunny Goyal04a324a2017-01-20 21:08:59 -08001402 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001403 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001404 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001405
Sunny Goyal2100c782016-08-22 16:00:03 -07001406 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001407 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001408 if (LOGD) {
1409 Log.d(TAG, "Adding widget from drop");
1410 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001411 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001412 }
1413
Sunny Goyal2100c782016-08-22 16:00:03 -07001414 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001415 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1416 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1417 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001418
Adam Cohenad4e15c2013-10-17 16:21:35 -07001419 Runnable onComplete = new Runnable() {
1420 @Override
1421 public void run() {
1422 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001423 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001424 }
1425 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001426 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001427 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001428 }
1429 }
Romain Guycbb89e42009-06-08 15:52:54 -07001430
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001431 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1432 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001433 info.container = container;
1434 info.screenId = screenId;
1435 if (cell != null) {
1436 info.cellX = cell[0];
1437 info.cellY = cell[1];
1438 }
1439 info.spanX = spanX;
1440 info.spanY = spanY;
1441
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001442 switch (info.itemType) {
1443 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1444 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001445 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001446 break;
1447 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001448 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001449 break;
1450 default:
1451 throw new IllegalStateException("Unknown item type: " + info.itemType);
1452 }
1453 }
1454
Adam Cohenfbba09b2011-07-18 21:43:05 -07001455 /**
1456 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001457 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001458 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001459 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1460 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001461 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1462 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1463 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001464 }
1465
Adam Cohenfbba09b2011-07-18 21:43:05 -07001466 /**
1467 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001468 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001469 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001470 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001471 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001472 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001473 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001474 // In the case where we've prebound the widget, we remove it from the DragLayer
1475 if (LOGD) {
1476 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1477 }
1478 getDragLayer().removeView(hostView);
1479
Adam Cohened66b2b2012-01-23 17:28:51 -08001480 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001481 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001482
1483 // Clear the boundWidget so that it doesn't get destroyed.
1484 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001485 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001486 // In this case, we either need to start an activity to get permission to bind
1487 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001488 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1489 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1490 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1491 this, info.componentName);
1492 } else {
1493 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1494 }
Adam Cohendd70d662012-10-04 16:53:44 -07001495 Bundle options = info.bindOptions;
1496
Sunny Goyalffe83f12014-08-14 17:39:34 -07001497 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1498 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001499 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001500 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001501 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001502 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001503 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001504 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001505 }
1506
Adam Cohendcd297f2013-06-18 13:13:40 -07001507 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001508 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001509 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001510 folderInfo.title = getText(R.string.folder_name);
1511
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001512 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001513 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001514
1515 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001516 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301517 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001518 // Force measure the new folder icon
1519 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1520 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001521 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001522 }
Romain Guycbb89e42009-06-08 15:52:54 -07001523
Winsonc0b52fe2015-09-09 16:38:15 -07001524 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001525 * Unbinds the view for the specified item, and removes the item and all its children.
1526 *
1527 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001528 * @param itemInfo the {@link ItemInfo} for this view.
1529 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001530 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001531 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001532 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001533 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001534 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1535 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001536 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001537 } else {
1538 mWorkspace.removeWorkspaceItem(v);
1539 }
Winsonc0b52fe2015-09-09 16:38:15 -07001540 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001541 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001542 }
1543 } else if (itemInfo instanceof FolderInfo) {
1544 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001545 if (v instanceof FolderIcon) {
1546 ((FolderIcon) v).removeListeners();
1547 }
Winsonc0b52fe2015-09-09 16:38:15 -07001548 mWorkspace.removeWorkspaceItem(v);
1549 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001550 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001551 }
1552 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1553 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001554 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001555 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07001556 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001557 }
1558 } else {
1559 return false;
1560 }
1561 return true;
1562 }
1563
1564 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07001565 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07001566 */
Sunny Goyal56c73602015-09-25 12:55:01 -07001567 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07001568 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07001569 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07001570 // Deleting an app widget ID is a void call but writes to disk before returning
1571 // to the caller...
1572 new AsyncTask<Void, Void, Void>() {
1573 public Void doInBackground(Void ... args) {
1574 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
1575 return null;
1576 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07001577 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07001578 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001579 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001580 }
1581
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001582 @Override
1583 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001584 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001585 }
1586
Joe Onorato88ec0992009-11-19 13:16:06 -08001587 @Override
1588 public void onBackPressed() {
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08001589 if (finishAutoCancelActionMode()) {
1590 return;
1591 }
Adam Cohen9211d422014-10-07 18:14:53 -07001592 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1593 return;
1594 }
1595
Sunny Goyal45478022015-06-08 16:52:41 -07001596 if (mDragController.isDragging()) {
1597 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001598 return;
1599 }
1600
Jon Mirandafeba90f2016-10-06 10:53:29 -07001601 // Note: There should be at most one log per method call. This is enforced implicitly
1602 // by using if-else statements.
1603 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001604 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
Tony Wickham52c1b662018-05-21 13:13:58 -07001605 if (topView != null && topView.onBackPressed()) {
1606 // Handled by the floating view.
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001607 } else {
Sunny Goyale39690b2018-08-02 13:35:07 -07001608 mStateManager.getState().onBackPressed(this);
Michael Jurkaaf442092010-06-10 17:01:57 -07001609 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001610 }
1611
Sunny Goyalfe770c92016-09-27 14:38:58 -07001612 @TargetApi(Build.VERSION_CODES.M)
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001613 @Override
Jon Miranda73c27ec2018-05-16 18:06:23 -07001614 public ActivityOptions getActivityLaunchOptions(View v) {
1615 return mAppTransitionManager.getActivityLaunchOptions(this, v);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001616 }
1617
Tony Wickham3a6746a2018-03-29 09:39:56 -07001618 public LauncherAppTransitionManager getAppTransitionManager() {
1619 return mAppTransitionManager;
1620 }
1621
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001622 @TargetApi(Build.VERSION_CODES.M)
1623 @Override
1624 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
1625 // Due to legacy reasons, direct call shortcuts require Launchers to have the
1626 // corresponding permission. Show the appropriate permission prompt if that
1627 // is the case.
1628 if (intent.getComponent() == null
1629 && Intent.ACTION_CALL.equals(intent.getAction())
1630 && checkSelfPermission(android.Manifest.permission.CALL_PHONE) !=
1631 PackageManager.PERMISSION_GRANTED) {
1632
1633 setWaitingForResult(PendingRequestArgs
1634 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
1635 requestPermissions(new String[]{android.Manifest.permission.CALL_PHONE},
1636 REQUEST_PERMISSION_CALL_PHONE);
1637 return true;
1638 } else {
1639 return false;
1640 }
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001641 }
1642
Hyunyoung Song46d07f72018-05-22 15:41:25 -07001643 @Override
1644 public void modifyUserEvent(LauncherLogProto.LauncherEvent event) {
1645 if (event.srcTarget != null && event.srcTarget.length > 0 &&
1646 event.srcTarget[1].containerType == ContainerType.PREDICTION) {
1647 Target[] targets = new Target[3];
1648 targets[0] = event.srcTarget[0];
1649 targets[1] = event.srcTarget[1];
1650 targets[2] = newTarget(Target.Type.CONTAINER);
1651 event.srcTarget = targets;
1652 LauncherState state = mStateManager.getState();
1653 if (state == LauncherState.ALL_APPS) {
1654 event.srcTarget[2].containerType = ContainerType.ALLAPPS;
1655 } else if (state == LauncherState.OVERVIEW) {
1656 event.srcTarget[2].containerType = ContainerType.TASKSWITCHER;
1657 }
1658 }
1659 }
1660
Sunny Goyala7ce1662016-05-31 15:01:35 -07001661 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Hyunyoung Song1241e612018-05-15 21:46:51 -07001662 boolean success = super.startActivitySafely(v, intent, item);
1663 if (success && v instanceof BubbleTextView) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001664 // This is set to the view that launched the activity that navigated the user away
1665 // from launcher. Since there is no callback for when the activity has finished
1666 // launching, enable the press state and keep this reference to reset the press
1667 // state when we return to launcher.
1668 BubbleTextView btv = (BubbleTextView) v;
1669 btv.setStayPressed(true);
1670 setOnResumeCallback(btv);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001671 }
Hyunyoung Song1241e612018-05-15 21:46:51 -07001672 return success;
Michael Jurka86a720e2012-05-09 11:23:23 -07001673 }
1674
Winson Chung3d503fb2011-07-13 17:25:49 -07001675 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001676 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07001677 return mHotseat != null && layout != null &&
1678 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
1679 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001680
Winson Chung3d503fb2011-07-13 17:25:49 -07001681 /**
1682 * Returns the CellLayout of the specified container at the specified screen.
1683 */
Sunny Goyal92820592015-03-02 11:31:35 -08001684 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001685 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1686 if (mHotseat != null) {
1687 return mHotseat.getLayout();
1688 } else {
1689 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08001690 }
Winson Chung3d503fb2011-07-13 17:25:49 -07001691 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08001692 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08001693 }
1694 }
1695
Michael Jurkae326f182011-11-21 14:05:46 -08001696 @Override
1697 public void onTrimMemory(int level) {
1698 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07001699 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
1700 // The widget preview db can result in holding onto over
1701 // 3MB of memory for caching which isn't necessary.
1702 SQLiteDatabase.releaseMemory();
1703
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08001704 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07001705 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08001706 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01001707 if (mLauncherCallbacks != null) {
1708 mLauncherCallbacks.onTrimMemory(level);
1709 }
Winson Chung62a70be2018-02-23 15:10:05 -08001710 UiFactory.onTrimMemory(this, level);
Michael Jurkae326f182011-11-21 14:05:46 -08001711 }
1712
Michael Jurkad7c28052012-04-27 15:43:36 -07001713 @Override
1714 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07001715 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07001716 final List<CharSequence> text = event.getText();
1717 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07001718 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001719 // TODO: When can workspace be null?
1720 text.add(mWorkspace == null
1721 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07001722 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07001723 return result;
1724 }
1725
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001726 public void setOnResumeCallback(OnResumeCallback callback) {
1727 if (mOnResumeCallback != null) {
1728 mOnResumeCallback.onLauncherResume();
1729 }
1730 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07001731 }
1732
Michael Jurka7607c2f2013-04-03 14:33:19 -07001733 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001734 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001735 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07001736 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001737 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07001738 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001739 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07001740 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07001741 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07001742 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001743 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001744
Joe Onorato9c1289c2009-08-17 11:03:03 -04001745 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07001746 * Clear any pending bind callbacks. This is called when is loader is planning to
1747 * perform a full rebind from scratch.
1748 */
1749 @Override
1750 public void clearPendingBinds() {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001751 if (mPendingExecutor != null) {
1752 mPendingExecutor.markCompleted();
1753 mPendingExecutor = null;
1754 }
1755 }
1756
1757 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001758 * Refreshes the shortcuts shown on the workspace.
1759 *
1760 * Implementation of the method from LauncherModel.Callbacks.
1761 */
1762 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001763 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001764 // Floating panels (except the full widget sheet) are associated with individual icons. If
1765 // we are starting a fresh bind, close all such panels as all the icons are about
1766 // to go away.
1767 AbstractFloatingView.closeOpenViews(this, true,
Sunny Goyal7ede6112017-12-05 15:11:21 -08001768 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_REBIND_SAFE);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08001769
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001770 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08001771
Winson Chungd64d1762013-08-20 14:37:16 -07001772 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07001773 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07001774 mWorkspace.removeAllWorkspaceScreens();
Sunny Goyalc11fac32018-02-27 19:20:15 -08001775 mAppWidgetHost.clearViews();
Winson Chungd64d1762013-08-20 14:37:16 -07001776
Winson Chung3d503fb2011-07-13 17:25:49 -07001777 if (mHotseat != null) {
Sunny Goyal11e96492018-03-23 17:06:00 -07001778 mHotseat.resetLayout(mDeviceProfile.isVerticalBarLayout());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001779 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001780 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001781 }
1782
Adam Cohendcd297f2013-06-18 13:13:40 -07001783 @Override
1784 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001785 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001786 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
1787 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001788 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
1789 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001790 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001791 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
1792 // 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 Goyalda4fe1a2016-05-26 16:05:17 -07001803 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001804 int count = orderedScreenIds.size();
1805 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001806 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -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
Sunny Goyal29947f02017-12-18 13:49:44 -08001815 public void bindAppsAdded(ArrayList<Long> newScreens, ArrayList<ItemInfo> addNotAnimated,
1816 ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07001817 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08001818 if (newScreens != null) {
1819 bindAddScreens(newScreens);
1820 }
Winson Chungd64d1762013-08-20 14:37:16 -07001821
1822 // We add the items without animation on non-visible pages, and with
1823 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08001824 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001825 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001826 }
Adam Cohen76a47a12014-02-05 11:47:43 -08001827 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001828 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07001829 }
Winson Chungc58497e2013-09-03 17:48:37 -07001830
Winson Chung87412982013-10-03 18:34:14 -07001831 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07001832 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001833 }
1834
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001835 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001836 * Bind the items start-end from the list.
1837 *
1838 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001839 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07001840 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07001841 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001842 // Get the list of added items and intersect them with the set of items here
Sunny Goyal849c6a22018-08-08 16:33:46 -07001843 final AnimatorSet anim = new AnimatorSet();
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001844 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07001845 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001846 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001847 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07001848 int end = items.size();
1849 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001850 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07001851
1852 // Short circuit if we are loading dock items for a configuration which has no dock
1853 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
1854 mHotseat == null) {
1855 continue;
1856 }
1857
Sunny Goyal639e9062015-08-19 19:17:06 -07001858 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001859 switch (item.itemType) {
1860 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1861 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08001862 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001863 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07001864 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001865 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001866 }
1867 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07001868 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001869 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001870 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001871 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001872 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07001873 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1874 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07001875 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07001876 if (view == null) {
1877 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001878 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08001879 break;
1880 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001881 default:
1882 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001883 }
Sunny Goyal639e9062015-08-19 19:17:06 -07001884
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001885 /*
1886 * Remove colliding items.
1887 */
1888 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1889 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
1890 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
1891 View v = cl.getChildAt(item.cellX, item.cellY);
1892 Object tag = v.getTag();
1893 String desc = "Collision while binding workspace item: " + item
1894 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08001895 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001896 throw (new RuntimeException(desc));
1897 } else {
1898 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001899 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001900 continue;
1901 }
1902 }
1903 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05301904 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07001905 if (animateIcons) {
1906 // Animate all the applications up now
1907 view.setAlpha(0f);
1908 view.setScaleX(0f);
1909 view.setScaleY(0f);
1910 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08001911 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07001912 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001913 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07001914
Winson Chung997a9232013-07-24 15:33:46 -07001915 if (animateIcons) {
1916 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08001917 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07001918 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08001919 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07001920 final Runnable startBounceAnimRunnable = new Runnable() {
1921 public void run() {
1922 anim.playTogether(bounceAnims);
1923 anim.start();
1924 }
1925 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08001926 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07001927 // We post the animation slightly delayed to prevent slowdowns
1928 // when we are loading right after we return to launcher.
1929 mWorkspace.postDelayed(new Runnable() {
1930 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00001931 if (mWorkspace != null) {
Jon Miranda64eb8ea2018-03-29 11:43:58 -07001932 AbstractFloatingView.closeAllOpenViews(Launcher.this, false);
1933
Ian Parkinson94449152014-01-21 13:09:59 +00001934 mWorkspace.snapToPage(newScreenIndex);
1935 mWorkspace.postDelayed(startBounceAnimRunnable,
1936 NEW_APPS_ANIMATION_DELAY);
1937 }
Winson Chung94d67682013-09-25 16:29:40 -07001938 }
1939 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07001940 } else {
1941 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07001942 }
1943 }
Winson Chung64359a52013-07-08 17:17:08 -07001944 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001945 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001946 }
1947
Joe Onorato9c1289c2009-08-17 11:03:03 -04001948 /**
1949 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001950 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07001951 public void bindAppWidget(LauncherAppWidgetInfo item) {
1952 View view = inflateAppWidget(item);
1953 if (view != null) {
1954 mWorkspace.addInScreen(view, item);
1955 mWorkspace.requestLayout();
1956 }
1957 }
1958
1959 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001960 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05301961 PendingAppWidgetHostView view =
1962 new PendingAppWidgetHostView(this, item, mIconCache, true);
1963 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07001964 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001965 }
1966
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001967 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001968
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001969 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08001970
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001971 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
1972 // If the provider is not ready, bind as a pending widget.
1973 appWidgetInfo = null;
1974 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
1975 // The widget id is not valid. Try to find the widget based on the provider info.
1976 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
1977 } else {
1978 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
1979 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07001980
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001981 // If the provider is ready, but the width is not yet restored, try to restore it.
1982 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
1983 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07001984 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001985 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
1986 + " belongs to component " + item.providerName
1987 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001988 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07001989 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07001990 }
Sunny Goyalff572272014-07-23 13:58:07 -07001991
Sunny Goyal84b4adc2015-08-12 15:12:16 -07001992 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001993 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07001994 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
1995 // Id has not been allocated yet. Allocate a new id.
1996 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
1997 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07001998
Sunny Goyald478c832016-04-01 12:04:16 -07001999 // Also try to bind the widget. If the bind fails, the user will be shown
2000 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08002001 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07002002 pendingInfo.spanX = item.spanX;
2003 pendingInfo.spanY = item.spanY;
2004 pendingInfo.minSpanX = item.minSpanX;
2005 pendingInfo.minSpanY = item.minSpanY;
2006 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07002007
2008 boolean isDirectConfig =
2009 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2010 if (isDirectConfig && item.bindOptions != null) {
2011 Bundle newOptions = item.bindOptions.getExtras();
2012 if (options != null) {
2013 newOptions.putAll(options);
2014 }
2015 options = newOptions;
2016 }
Sunny Goyald478c832016-04-01 12:04:16 -07002017 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2018 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002019
Sunny Goyal86df1382016-08-10 15:03:22 -07002020 // We tried to bind once. If we were not able to bind, we would need to
2021 // go through the permission dialog, which means we cannot skip the config
2022 // activity.
2023 item.bindOptions = null;
2024 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2025
Sunny Goyald478c832016-04-01 12:04:16 -07002026 // Bind succeeded
2027 if (success) {
2028 // If the widget has a configure activity, it is still needs to set it up,
2029 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002030 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002031 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2032 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002033 }
Sunny Goyald478c832016-04-01 12:04:16 -07002034
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002035 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002036 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002037 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002038 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002039 // The widget was marked as UI not ready, but there is no configure activity to
2040 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002041 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002042 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002043 }
Sunny Goyalff572272014-07-23 13:58:07 -07002044 }
2045
Sunny Goyald5462aa2016-11-22 16:52:39 +05302046 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002047 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002048 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002049 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002050 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07002051 deleteWidgetInfo(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002052 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002053 }
2054
Sunny Goyal233ee962015-08-03 13:05:01 -07002055 item.minSpanX = appWidgetInfo.minSpanX;
2056 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302057 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002058 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302059 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002060 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302061 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002062
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002063 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002064 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002065 }
2066
Sunny Goyalff572272014-07-23 13:58:07 -07002067 /**
2068 * Restores a pending widget.
2069 *
2070 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002071 */
Sunny Goyald478c832016-04-01 12:04:16 -07002072 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002073 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2074 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2075 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002076 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002077 }
2078
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002079 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002080 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002081 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2082 info.pendingItemInfo = null;
2083 }
Sunny Goyalff572272014-07-23 13:58:07 -07002084
Sunny Goyalba47faa2017-10-04 16:50:57 -07002085 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002086 view.reInflate();
Sunny Goyalba47faa2017-10-04 16:50:57 -07002087 }
2088
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002089 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002090 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002091 }
2092
Adam Cohen1462de32012-07-24 22:34:36 -07002093 public void onPageBoundSynchronously(int page) {
Sunny Goyalf4aceab2017-12-06 16:16:31 -08002094 mSynchronouslyBoundPage = page;
Adam Cohen1462de32012-07-24 22:34:36 -07002095 }
2096
Sunny Goyal527c7d32015-08-28 15:19:36 -07002097 @Override
2098 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2099 if (mPendingExecutor != null) {
2100 mPendingExecutor.markCompleted();
2101 }
2102 mPendingExecutor = executor;
Sunny Goyal60180b02018-02-07 12:56:30 -08002103 if (!isInState(ALL_APPS)) {
2104 mAppsView.getAppsStore().setDeferUpdates(true);
2105 mPendingExecutor.execute(() -> mAppsView.getAppsStore().setDeferUpdates(false));
2106 }
2107
Sunny Goyal527c7d32015-08-28 15:19:36 -07002108 executor.attachTo(this);
2109 }
2110
2111 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2112 if (mPendingExecutor == executor) {
2113 mPendingExecutor = null;
2114 }
2115 }
2116
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002117 @Override
2118 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
Sunny Goyal6001ea22018-05-10 16:31:00 -07002119 AlphaProperty property = mDragLayer.getAlphaProperty(ALPHA_INDEX_LAUNCHER_LOAD);
2120 if (property.getValue() < 1) {
2121 ObjectAnimator anim = ObjectAnimator.ofFloat(property, MultiValueAlpha.VALUE, 1);
2122 if (executor != null) {
2123 anim.addListener(new AnimatorListenerAdapter() {
2124 @Override
2125 public void onAnimationEnd(Animator animation) {
2126 executor.onLoadAnimationCompleted();
2127 }
2128 });
2129 }
2130 anim.start();
Sunny Goyal29947f02017-12-18 13:49:44 -08002131 } else if (executor != null) {
2132 executor.onLoadAnimationCompleted();
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002133 }
2134 }
2135
Joe Onorato9c1289c2009-08-17 11:03:03 -04002136 /**
2137 * Callback saying that there aren't any more items to bind.
2138 *
2139 * Implementation of the method from LauncherModel.Callbacks.
2140 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002141 public void finishBindingItems() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002142 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002143 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002144
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002145 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002146
Sunny Goyal2100c782016-08-22 16:00:03 -07002147 if (mPendingActivityResult != null) {
2148 handleActivityResult(mPendingActivityResult.requestCode,
2149 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2150 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002151 }
2152
Sunny Goyala474a9b2017-05-04 16:47:11 -07002153 InstallShortcutReceiver.disableAndFlushInstallQueue(
2154 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002155
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002156 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002157 }
2158
Winson Chunga2413752012-04-03 14:22:34 -07002159 private boolean canRunNewAppsAnimation() {
2160 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002161 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002162 }
2163
Winson Chungc9168342013-06-26 14:54:55 -07002164 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal849c6a22018-08-08 16:33:46 -07002165 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
2166 PropertyValuesHolder.ofFloat(View.ALPHA, 1f),
2167 PropertyValuesHolder.ofFloat(View.SCALE_X, 1f),
2168 PropertyValuesHolder.ofFloat(View.SCALE_Y, 1f));
Winson Chungc9168342013-06-26 14:54:55 -07002169 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
2170 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002171 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002172 return bounceAnim;
2173 }
2174
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002175 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002176 * Add the icons for all apps.
2177 *
2178 * Implementation of the method from LauncherModel.Callbacks.
2179 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002180 public void bindAllApplications(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002181 mAppsView.getAppsStore().setApps(apps);
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002182
Adam Cohen9211d422014-10-07 18:14:53 -07002183 if (mLauncherCallbacks != null) {
2184 mLauncherCallbacks.bindAllApplications(apps);
2185 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002186 }
2187
2188 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002189 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
2190 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2191 */
2192 @Override
2193 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002194 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002195 }
2196
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002197 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002198 * A package was updated.
2199 *
2200 * Implementation of the method from LauncherModel.Callbacks.
2201 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002202 @Override
2203 public void bindAppsAddedOrUpdated(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002204 mAppsView.getAppsStore().addOrUpdateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002205 }
2206
Sunny Goyal4390ace2014-10-13 11:33:11 -07002207 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002208 public void bindPromiseAppProgressUpdated(PromiseAppInfo app) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002209 mAppsView.getAppsStore().updatePromiseAppProgress(app);
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002210 }
2211
2212 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002213 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002214 mWorkspace.widgetsRestored(widgets);
2215 }
2216
Joe Onorato9c1289c2009-08-17 11:03:03 -04002217 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002218 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002219 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002220 *
2221 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002222 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002223 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002224 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002225 if (!updated.isEmpty()) {
2226 mWorkspace.updateShortcuts(updated);
2227 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002228 }
2229
2230 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002231 * Update the state of a package, typically related to install state.
2232 *
2233 * Implementation of the method from LauncherModel.Callbacks.
2234 */
Sunny Goyale755d462014-07-22 13:48:29 -07002235 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002236 public void bindRestoreItemsChange(HashSet<ItemInfo> updates) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07002237 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07002238 }
2239
2240 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002241 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07002242 * in addition to specific applications to remove, the reason being that
2243 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002244 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07002245 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002246 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07002247 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002248 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002249 mWorkspace.removeItemsByMatcher(matcher);
2250 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002251 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002252
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002253 @Override
2254 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002255 mAppsView.getAppsStore().removeApps(appInfos);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002256 }
Joe Onoratobe386092009-11-17 17:32:16 -08002257
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002258 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002259 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
2260 mPopupDataProvider.setAllWidgets(allWidgets);
Tony Wickham26b17462017-03-20 17:12:24 -07002261 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
2262 if (topView != null) {
2263 topView.onWidgetsBound();
2264 }
2265 }
2266
Tony Wickham86222d22017-03-29 15:30:43 -07002267 /**
2268 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
2269 * refreshes the widgets and shortcuts associated with the given package/user
2270 */
2271 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07002272 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002273 }
2274
Winson Chung80baf5a2010-08-09 16:03:15 -07002275 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002276 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08002277 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002278 @Override
2279 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
2280 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002281
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002282 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
2283 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07002284 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002285 writer.println(prefix + " Homescreen " + i);
2286
2287 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
2288 for (int j = 0; j < layout.getChildCount(); j++) {
2289 Object tag = layout.getChildAt(j).getTag();
2290 if (tag != null) {
2291 writer.println(prefix + " " + tag.toString());
2292 }
2293 }
2294 }
2295
2296 writer.println(prefix + " Hotseat");
2297 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07002298 for (int j = 0; j < layout.getChildCount(); j++) {
2299 Object tag = layout.getChildAt(j).getTag();
2300 if (tag != null) {
2301 writer.println(prefix + " " + tag.toString());
2302 }
Adam Cohen4caf2982013-08-20 18:54:31 -07002303 }
Sunny Goyala1365452015-10-01 15:46:24 -07002304 }
2305
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002306 writer.println(prefix + "Misc:");
2307 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
2308 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
2309 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
Sunny Goyale43d00d2018-05-14 14:23:18 -07002310 writer.println(" mRotationHelper: " + mRotationHelper);
2311 dumpMisc(writer);
Sunny Goyal2bba1902018-02-27 12:20:50 -08002312
2313 try {
2314 FileLog.flushAll(writer);
2315 } catch (Exception e) {
2316 // Ignore
2317 }
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002318
2319 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002320
Adam Cohen9211d422014-10-07 18:14:53 -07002321 if (mLauncherCallbacks != null) {
2322 mLauncherCallbacks.dump(prefix, fd, writer, args);
2323 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002324 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002325
Sunny Goyal66b24572016-09-21 15:57:55 -07002326 @Override
2327 @TargetApi(Build.VERSION_CODES.N)
2328 public void onProvideKeyboardShortcuts(
2329 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
2330
2331 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002332 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002333 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
2334 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
Vadim Tryshev74c261c2018-06-12 13:44:43 -07002335 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.widget_button_text),
2336 KeyEvent.KEYCODE_W, KeyEvent.META_CTRL_ON));
Sunny Goyal66b24572016-09-21 15:57:55 -07002337 }
Vadim Tryshev3455f8d2018-05-11 20:02:20 -07002338 final View currentFocus = getCurrentFocus();
2339 if (currentFocus != null) {
2340 if (new CustomActionsPopup(this, currentFocus).canShow()) {
2341 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
2342 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
2343 }
2344 if (currentFocus.getTag() instanceof ItemInfo
2345 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
2346 shortcutInfos.add(new KeyboardShortcutInfo(
2347 getString(R.string.shortcuts_menu_with_notifications_description),
2348 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
2349 }
Sunny Goyal66b24572016-09-21 15:57:55 -07002350 }
2351 if (!shortcutInfos.isEmpty()) {
2352 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
2353 }
2354
2355 super.onProvideKeyboardShortcuts(data, menu, deviceId);
2356 }
2357
2358 @Override
2359 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
2360 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
2361 switch (keyCode) {
2362 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002363 if (isInState(NORMAL)) {
2364 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07002365 return true;
2366 }
2367 break;
2368 case KeyEvent.KEYCODE_S: {
2369 View focusedView = getCurrentFocus();
2370 if (focusedView instanceof BubbleTextView
2371 && focusedView.getTag() instanceof ItemInfo
2372 && mAccessibilityDelegate.performAction(focusedView,
2373 (ItemInfo) focusedView.getTag(),
2374 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal00ac9202017-11-09 15:24:06 -08002375 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07002376 return true;
2377 }
2378 break;
2379 }
2380 case KeyEvent.KEYCODE_O:
2381 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
2382 return true;
2383 }
2384 break;
Vadim Tryshev74c261c2018-06-12 13:44:43 -07002385 case KeyEvent.KEYCODE_W:
2386 if (isInState(NORMAL)) {
2387 OptionsPopupView.openWidgets(this);
2388 return true;
2389 }
2390 break;
Sunny Goyal66b24572016-09-21 15:57:55 -07002391 }
2392 }
2393 return super.onKeyShortcut(keyCode, event);
2394 }
2395
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002396 @Override
2397 public boolean onKeyUp(int keyCode, KeyEvent event) {
2398 if (keyCode == KeyEvent.KEYCODE_MENU) {
2399 // KEYCODE_MENU is sent by some tests, for example
2400 // LauncherJankTests#testWidgetsContainerFling. Don't just remove its handling.
2401 if (!mDragController.isDragging() && !mWorkspace.isSwitchingState() &&
2402 isInState(NORMAL)) {
2403 // Close any open floating views.
2404 AbstractFloatingView.closeAllOpenViews(this);
2405
2406 // Setting the touch point to (-1, -1) will show the options popup in the center of
2407 // the screen.
Sunny Goyal2fd7a8b2018-03-30 17:10:13 -07002408 OptionsPopupView.showDefaultOptions(this, -1, -1);
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002409 }
2410 return true;
2411 }
2412 return super.onKeyUp(keyCode, event);
2413 }
2414
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002415 public static Launcher getLauncher(Context context) {
Sunny Goyal87b5eb62018-07-03 15:53:39 -07002416 return (Launcher) fromContext(context);
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002417 }
2418
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002419 /**
2420 * Callback for listening for onResume
2421 */
2422 public interface OnResumeCallback {
2423
2424 void onLauncherResume();
2425 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002426}