blob: 9650e4eb7a2d9ad61cc1e8c4ad4ee81ba36d0a22 [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;
Tony Wickham86222d22017-03-29 15:30:43 -070060import android.support.annotation.Nullable;
Michael Jurkaa33411c2012-06-14 16:18:21 -070061import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070063import android.util.Log;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070064import android.util.SparseArray;
Sunny Goyal59d086c2018-05-07 17:31:40 -070065import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070066import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070067import android.view.KeyboardShortcutGroup;
68import android.view.KeyboardShortcutInfo;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080069import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -070070import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070071import android.view.View;
Adam Cohen96d30a12013-07-16 18:13:21 -070072import android.view.ViewGroup;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080073import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070074import android.view.animation.OvershootInterpolator;
Adam Cohen96d30a12013-07-16 18:13:21 -070075import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070076
Sunny Goyal651077b2014-06-30 14:15:31 -070077import com.android.launcher3.DropTarget.DragObject;
Sunny Goyalae502842016-06-17 08:43:56 -070078import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070079import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070080import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal7ede6112017-12-05 15:11:21 -080081import com.android.launcher3.allapps.DiscoveryBounce;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070082import com.android.launcher3.badge.BadgeInfo;
Sunny Goyalffe83f12014-08-14 17:39:34 -070083import com.android.launcher3.compat.AppWidgetManagerCompat;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -070084import com.android.launcher3.compat.LauncherAppsCompatVO;
Tony Wickhame0c33232016-02-08 11:37:04 -080085import com.android.launcher3.config.FeatureFlags;
Vadim Tryshevfedca432015-08-19 17:55:02 -070086import com.android.launcher3.dragndrop.DragController;
87import com.android.launcher3.dragndrop.DragLayer;
88import com.android.launcher3.dragndrop.DragView;
Jon Mirandaaeb4dd02018-07-11 14:16:23 -070089import com.android.launcher3.folder.Folder;
Sunny Goyal26119432016-02-18 22:09:23 +000090import com.android.launcher3.folder.FolderIcon;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070091import com.android.launcher3.folder.FolderIconPreviewVerifier;
Sunny Goyal66b24572016-09-21 15:57:55 -070092import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -070093import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -070094import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -070095import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Song46d07f72018-05-22 15:41:25 -070096import com.android.launcher3.logging.UserEventDispatcher.UserEventDelegate;
Sunny Goyala535ae42017-02-27 10:07:13 -080097import com.android.launcher3.model.ModelWriter;
Sunny Goyala535ae42017-02-27 10:07:13 -080098import com.android.launcher3.notification.NotificationListener;
Tony Wickham540913e2017-01-23 11:47:51 -080099import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -0800100import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700101import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyalf8088ee2017-11-10 14:52:00 -0800102import com.android.launcher3.states.InternalStateHandler;
Sunny Goyal623eddd2018-03-02 12:24:41 -0800103import com.android.launcher3.states.RotationHelper;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800104import com.android.launcher3.touch.ItemClickHandler;
Sunny Goyala5ace712017-11-15 17:12:51 -0800105import com.android.launcher3.uioverrides.UiFactory;
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700106import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530107import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
108import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
Sunny Goyalaeb16432017-10-16 11:46:41 -0700109import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyal2100c782016-08-22 16:00:03 -0700110import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700111import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700112import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700113import com.android.launcher3.util.MultiHashMap;
Sunny Goyal6001ea22018-05-10 16:31:00 -0700114import com.android.launcher3.util.MultiValueAlpha;
115import com.android.launcher3.util.MultiValueAlpha.AlphaProperty;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700116import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800117import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700118import com.android.launcher3.util.PendingRequestArgs;
Sunny Goyal8392c822017-06-20 10:03:56 -0700119import com.android.launcher3.util.SystemUiController;
Mario Bertschlera6936942017-05-31 14:48:19 -0700120import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700121import com.android.launcher3.util.Thunk;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700122import com.android.launcher3.util.TraceHelper;
Sunny Goyal326403e2017-10-02 12:45:10 -0700123import com.android.launcher3.util.UiThreadHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700124import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800125import com.android.launcher3.views.OptionsPopupView;
Sunny Goyal29947f02017-12-18 13:49:44 -0800126import com.android.launcher3.widget.LauncherAppWidgetHostView;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800127import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700128import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal29947f02017-12-18 13:49:44 -0800129import com.android.launcher3.widget.PendingAppWidgetHostView;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800130import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700131import com.android.launcher3.widget.WidgetHostViewLoader;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700132import com.android.launcher3.widget.WidgetListRowEntry;
133import com.android.launcher3.widget.WidgetsFullSheet;
Sunny Goyal952e63d2017-08-16 04:59:08 -0700134import com.android.launcher3.widget.custom.CustomWidgetParser;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700135
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700136import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700137import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700138import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700139import java.util.Collection;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700140import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700141import java.util.List;
Tony Wickham010d2552017-01-20 08:15:28 -0800142import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700143
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144/**
145 * Default launcher application.
146 */
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700147public class Launcher extends BaseDraggingActivity implements LauncherExterns,
148 LauncherModel.Callbacks, LauncherProviderChangeListener, UserEventDelegate{
Vadim Tryshevfedca432015-08-19 17:55:02 -0700149 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700150 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151
Winson Chunga2413752012-04-03 14:22:34 -0700152 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700153
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700155 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700156
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700157 private static final int REQUEST_PICK_APPWIDGET = 9;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158
Michael Jurka8b805b12012-04-18 14:23:14 -0700159 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800160 public static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
161 public static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700162
Jon Mirandac476d6e2017-05-04 16:30:01 -0700163 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700164
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700165 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
166
Mathew Inwood876a8462013-06-14 14:12:41 +0100167 /**
168 * IntentStarter uses request codes starting with this. This must be greater than all activity
169 * request codes used internally.
170 */
171 protected static final int REQUEST_LAST = 100;
172
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173 // Type: int
174 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700175 // Type: int
176 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700177 // Type: PendingRequestArgs
178 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
179 // Type: ActivityResultInfo
180 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700181 // Type: SparseArray<Parcelable>
182 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700184 private LauncherStateManager mStateManager;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700185
Adam Cohenad4e15c2013-10-17 16:21:35 -0700186 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187
Winson Chunga2413752012-04-03 14:22:34 -0700188 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700189 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
190 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
191 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700192
Jon Miranda54441f52018-01-24 15:38:25 -0800193 private LauncherAppTransitionManager mAppTransitionManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800194 private Configuration mOldConfig;
Jon Miranda54441f52018-01-24 15:38:25 -0800195
Adam Cohen091440a2015-03-18 14:16:05 -0700196 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700197 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700198 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800199 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700200
Sunny Goyalffe83f12014-08-14 17:39:34 -0700201 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700202 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700203
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700204 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700205
Sunny Goyal316490e2015-06-02 09:38:28 -0700206 @Thunk Hotseat mHotseat;
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700207 @Nullable private View mHotseatSearchBox;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700208
Sunny Goyal47328fd2016-05-25 18:56:41 -0700209 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700210
211 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700212 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700213 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700214
Winson Chung8ae41982017-11-10 11:42:13 -0800215 // UI and state for the overview panel
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800216 private View mOverviewPanel;
Winson Chung8ae41982017-11-10 11:42:13 -0800217
Adam Cohen091440a2015-03-18 14:16:05 -0700218 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400219
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700220 private OnResumeCallback mOnResumeCallback;
221
Sunny Goyal527c7d32015-08-28 15:19:36 -0700222 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700223
Joe Onorato9c1289c2009-08-17 11:03:03 -0400224 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800225 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800226 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700227 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400228
Tony Wickham010d2552017-01-20 08:15:28 -0800229 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700230
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800231 private int mSynchronouslyBoundPage = PagedView.INVALID_PAGE;
Adam Cohen1462de32012-07-24 22:34:36 -0700232
Winson Chung46353de2012-02-16 14:05:10 -0800233 // We only want to get the SharedPreferences once since it does an FS stat each time we get
234 // it from the context.
235 private SharedPreferences mSharedPrefs;
236
Sunny Goyal2100c782016-08-22 16:00:03 -0700237 // Activity result which needs to be processed after workspace has loaded.
238 private ActivityResultInfo mPendingActivityResult;
239 /**
240 * Holds extra information required to handle a result from an external call, like
241 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
242 */
243 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800244
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700245 public ViewGroupFocusHelper mFocusHandler;
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700246
Sunny Goyal623eddd2018-03-02 12:24:41 -0800247 private RotationHelper mRotationHelper;
Sunny Goyal7779d622015-06-11 16:18:39 -0700248
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700249
250 private final Handler mHandler = new Handler();
251 private final Runnable mLogOnDelayedResume = this::logOnDelayedResume;
252
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800253 @Override
254 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700255 if (DEBUG_STRICT_MODE) {
256 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
257 .detectDiskReads()
258 .detectDiskWrites()
259 .detectNetwork() // or .detectAll() for all detectable problems
260 .penaltyLog()
261 .build());
262 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
263 .detectLeakedSqlLiteObjects()
264 .detectLeakedClosableObjects()
265 .penaltyLog()
266 .penaltyDeath()
267 .build());
268 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700269 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700270
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800271 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700272 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400273
Sunny Goyal87f784c2017-01-11 10:48:34 -0800274 LauncherAppState app = LauncherAppState.getInstance(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800275 mOldConfig = new Configuration(getResources().getConfiguration());
Sunny Goyal0e7724c2018-02-22 13:22:21 -0800276 mModel = app.setLauncher(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800277 initDeviceProfile(app.getInvariantDeviceProfile());
Winson Chung6e1c0d32013-10-25 15:24:24 -0700278
Sunny Goyalf7258242015-10-19 16:59:07 -0700279 mSharedPrefs = Utilities.getPrefs(this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800280 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700281 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700282
Joe Onorato41a12d22009-10-31 18:30:00 -0400283 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700284 mAllAppsController = new AllAppsTransitionController(this);
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800285 mStateManager = new LauncherStateManager(this);
Tracy Zhou8b23c6f2018-04-30 17:13:27 -0700286 UiFactory.onCreate(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700287
Sunny Goyalffe83f12014-08-14 17:39:34 -0700288 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700289
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700290 mAppWidgetHost = new LauncherAppWidgetHost(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700291 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700292
Sunny Goyal60820d72017-05-09 12:40:11 -0700293 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700294
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800295 setupViews();
Tony Wickham010d2552017-01-20 08:15:28 -0800296 mPopupDataProvider = new PopupDataProvider(this);
297
Sunny Goyal623eddd2018-03-02 12:24:41 -0800298 mRotationHelper = new RotationHelper(this);
Sunny Goyal90548432018-04-04 17:17:00 -0700299 mAppTransitionManager = LauncherAppTransitionManager.newInstance(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800300
Sunny Goyald3864fa2017-11-14 15:06:36 -0800301 boolean internalStateHandled = InternalStateHandler.handleCreate(this, getIntent());
302 if (internalStateHandled) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800303 if (savedInstanceState != null) {
304 // InternalStateHandler has already set the appropriate state.
305 // We dont need to do anything.
306 savedInstanceState.remove(RUNTIME_STATE);
307 }
308 }
309 restoreState(savedInstanceState);
Winson Chungb63b44c2017-11-10 17:54:44 -0800310
Sunny Goyal2100c782016-08-22 16:00:03 -0700311 // We only load the page synchronously if the user rotates (or triggers a
312 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700313 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
314 if (savedInstanceState != null) {
315 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
316 }
Sunny Goyald3864fa2017-11-14 15:06:36 -0800317
Sunny Goyalfe770c92016-09-27 14:38:58 -0700318 if (!mModel.startLoader(currentScreen)) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800319 if (!internalStateHandled) {
320 // If we are not binding synchronously, show a fade in animation when
321 // the first page bind completes.
Sunny Goyal6001ea22018-05-10 16:31:00 -0700322 mDragLayer.getAlphaProperty(ALPHA_INDEX_LAUNCHER_LOAD).setValue(0);
Sunny Goyald3864fa2017-11-14 15:06:36 -0800323 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700324 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700325 // Pages bound synchronously.
326 mWorkspace.setCurrentPage(currentScreen);
327
Sunny Goyal2100c782016-08-22 16:00:03 -0700328 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800329 }
330
331 // For handling default keys
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800332 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800333
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800334 setContentView(mLauncherView);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800335 getRootView().dispatchInsets();
Mario Bertschlera6936942017-05-31 14:48:19 -0700336
Jon Miranda7fda2852017-07-19 16:07:01 -0700337 // Listen for broadcasts
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700338 registerReceiver(mScreenOffReceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));
Sunny Goyala616d2b2017-06-12 13:54:01 -0700339
Sunny Goyal8392c822017-06-20 10:03:56 -0700340 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
341 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700342
343 if (mLauncherCallbacks != null) {
344 mLauncherCallbacks.onCreate(savedInstanceState);
345 }
Sunny Goyal623eddd2018-03-02 12:24:41 -0800346 mRotationHelper.initialize();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700347
348 TraceHelper.endSection("Launcher-onCreate");
Adam Cohen9211d422014-10-07 18:14:53 -0700349 }
350
Sunny Goyal7779d622015-06-11 16:18:39 -0700351 @Override
Winson Chung8eb49e02018-07-16 13:33:31 -0700352 public void onEnterAnimationComplete() {
353 super.onEnterAnimationComplete();
354 UiFactory.onEnterAnimationComplete(this);
355 }
356
357 @Override
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800358 public void onConfigurationChanged(Configuration newConfig) {
359 int diff = newConfig.diff(mOldConfig);
Jon Mirandaaeb4dd02018-07-11 14:16:23 -0700360
361 if ((diff & CONFIG_LOCALE) != 0) {
362 Folder.setLocaleDependentFields(getResources(), true /* force */);
363 }
364
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800365 if ((diff & (CONFIG_ORIENTATION | CONFIG_SCREEN_SIZE)) != 0) {
366 mUserEventDispatcher = null;
367 initDeviceProfile(mDeviceProfile.inv);
Sunny Goyalfde55052018-02-01 14:46:13 -0800368 dispatchDeviceProfileChanged();
Sunny Goyal59d086c2018-05-07 17:31:40 -0700369 reapplyUi();
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700370 mDragLayer.recreateControllers();
Sunny Goyal7185dd62018-03-14 17:51:49 -0700371
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800372 // TODO: We can probably avoid rebind when only screen size changed.
Sunny Goyal605bcf32018-03-02 15:16:12 -0800373 rebindModel();
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800374 }
375
376 mOldConfig.setTo(newConfig);
Tracy Zhoua706f002018-03-28 13:55:19 -0700377 UiFactory.onLauncherStateOrResumeChanged(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800378 super.onConfigurationChanged(newConfig);
379 }
380
Sunny Goyal605bcf32018-03-02 15:16:12 -0800381 @Override
Sunny Goyal59d086c2018-05-07 17:31:40 -0700382 protected void reapplyUi() {
383 getRootView().dispatchInsets();
384 getStateManager().reapplyState(true /* cancelCurrentAnimation */);
385 }
386
387 @Override
Sunny Goyal605bcf32018-03-02 15:16:12 -0800388 public void rebindModel() {
389 int currentPage = mWorkspace.getNextPage();
390 if (mModel.startLoader(currentPage)) {
391 mWorkspace.setCurrentPage(currentPage);
392 setWorkspaceLoading(true);
393 }
394 }
395
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800396 private void initDeviceProfile(InvariantDeviceProfile idp) {
397 // Load configuration-specific DeviceProfile
Sunny Goyal59d086c2018-05-07 17:31:40 -0700398 mDeviceProfile = idp.getDeviceProfile(this);
399 if (isInMultiWindowModeCompat()) {
400 Display display = getWindowManager().getDefaultDisplay();
401 Point mwSize = new Point();
402 display.getSize(mwSize);
403 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
404 }
405 onDeviceProfileInitiated();
Sunny Goyal605bcf32018-03-02 15:16:12 -0800406 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout(), true);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800407 }
408
Sunny Goyal623eddd2018-03-02 12:24:41 -0800409 public RotationHelper getRotationHelper() {
410 return mRotationHelper;
411 }
412
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700413 public LauncherStateManager getStateManager() {
414 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700415 }
416
Mario Bertschler27288382017-05-24 15:35:09 -0700417 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700418 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800419 return mLauncherView.findViewById(id);
420 }
421
422 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700423 public void onAppWidgetHostReset() {
424 if (mAppWidgetHost != null) {
425 mAppWidgetHost.startListening();
426 }
427 }
428
Adam Cohen9211d422014-10-07 18:14:53 -0700429 private LauncherCallbacks mLauncherCallbacks;
430
Sunny Goyal32554d12015-12-03 15:31:25 -0800431 /**
432 * Call this after onCreate to set or clear overlay.
433 */
434 public void setLauncherOverlay(LauncherOverlay overlay) {
435 if (overlay != null) {
436 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
437 }
438 mWorkspace.setLauncherOverlay(overlay);
439 }
440
Adam Cohen9211d422014-10-07 18:14:53 -0700441 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
442 mLauncherCallbacks = callbacks;
443 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700444 }
445
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700446 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700447 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700448 if (mLauncherCallbacks != null) {
449 mLauncherCallbacks.onLauncherProviderChange();
450 }
451 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700452
Hyunyoung Song3f471442015-04-08 19:01:34 -0700453 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700454 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
455 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700456 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700457 }
458
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000459 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700460 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
461 // This cast is safe as long as the id < 0x00FFFFFF
462 // Since we jail all the dynamically generated views, there should be no clashes
463 // with any other views.
464 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000465 }
466
Tony Wickham010d2552017-01-20 08:15:28 -0800467 public PopupDataProvider getPopupDataProvider() {
468 return mPopupDataProvider;
469 }
470
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700471 @Override
472 public BadgeInfo getBadgeInfoForItem(ItemInfo info) {
473 return mPopupDataProvider.getBadgeInfoForItem(info);
474 }
475
476 @Override
477 public void invalidateParent(ItemInfo info) {
478 FolderIconPreviewVerifier verifier = new FolderIconPreviewVerifier(getDeviceProfile().inv);
479 if (verifier.isItemInPreview(info.rank) && (info.container >= 0)) {
480 View folderIcon = getWorkspace().getHomescreenIconByItemId(info.container);
481 if (folderIcon != null) {
482 folderIcon.invalidate();
483 }
484 }
485 }
486
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000487 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700488 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
489 * a configuration step, this allows the proper animations to run after other transitions.
490 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700491 private long completeAdd(
492 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
493 long screenId = info.screenId;
494 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700495 // When the screen id represents an actual screen (as opposed to a rank) we make sure
496 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700497 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700498 }
Adam Cohendb364c32014-05-20 14:23:40 -0700499
Sunny Goyal2100c782016-08-22 16:00:03 -0700500 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800501 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700502 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700503 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800504 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700505 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700506 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700507 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700508 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700509 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700510 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700511 int widgetId = appWidgetId;
512 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700513 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700514 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700515 // Since the view was just bound, also launch the configure activity if needed
516 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
517 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800518 if (provider != null) {
519 new WidgetAddFlowHandler(provider)
520 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700521 }
522 }
523 break;
524 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800525 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700526
Adam Cohendb364c32014-05-20 14:23:40 -0700527 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800528 }
529
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800530 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700531 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700532 if (isWorkspaceLoading()) {
533 // process the result once the workspace has loaded.
534 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
535 return;
536 }
537 mPendingActivityResult = null;
538
Winson Chunge341d302013-08-16 14:31:00 -0700539 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700540 final PendingRequestArgs requestArgs = mPendingRequestArgs;
541 setWaitingForResult(null);
542 if (requestArgs == null) {
543 return;
544 }
545
546 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700547
Adam Cohenad4e15c2013-10-17 16:21:35 -0700548 Runnable exitSpringLoaded = new Runnable() {
549 @Override
550 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700551 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700552 }
553 };
554
Michael Jurka8b805b12012-04-18 14:23:14 -0700555 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700556 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700557 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700558 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
559 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700560 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700561 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700562 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700563 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700564 addAppWidgetImpl(
565 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800566 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700567 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700568 }
569 return;
570 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200571
Adam Cohened66b2b2012-01-23 17:28:51 -0800572 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700573 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700574
Adam Cohened66b2b2012-01-23 17:28:51 -0800575 // We have special handling for widgets
576 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800577 final int appWidgetId;
578 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
579 : -1;
580 if (widgetId < 0) {
581 appWidgetId = pendingAddWidgetId;
582 } else {
583 appWidgetId = widgetId;
584 }
585
Adam Cohenad4e15c2013-10-17 16:21:35 -0700586 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800587 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700588 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
589 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700590 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700591 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700592 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700593 @Override
594 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700595 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700596 }
597 };
Adam Cohendb364c32014-05-20 14:23:40 -0700598
Sunny Goyal2100c782016-08-22 16:00:03 -0700599 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
600 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
601 } else {
602 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
603 // When the screen id represents an actual screen (as opposed to a rank)
604 // we make sure that the drop page actually exists.
605 requestArgs.screenId =
606 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700607 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700608 final CellLayout dropLayout =
609 mWorkspace.getScreenWithId(requestArgs.screenId);
610
611 dropLayout.setDropPending(true);
612 final Runnable onComplete = new Runnable() {
613 @Override
614 public void run() {
615 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
616 dropLayout.setDropPending(false);
617 }
618 };
619 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
620 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700621 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800622 return;
623 }
624
Sunny Goyald478c832016-04-01 12:04:16 -0700625 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
626 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700627 if (resultCode == RESULT_OK) {
628 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700629 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700630 }
631 // Leave the widget in the pending state if the user canceled the configure.
632 return;
633 }
634
Sunny Goyalaad90582015-07-09 14:22:50 -0700635 if (requestCode == REQUEST_CREATE_SHORTCUT) {
636 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700637 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
638 completeAdd(requestCode, data, -1, requestArgs);
639 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
640 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
641
Sunny Goyalaad90582015-07-09 14:22:50 -0700642 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700643 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
644 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800645 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800646 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800647 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800648 }
649
650 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700651 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800652 final int requestCode, final int resultCode, final Intent data) {
653 handleActivityResult(requestCode, resultCode, data);
654 if (mLauncherCallbacks != null) {
655 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
656 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800657 }
658
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700659 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700660 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600661 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700662 PendingRequestArgs pendingArgs = mPendingRequestArgs;
663 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
664 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
665 setWaitingForResult(null);
666
Sunny Goyal28c6b962015-10-12 11:42:05 -0700667 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700668 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700669 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700670 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700671 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700672 Intent intent = pendingArgs.getPendingIntent();
673
Sunny Goyal28c6b962015-10-12 11:42:05 -0700674 if (grantResults.length > 0
675 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700676 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700677 } else {
678 // TODO: Show a snack bar with link to settings
679 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700680 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700681 }
682 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600683 if (mLauncherCallbacks != null) {
684 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
685 grantResults);
686 }
687 }
688
Adam Cohendb364c32014-05-20 14:23:40 -0700689 /**
690 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
691 *
692 * @param screenId the screen id to check
693 * @return the new screen, or screenId if it exists
694 */
695 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800696 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700697 if (dropLayout == null) {
698 // it's possible that the add screen was removed because it was
699 // empty and a re-bind occurred
700 mWorkspace.addExtraEmptyScreen();
701 return mWorkspace.commitExtraEmptyScreen();
702 } else {
703 return screenId;
704 }
705 }
706
Sunny Goyal2100c782016-08-22 16:00:03 -0700707 @Thunk void completeTwoStageWidgetDrop(
708 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
709 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800710 Runnable onCompleteRunnable = null;
711 int animationType = 0;
712
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700713 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800714 if (resultCode == RESULT_OK) {
715 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
716 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800717 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700718 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800719 onCompleteRunnable = new Runnable() {
720 @Override
721 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700722 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700723 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800724 }
725 };
726 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800727 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800728 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800729 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700730 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700731 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700732 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
733 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700734 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700735 // The animated view may be null in the case of a rotation during widget configuration
736 onCompleteRunnable.run();
737 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800738 }
739
740 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700741 protected void onStop() {
742 super.onStop();
743 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700744
745 if (mLauncherCallbacks != null) {
746 mLauncherCallbacks.onStop();
747 }
Hyunyoung Song1241e612018-05-15 21:46:51 -0700748 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
749 mStateManager.getState().containerType, -1);
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700750
751 mAppWidgetHost.setListenIfResumed(false);
752
Tony Wickham010d2552017-01-20 08:15:28 -0800753 NotificationListener.removeNotificationsChangedListener();
Sunny Goyalaf161fd2018-02-05 16:34:57 -0800754 getStateManager().moveToRestState();
Winson Chungedb0d6b2018-03-21 17:32:55 -0700755
Tracy Zhou62646712018-06-26 14:19:02 -0700756 UiFactory.onLauncherStateOrResumeChanged(this);
757
Winson Chungdea51352018-04-24 13:02:10 -0700758 // Workaround for b/78520668, explicitly trim memory once UI is hidden
Winson Chung473730f2018-04-26 11:54:22 -0700759 onTrimMemory(TRIM_MEMORY_UI_HIDDEN);
Michael Jurkacd496d72013-04-11 11:32:45 -0700760 }
761
762 @Override
763 protected void onStart() {
764 super.onStart();
765 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700766
767 if (mLauncherCallbacks != null) {
768 mLauncherCallbacks.onStart();
769 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800770 mAppWidgetHost.setListenIfResumed(true);
Sunny Goyalcac002d2018-01-04 09:51:45 -0800771 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
Winson Chung62a70be2018-02-23 15:10:05 -0800772 UiFactory.onStart(this);
Michael Jurkacd496d72013-04-11 11:32:45 -0700773 }
774
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700775 private void logOnDelayedResume() {
776 if (hasBeenResumed()) {
777 getUserEventDispatcher().logActionCommand(Action.Command.RESUME,
778 mStateManager.getState().containerType, -1);
779 getUserEventDispatcher().startSession();
780 }
781 }
782
Michael Jurkacd496d72013-04-11 11:32:45 -0700783 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800784 protected void onResume() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700785 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800786 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700787 TraceHelper.partitionSection("ON_RESUME", "superCall");
788
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700789 mHandler.removeCallbacks(mLogOnDelayedResume);
790 Utilities.postAsyncCallback(mHandler, mLogOnDelayedResume);
791
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700792 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700793 // Process any items that were added while Launcher was away.
794 InstallShortcutReceiver.disableAndFlushInstallQueue(
795 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700796
Sunny Goyala474a9b2017-05-04 16:47:11 -0700797 // Refresh shortcuts if the permission changed.
798 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700799
Tracy Zhoue5575f92018-04-19 11:12:53 -0700800 DiscoveryBounce.showForHomeIfNeeded(this);
Adam Cohen9211d422014-10-07 18:14:53 -0700801 if (mLauncherCallbacks != null) {
802 mLauncherCallbacks.onResume();
803 }
Tracy Zhoua706f002018-03-28 13:55:19 -0700804 UiFactory.onLauncherStateOrResumeChanged(this);
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800805
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700806 TraceHelper.endSection("ON_RESUME");
Adam Cohen06dff352012-06-01 17:17:08 -0700807 }
808
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800809 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700810 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700811 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700812 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700813
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700814 super.onPause();
Joe Onorato24b6fd82009-11-12 13:47:09 -0800815 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700816 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700817
Adam Cohen9211d422014-10-07 18:14:53 -0700818 if (mLauncherCallbacks != null) {
819 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700820 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700821 }
822
Winson Chungd7823942018-02-16 03:23:47 +0000823 @Override
Tracy Zhoua706f002018-03-28 13:55:19 -0700824 protected void onUserLeaveHint() {
825 super.onUserLeaveHint();
826 UiFactory.onLauncherStateOrResumeChanged(this);
827 }
828
829 @Override
Winson Chungd7823942018-02-16 03:23:47 +0000830 public void onWindowFocusChanged(boolean hasFocus) {
831 super.onWindowFocusChanged(hasFocus);
832 mStateManager.onWindowFocusChanged();
833 }
834
Adam Cohenc2d6e892014-10-16 09:49:24 -0700835 public interface LauncherOverlay {
836
837 /**
838 * Touch interaction leading to overscroll has begun
839 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700840 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700841
842 /**
843 * Touch interaction related to overscroll has ended
844 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700845 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700846
847 /**
848 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
849 * screen (or in the case of RTL, the rightmost screen).
850 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700851 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700852
853 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800854 * Called when the launcher is ready to use the overlay
855 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700856 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700857 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700858 }
859
860 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700861 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700862 }
863
864 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
865
Sunny Goyalc86df472016-02-25 09:19:38 -0800866 public void onScrollChanged(float progress) {
867 if (mWorkspace != null) {
868 mWorkspace.onOverlayScrollChanged(progress);
869 }
870 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700871 }
872
Sunny Goyal16764582017-11-06 16:00:34 -0800873 public boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -0700874 if (mLauncherCallbacks != null) {
875 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700876 } else {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800877 // On O and above we there is always some setting present settings (add icon to
878 // home screen or icon badging). On earlier APIs we will have the allow rotation
879 // setting, on devices with a locked orientation,
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700880 return Utilities.ATLEAST_OREO || !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -0700881 }
Jorim Jaggid017f882014-01-14 17:08:48 -0800882 }
883
Sunny Goyalf9403d92017-10-18 10:55:56 -0700884 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -0700885 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700886 }
887
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800888 /**
889 * Restores the previous state, if it exists.
890 *
891 * @param savedState The previous state.
892 */
893 private void restoreState(Bundle savedState) {
894 if (savedState == null) {
895 return;
896 }
897
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700898 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -0700899 LauncherState[] stateValues = LauncherState.values();
900 LauncherState state = stateValues[stateOrdinal];
Sunny Goyalbc683e92017-12-06 10:25:07 -0800901 if (!state.disableRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700902 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800903 }
904
Sunny Goyal2100c782016-08-22 16:00:03 -0700905 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
906 if (requestArgs != null) {
907 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800908 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700909
910 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700911
912 SparseArray<Parcelable> widgetsState =
913 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
914 if (widgetsState != null) {
915 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
916 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800917 }
918
919 /**
920 * Finds all the views we need and configure them properly.
921 */
922 private void setupViews() {
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800923 mDragLayer = findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700924 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -0700925 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700926 mWorkspace.initParentViews(mDragLayer);
Sunny Goyal16764582017-11-06 16:00:34 -0800927 mOverviewPanel = findViewById(R.id.overview_panel);
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800928 mHotseat = findViewById(R.id.hotseat);
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700929 mHotseatSearchBox = findViewById(R.id.search_container_hotseat);
Craig Mautner360310b2012-10-26 15:13:08 -0700930
Sunny Goyal784f9c32016-03-23 16:56:54 -0700931 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
932 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
933 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800934
Winson Chung4c98d922011-05-31 16:50:48 -0700935 // Setup the drag layer
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700936 mDragLayer.setup(mDragController, mWorkspace);
937 UiFactory.setOnTouchControllersChangedListener(this, mDragLayer::recreateControllers);
Winson Chung4c98d922011-05-31 16:50:48 -0700938
Hyunyoung Song645764e2016-06-06 14:19:02 -0700939 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -0700940 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
941 // default state, otherwise we will update to the wrong offsets in RTL
942 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700943 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700944 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700945
Tony Wickham34d2c912015-09-11 09:27:58 -0700946 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700947 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700948
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700949 // Setup Apps
950 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -0700951
Winson Chung3d503fb2011-07-13 17:25:49 -0700952 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -0700953 mDragController.setMoveTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700954 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -0400955
Sunny Goyal7185dd62018-03-14 17:51:49 -0700956 mAllAppsController.setupViews(mAppsView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800957 }
958
959 /**
960 * Creates a view representing a shortcut.
961 *
962 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800963 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800964 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700965 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800966 }
967
968 /**
969 * Creates a view representing a shortcut inflated from the specified resource.
970 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800971 * @param parent The group the shortcut belongs to.
972 * @param info The data structure describing the shortcut.
973 *
974 * @return A View inflated from layoutResId.
975 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700976 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -0800977 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
978 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800979 favorite.applyFromShortcutInfo(info);
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800980 favorite.setOnClickListener(ItemClickHandler.INSTANCE);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700981 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800982 return favorite;
983 }
984
985 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -0700986 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800987 *
988 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800989 */
Adam Cohendcd297f2013-06-18 13:13:40 -0700990 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700991 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -0700992 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
993 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700994 return;
995 }
Adam Cohen558baaf2011-08-15 15:22:57 -0700996
Jon Mirandac476d6e2017-05-04 16:30:01 -0700997 int[] cellXY = mTmpAddItemCellCoordinates;
998 CellLayout layout = getCellLayout(container, screenId);
999
Sunny Goyal782f0c92017-01-19 10:27:54 -08001000 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001001 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001002 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
1003 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001004 }
1005
1006 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001007 // Legacy shortcuts are only supported for primary profile.
1008 info = Process.myUserHandle().equals(args.user)
1009 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001010
Sunny Goyalb57645f2017-03-20 13:57:28 -07001011 if (info == null) {
1012 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1013 return;
1014 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001015 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001016 // The app is trying to add a shortcut without sufficient permissions
1017 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1018 return;
1019 }
1020 }
1021
Jon Mirandac476d6e2017-05-04 16:30:01 -07001022 if (container < 0) {
1023 // Adding a shortcut to the Workspace.
1024 final View view = createShortcut(info);
1025 boolean foundCellSpan = false;
1026 // First we check if we already know the exact location where we want to add this item.
1027 if (cellX >= 0 && cellY >= 0) {
1028 cellXY[0] = cellX;
1029 cellXY[1] = cellY;
1030 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001031
Jon Mirandac476d6e2017-05-04 16:30:01 -07001032 // If appropriate, either create a folder or add to an existing folder
1033 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001034 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001035 return;
1036 }
1037 DragObject dragObject = new DragObject();
1038 dragObject.dragInfo = info;
1039 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1040 true)) {
1041 return;
1042 }
1043 } else {
1044 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1045 }
1046
1047 if (!foundCellSpan) {
1048 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001049 return;
1050 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001051
1052 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1053 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001054 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001055 // Adding a shortcut to a Folder.
Sunny Goyale29897f52017-07-20 10:09:42 -07001056 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001057 if (folderIcon != null) {
1058 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1059 folderInfo.add(info, args.rank, false);
1060 } else {
Sunny Goyale29897f52017-07-20 10:09:42 -07001061 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001062 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001063 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001064 }
1065
Sunny Goyale29897f52017-07-20 10:09:42 -07001066 public FolderIcon findFolderIcon(final long folderIconId) {
Sunny Goyal83fd25e2018-07-09 16:47:01 -07001067 return (FolderIcon) mWorkspace.getHomescreenIconByItemId(folderIconId);
Sunny Goyale29897f52017-07-20 10:09:42 -07001068 }
1069
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001070 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001071 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001072 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001073 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001074 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001075 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001076 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1077
Adam Cohened66b2b2012-01-23 17:28:51 -08001078 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001079 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001080 }
Romain Guycbb89e42009-06-08 15:52:54 -07001081
Adam Cohen59400422014-03-05 18:07:04 -08001082 LauncherAppWidgetInfo launcherInfo;
1083 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001084 launcherInfo.spanX = itemInfo.spanX;
1085 launcherInfo.spanY = itemInfo.spanY;
1086 launcherInfo.minSpanX = itemInfo.minSpanX;
1087 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001088 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001089
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001090 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001091 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001092
Sunny Goyal2100c782016-08-22 16:00:03 -07001093 if (hostView == null) {
1094 // Perform actual inflation because we're live
1095 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001096 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001097 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301098 prepareAppWidget(hostView, launcherInfo);
1099 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001100 }
Romain Guycbb89e42009-06-08 15:52:54 -07001101
Sunny Goyald5462aa2016-11-22 16:52:39 +05301102 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001103 hostView.setTag(item);
1104 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001105 hostView.setFocusable(true);
1106 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001107 }
1108
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001109 private final BroadcastReceiver mScreenOffReceiver = new BroadcastReceiver() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001110 @Override
1111 public void onReceive(Context context, Intent intent) {
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001112 // Reset AllApps to its initial state only if we are not in the middle of
1113 // processing a multi-step drop
1114 if (mPendingRequestArgs == null) {
1115 mStateManager.goToState(NORMAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001116 }
1117 }
1118 };
1119
Tony Wickham010d2552017-01-20 08:15:28 -08001120 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
Sunny Goyal29947f02017-12-18 13:49:44 -08001121 mWorkspace.updateIconBadges(updatedBadges);
Sunny Goyal60180b02018-02-07 12:56:30 -08001122 mAppsView.getAppsStore().updateIconBadges(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001123
Sunny Goyal29947f02017-12-18 13:49:44 -08001124 PopupContainerWithArrow popup = PopupContainerWithArrow.getOpen(Launcher.this);
1125 if (popup != null) {
1126 popup.updateNotificationHeader(updatedBadges);
Tony Wickham010d2552017-01-20 08:15:28 -08001127 }
1128 }
1129
Adam Cohended9f8d2010-11-03 13:25:16 -07001130 @Override
1131 public void onAttachedToWindow() {
1132 super.onAttachedToWindow();
1133
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001134 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyalc86df472016-02-25 09:19:38 -08001135 if (mLauncherCallbacks != null) {
1136 mLauncherCallbacks.onAttachedToWindow();
1137 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001138 }
1139
1140 @Override
1141 public void onDetachedFromWindow() {
1142 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001143
1144 if (mLauncherCallbacks != null) {
1145 mLauncherCallbacks.onDetachedFromWindow();
1146 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001147 }
1148
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001149 public AllAppsTransitionController getAllAppsController() {
1150 return mAllAppsController;
1151 }
1152
Sunny Goyal9d69c8d2018-03-19 13:41:31 -07001153 @Override
Winson Chung1a341002018-01-17 10:00:23 -08001154 public LauncherRootView getRootView() {
1155 return (LauncherRootView) mLauncherView;
1156 }
1157
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001158 @Override
Winson Chunga6945242014-01-08 14:04:34 -08001159 public DragLayer getDragLayer() {
1160 return mDragLayer;
1161 }
1162
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001163 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001164 return mAppsView;
1165 }
1166
Winson Chunga6945242014-01-08 14:04:34 -08001167 public Workspace getWorkspace() {
1168 return mWorkspace;
1169 }
1170
1171 public Hotseat getHotseat() {
1172 return mHotseat;
1173 }
1174
Sunny Goyal81b4c7b2018-03-26 12:10:31 -07001175 public View getHotseatSearchBox() {
1176 return mHotseatSearchBox;
1177 }
1178
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001179 public <T extends View> T getOverviewPanel() {
Winson Chung8ae41982017-11-10 11:42:13 -08001180 return (T) mOverviewPanel;
Winson Chunga6945242014-01-08 14:04:34 -08001181 }
1182
Sunny Goyal47328fd2016-05-25 18:56:41 -07001183 public DropTargetBar getDropTargetBar() {
1184 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001185 }
1186
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001187 public LauncherAppWidgetHost getAppWidgetHost() {
1188 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001189 }
Romain Guycbb89e42009-06-08 15:52:54 -07001190
Winson Chunga9abd0e2010-10-27 17:18:37 -07001191 public LauncherModel getModel() {
1192 return mModel;
1193 }
1194
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001195 public ModelWriter getModelWriter() {
1196 return mModelWriter;
1197 }
1198
Adam Cohen79d90c52016-04-22 13:29:20 -07001199 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001200 return mSharedPrefs;
1201 }
1202
Sunny Goyalf8d56fc2018-01-31 15:18:11 -08001203 public int getOrientation() { return mOldConfig.orientation; }
Jon Miranda6bed3502017-09-19 14:43:17 -07001204
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001205 @Override
1206 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001207 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001208 super.onNewIntent(intent);
1209
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001210 boolean alreadyOnHome = hasWindowFocus() && ((intent.getFlags() &
Winson15f8b172015-08-19 11:02:39 -07001211 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1212 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001213
1214 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001215 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001216 && AbstractFloatingView.getTopOpenView(this) == null;
Winson15f8b172015-08-19 11:02:39 -07001217 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001218 boolean internalStateHandled = InternalStateHandler
Sunny Goyal8b264562018-02-03 23:00:13 -08001219 .handleNewIntent(this, intent, isStarted());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001220
Winson15f8b172015-08-19 11:02:39 -07001221 if (isActionMain) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001222 if (!internalStateHandled) {
1223 // Note: There should be at most one log per method call. This is enforced
1224 // implicitly by using if-else statements.
1225 UserEventDispatcher ued = getUserEventDispatcher();
1226 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
1227 if (topOpenView != null) {
1228 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
1229 } else if (alreadyOnHome) {
1230 Target target = newContainerTarget(mStateManager.getState().containerType);
1231 target.pageIndex = mWorkspace.getCurrentPage();
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001232 ued.logActionCommand(Action.Command.HOME_INTENT, target,
1233 newContainerTarget(ContainerType.WORKSPACE));
Sunny Goyald3864fa2017-11-14 15:06:36 -08001234 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001235
Sunny Goyald3864fa2017-11-14 15:06:36 -08001236 // In all these cases, only animate if we're already on home
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001237 AbstractFloatingView.closeAllOpenViews(this, isStarted());
Jon Mirandafeba90f2016-10-06 10:53:29 -07001238
Sunny Goyala2467272018-03-16 14:53:05 -07001239 if (!isInState(NORMAL)) {
1240 // Only change state, if not already the same. This prevents cancelling any
1241 // animations running as part of resume
1242 mStateManager.goToState(NORMAL);
1243 }
Sunny Goyald3864fa2017-11-14 15:06:36 -08001244
1245 // Reset the apps view
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001246 if (!alreadyOnHome) {
Sunny Goyal7185dd62018-03-14 17:51:49 -07001247 mAppsView.reset(isStarted() /* animate */);
Sunny Goyald3864fa2017-11-14 15:06:36 -08001248 }
1249
Sunny Goyalaad33592018-08-07 17:20:35 -07001250 if (shouldMoveToDefaultScreen && !mWorkspace.isHandlingTouch()) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001251 mWorkspace.post(mWorkspace::moveToDefaultScreen);
1252 }
1253 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001254
1255 final View v = getWindow().peekDecorView();
1256 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001257 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001258 }
1259
Adam Cohen9211d422014-10-07 18:14:53 -07001260 if (mLauncherCallbacks != null) {
Sunny Goyal85462132018-04-24 11:39:37 -07001261 mLauncherCallbacks.onHomeIntent(internalStateHandled);
Adam Cohen9211d422014-10-07 18:14:53 -07001262 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001263 }
Winson15f8b172015-08-19 11:02:39 -07001264
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001265 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001266 }
1267
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001268 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001269 public void onRestoreInstanceState(Bundle state) {
1270 super.onRestoreInstanceState(state);
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001271 mWorkspace.restoreInstanceStateForChild(mSynchronouslyBoundPage);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001272 }
1273
1274 @Override
1275 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001276 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001277 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001278
Adam Cohen21cd0022013-10-09 18:57:02 -07001279 }
Sunny Goyalea609262017-10-25 15:47:38 -07001280 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001281
1282
1283 AbstractFloatingView widgets = AbstractFloatingView
1284 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1285 if (widgets != null) {
1286 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1287 widgets.saveHierarchyState(widgetsState);
1288 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1289 } else {
1290 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1291 }
1292
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001293 // We close any open folders and shortcut containers since they will not be re-opened,
1294 // and we need to make sure this state is reflected.
1295 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001296
Sunny Goyal2100c782016-08-22 16:00:03 -07001297 if (mPendingRequestArgs != null) {
1298 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1299 }
1300 if (mPendingActivityResult != null) {
1301 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001302 }
1303
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001304 super.onSaveInstanceState(outState);
1305
Adam Cohen9211d422014-10-07 18:14:53 -07001306 if (mLauncherCallbacks != null) {
1307 mLauncherCallbacks.onSaveInstanceState(outState);
1308 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001309 }
1310
1311 @Override
1312 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001313 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001314
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001315 unregisterReceiver(mScreenOffReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001316 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001317
Tony Wickhama0068302018-04-11 16:16:11 -07001318 UiFactory.setOnTouchControllersChangedListener(this, null);
1319
Winson Chungcd2b0142011-06-08 16:02:26 -07001320 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001321 // It's possible to receive onDestroy after a new Launcher activity has
1322 // been created. In this case, don't interfere with the new Launcher.
1323 if (mModel.isCurrentCallbacks(this)) {
1324 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001325 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001326 }
Sunny Goyal623eddd2018-03-02 12:24:41 -08001327 mRotationHelper.destroy();
Sunny Goyal745bad92016-05-02 10:54:12 -07001328
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001329 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001330 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001331 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001332 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001333 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001334
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001335 TextKeyListener.getInstance().release();
Tony2917a8b2017-07-31 23:29:42 -07001336 clearPendingBinds();
1337
Adam Cohen9211d422014-10-07 18:14:53 -07001338 if (mLauncherCallbacks != null) {
1339 mLauncherCallbacks.onDestroy();
1340 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001341 }
1342
Sunny Goyalae502842016-06-17 08:43:56 -07001343 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1344 return mAccessibilityDelegate;
1345 }
1346
Adam Cohena9cf38f2011-05-02 15:36:58 -07001347 public DragController getDragController() {
1348 return mDragController;
1349 }
1350
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001351 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001352 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001353 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001354 }
1355
1356 @Override
1357 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1358 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001359 try {
1360 super.startIntentSenderForResult(intent, requestCode,
1361 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1362 } catch (IntentSender.SendIntentException e) {
1363 throw new ActivityNotFoundException();
1364 }
1365 }
1366
Winson Chung70d72102011-08-12 11:24:35 -07001367 /**
1368 * Indicates that we want global search for this activity by setting the globalSearch
1369 * argument for {@link #startSearch} to true.
1370 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001371 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001372 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001373 Bundle appSearchData, boolean globalSearch) {
1374 if (appSearchData == null) {
1375 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001376 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001377 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001378
Sunny Goyal6f28e712016-09-13 14:19:29 -07001379 if (mLauncherCallbacks == null ||
1380 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1381 // Starting search from the callbacks failed. Start the default global search.
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001382 super.startSearch(initialQuery, selectInitialQuery, appSearchData, true);
Ian Parkinson047036e2014-09-01 15:40:53 +01001383 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001384
1385 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001386 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001387 }
1388
Joe Onorato9c1289c2009-08-17 11:03:03 -04001389 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001390 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001391 }
1392
Adam Cohen517a7f52014-03-01 12:12:59 -08001393 public boolean isWorkspaceLoading() {
1394 return mWorkspaceLoading;
1395 }
1396
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001397 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001398 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001399 }
1400
Sunny Goyal04a324a2017-01-20 21:08:59 -08001401 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001402 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001403 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001404
Sunny Goyal2100c782016-08-22 16:00:03 -07001405 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001406 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001407 if (LOGD) {
1408 Log.d(TAG, "Adding widget from drop");
1409 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001410 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001411 }
1412
Sunny Goyal2100c782016-08-22 16:00:03 -07001413 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001414 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1415 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1416 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001417
Adam Cohenad4e15c2013-10-17 16:21:35 -07001418 Runnable onComplete = new Runnable() {
1419 @Override
1420 public void run() {
1421 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001422 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001423 }
1424 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001425 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001426 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001427 }
1428 }
Romain Guycbb89e42009-06-08 15:52:54 -07001429
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001430 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1431 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001432 info.container = container;
1433 info.screenId = screenId;
1434 if (cell != null) {
1435 info.cellX = cell[0];
1436 info.cellY = cell[1];
1437 }
1438 info.spanX = spanX;
1439 info.spanY = spanY;
1440
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001441 switch (info.itemType) {
1442 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1443 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001444 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001445 break;
1446 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001447 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001448 break;
1449 default:
1450 throw new IllegalStateException("Unknown item type: " + info.itemType);
1451 }
1452 }
1453
Adam Cohenfbba09b2011-07-18 21:43:05 -07001454 /**
1455 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001456 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001457 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001458 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1459 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001460 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1461 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1462 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001463 }
1464
Adam Cohenfbba09b2011-07-18 21:43:05 -07001465 /**
1466 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001467 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001468 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001469 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001470 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001471 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001472 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001473 // In the case where we've prebound the widget, we remove it from the DragLayer
1474 if (LOGD) {
1475 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1476 }
1477 getDragLayer().removeView(hostView);
1478
Adam Cohened66b2b2012-01-23 17:28:51 -08001479 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001480 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001481
1482 // Clear the boundWidget so that it doesn't get destroyed.
1483 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001484 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001485 // In this case, we either need to start an activity to get permission to bind
1486 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001487 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1488 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1489 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1490 this, info.componentName);
1491 } else {
1492 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1493 }
Adam Cohendd70d662012-10-04 16:53:44 -07001494 Bundle options = info.bindOptions;
1495
Sunny Goyalffe83f12014-08-14 17:39:34 -07001496 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1497 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001498 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001499 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001500 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001501 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001502 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001503 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001504 }
1505
Adam Cohendcd297f2013-06-18 13:13:40 -07001506 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001507 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001508 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001509 folderInfo.title = getText(R.string.folder_name);
1510
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001512 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001513
1514 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001515 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301516 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001517 // Force measure the new folder icon
1518 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1519 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001520 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001521 }
Romain Guycbb89e42009-06-08 15:52:54 -07001522
Winsonc0b52fe2015-09-09 16:38:15 -07001523 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001524 * Unbinds the view for the specified item, and removes the item and all its children.
1525 *
1526 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001527 * @param itemInfo the {@link ItemInfo} for this view.
1528 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001529 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001530 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001531 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001532 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001533 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1534 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001535 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001536 } else {
1537 mWorkspace.removeWorkspaceItem(v);
1538 }
Winsonc0b52fe2015-09-09 16:38:15 -07001539 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001540 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001541 }
1542 } else if (itemInfo instanceof FolderInfo) {
1543 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001544 if (v instanceof FolderIcon) {
1545 ((FolderIcon) v).removeListeners();
1546 }
Winsonc0b52fe2015-09-09 16:38:15 -07001547 mWorkspace.removeWorkspaceItem(v);
1548 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001549 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001550 }
1551 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1552 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001553 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001554 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07001555 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001556 }
1557 } else {
1558 return false;
1559 }
1560 return true;
1561 }
1562
1563 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07001564 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07001565 */
Sunny Goyal56c73602015-09-25 12:55:01 -07001566 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07001567 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07001568 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07001569 // Deleting an app widget ID is a void call but writes to disk before returning
1570 // to the caller...
1571 new AsyncTask<Void, Void, Void>() {
1572 public Void doInBackground(Void ... args) {
1573 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
1574 return null;
1575 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07001576 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07001577 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001578 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001579 }
1580
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001581 @Override
1582 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001583 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001584 }
1585
Joe Onorato88ec0992009-11-19 13:16:06 -08001586 @Override
1587 public void onBackPressed() {
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08001588 if (finishAutoCancelActionMode()) {
1589 return;
1590 }
Adam Cohen9211d422014-10-07 18:14:53 -07001591 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1592 return;
1593 }
1594
Sunny Goyal45478022015-06-08 16:52:41 -07001595 if (mDragController.isDragging()) {
1596 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001597 return;
1598 }
1599
Jon Mirandafeba90f2016-10-06 10:53:29 -07001600 // Note: There should be at most one log per method call. This is enforced implicitly
1601 // by using if-else statements.
1602 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001603 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
Tony Wickham52c1b662018-05-21 13:13:58 -07001604 if (topView != null && topView.onBackPressed()) {
1605 // Handled by the floating view.
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001606 } else {
Sunny Goyale39690b2018-08-02 13:35:07 -07001607 mStateManager.getState().onBackPressed(this);
Michael Jurkaaf442092010-06-10 17:01:57 -07001608 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001609 }
1610
Sunny Goyalfe770c92016-09-27 14:38:58 -07001611 @TargetApi(Build.VERSION_CODES.M)
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001612 @Override
Jon Miranda73c27ec2018-05-16 18:06:23 -07001613 public ActivityOptions getActivityLaunchOptions(View v) {
1614 return mAppTransitionManager.getActivityLaunchOptions(this, v);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001615 }
1616
Tony Wickham3a6746a2018-03-29 09:39:56 -07001617 public LauncherAppTransitionManager getAppTransitionManager() {
1618 return mAppTransitionManager;
1619 }
1620
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001621 @TargetApi(Build.VERSION_CODES.M)
1622 @Override
1623 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
1624 // Due to legacy reasons, direct call shortcuts require Launchers to have the
1625 // corresponding permission. Show the appropriate permission prompt if that
1626 // is the case.
1627 if (intent.getComponent() == null
1628 && Intent.ACTION_CALL.equals(intent.getAction())
1629 && checkSelfPermission(android.Manifest.permission.CALL_PHONE) !=
1630 PackageManager.PERMISSION_GRANTED) {
1631
1632 setWaitingForResult(PendingRequestArgs
1633 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
1634 requestPermissions(new String[]{android.Manifest.permission.CALL_PHONE},
1635 REQUEST_PERMISSION_CALL_PHONE);
1636 return true;
1637 } else {
1638 return false;
1639 }
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001640 }
1641
Hyunyoung Song46d07f72018-05-22 15:41:25 -07001642 @Override
1643 public void modifyUserEvent(LauncherLogProto.LauncherEvent event) {
1644 if (event.srcTarget != null && event.srcTarget.length > 0 &&
1645 event.srcTarget[1].containerType == ContainerType.PREDICTION) {
1646 Target[] targets = new Target[3];
1647 targets[0] = event.srcTarget[0];
1648 targets[1] = event.srcTarget[1];
1649 targets[2] = newTarget(Target.Type.CONTAINER);
1650 event.srcTarget = targets;
1651 LauncherState state = mStateManager.getState();
1652 if (state == LauncherState.ALL_APPS) {
1653 event.srcTarget[2].containerType = ContainerType.ALLAPPS;
1654 } else if (state == LauncherState.OVERVIEW) {
1655 event.srcTarget[2].containerType = ContainerType.TASKSWITCHER;
1656 }
1657 }
1658 }
1659
Sunny Goyala7ce1662016-05-31 15:01:35 -07001660 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Hyunyoung Song1241e612018-05-15 21:46:51 -07001661 boolean success = super.startActivitySafely(v, intent, item);
1662 if (success && v instanceof BubbleTextView) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001663 // This is set to the view that launched the activity that navigated the user away
1664 // from launcher. Since there is no callback for when the activity has finished
1665 // launching, enable the press state and keep this reference to reset the press
1666 // state when we return to launcher.
1667 BubbleTextView btv = (BubbleTextView) v;
1668 btv.setStayPressed(true);
1669 setOnResumeCallback(btv);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001670 }
Hyunyoung Song1241e612018-05-15 21:46:51 -07001671 return success;
Michael Jurka86a720e2012-05-09 11:23:23 -07001672 }
1673
Winson Chung3d503fb2011-07-13 17:25:49 -07001674 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001675 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07001676 return mHotseat != null && layout != null &&
1677 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
1678 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001679
Winson Chung3d503fb2011-07-13 17:25:49 -07001680 /**
1681 * Returns the CellLayout of the specified container at the specified screen.
1682 */
Sunny Goyal92820592015-03-02 11:31:35 -08001683 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001684 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1685 if (mHotseat != null) {
1686 return mHotseat.getLayout();
1687 } else {
1688 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08001689 }
Winson Chung3d503fb2011-07-13 17:25:49 -07001690 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08001691 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08001692 }
1693 }
1694
Michael Jurkae326f182011-11-21 14:05:46 -08001695 @Override
1696 public void onTrimMemory(int level) {
1697 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07001698 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
1699 // The widget preview db can result in holding onto over
1700 // 3MB of memory for caching which isn't necessary.
1701 SQLiteDatabase.releaseMemory();
1702
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08001703 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07001704 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08001705 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01001706 if (mLauncherCallbacks != null) {
1707 mLauncherCallbacks.onTrimMemory(level);
1708 }
Winson Chung62a70be2018-02-23 15:10:05 -08001709 UiFactory.onTrimMemory(this, level);
Michael Jurkae326f182011-11-21 14:05:46 -08001710 }
1711
Michael Jurkad7c28052012-04-27 15:43:36 -07001712 @Override
1713 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07001714 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07001715 final List<CharSequence> text = event.getText();
1716 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07001717 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001718 // TODO: When can workspace be null?
1719 text.add(mWorkspace == null
1720 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07001721 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07001722 return result;
1723 }
1724
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001725 public void setOnResumeCallback(OnResumeCallback callback) {
1726 if (mOnResumeCallback != null) {
1727 mOnResumeCallback.onLauncherResume();
1728 }
1729 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07001730 }
1731
Michael Jurka7607c2f2013-04-03 14:33:19 -07001732 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001733 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001734 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07001735 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001736 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07001737 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001738 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07001739 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07001740 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07001741 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001742 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001743
Joe Onorato9c1289c2009-08-17 11:03:03 -04001744 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07001745 * Clear any pending bind callbacks. This is called when is loader is planning to
1746 * perform a full rebind from scratch.
1747 */
1748 @Override
1749 public void clearPendingBinds() {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001750 if (mPendingExecutor != null) {
1751 mPendingExecutor.markCompleted();
1752 mPendingExecutor = null;
1753 }
1754 }
1755
1756 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001757 * Refreshes the shortcuts shown on the workspace.
1758 *
1759 * Implementation of the method from LauncherModel.Callbacks.
1760 */
1761 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001762 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001763 // Floating panels (except the full widget sheet) are associated with individual icons. If
1764 // we are starting a fresh bind, close all such panels as all the icons are about
1765 // to go away.
1766 AbstractFloatingView.closeOpenViews(this, true,
Sunny Goyal7ede6112017-12-05 15:11:21 -08001767 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_REBIND_SAFE);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08001768
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001769 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08001770
Winson Chungd64d1762013-08-20 14:37:16 -07001771 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07001772 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07001773 mWorkspace.removeAllWorkspaceScreens();
Sunny Goyalc11fac32018-02-27 19:20:15 -08001774 mAppWidgetHost.clearViews();
Winson Chungd64d1762013-08-20 14:37:16 -07001775
Winson Chung3d503fb2011-07-13 17:25:49 -07001776 if (mHotseat != null) {
Sunny Goyal11e96492018-03-23 17:06:00 -07001777 mHotseat.resetLayout(mDeviceProfile.isVerticalBarLayout());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001778 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001779 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001780 }
1781
Adam Cohendcd297f2013-06-18 13:13:40 -07001782 @Override
1783 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001784 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001785 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
1786 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001787 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
1788 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001789 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001790 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
1791 // If there are no screens, we need to have an empty screen
1792 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07001793 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001794 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07001795
Winsonc7d2e832016-07-28 12:24:55 -07001796 // After we have added all the screens, if the wallpaper was locked to the default state,
1797 // then notify to indicate that it can be released and a proper wallpaper offset can be
1798 // computed before the next layout
1799 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07001800 }
1801
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001802 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001803 int count = orderedScreenIds.size();
1804 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001805 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001806 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001807 // No need to bind the first screen, as its always bound.
1808 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
1809 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001810 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001811 }
1812
Sunny Goyalb23980c2017-08-17 07:45:25 -07001813 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08001814 public void bindAppsAdded(ArrayList<Long> newScreens, ArrayList<ItemInfo> addNotAnimated,
1815 ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07001816 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08001817 if (newScreens != null) {
1818 bindAddScreens(newScreens);
1819 }
Winson Chungd64d1762013-08-20 14:37:16 -07001820
1821 // We add the items without animation on non-visible pages, and with
1822 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08001823 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001824 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001825 }
Adam Cohen76a47a12014-02-05 11:47:43 -08001826 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001827 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07001828 }
Winson Chungc58497e2013-09-03 17:48:37 -07001829
Winson Chung87412982013-10-03 18:34:14 -07001830 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07001831 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001832 }
1833
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001834 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001835 * Bind the items start-end from the list.
1836 *
1837 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001838 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07001839 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07001840 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001841 // Get the list of added items and intersect them with the set of items here
Sunny Goyal849c6a22018-08-08 16:33:46 -07001842 final AnimatorSet anim = new AnimatorSet();
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001843 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07001844 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001845 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001846 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07001847 int end = items.size();
1848 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001849 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07001850
1851 // Short circuit if we are loading dock items for a configuration which has no dock
1852 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
1853 mHotseat == null) {
1854 continue;
1855 }
1856
Sunny Goyal639e9062015-08-19 19:17:06 -07001857 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001858 switch (item.itemType) {
1859 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1860 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08001861 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001862 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07001863 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001864 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001865 }
1866 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07001867 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001868 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001869 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001870 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001871 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07001872 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1873 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07001874 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07001875 if (view == null) {
1876 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001877 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08001878 break;
1879 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001880 default:
1881 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001882 }
Sunny Goyal639e9062015-08-19 19:17:06 -07001883
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001884 /*
1885 * Remove colliding items.
1886 */
1887 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1888 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
1889 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
1890 View v = cl.getChildAt(item.cellX, item.cellY);
1891 Object tag = v.getTag();
1892 String desc = "Collision while binding workspace item: " + item
1893 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08001894 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001895 throw (new RuntimeException(desc));
1896 } else {
1897 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001898 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001899 continue;
1900 }
1901 }
1902 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05301903 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07001904 if (animateIcons) {
1905 // Animate all the applications up now
1906 view.setAlpha(0f);
1907 view.setScaleX(0f);
1908 view.setScaleY(0f);
1909 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08001910 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07001911 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001912 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07001913
Winson Chung997a9232013-07-24 15:33:46 -07001914 if (animateIcons) {
1915 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08001916 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07001917 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08001918 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07001919 final Runnable startBounceAnimRunnable = new Runnable() {
1920 public void run() {
1921 anim.playTogether(bounceAnims);
1922 anim.start();
1923 }
1924 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08001925 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07001926 // We post the animation slightly delayed to prevent slowdowns
1927 // when we are loading right after we return to launcher.
1928 mWorkspace.postDelayed(new Runnable() {
1929 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00001930 if (mWorkspace != null) {
Jon Miranda64eb8ea2018-03-29 11:43:58 -07001931 AbstractFloatingView.closeAllOpenViews(Launcher.this, false);
1932
Ian Parkinson94449152014-01-21 13:09:59 +00001933 mWorkspace.snapToPage(newScreenIndex);
1934 mWorkspace.postDelayed(startBounceAnimRunnable,
1935 NEW_APPS_ANIMATION_DELAY);
1936 }
Winson Chung94d67682013-09-25 16:29:40 -07001937 }
1938 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07001939 } else {
1940 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07001941 }
1942 }
Winson Chung64359a52013-07-08 17:17:08 -07001943 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001944 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001945 }
1946
Joe Onorato9c1289c2009-08-17 11:03:03 -04001947 /**
1948 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001949 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07001950 public void bindAppWidget(LauncherAppWidgetInfo item) {
1951 View view = inflateAppWidget(item);
1952 if (view != null) {
1953 mWorkspace.addInScreen(view, item);
1954 mWorkspace.requestLayout();
1955 }
1956 }
1957
1958 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001959 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05301960 PendingAppWidgetHostView view =
1961 new PendingAppWidgetHostView(this, item, mIconCache, true);
1962 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07001963 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001964 }
1965
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001966 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001967
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001968 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08001969
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001970 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
1971 // If the provider is not ready, bind as a pending widget.
1972 appWidgetInfo = null;
1973 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
1974 // The widget id is not valid. Try to find the widget based on the provider info.
1975 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
1976 } else {
1977 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
1978 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07001979
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001980 // If the provider is ready, but the width is not yet restored, try to restore it.
1981 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
1982 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07001983 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001984 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
1985 + " belongs to component " + item.providerName
1986 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001987 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07001988 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07001989 }
Sunny Goyalff572272014-07-23 13:58:07 -07001990
Sunny Goyal84b4adc2015-08-12 15:12:16 -07001991 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001992 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07001993 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
1994 // Id has not been allocated yet. Allocate a new id.
1995 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
1996 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07001997
Sunny Goyald478c832016-04-01 12:04:16 -07001998 // Also try to bind the widget. If the bind fails, the user will be shown
1999 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08002000 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07002001 pendingInfo.spanX = item.spanX;
2002 pendingInfo.spanY = item.spanY;
2003 pendingInfo.minSpanX = item.minSpanX;
2004 pendingInfo.minSpanY = item.minSpanY;
2005 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07002006
2007 boolean isDirectConfig =
2008 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2009 if (isDirectConfig && item.bindOptions != null) {
2010 Bundle newOptions = item.bindOptions.getExtras();
2011 if (options != null) {
2012 newOptions.putAll(options);
2013 }
2014 options = newOptions;
2015 }
Sunny Goyald478c832016-04-01 12:04:16 -07002016 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2017 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002018
Sunny Goyal86df1382016-08-10 15:03:22 -07002019 // We tried to bind once. If we were not able to bind, we would need to
2020 // go through the permission dialog, which means we cannot skip the config
2021 // activity.
2022 item.bindOptions = null;
2023 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2024
Sunny Goyald478c832016-04-01 12:04:16 -07002025 // Bind succeeded
2026 if (success) {
2027 // If the widget has a configure activity, it is still needs to set it up,
2028 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002029 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002030 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2031 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002032 }
Sunny Goyald478c832016-04-01 12:04:16 -07002033
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002034 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002035 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002036 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002037 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002038 // The widget was marked as UI not ready, but there is no configure activity to
2039 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002040 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002041 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002042 }
Sunny Goyalff572272014-07-23 13:58:07 -07002043 }
2044
Sunny Goyald5462aa2016-11-22 16:52:39 +05302045 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002046 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002047 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002048 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002049 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07002050 deleteWidgetInfo(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002051 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002052 }
2053
Sunny Goyal233ee962015-08-03 13:05:01 -07002054 item.minSpanX = appWidgetInfo.minSpanX;
2055 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302056 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002057 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302058 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002059 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302060 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002061
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002062 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002063 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002064 }
2065
Sunny Goyalff572272014-07-23 13:58:07 -07002066 /**
2067 * Restores a pending widget.
2068 *
2069 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002070 */
Sunny Goyald478c832016-04-01 12:04:16 -07002071 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002072 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2073 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2074 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002075 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002076 }
2077
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002078 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002079 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002080 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2081 info.pendingItemInfo = null;
2082 }
Sunny Goyalff572272014-07-23 13:58:07 -07002083
Sunny Goyalba47faa2017-10-04 16:50:57 -07002084 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002085 view.reInflate();
Sunny Goyalba47faa2017-10-04 16:50:57 -07002086 }
2087
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002088 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002089 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002090 }
2091
Adam Cohen1462de32012-07-24 22:34:36 -07002092 public void onPageBoundSynchronously(int page) {
Sunny Goyalf4aceab2017-12-06 16:16:31 -08002093 mSynchronouslyBoundPage = page;
Adam Cohen1462de32012-07-24 22:34:36 -07002094 }
2095
Sunny Goyal527c7d32015-08-28 15:19:36 -07002096 @Override
2097 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2098 if (mPendingExecutor != null) {
2099 mPendingExecutor.markCompleted();
2100 }
2101 mPendingExecutor = executor;
Sunny Goyal60180b02018-02-07 12:56:30 -08002102 if (!isInState(ALL_APPS)) {
2103 mAppsView.getAppsStore().setDeferUpdates(true);
2104 mPendingExecutor.execute(() -> mAppsView.getAppsStore().setDeferUpdates(false));
2105 }
2106
Sunny Goyal527c7d32015-08-28 15:19:36 -07002107 executor.attachTo(this);
2108 }
2109
2110 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2111 if (mPendingExecutor == executor) {
2112 mPendingExecutor = null;
2113 }
2114 }
2115
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002116 @Override
2117 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
Sunny Goyal6001ea22018-05-10 16:31:00 -07002118 AlphaProperty property = mDragLayer.getAlphaProperty(ALPHA_INDEX_LAUNCHER_LOAD);
2119 if (property.getValue() < 1) {
2120 ObjectAnimator anim = ObjectAnimator.ofFloat(property, MultiValueAlpha.VALUE, 1);
2121 if (executor != null) {
2122 anim.addListener(new AnimatorListenerAdapter() {
2123 @Override
2124 public void onAnimationEnd(Animator animation) {
2125 executor.onLoadAnimationCompleted();
2126 }
2127 });
2128 }
2129 anim.start();
Sunny Goyal29947f02017-12-18 13:49:44 -08002130 } else if (executor != null) {
2131 executor.onLoadAnimationCompleted();
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002132 }
2133 }
2134
Joe Onorato9c1289c2009-08-17 11:03:03 -04002135 /**
2136 * Callback saying that there aren't any more items to bind.
2137 *
2138 * Implementation of the method from LauncherModel.Callbacks.
2139 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002140 public void finishBindingItems() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002141 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002142 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002143
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002144 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002145
Sunny Goyal2100c782016-08-22 16:00:03 -07002146 if (mPendingActivityResult != null) {
2147 handleActivityResult(mPendingActivityResult.requestCode,
2148 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2149 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002150 }
2151
Sunny Goyala474a9b2017-05-04 16:47:11 -07002152 InstallShortcutReceiver.disableAndFlushInstallQueue(
2153 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002154
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002155 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002156 }
2157
Winson Chunga2413752012-04-03 14:22:34 -07002158 private boolean canRunNewAppsAnimation() {
2159 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002160 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002161 }
2162
Winson Chungc9168342013-06-26 14:54:55 -07002163 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal849c6a22018-08-08 16:33:46 -07002164 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
2165 PropertyValuesHolder.ofFloat(View.ALPHA, 1f),
2166 PropertyValuesHolder.ofFloat(View.SCALE_X, 1f),
2167 PropertyValuesHolder.ofFloat(View.SCALE_Y, 1f));
Winson Chungc9168342013-06-26 14:54:55 -07002168 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
2169 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002170 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002171 return bounceAnim;
2172 }
2173
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002174 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002175 * Add the icons for all apps.
2176 *
2177 * Implementation of the method from LauncherModel.Callbacks.
2178 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002179 public void bindAllApplications(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002180 mAppsView.getAppsStore().setApps(apps);
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002181
Adam Cohen9211d422014-10-07 18:14:53 -07002182 if (mLauncherCallbacks != null) {
2183 mLauncherCallbacks.bindAllApplications(apps);
2184 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002185 }
2186
2187 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002188 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
2189 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2190 */
2191 @Override
2192 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002193 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002194 }
2195
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002196 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002197 * A package was updated.
2198 *
2199 * Implementation of the method from LauncherModel.Callbacks.
2200 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002201 @Override
2202 public void bindAppsAddedOrUpdated(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002203 mAppsView.getAppsStore().addOrUpdateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002204 }
2205
Sunny Goyal4390ace2014-10-13 11:33:11 -07002206 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002207 public void bindPromiseAppProgressUpdated(PromiseAppInfo app) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002208 mAppsView.getAppsStore().updatePromiseAppProgress(app);
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002209 }
2210
2211 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002212 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002213 mWorkspace.widgetsRestored(widgets);
2214 }
2215
Joe Onorato9c1289c2009-08-17 11:03:03 -04002216 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002217 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002218 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002219 *
2220 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002221 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002222 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002223 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002224 if (!updated.isEmpty()) {
2225 mWorkspace.updateShortcuts(updated);
2226 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002227 }
2228
2229 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002230 * Update the state of a package, typically related to install state.
2231 *
2232 * Implementation of the method from LauncherModel.Callbacks.
2233 */
Sunny Goyale755d462014-07-22 13:48:29 -07002234 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002235 public void bindRestoreItemsChange(HashSet<ItemInfo> updates) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07002236 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07002237 }
2238
2239 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002240 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07002241 * in addition to specific applications to remove, the reason being that
2242 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002243 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07002244 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002245 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07002246 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002247 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002248 mWorkspace.removeItemsByMatcher(matcher);
2249 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002250 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002251
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002252 @Override
2253 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002254 mAppsView.getAppsStore().removeApps(appInfos);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002255 }
Joe Onoratobe386092009-11-17 17:32:16 -08002256
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002257 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002258 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
2259 mPopupDataProvider.setAllWidgets(allWidgets);
Tony Wickham26b17462017-03-20 17:12:24 -07002260 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
2261 if (topView != null) {
2262 topView.onWidgetsBound();
2263 }
2264 }
2265
Tony Wickham86222d22017-03-29 15:30:43 -07002266 /**
2267 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
2268 * refreshes the widgets and shortcuts associated with the given package/user
2269 */
2270 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07002271 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002272 }
2273
Winson Chung80baf5a2010-08-09 16:03:15 -07002274 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002275 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08002276 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002277 @Override
2278 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
2279 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002280
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002281 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
2282 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07002283 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002284 writer.println(prefix + " Homescreen " + i);
2285
2286 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
2287 for (int j = 0; j < layout.getChildCount(); j++) {
2288 Object tag = layout.getChildAt(j).getTag();
2289 if (tag != null) {
2290 writer.println(prefix + " " + tag.toString());
2291 }
2292 }
2293 }
2294
2295 writer.println(prefix + " Hotseat");
2296 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07002297 for (int j = 0; j < layout.getChildCount(); j++) {
2298 Object tag = layout.getChildAt(j).getTag();
2299 if (tag != null) {
2300 writer.println(prefix + " " + tag.toString());
2301 }
Adam Cohen4caf2982013-08-20 18:54:31 -07002302 }
Sunny Goyala1365452015-10-01 15:46:24 -07002303 }
2304
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002305 writer.println(prefix + "Misc:");
2306 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
2307 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
2308 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
Sunny Goyale43d00d2018-05-14 14:23:18 -07002309 writer.println(" mRotationHelper: " + mRotationHelper);
2310 dumpMisc(writer);
Sunny Goyal2bba1902018-02-27 12:20:50 -08002311
2312 try {
2313 FileLog.flushAll(writer);
2314 } catch (Exception e) {
2315 // Ignore
2316 }
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002317
2318 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002319
Adam Cohen9211d422014-10-07 18:14:53 -07002320 if (mLauncherCallbacks != null) {
2321 mLauncherCallbacks.dump(prefix, fd, writer, args);
2322 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002323 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002324
Sunny Goyal66b24572016-09-21 15:57:55 -07002325 @Override
2326 @TargetApi(Build.VERSION_CODES.N)
2327 public void onProvideKeyboardShortcuts(
2328 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
2329
2330 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002331 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002332 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
2333 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
Vadim Tryshev74c261c2018-06-12 13:44:43 -07002334 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.widget_button_text),
2335 KeyEvent.KEYCODE_W, KeyEvent.META_CTRL_ON));
Sunny Goyal66b24572016-09-21 15:57:55 -07002336 }
Vadim Tryshev3455f8d2018-05-11 20:02:20 -07002337 final View currentFocus = getCurrentFocus();
2338 if (currentFocus != null) {
2339 if (new CustomActionsPopup(this, currentFocus).canShow()) {
2340 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
2341 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
2342 }
2343 if (currentFocus.getTag() instanceof ItemInfo
2344 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
2345 shortcutInfos.add(new KeyboardShortcutInfo(
2346 getString(R.string.shortcuts_menu_with_notifications_description),
2347 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
2348 }
Sunny Goyal66b24572016-09-21 15:57:55 -07002349 }
2350 if (!shortcutInfos.isEmpty()) {
2351 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
2352 }
2353
2354 super.onProvideKeyboardShortcuts(data, menu, deviceId);
2355 }
2356
2357 @Override
2358 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
2359 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
2360 switch (keyCode) {
2361 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002362 if (isInState(NORMAL)) {
2363 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07002364 return true;
2365 }
2366 break;
2367 case KeyEvent.KEYCODE_S: {
2368 View focusedView = getCurrentFocus();
2369 if (focusedView instanceof BubbleTextView
2370 && focusedView.getTag() instanceof ItemInfo
2371 && mAccessibilityDelegate.performAction(focusedView,
2372 (ItemInfo) focusedView.getTag(),
2373 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal00ac9202017-11-09 15:24:06 -08002374 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07002375 return true;
2376 }
2377 break;
2378 }
2379 case KeyEvent.KEYCODE_O:
2380 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
2381 return true;
2382 }
2383 break;
Vadim Tryshev74c261c2018-06-12 13:44:43 -07002384 case KeyEvent.KEYCODE_W:
2385 if (isInState(NORMAL)) {
2386 OptionsPopupView.openWidgets(this);
2387 return true;
2388 }
2389 break;
Sunny Goyal66b24572016-09-21 15:57:55 -07002390 }
2391 }
2392 return super.onKeyShortcut(keyCode, event);
2393 }
2394
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002395 @Override
2396 public boolean onKeyUp(int keyCode, KeyEvent event) {
2397 if (keyCode == KeyEvent.KEYCODE_MENU) {
2398 // KEYCODE_MENU is sent by some tests, for example
2399 // LauncherJankTests#testWidgetsContainerFling. Don't just remove its handling.
2400 if (!mDragController.isDragging() && !mWorkspace.isSwitchingState() &&
2401 isInState(NORMAL)) {
2402 // Close any open floating views.
2403 AbstractFloatingView.closeAllOpenViews(this);
2404
2405 // Setting the touch point to (-1, -1) will show the options popup in the center of
2406 // the screen.
Sunny Goyal2fd7a8b2018-03-30 17:10:13 -07002407 OptionsPopupView.showDefaultOptions(this, -1, -1);
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002408 }
2409 return true;
2410 }
2411 return super.onKeyUp(keyCode, event);
2412 }
2413
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002414 public static Launcher getLauncher(Context context) {
Sunny Goyal87b5eb62018-07-03 15:53:39 -07002415 return (Launcher) fromContext(context);
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002416 }
2417
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002418 /**
2419 * Callback for listening for onResume
2420 */
2421 public interface OnResumeCallback {
2422
2423 void onLauncherResume();
2424 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002425}