blob: 55074f8134164965e85c09975bef3983ed685ca1 [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;
Tony Wickham6a71a5b2018-08-21 11:40:23 -070022import static com.android.launcher3.AbstractFloatingView.TYPE_SNACKBAR;
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;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070034import android.animation.ValueAnimator;
Adam Cohen0b395352014-06-09 22:54:36 +000035import android.annotation.TargetApi;
Tony Wickham005df0b2018-02-13 11:28:12 -080036import android.app.ActivityOptions;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080037import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070038import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080040import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080041import android.content.ComponentCallbacks2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070044import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070045import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070046import android.content.SharedPreferences;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.content.pm.PackageManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080048import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070049import android.database.sqlite.SQLiteDatabase;
Sunny Goyal59d086c2018-05-07 17:31:40 -070050import android.graphics.Point;
Adam Cohen0f668f32014-09-08 19:54:17 +020051import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052import android.os.Bundle;
Hyunyoung Songbd6fba92018-05-16 15:54:31 -070053import android.os.Handler;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070054import android.os.Parcelable;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080055import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070056import android.os.StrictMode;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070057import android.os.UserHandle;
Michael Jurkaa33411c2012-06-14 16:18:21 -070058import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070060import android.util.Log;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070061import android.util.SparseArray;
Sunny Goyal59d086c2018-05-07 17:31:40 -070062import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070063import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070064import android.view.KeyboardShortcutGroup;
65import android.view.KeyboardShortcutInfo;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080066import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -070067import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070068import android.view.View;
Adam Cohen96d30a12013-07-16 18:13:21 -070069import android.view.ViewGroup;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080070import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070071import android.view.animation.OvershootInterpolator;
Adam Cohen96d30a12013-07-16 18:13:21 -070072import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070073
Sunny Goyal651077b2014-06-30 14:15:31 -070074import com.android.launcher3.DropTarget.DragObject;
Sunny Goyalae502842016-06-17 08:43:56 -070075import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070076import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070077import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal7ede6112017-12-05 15:11:21 -080078import com.android.launcher3.allapps.DiscoveryBounce;
Sunny Goyalf0b6db72018-08-13 16:10:14 -070079import com.android.launcher3.anim.PropertyListBuilder;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070080import com.android.launcher3.badge.BadgeInfo;
Sunny Goyalffe83f12014-08-14 17:39:34 -070081import com.android.launcher3.compat.AppWidgetManagerCompat;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -070082import com.android.launcher3.compat.LauncherAppsCompatVO;
Tony Wickhame0c33232016-02-08 11:37:04 -080083import com.android.launcher3.config.FeatureFlags;
Vadim Tryshevfedca432015-08-19 17:55:02 -070084import com.android.launcher3.dragndrop.DragController;
85import com.android.launcher3.dragndrop.DragLayer;
86import com.android.launcher3.dragndrop.DragView;
Jon Mirandaaeb4dd02018-07-11 14:16:23 -070087import com.android.launcher3.folder.Folder;
Sunny Goyal26119432016-02-18 22:09:23 +000088import com.android.launcher3.folder.FolderIcon;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070089import com.android.launcher3.folder.FolderIconPreviewVerifier;
Sunny Goyal66b24572016-09-21 15:57:55 -070090import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -070091import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -070092import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -070093import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Song46d07f72018-05-22 15:41:25 -070094import com.android.launcher3.logging.UserEventDispatcher.UserEventDelegate;
Sunny Goyala535ae42017-02-27 10:07:13 -080095import com.android.launcher3.model.ModelWriter;
Sunny Goyala535ae42017-02-27 10:07:13 -080096import com.android.launcher3.notification.NotificationListener;
Tony Wickham540913e2017-01-23 11:47:51 -080097import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -080098import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -070099import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyalf8088ee2017-11-10 14:52:00 -0800100import com.android.launcher3.states.InternalStateHandler;
Sunny Goyal623eddd2018-03-02 12:24:41 -0800101import com.android.launcher3.states.RotationHelper;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800102import com.android.launcher3.touch.ItemClickHandler;
Sunny Goyala5ace712017-11-15 17:12:51 -0800103import com.android.launcher3.uioverrides.UiFactory;
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700104import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530105import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
106import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
Sunny Goyalaeb16432017-10-16 11:46:41 -0700107import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyal2100c782016-08-22 16:00:03 -0700108import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700109import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700110import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700111import com.android.launcher3.util.MultiHashMap;
Sunny Goyal6001ea22018-05-10 16:31:00 -0700112import com.android.launcher3.util.MultiValueAlpha;
113import com.android.launcher3.util.MultiValueAlpha.AlphaProperty;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700114import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800115import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700116import com.android.launcher3.util.PendingRequestArgs;
Sunny Goyal8392c822017-06-20 10:03:56 -0700117import com.android.launcher3.util.SystemUiController;
Mario Bertschlera6936942017-05-31 14:48:19 -0700118import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700119import com.android.launcher3.util.Thunk;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700120import com.android.launcher3.util.TraceHelper;
Sunny Goyal326403e2017-10-02 12:45:10 -0700121import com.android.launcher3.util.UiThreadHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700122import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800123import com.android.launcher3.views.OptionsPopupView;
Sunny Goyal29947f02017-12-18 13:49:44 -0800124import com.android.launcher3.widget.LauncherAppWidgetHostView;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800125import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700126import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal29947f02017-12-18 13:49:44 -0800127import com.android.launcher3.widget.PendingAppWidgetHostView;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800128import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700129import com.android.launcher3.widget.WidgetHostViewLoader;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700130import com.android.launcher3.widget.WidgetListRowEntry;
131import com.android.launcher3.widget.WidgetsFullSheet;
Sunny Goyal952e63d2017-08-16 04:59:08 -0700132import com.android.launcher3.widget.custom.CustomWidgetParser;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700133
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700134import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700135import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700136import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700137import java.util.Collection;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700138import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700139import java.util.List;
Tony Wickham010d2552017-01-20 08:15:28 -0800140import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700141
Sunny Goyald2303072018-08-14 15:21:45 -0700142import androidx.annotation.Nullable;
143
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);
arangelovad783052018-08-17 14:46:45 +0100355 mAllAppsController.highlightWorkTabIfNecessary();
Winson Chung8eb49e02018-07-16 13:33:31 -0700356 }
357
358 @Override
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800359 public void onConfigurationChanged(Configuration newConfig) {
360 int diff = newConfig.diff(mOldConfig);
Jon Mirandaaeb4dd02018-07-11 14:16:23 -0700361
362 if ((diff & CONFIG_LOCALE) != 0) {
363 Folder.setLocaleDependentFields(getResources(), true /* force */);
364 }
365
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800366 if ((diff & (CONFIG_ORIENTATION | CONFIG_SCREEN_SIZE)) != 0) {
367 mUserEventDispatcher = null;
368 initDeviceProfile(mDeviceProfile.inv);
Sunny Goyalfde55052018-02-01 14:46:13 -0800369 dispatchDeviceProfileChanged();
Sunny Goyal59d086c2018-05-07 17:31:40 -0700370 reapplyUi();
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700371 mDragLayer.recreateControllers();
Sunny Goyal7185dd62018-03-14 17:51:49 -0700372
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800373 // TODO: We can probably avoid rebind when only screen size changed.
Sunny Goyal605bcf32018-03-02 15:16:12 -0800374 rebindModel();
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800375 }
376
377 mOldConfig.setTo(newConfig);
Tracy Zhoua706f002018-03-28 13:55:19 -0700378 UiFactory.onLauncherStateOrResumeChanged(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800379 super.onConfigurationChanged(newConfig);
380 }
381
Sunny Goyal605bcf32018-03-02 15:16:12 -0800382 @Override
Sunny Goyal59d086c2018-05-07 17:31:40 -0700383 protected void reapplyUi() {
384 getRootView().dispatchInsets();
385 getStateManager().reapplyState(true /* cancelCurrentAnimation */);
386 }
387
388 @Override
Sunny Goyal605bcf32018-03-02 15:16:12 -0800389 public void rebindModel() {
390 int currentPage = mWorkspace.getNextPage();
391 if (mModel.startLoader(currentPage)) {
392 mWorkspace.setCurrentPage(currentPage);
393 setWorkspaceLoading(true);
394 }
395 }
396
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800397 private void initDeviceProfile(InvariantDeviceProfile idp) {
398 // Load configuration-specific DeviceProfile
Sunny Goyal59d086c2018-05-07 17:31:40 -0700399 mDeviceProfile = idp.getDeviceProfile(this);
400 if (isInMultiWindowModeCompat()) {
401 Display display = getWindowManager().getDefaultDisplay();
402 Point mwSize = new Point();
403 display.getSize(mwSize);
404 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
405 }
406 onDeviceProfileInitiated();
Sunny Goyal605bcf32018-03-02 15:16:12 -0800407 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout(), true);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800408 }
409
Sunny Goyal623eddd2018-03-02 12:24:41 -0800410 public RotationHelper getRotationHelper() {
411 return mRotationHelper;
412 }
413
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700414 public LauncherStateManager getStateManager() {
415 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700416 }
417
Mario Bertschler27288382017-05-24 15:35:09 -0700418 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700419 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800420 return mLauncherView.findViewById(id);
421 }
422
423 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700424 public void onAppWidgetHostReset() {
425 if (mAppWidgetHost != null) {
426 mAppWidgetHost.startListening();
427 }
428 }
429
Adam Cohen9211d422014-10-07 18:14:53 -0700430 private LauncherCallbacks mLauncherCallbacks;
431
Sunny Goyal32554d12015-12-03 15:31:25 -0800432 /**
433 * Call this after onCreate to set or clear overlay.
434 */
435 public void setLauncherOverlay(LauncherOverlay overlay) {
436 if (overlay != null) {
437 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
438 }
439 mWorkspace.setLauncherOverlay(overlay);
440 }
441
Adam Cohen9211d422014-10-07 18:14:53 -0700442 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
443 mLauncherCallbacks = callbacks;
444 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700445 }
446
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700447 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700448 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700449 if (mLauncherCallbacks != null) {
450 mLauncherCallbacks.onLauncherProviderChange();
451 }
452 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700453
Hyunyoung Song3f471442015-04-08 19:01:34 -0700454 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700455 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
456 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700457 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700458 }
459
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000460 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700461 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
462 // This cast is safe as long as the id < 0x00FFFFFF
463 // Since we jail all the dynamically generated views, there should be no clashes
464 // with any other views.
465 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000466 }
467
Tony Wickham010d2552017-01-20 08:15:28 -0800468 public PopupDataProvider getPopupDataProvider() {
469 return mPopupDataProvider;
470 }
471
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700472 @Override
473 public BadgeInfo getBadgeInfoForItem(ItemInfo info) {
474 return mPopupDataProvider.getBadgeInfoForItem(info);
475 }
476
477 @Override
478 public void invalidateParent(ItemInfo info) {
479 FolderIconPreviewVerifier verifier = new FolderIconPreviewVerifier(getDeviceProfile().inv);
480 if (verifier.isItemInPreview(info.rank) && (info.container >= 0)) {
481 View folderIcon = getWorkspace().getHomescreenIconByItemId(info.container);
482 if (folderIcon != null) {
483 folderIcon.invalidate();
484 }
485 }
486 }
487
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000488 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700489 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
490 * a configuration step, this allows the proper animations to run after other transitions.
491 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700492 private long completeAdd(
493 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
494 long screenId = info.screenId;
495 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700496 // When the screen id represents an actual screen (as opposed to a rank) we make sure
497 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700498 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700499 }
Adam Cohendb364c32014-05-20 14:23:40 -0700500
Sunny Goyal2100c782016-08-22 16:00:03 -0700501 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800502 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700503 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700504 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800505 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700506 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700507 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700508 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700509 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700510 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700511 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700512 int widgetId = appWidgetId;
513 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700514 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700515 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700516 // Since the view was just bound, also launch the configure activity if needed
517 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
518 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800519 if (provider != null) {
520 new WidgetAddFlowHandler(provider)
521 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700522 }
523 }
524 break;
525 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800526 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700527
Adam Cohendb364c32014-05-20 14:23:40 -0700528 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800529 }
530
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800531 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700532 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700533 if (isWorkspaceLoading()) {
534 // process the result once the workspace has loaded.
535 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
536 return;
537 }
538 mPendingActivityResult = null;
539
Winson Chunge341d302013-08-16 14:31:00 -0700540 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700541 final PendingRequestArgs requestArgs = mPendingRequestArgs;
542 setWaitingForResult(null);
543 if (requestArgs == null) {
544 return;
545 }
546
547 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700548
Adam Cohenad4e15c2013-10-17 16:21:35 -0700549 Runnable exitSpringLoaded = new Runnable() {
550 @Override
551 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700552 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700553 }
554 };
555
Michael Jurka8b805b12012-04-18 14:23:14 -0700556 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700557 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700558 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700559 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
560 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700561 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700562 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700563 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700564 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700565 addAppWidgetImpl(
566 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800567 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700568 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700569 }
570 return;
571 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200572
Adam Cohened66b2b2012-01-23 17:28:51 -0800573 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700574 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700575
Adam Cohened66b2b2012-01-23 17:28:51 -0800576 // We have special handling for widgets
577 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800578 final int appWidgetId;
579 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
580 : -1;
581 if (widgetId < 0) {
582 appWidgetId = pendingAddWidgetId;
583 } else {
584 appWidgetId = widgetId;
585 }
586
Adam Cohenad4e15c2013-10-17 16:21:35 -0700587 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800588 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700589 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
590 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700591 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700592 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700593 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700594 @Override
595 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700596 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700597 }
598 };
Adam Cohendb364c32014-05-20 14:23:40 -0700599
Sunny Goyal2100c782016-08-22 16:00:03 -0700600 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
601 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
602 } else {
603 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
604 // When the screen id represents an actual screen (as opposed to a rank)
605 // we make sure that the drop page actually exists.
606 requestArgs.screenId =
607 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700608 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700609 final CellLayout dropLayout =
610 mWorkspace.getScreenWithId(requestArgs.screenId);
611
612 dropLayout.setDropPending(true);
613 final Runnable onComplete = new Runnable() {
614 @Override
615 public void run() {
616 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
617 dropLayout.setDropPending(false);
618 }
619 };
620 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
621 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700622 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800623 return;
624 }
625
Sunny Goyald478c832016-04-01 12:04:16 -0700626 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
627 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700628 if (resultCode == RESULT_OK) {
629 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700630 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700631 }
632 // Leave the widget in the pending state if the user canceled the configure.
633 return;
634 }
635
Sunny Goyalaad90582015-07-09 14:22:50 -0700636 if (requestCode == REQUEST_CREATE_SHORTCUT) {
637 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700638 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
639 completeAdd(requestCode, data, -1, requestArgs);
640 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
641 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
642
Sunny Goyalaad90582015-07-09 14:22:50 -0700643 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700644 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
645 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800646 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800647 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800648 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800649 }
650
651 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700652 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800653 final int requestCode, final int resultCode, final Intent data) {
654 handleActivityResult(requestCode, resultCode, data);
655 if (mLauncherCallbacks != null) {
656 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
657 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800658 }
659
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700660 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700661 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600662 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700663 PendingRequestArgs pendingArgs = mPendingRequestArgs;
664 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
665 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
666 setWaitingForResult(null);
667
Sunny Goyal28c6b962015-10-12 11:42:05 -0700668 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700669 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700670 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700671 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700672 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700673 Intent intent = pendingArgs.getPendingIntent();
674
Sunny Goyal28c6b962015-10-12 11:42:05 -0700675 if (grantResults.length > 0
676 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700677 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700678 } else {
679 // TODO: Show a snack bar with link to settings
680 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700681 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700682 }
683 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600684 if (mLauncherCallbacks != null) {
685 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
686 grantResults);
687 }
688 }
689
Adam Cohendb364c32014-05-20 14:23:40 -0700690 /**
691 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
692 *
693 * @param screenId the screen id to check
694 * @return the new screen, or screenId if it exists
695 */
696 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800697 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700698 if (dropLayout == null) {
699 // it's possible that the add screen was removed because it was
700 // empty and a re-bind occurred
701 mWorkspace.addExtraEmptyScreen();
702 return mWorkspace.commitExtraEmptyScreen();
703 } else {
704 return screenId;
705 }
706 }
707
Sunny Goyal2100c782016-08-22 16:00:03 -0700708 @Thunk void completeTwoStageWidgetDrop(
709 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
710 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800711 Runnable onCompleteRunnable = null;
712 int animationType = 0;
713
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700714 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800715 if (resultCode == RESULT_OK) {
716 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
717 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800718 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700719 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800720 onCompleteRunnable = new Runnable() {
721 @Override
722 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700723 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700724 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800725 }
726 };
727 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800728 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800729 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800730 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700731 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700732 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700733 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
734 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700735 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700736 // The animated view may be null in the case of a rotation during widget configuration
737 onCompleteRunnable.run();
738 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800739 }
740
741 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700742 protected void onStop() {
743 super.onStop();
Adam Cohen9211d422014-10-07 18:14:53 -0700744 if (mLauncherCallbacks != null) {
745 mLauncherCallbacks.onStop();
746 }
Hyunyoung Song1241e612018-05-15 21:46:51 -0700747 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
748 mStateManager.getState().containerType, -1);
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700749
750 mAppWidgetHost.setListenIfResumed(false);
751
Tony Wickham010d2552017-01-20 08:15:28 -0800752 NotificationListener.removeNotificationsChangedListener();
Sunny Goyalaf161fd2018-02-05 16:34:57 -0800753 getStateManager().moveToRestState();
Winson Chungedb0d6b2018-03-21 17:32:55 -0700754
Tracy Zhou62646712018-06-26 14:19:02 -0700755 UiFactory.onLauncherStateOrResumeChanged(this);
756
Winson Chungdea51352018-04-24 13:02:10 -0700757 // Workaround for b/78520668, explicitly trim memory once UI is hidden
Winson Chung473730f2018-04-26 11:54:22 -0700758 onTrimMemory(TRIM_MEMORY_UI_HIDDEN);
Michael Jurkacd496d72013-04-11 11:32:45 -0700759 }
760
761 @Override
762 protected void onStart() {
763 super.onStart();
Adam Cohen9211d422014-10-07 18:14:53 -0700764 if (mLauncherCallbacks != null) {
765 mLauncherCallbacks.onStart();
766 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800767 mAppWidgetHost.setListenIfResumed(true);
Sunny Goyalcac002d2018-01-04 09:51:45 -0800768 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
Winson Chung62a70be2018-02-23 15:10:05 -0800769 UiFactory.onStart(this);
Michael Jurkacd496d72013-04-11 11:32:45 -0700770 }
771
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700772 private void logOnDelayedResume() {
773 if (hasBeenResumed()) {
774 getUserEventDispatcher().logActionCommand(Action.Command.RESUME,
775 mStateManager.getState().containerType, -1);
776 getUserEventDispatcher().startSession();
777 }
778 }
779
Michael Jurkacd496d72013-04-11 11:32:45 -0700780 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800781 protected void onResume() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700782 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800783 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700784 TraceHelper.partitionSection("ON_RESUME", "superCall");
785
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700786 mHandler.removeCallbacks(mLogOnDelayedResume);
787 Utilities.postAsyncCallback(mHandler, mLogOnDelayedResume);
788
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700789 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700790 // Process any items that were added while Launcher was away.
791 InstallShortcutReceiver.disableAndFlushInstallQueue(
792 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700793
Sunny Goyala474a9b2017-05-04 16:47:11 -0700794 // Refresh shortcuts if the permission changed.
795 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700796
Tracy Zhoue5575f92018-04-19 11:12:53 -0700797 DiscoveryBounce.showForHomeIfNeeded(this);
Adam Cohen9211d422014-10-07 18:14:53 -0700798 if (mLauncherCallbacks != null) {
799 mLauncherCallbacks.onResume();
800 }
Tracy Zhoua706f002018-03-28 13:55:19 -0700801 UiFactory.onLauncherStateOrResumeChanged(this);
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800802
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700803 TraceHelper.endSection("ON_RESUME");
Adam Cohen06dff352012-06-01 17:17:08 -0700804 }
805
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800806 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700807 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700808 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700809 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700810
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700811 super.onPause();
Joe Onorato24b6fd82009-11-12 13:47:09 -0800812 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700813 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700814
Adam Cohen9211d422014-10-07 18:14:53 -0700815 if (mLauncherCallbacks != null) {
816 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700817 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700818 }
819
Winson Chungd7823942018-02-16 03:23:47 +0000820 @Override
Tracy Zhoua706f002018-03-28 13:55:19 -0700821 protected void onUserLeaveHint() {
822 super.onUserLeaveHint();
823 UiFactory.onLauncherStateOrResumeChanged(this);
824 }
825
826 @Override
Winson Chungd7823942018-02-16 03:23:47 +0000827 public void onWindowFocusChanged(boolean hasFocus) {
828 super.onWindowFocusChanged(hasFocus);
829 mStateManager.onWindowFocusChanged();
830 }
831
Adam Cohenc2d6e892014-10-16 09:49:24 -0700832 public interface LauncherOverlay {
833
834 /**
835 * Touch interaction leading to overscroll has begun
836 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700837 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700838
839 /**
840 * Touch interaction related to overscroll has ended
841 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700842 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700843
844 /**
845 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
846 * screen (or in the case of RTL, the rightmost screen).
847 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700848 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700849
850 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800851 * Called when the launcher is ready to use the overlay
852 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700853 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700854 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700855 }
856
857 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700858 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700859 }
860
861 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
862
Sunny Goyalc86df472016-02-25 09:19:38 -0800863 public void onScrollChanged(float progress) {
864 if (mWorkspace != null) {
865 mWorkspace.onOverlayScrollChanged(progress);
866 }
867 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700868 }
869
Sunny Goyal16764582017-11-06 16:00:34 -0800870 public boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -0700871 if (mLauncherCallbacks != null) {
872 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700873 } else {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800874 // On O and above we there is always some setting present settings (add icon to
875 // home screen or icon badging). On earlier APIs we will have the allow rotation
876 // setting, on devices with a locked orientation,
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700877 return Utilities.ATLEAST_OREO || !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -0700878 }
Jorim Jaggid017f882014-01-14 17:08:48 -0800879 }
880
Sunny Goyalf9403d92017-10-18 10:55:56 -0700881 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -0700882 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700883 }
884
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800885 /**
886 * Restores the previous state, if it exists.
887 *
888 * @param savedState The previous state.
889 */
890 private void restoreState(Bundle savedState) {
891 if (savedState == null) {
892 return;
893 }
894
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700895 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -0700896 LauncherState[] stateValues = LauncherState.values();
897 LauncherState state = stateValues[stateOrdinal];
Sunny Goyalbc683e92017-12-06 10:25:07 -0800898 if (!state.disableRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700899 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800900 }
901
Sunny Goyal2100c782016-08-22 16:00:03 -0700902 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
903 if (requestArgs != null) {
904 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800905 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700906
907 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700908
909 SparseArray<Parcelable> widgetsState =
910 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
911 if (widgetsState != null) {
912 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
913 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800914 }
915
916 /**
917 * Finds all the views we need and configure them properly.
918 */
919 private void setupViews() {
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800920 mDragLayer = findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700921 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -0700922 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700923 mWorkspace.initParentViews(mDragLayer);
Sunny Goyal16764582017-11-06 16:00:34 -0800924 mOverviewPanel = findViewById(R.id.overview_panel);
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800925 mHotseat = findViewById(R.id.hotseat);
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700926 mHotseatSearchBox = findViewById(R.id.search_container_hotseat);
Craig Mautner360310b2012-10-26 15:13:08 -0700927
Sunny Goyal784f9c32016-03-23 16:56:54 -0700928 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
929 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
930 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800931
Winson Chung4c98d922011-05-31 16:50:48 -0700932 // Setup the drag layer
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700933 mDragLayer.setup(mDragController, mWorkspace);
934 UiFactory.setOnTouchControllersChangedListener(this, mDragLayer::recreateControllers);
Winson Chung4c98d922011-05-31 16:50:48 -0700935
Hyunyoung Song645764e2016-06-06 14:19:02 -0700936 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -0700937 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
938 // default state, otherwise we will update to the wrong offsets in RTL
939 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700940 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700941 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700942
Tony Wickham34d2c912015-09-11 09:27:58 -0700943 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700944 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700945
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700946 // Setup Apps
947 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -0700948
Winson Chung3d503fb2011-07-13 17:25:49 -0700949 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -0700950 mDragController.setMoveTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700951 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -0400952
Sunny Goyal7185dd62018-03-14 17:51:49 -0700953 mAllAppsController.setupViews(mAppsView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800954 }
955
956 /**
957 * Creates a view representing a shortcut.
958 *
959 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800960 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800961 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700962 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800963 }
964
965 /**
966 * Creates a view representing a shortcut inflated from the specified resource.
967 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800968 * @param parent The group the shortcut belongs to.
969 * @param info The data structure describing the shortcut.
970 *
971 * @return A View inflated from layoutResId.
972 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700973 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -0800974 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
975 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800976 favorite.applyFromShortcutInfo(info);
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800977 favorite.setOnClickListener(ItemClickHandler.INSTANCE);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700978 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800979 return favorite;
980 }
981
982 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -0700983 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800984 *
985 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800986 */
Adam Cohendcd297f2013-06-18 13:13:40 -0700987 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700988 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -0700989 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
990 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700991 return;
992 }
Adam Cohen558baaf2011-08-15 15:22:57 -0700993
Jon Mirandac476d6e2017-05-04 16:30:01 -0700994 int[] cellXY = mTmpAddItemCellCoordinates;
995 CellLayout layout = getCellLayout(container, screenId);
996
Sunny Goyal782f0c92017-01-19 10:27:54 -0800997 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700998 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -0700999 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
1000 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001001 }
1002
1003 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001004 // Legacy shortcuts are only supported for primary profile.
1005 info = Process.myUserHandle().equals(args.user)
1006 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001007
Sunny Goyalb57645f2017-03-20 13:57:28 -07001008 if (info == null) {
1009 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1010 return;
1011 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001012 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001013 // The app is trying to add a shortcut without sufficient permissions
1014 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1015 return;
1016 }
1017 }
1018
Jon Mirandac476d6e2017-05-04 16:30:01 -07001019 if (container < 0) {
1020 // Adding a shortcut to the Workspace.
1021 final View view = createShortcut(info);
1022 boolean foundCellSpan = false;
1023 // First we check if we already know the exact location where we want to add this item.
1024 if (cellX >= 0 && cellY >= 0) {
1025 cellXY[0] = cellX;
1026 cellXY[1] = cellY;
1027 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001028
Jon Mirandac476d6e2017-05-04 16:30:01 -07001029 // If appropriate, either create a folder or add to an existing folder
1030 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001031 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001032 return;
1033 }
1034 DragObject dragObject = new DragObject();
1035 dragObject.dragInfo = info;
1036 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1037 true)) {
1038 return;
1039 }
1040 } else {
1041 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1042 }
1043
1044 if (!foundCellSpan) {
1045 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001046 return;
1047 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001048
1049 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1050 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001051 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001052 // Adding a shortcut to a Folder.
Sunny Goyale29897f52017-07-20 10:09:42 -07001053 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001054 if (folderIcon != null) {
1055 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1056 folderInfo.add(info, args.rank, false);
1057 } else {
Sunny Goyale29897f52017-07-20 10:09:42 -07001058 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001059 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001060 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001061 }
1062
Sunny Goyale29897f52017-07-20 10:09:42 -07001063 public FolderIcon findFolderIcon(final long folderIconId) {
Sunny Goyal83fd25e2018-07-09 16:47:01 -07001064 return (FolderIcon) mWorkspace.getHomescreenIconByItemId(folderIconId);
Sunny Goyale29897f52017-07-20 10:09:42 -07001065 }
1066
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001067 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001068 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001069 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001070 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001071 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001072 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001073 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1074
Adam Cohened66b2b2012-01-23 17:28:51 -08001075 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001076 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001077 }
Romain Guycbb89e42009-06-08 15:52:54 -07001078
Adam Cohen59400422014-03-05 18:07:04 -08001079 LauncherAppWidgetInfo launcherInfo;
1080 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001081 launcherInfo.spanX = itemInfo.spanX;
1082 launcherInfo.spanY = itemInfo.spanY;
1083 launcherInfo.minSpanX = itemInfo.minSpanX;
1084 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001085 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001086
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001087 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001088 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001089
Sunny Goyal2100c782016-08-22 16:00:03 -07001090 if (hostView == null) {
1091 // Perform actual inflation because we're live
1092 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001093 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001094 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301095 prepareAppWidget(hostView, launcherInfo);
1096 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001097 }
Romain Guycbb89e42009-06-08 15:52:54 -07001098
Sunny Goyald5462aa2016-11-22 16:52:39 +05301099 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001100 hostView.setTag(item);
1101 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001102 hostView.setFocusable(true);
1103 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001104 }
1105
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001106 private final BroadcastReceiver mScreenOffReceiver = new BroadcastReceiver() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001107 @Override
1108 public void onReceive(Context context, Intent intent) {
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001109 // Reset AllApps to its initial state only if we are not in the middle of
1110 // processing a multi-step drop
1111 if (mPendingRequestArgs == null) {
1112 mStateManager.goToState(NORMAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001113 }
1114 }
1115 };
1116
Tony Wickham010d2552017-01-20 08:15:28 -08001117 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
Sunny Goyal29947f02017-12-18 13:49:44 -08001118 mWorkspace.updateIconBadges(updatedBadges);
Sunny Goyal60180b02018-02-07 12:56:30 -08001119 mAppsView.getAppsStore().updateIconBadges(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001120
Sunny Goyal29947f02017-12-18 13:49:44 -08001121 PopupContainerWithArrow popup = PopupContainerWithArrow.getOpen(Launcher.this);
1122 if (popup != null) {
1123 popup.updateNotificationHeader(updatedBadges);
Tony Wickham010d2552017-01-20 08:15:28 -08001124 }
1125 }
1126
Adam Cohended9f8d2010-11-03 13:25:16 -07001127 @Override
1128 public void onAttachedToWindow() {
1129 super.onAttachedToWindow();
1130
Sunny Goyalc86df472016-02-25 09:19:38 -08001131 if (mLauncherCallbacks != null) {
1132 mLauncherCallbacks.onAttachedToWindow();
1133 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001134 }
1135
1136 @Override
1137 public void onDetachedFromWindow() {
1138 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001139
1140 if (mLauncherCallbacks != null) {
1141 mLauncherCallbacks.onDetachedFromWindow();
1142 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001143 }
1144
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001145 public AllAppsTransitionController getAllAppsController() {
1146 return mAllAppsController;
1147 }
1148
Sunny Goyal9d69c8d2018-03-19 13:41:31 -07001149 @Override
Winson Chung1a341002018-01-17 10:00:23 -08001150 public LauncherRootView getRootView() {
1151 return (LauncherRootView) mLauncherView;
1152 }
1153
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001154 @Override
Winson Chunga6945242014-01-08 14:04:34 -08001155 public DragLayer getDragLayer() {
1156 return mDragLayer;
1157 }
1158
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001159 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001160 return mAppsView;
1161 }
1162
Winson Chunga6945242014-01-08 14:04:34 -08001163 public Workspace getWorkspace() {
1164 return mWorkspace;
1165 }
1166
1167 public Hotseat getHotseat() {
1168 return mHotseat;
1169 }
1170
Sunny Goyal81b4c7b2018-03-26 12:10:31 -07001171 public View getHotseatSearchBox() {
1172 return mHotseatSearchBox;
1173 }
1174
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001175 public <T extends View> T getOverviewPanel() {
Winson Chung8ae41982017-11-10 11:42:13 -08001176 return (T) mOverviewPanel;
Winson Chunga6945242014-01-08 14:04:34 -08001177 }
1178
Sunny Goyal47328fd2016-05-25 18:56:41 -07001179 public DropTargetBar getDropTargetBar() {
1180 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001181 }
1182
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001183 public LauncherAppWidgetHost getAppWidgetHost() {
1184 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001185 }
Romain Guycbb89e42009-06-08 15:52:54 -07001186
Winson Chunga9abd0e2010-10-27 17:18:37 -07001187 public LauncherModel getModel() {
1188 return mModel;
1189 }
1190
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001191 public ModelWriter getModelWriter() {
1192 return mModelWriter;
1193 }
1194
Adam Cohen79d90c52016-04-22 13:29:20 -07001195 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001196 return mSharedPrefs;
1197 }
1198
Sunny Goyalf8d56fc2018-01-31 15:18:11 -08001199 public int getOrientation() { return mOldConfig.orientation; }
Jon Miranda6bed3502017-09-19 14:43:17 -07001200
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001201 @Override
1202 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001203 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001204 super.onNewIntent(intent);
1205
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001206 boolean alreadyOnHome = hasWindowFocus() && ((intent.getFlags() &
Winson15f8b172015-08-19 11:02:39 -07001207 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1208 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001209
1210 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001211 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001212 && AbstractFloatingView.getTopOpenView(this) == null;
Winson15f8b172015-08-19 11:02:39 -07001213 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001214 boolean internalStateHandled = InternalStateHandler
Sunny Goyal8b264562018-02-03 23:00:13 -08001215 .handleNewIntent(this, intent, isStarted());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001216
Winson15f8b172015-08-19 11:02:39 -07001217 if (isActionMain) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001218 if (!internalStateHandled) {
1219 // Note: There should be at most one log per method call. This is enforced
1220 // implicitly by using if-else statements.
1221 UserEventDispatcher ued = getUserEventDispatcher();
1222 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
1223 if (topOpenView != null) {
1224 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
1225 } else if (alreadyOnHome) {
1226 Target target = newContainerTarget(mStateManager.getState().containerType);
1227 target.pageIndex = mWorkspace.getCurrentPage();
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001228 ued.logActionCommand(Action.Command.HOME_INTENT, target,
1229 newContainerTarget(ContainerType.WORKSPACE));
Sunny Goyald3864fa2017-11-14 15:06:36 -08001230 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001231
Sunny Goyald3864fa2017-11-14 15:06:36 -08001232 // In all these cases, only animate if we're already on home
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001233 AbstractFloatingView.closeAllOpenViews(this, isStarted());
Jon Mirandafeba90f2016-10-06 10:53:29 -07001234
Sunny Goyala2467272018-03-16 14:53:05 -07001235 if (!isInState(NORMAL)) {
1236 // Only change state, if not already the same. This prevents cancelling any
1237 // animations running as part of resume
1238 mStateManager.goToState(NORMAL);
1239 }
Sunny Goyald3864fa2017-11-14 15:06:36 -08001240
1241 // Reset the apps view
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001242 if (!alreadyOnHome) {
Sunny Goyal7185dd62018-03-14 17:51:49 -07001243 mAppsView.reset(isStarted() /* animate */);
Sunny Goyald3864fa2017-11-14 15:06:36 -08001244 }
1245
Sunny Goyalaad33592018-08-07 17:20:35 -07001246 if (shouldMoveToDefaultScreen && !mWorkspace.isHandlingTouch()) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001247 mWorkspace.post(mWorkspace::moveToDefaultScreen);
1248 }
1249 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001250
1251 final View v = getWindow().peekDecorView();
1252 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001253 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001254 }
1255
Adam Cohen9211d422014-10-07 18:14:53 -07001256 if (mLauncherCallbacks != null) {
Sunny Goyal85462132018-04-24 11:39:37 -07001257 mLauncherCallbacks.onHomeIntent(internalStateHandled);
Adam Cohen9211d422014-10-07 18:14:53 -07001258 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001259 }
Winson15f8b172015-08-19 11:02:39 -07001260
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001261 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001262 }
1263
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001264 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001265 public void onRestoreInstanceState(Bundle state) {
1266 super.onRestoreInstanceState(state);
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001267 mWorkspace.restoreInstanceStateForChild(mSynchronouslyBoundPage);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001268 }
1269
1270 @Override
1271 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001272 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001273 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001274
Adam Cohen21cd0022013-10-09 18:57:02 -07001275 }
Sunny Goyalea609262017-10-25 15:47:38 -07001276 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001277
1278
1279 AbstractFloatingView widgets = AbstractFloatingView
1280 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1281 if (widgets != null) {
1282 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1283 widgets.saveHierarchyState(widgetsState);
1284 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1285 } else {
1286 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1287 }
1288
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001289 // We close any open folders and shortcut containers since they will not be re-opened,
1290 // and we need to make sure this state is reflected.
1291 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001292
Sunny Goyal2100c782016-08-22 16:00:03 -07001293 if (mPendingRequestArgs != null) {
1294 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1295 }
1296 if (mPendingActivityResult != null) {
1297 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001298 }
1299
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001300 super.onSaveInstanceState(outState);
1301
Adam Cohen9211d422014-10-07 18:14:53 -07001302 if (mLauncherCallbacks != null) {
1303 mLauncherCallbacks.onSaveInstanceState(outState);
1304 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001305 }
1306
1307 @Override
1308 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001309 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001310
Sunny Goyal5d1873a2018-05-08 11:10:44 -07001311 unregisterReceiver(mScreenOffReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001312 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001313
Tony Wickhama0068302018-04-11 16:16:11 -07001314 UiFactory.setOnTouchControllersChangedListener(this, null);
1315
Winson Chungcd2b0142011-06-08 16:02:26 -07001316 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001317 // It's possible to receive onDestroy after a new Launcher activity has
1318 // been created. In this case, don't interfere with the new Launcher.
1319 if (mModel.isCurrentCallbacks(this)) {
1320 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001321 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001322 }
Sunny Goyal623eddd2018-03-02 12:24:41 -08001323 mRotationHelper.destroy();
Sunny Goyal745bad92016-05-02 10:54:12 -07001324
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001325 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001326 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001327 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001328 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001329 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001330
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001331 TextKeyListener.getInstance().release();
Tony2917a8b2017-07-31 23:29:42 -07001332 clearPendingBinds();
1333
Adam Cohen9211d422014-10-07 18:14:53 -07001334 if (mLauncherCallbacks != null) {
1335 mLauncherCallbacks.onDestroy();
1336 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001337 }
1338
Sunny Goyalae502842016-06-17 08:43:56 -07001339 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1340 return mAccessibilityDelegate;
1341 }
1342
Adam Cohena9cf38f2011-05-02 15:36:58 -07001343 public DragController getDragController() {
1344 return mDragController;
1345 }
1346
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001347 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001348 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001349 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001350 }
1351
1352 @Override
1353 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1354 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001355 try {
1356 super.startIntentSenderForResult(intent, requestCode,
1357 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1358 } catch (IntentSender.SendIntentException e) {
1359 throw new ActivityNotFoundException();
1360 }
1361 }
1362
Winson Chung70d72102011-08-12 11:24:35 -07001363 /**
1364 * Indicates that we want global search for this activity by setting the globalSearch
1365 * argument for {@link #startSearch} to true.
1366 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001367 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001368 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001369 Bundle appSearchData, boolean globalSearch) {
1370 if (appSearchData == null) {
1371 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001372 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001373 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001374
Sunny Goyal6f28e712016-09-13 14:19:29 -07001375 if (mLauncherCallbacks == null ||
1376 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1377 // Starting search from the callbacks failed. Start the default global search.
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001378 super.startSearch(initialQuery, selectInitialQuery, appSearchData, true);
Ian Parkinson047036e2014-09-01 15:40:53 +01001379 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001380
1381 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001382 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001383 }
1384
Joe Onorato9c1289c2009-08-17 11:03:03 -04001385 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001386 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001387 }
1388
Adam Cohen517a7f52014-03-01 12:12:59 -08001389 public boolean isWorkspaceLoading() {
1390 return mWorkspaceLoading;
1391 }
1392
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001393 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001394 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001395 }
1396
Sunny Goyal04a324a2017-01-20 21:08:59 -08001397 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001398 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001399 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001400
Sunny Goyal2100c782016-08-22 16:00:03 -07001401 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001402 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001403 if (LOGD) {
1404 Log.d(TAG, "Adding widget from drop");
1405 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001406 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001407 }
1408
Sunny Goyal2100c782016-08-22 16:00:03 -07001409 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001410 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1411 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1412 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001413
Adam Cohenad4e15c2013-10-17 16:21:35 -07001414 Runnable onComplete = new Runnable() {
1415 @Override
1416 public void run() {
1417 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001418 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001419 }
1420 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001421 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001422 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001423 }
1424 }
Romain Guycbb89e42009-06-08 15:52:54 -07001425
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001426 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1427 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001428 info.container = container;
1429 info.screenId = screenId;
1430 if (cell != null) {
1431 info.cellX = cell[0];
1432 info.cellY = cell[1];
1433 }
1434 info.spanX = spanX;
1435 info.spanY = spanY;
1436
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001437 switch (info.itemType) {
1438 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1439 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001440 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001441 break;
1442 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001443 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001444 break;
1445 default:
1446 throw new IllegalStateException("Unknown item type: " + info.itemType);
1447 }
1448 }
1449
Adam Cohenfbba09b2011-07-18 21:43:05 -07001450 /**
1451 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001452 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001453 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001454 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1455 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001456 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1457 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1458 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001459 }
1460
Adam Cohenfbba09b2011-07-18 21:43:05 -07001461 /**
1462 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001463 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001464 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001465 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001466 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001467 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001468 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001469 // In the case where we've prebound the widget, we remove it from the DragLayer
1470 if (LOGD) {
1471 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1472 }
1473 getDragLayer().removeView(hostView);
1474
Adam Cohened66b2b2012-01-23 17:28:51 -08001475 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001476 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001477
1478 // Clear the boundWidget so that it doesn't get destroyed.
1479 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001480 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001481 // In this case, we either need to start an activity to get permission to bind
1482 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001483 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1484 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1485 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1486 this, info.componentName);
1487 } else {
1488 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1489 }
Adam Cohendd70d662012-10-04 16:53:44 -07001490 Bundle options = info.bindOptions;
1491
Sunny Goyalffe83f12014-08-14 17:39:34 -07001492 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1493 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001494 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001495 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001496 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001497 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001498 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001499 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001500 }
1501
Adam Cohendcd297f2013-06-18 13:13:40 -07001502 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001503 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001504 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001505 folderInfo.title = getText(R.string.folder_name);
1506
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001507 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001508 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001509
1510 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001511 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301512 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001513 // Force measure the new folder icon
1514 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1515 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001516 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001517 }
Romain Guycbb89e42009-06-08 15:52:54 -07001518
Winsonc0b52fe2015-09-09 16:38:15 -07001519 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001520 * Unbinds the view for the specified item, and removes the item and all its children.
1521 *
1522 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001523 * @param itemInfo the {@link ItemInfo} for this view.
1524 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001525 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001526 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001527 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001528 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001529 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1530 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001531 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001532 } else {
1533 mWorkspace.removeWorkspaceItem(v);
1534 }
Winsonc0b52fe2015-09-09 16:38:15 -07001535 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001536 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001537 }
1538 } else if (itemInfo instanceof FolderInfo) {
1539 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001540 if (v instanceof FolderIcon) {
1541 ((FolderIcon) v).removeListeners();
1542 }
Winsonc0b52fe2015-09-09 16:38:15 -07001543 mWorkspace.removeWorkspaceItem(v);
1544 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001545 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001546 }
1547 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1548 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001549 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001550 if (deleteFromDb) {
Tony Wickham6a71a5b2018-08-21 11:40:23 -07001551 getModelWriter().deleteWidgetInfo(widgetInfo, getAppWidgetHost());
Winsonc0b52fe2015-09-09 16:38:15 -07001552 }
1553 } else {
1554 return false;
1555 }
1556 return true;
1557 }
1558
Tony Wickham6a71a5b2018-08-21 11:40:23 -07001559
Winsonc0b52fe2015-09-09 16:38:15 -07001560
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001561 @Override
1562 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001563 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001564 }
1565
Joe Onorato88ec0992009-11-19 13:16:06 -08001566 @Override
1567 public void onBackPressed() {
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08001568 if (finishAutoCancelActionMode()) {
1569 return;
1570 }
Adam Cohen9211d422014-10-07 18:14:53 -07001571 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1572 return;
1573 }
1574
Sunny Goyal45478022015-06-08 16:52:41 -07001575 if (mDragController.isDragging()) {
1576 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001577 return;
1578 }
1579
Jon Mirandafeba90f2016-10-06 10:53:29 -07001580 // Note: There should be at most one log per method call. This is enforced implicitly
1581 // by using if-else statements.
1582 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001583 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
Tony Wickham52c1b662018-05-21 13:13:58 -07001584 if (topView != null && topView.onBackPressed()) {
1585 // Handled by the floating view.
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001586 } else {
Sunny Goyale39690b2018-08-02 13:35:07 -07001587 mStateManager.getState().onBackPressed(this);
Michael Jurkaaf442092010-06-10 17:01:57 -07001588 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001589 }
1590
Sunny Goyalfe770c92016-09-27 14:38:58 -07001591 @TargetApi(Build.VERSION_CODES.M)
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001592 @Override
Jon Miranda73c27ec2018-05-16 18:06:23 -07001593 public ActivityOptions getActivityLaunchOptions(View v) {
1594 return mAppTransitionManager.getActivityLaunchOptions(this, v);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001595 }
1596
Tony Wickham3a6746a2018-03-29 09:39:56 -07001597 public LauncherAppTransitionManager getAppTransitionManager() {
1598 return mAppTransitionManager;
1599 }
1600
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001601 @TargetApi(Build.VERSION_CODES.M)
1602 @Override
1603 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
1604 // Due to legacy reasons, direct call shortcuts require Launchers to have the
1605 // corresponding permission. Show the appropriate permission prompt if that
1606 // is the case.
1607 if (intent.getComponent() == null
1608 && Intent.ACTION_CALL.equals(intent.getAction())
1609 && checkSelfPermission(android.Manifest.permission.CALL_PHONE) !=
1610 PackageManager.PERMISSION_GRANTED) {
1611
1612 setWaitingForResult(PendingRequestArgs
1613 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
1614 requestPermissions(new String[]{android.Manifest.permission.CALL_PHONE},
1615 REQUEST_PERMISSION_CALL_PHONE);
1616 return true;
1617 } else {
1618 return false;
1619 }
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001620 }
1621
Hyunyoung Song46d07f72018-05-22 15:41:25 -07001622 @Override
1623 public void modifyUserEvent(LauncherLogProto.LauncherEvent event) {
1624 if (event.srcTarget != null && event.srcTarget.length > 0 &&
1625 event.srcTarget[1].containerType == ContainerType.PREDICTION) {
1626 Target[] targets = new Target[3];
1627 targets[0] = event.srcTarget[0];
1628 targets[1] = event.srcTarget[1];
1629 targets[2] = newTarget(Target.Type.CONTAINER);
1630 event.srcTarget = targets;
1631 LauncherState state = mStateManager.getState();
1632 if (state == LauncherState.ALL_APPS) {
1633 event.srcTarget[2].containerType = ContainerType.ALLAPPS;
1634 } else if (state == LauncherState.OVERVIEW) {
1635 event.srcTarget[2].containerType = ContainerType.TASKSWITCHER;
1636 }
1637 }
1638 }
1639
Sunny Goyala7ce1662016-05-31 15:01:35 -07001640 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Hyunyoung Song1241e612018-05-15 21:46:51 -07001641 boolean success = super.startActivitySafely(v, intent, item);
1642 if (success && v instanceof BubbleTextView) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001643 // This is set to the view that launched the activity that navigated the user away
1644 // from launcher. Since there is no callback for when the activity has finished
1645 // launching, enable the press state and keep this reference to reset the press
1646 // state when we return to launcher.
1647 BubbleTextView btv = (BubbleTextView) v;
1648 btv.setStayPressed(true);
1649 setOnResumeCallback(btv);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001650 }
Hyunyoung Song1241e612018-05-15 21:46:51 -07001651 return success;
Michael Jurka86a720e2012-05-09 11:23:23 -07001652 }
1653
Winson Chung3d503fb2011-07-13 17:25:49 -07001654 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001655 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07001656 return mHotseat != null && layout != null &&
1657 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
1658 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001659
Winson Chung3d503fb2011-07-13 17:25:49 -07001660 /**
1661 * Returns the CellLayout of the specified container at the specified screen.
1662 */
Sunny Goyal92820592015-03-02 11:31:35 -08001663 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001664 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1665 if (mHotseat != null) {
1666 return mHotseat.getLayout();
1667 } else {
1668 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08001669 }
Winson Chung3d503fb2011-07-13 17:25:49 -07001670 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08001671 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08001672 }
1673 }
1674
Michael Jurkae326f182011-11-21 14:05:46 -08001675 @Override
1676 public void onTrimMemory(int level) {
1677 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07001678 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
1679 // The widget preview db can result in holding onto over
1680 // 3MB of memory for caching which isn't necessary.
1681 SQLiteDatabase.releaseMemory();
1682
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08001683 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07001684 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08001685 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01001686 if (mLauncherCallbacks != null) {
1687 mLauncherCallbacks.onTrimMemory(level);
1688 }
Winson Chung62a70be2018-02-23 15:10:05 -08001689 UiFactory.onTrimMemory(this, level);
Michael Jurkae326f182011-11-21 14:05:46 -08001690 }
1691
Michael Jurkad7c28052012-04-27 15:43:36 -07001692 @Override
1693 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07001694 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07001695 final List<CharSequence> text = event.getText();
1696 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07001697 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001698 // TODO: When can workspace be null?
1699 text.add(mWorkspace == null
1700 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07001701 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07001702 return result;
1703 }
1704
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001705 public void setOnResumeCallback(OnResumeCallback callback) {
1706 if (mOnResumeCallback != null) {
1707 mOnResumeCallback.onLauncherResume();
1708 }
1709 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07001710 }
1711
Michael Jurka7607c2f2013-04-03 14:33:19 -07001712 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001713 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001714 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07001715 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001716 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07001717 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001718 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07001719 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07001720 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07001721 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001722 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001723
Joe Onorato9c1289c2009-08-17 11:03:03 -04001724 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07001725 * Clear any pending bind callbacks. This is called when is loader is planning to
1726 * perform a full rebind from scratch.
1727 */
1728 @Override
1729 public void clearPendingBinds() {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001730 if (mPendingExecutor != null) {
1731 mPendingExecutor.markCompleted();
1732 mPendingExecutor = null;
1733 }
1734 }
1735
1736 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001737 * Refreshes the shortcuts shown on the workspace.
1738 *
1739 * Implementation of the method from LauncherModel.Callbacks.
1740 */
1741 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001742 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001743 // Floating panels (except the full widget sheet) are associated with individual icons. If
1744 // we are starting a fresh bind, close all such panels as all the icons are about
1745 // to go away.
1746 AbstractFloatingView.closeOpenViews(this, true,
Sunny Goyal7ede6112017-12-05 15:11:21 -08001747 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_REBIND_SAFE);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08001748
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001749 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08001750
Winson Chungd64d1762013-08-20 14:37:16 -07001751 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07001752 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07001753 mWorkspace.removeAllWorkspaceScreens();
Sunny Goyalc11fac32018-02-27 19:20:15 -08001754 mAppWidgetHost.clearViews();
Winson Chungd64d1762013-08-20 14:37:16 -07001755
Winson Chung3d503fb2011-07-13 17:25:49 -07001756 if (mHotseat != null) {
Sunny Goyal11e96492018-03-23 17:06:00 -07001757 mHotseat.resetLayout(mDeviceProfile.isVerticalBarLayout());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001758 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001759 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001760 }
1761
Adam Cohendcd297f2013-06-18 13:13:40 -07001762 @Override
1763 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001764 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001765 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
1766 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001767 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
1768 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001769 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001770 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
1771 // If there are no screens, we need to have an empty screen
1772 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07001773 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001774 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07001775
Winsonc7d2e832016-07-28 12:24:55 -07001776 // After we have added all the screens, if the wallpaper was locked to the default state,
1777 // then notify to indicate that it can be released and a proper wallpaper offset can be
1778 // computed before the next layout
1779 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07001780 }
1781
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001782 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001783 int count = orderedScreenIds.size();
1784 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001785 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001786 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001787 // No need to bind the first screen, as its always bound.
1788 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
1789 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001790 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001791 }
1792
Sunny Goyalb23980c2017-08-17 07:45:25 -07001793 @Override
Tony Wickham6a71a5b2018-08-21 11:40:23 -07001794 public void preAddApps() {
1795 // If there's an undo snackbar, force it to complete to ensure empty screens are removed
1796 // before trying to add new items.
1797 mModelWriter.commitDelete();
1798 AbstractFloatingView snackbar = AbstractFloatingView.getOpenView(this, TYPE_SNACKBAR);
1799 if (snackbar != null) {
1800 snackbar.post(() -> snackbar.close(true));
1801 }
1802 }
1803
1804 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08001805 public void bindAppsAdded(ArrayList<Long> newScreens, ArrayList<ItemInfo> addNotAnimated,
1806 ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07001807 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08001808 if (newScreens != null) {
1809 bindAddScreens(newScreens);
1810 }
Winson Chungd64d1762013-08-20 14:37:16 -07001811
1812 // We add the items without animation on non-visible pages, and with
1813 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08001814 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001815 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001816 }
Adam Cohen76a47a12014-02-05 11:47:43 -08001817 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001818 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07001819 }
Winson Chungc58497e2013-09-03 17:48:37 -07001820
Winson Chung87412982013-10-03 18:34:14 -07001821 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07001822 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001823 }
1824
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001825 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001826 * Bind the items start-end from the list.
1827 *
1828 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001829 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07001830 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07001831 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001832 // Get the list of added items and intersect them with the set of items here
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001833 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07001834 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001835 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001836 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07001837 int end = items.size();
1838 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001839 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07001840
1841 // Short circuit if we are loading dock items for a configuration which has no dock
1842 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
1843 mHotseat == null) {
1844 continue;
1845 }
1846
Sunny Goyal639e9062015-08-19 19:17:06 -07001847 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001848 switch (item.itemType) {
1849 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1850 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08001851 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001852 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07001853 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001854 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001855 }
1856 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07001857 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001858 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001859 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001860 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001861 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07001862 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1863 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07001864 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07001865 if (view == null) {
1866 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001867 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08001868 break;
1869 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001870 default:
1871 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001872 }
Sunny Goyal639e9062015-08-19 19:17:06 -07001873
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001874 /*
1875 * Remove colliding items.
1876 */
1877 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1878 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
1879 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
1880 View v = cl.getChildAt(item.cellX, item.cellY);
1881 Object tag = v.getTag();
1882 String desc = "Collision while binding workspace item: " + item
1883 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08001884 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001885 throw (new RuntimeException(desc));
1886 } else {
1887 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001888 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001889 continue;
1890 }
1891 }
1892 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05301893 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07001894 if (animateIcons) {
1895 // Animate all the applications up now
1896 view.setAlpha(0f);
1897 view.setScaleX(0f);
1898 view.setScaleY(0f);
1899 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08001900 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07001901 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001902 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07001903
Sunny Goyalf0b6db72018-08-13 16:10:14 -07001904 // Animate to the correct page
1905 if (animateIcons && newItemsScreenId > -1) {
1906 AnimatorSet anim = new AnimatorSet();
1907 anim.playTogether(bounceAnims);
Jon Miranda64eb8ea2018-03-29 11:43:58 -07001908
Sunny Goyalf0b6db72018-08-13 16:10:14 -07001909 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
1910 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
1911 final Runnable startBounceAnimRunnable = anim::start;
1912
1913 if (newItemsScreenId != currentScreenId) {
1914 // We post the animation slightly delayed to prevent slowdowns
1915 // when we are loading right after we return to launcher.
1916 mWorkspace.postDelayed(new Runnable() {
1917 public void run() {
1918 if (mWorkspace != null) {
1919 AbstractFloatingView.closeAllOpenViews(Launcher.this, false);
1920
1921 mWorkspace.snapToPage(newScreenIndex);
1922 mWorkspace.postDelayed(startBounceAnimRunnable,
1923 NEW_APPS_ANIMATION_DELAY);
Winson Chung94d67682013-09-25 16:29:40 -07001924 }
Sunny Goyalf0b6db72018-08-13 16:10:14 -07001925 }
1926 }, NEW_APPS_PAGE_MOVE_DELAY);
1927 } else {
1928 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07001929 }
Winson Chung64359a52013-07-08 17:17:08 -07001930 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001931 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001932 }
1933
Joe Onorato9c1289c2009-08-17 11:03:03 -04001934 /**
1935 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001936 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07001937 public void bindAppWidget(LauncherAppWidgetInfo item) {
1938 View view = inflateAppWidget(item);
1939 if (view != null) {
1940 mWorkspace.addInScreen(view, item);
1941 mWorkspace.requestLayout();
1942 }
1943 }
1944
1945 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001946 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05301947 PendingAppWidgetHostView view =
1948 new PendingAppWidgetHostView(this, item, mIconCache, true);
1949 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07001950 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001951 }
1952
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001953 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001954
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001955 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08001956
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001957 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
1958 // If the provider is not ready, bind as a pending widget.
1959 appWidgetInfo = null;
1960 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
1961 // The widget id is not valid. Try to find the widget based on the provider info.
1962 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
1963 } else {
1964 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
1965 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07001966
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001967 // If the provider is ready, but the width is not yet restored, try to restore it.
1968 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
1969 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07001970 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001971 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
1972 + " belongs to component " + item.providerName
1973 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001974 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07001975 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07001976 }
Sunny Goyalff572272014-07-23 13:58:07 -07001977
Sunny Goyal84b4adc2015-08-12 15:12:16 -07001978 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001979 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07001980 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
1981 // Id has not been allocated yet. Allocate a new id.
1982 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
1983 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07001984
Sunny Goyald478c832016-04-01 12:04:16 -07001985 // Also try to bind the widget. If the bind fails, the user will be shown
1986 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08001987 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07001988 pendingInfo.spanX = item.spanX;
1989 pendingInfo.spanY = item.spanY;
1990 pendingInfo.minSpanX = item.minSpanX;
1991 pendingInfo.minSpanY = item.minSpanY;
1992 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07001993
1994 boolean isDirectConfig =
1995 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
1996 if (isDirectConfig && item.bindOptions != null) {
1997 Bundle newOptions = item.bindOptions.getExtras();
1998 if (options != null) {
1999 newOptions.putAll(options);
2000 }
2001 options = newOptions;
2002 }
Sunny Goyald478c832016-04-01 12:04:16 -07002003 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2004 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002005
Sunny Goyal86df1382016-08-10 15:03:22 -07002006 // We tried to bind once. If we were not able to bind, we would need to
2007 // go through the permission dialog, which means we cannot skip the config
2008 // activity.
2009 item.bindOptions = null;
2010 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2011
Sunny Goyald478c832016-04-01 12:04:16 -07002012 // Bind succeeded
2013 if (success) {
2014 // If the widget has a configure activity, it is still needs to set it up,
2015 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002016 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002017 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2018 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002019 }
Sunny Goyald478c832016-04-01 12:04:16 -07002020
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002021 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002022 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002023 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002024 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002025 // The widget was marked as UI not ready, but there is no configure activity to
2026 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002027 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002028 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002029 }
Sunny Goyalff572272014-07-23 13:58:07 -07002030 }
2031
Sunny Goyald5462aa2016-11-22 16:52:39 +05302032 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002033 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002034 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002035 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002036 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Tony Wickham6a71a5b2018-08-21 11:40:23 -07002037 getModelWriter().deleteWidgetInfo(item, getAppWidgetHost());
Sunny Goyalb23980c2017-08-17 07:45:25 -07002038 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002039 }
2040
Sunny Goyal233ee962015-08-03 13:05:01 -07002041 item.minSpanX = appWidgetInfo.minSpanX;
2042 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302043 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002044 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302045 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002046 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302047 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002048
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002049 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002050 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002051 }
2052
Sunny Goyalff572272014-07-23 13:58:07 -07002053 /**
2054 * Restores a pending widget.
2055 *
2056 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002057 */
Sunny Goyald478c832016-04-01 12:04:16 -07002058 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002059 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2060 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2061 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002062 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002063 }
2064
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002065 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002066 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002067 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2068 info.pendingItemInfo = null;
2069 }
Sunny Goyalff572272014-07-23 13:58:07 -07002070
Sunny Goyalba47faa2017-10-04 16:50:57 -07002071 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002072 view.reInflate();
Sunny Goyalba47faa2017-10-04 16:50:57 -07002073 }
2074
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002075 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002076 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002077 }
2078
Adam Cohen1462de32012-07-24 22:34:36 -07002079 public void onPageBoundSynchronously(int page) {
Sunny Goyalf4aceab2017-12-06 16:16:31 -08002080 mSynchronouslyBoundPage = page;
Adam Cohen1462de32012-07-24 22:34:36 -07002081 }
2082
Sunny Goyal527c7d32015-08-28 15:19:36 -07002083 @Override
2084 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2085 if (mPendingExecutor != null) {
2086 mPendingExecutor.markCompleted();
2087 }
2088 mPendingExecutor = executor;
Sunny Goyal60180b02018-02-07 12:56:30 -08002089 if (!isInState(ALL_APPS)) {
2090 mAppsView.getAppsStore().setDeferUpdates(true);
2091 mPendingExecutor.execute(() -> mAppsView.getAppsStore().setDeferUpdates(false));
2092 }
2093
Sunny Goyal527c7d32015-08-28 15:19:36 -07002094 executor.attachTo(this);
2095 }
2096
2097 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2098 if (mPendingExecutor == executor) {
2099 mPendingExecutor = null;
2100 }
2101 }
2102
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002103 @Override
2104 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
Sunny Goyal6001ea22018-05-10 16:31:00 -07002105 AlphaProperty property = mDragLayer.getAlphaProperty(ALPHA_INDEX_LAUNCHER_LOAD);
2106 if (property.getValue() < 1) {
2107 ObjectAnimator anim = ObjectAnimator.ofFloat(property, MultiValueAlpha.VALUE, 1);
2108 if (executor != null) {
2109 anim.addListener(new AnimatorListenerAdapter() {
2110 @Override
2111 public void onAnimationEnd(Animator animation) {
2112 executor.onLoadAnimationCompleted();
2113 }
2114 });
2115 }
2116 anim.start();
Sunny Goyal29947f02017-12-18 13:49:44 -08002117 } else if (executor != null) {
2118 executor.onLoadAnimationCompleted();
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002119 }
2120 }
2121
Joe Onorato9c1289c2009-08-17 11:03:03 -04002122 /**
2123 * Callback saying that there aren't any more items to bind.
2124 *
2125 * Implementation of the method from LauncherModel.Callbacks.
2126 */
Tony Wickham6a71a5b2018-08-21 11:40:23 -07002127 public void finishBindingItems(int currentScreen) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002128 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002129 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002130
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002131 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002132
Sunny Goyal2100c782016-08-22 16:00:03 -07002133 if (mPendingActivityResult != null) {
2134 handleActivityResult(mPendingActivityResult.requestCode,
2135 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2136 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002137 }
2138
Sunny Goyala474a9b2017-05-04 16:47:11 -07002139 InstallShortcutReceiver.disableAndFlushInstallQueue(
2140 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002141
Tony Wickham6a71a5b2018-08-21 11:40:23 -07002142 mWorkspace.setCurrentPage(currentScreen);
2143
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002144 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002145 }
2146
Winson Chunga2413752012-04-03 14:22:34 -07002147 private boolean canRunNewAppsAnimation() {
2148 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002149 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002150 }
2151
Winson Chungc9168342013-06-26 14:54:55 -07002152 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyalf0b6db72018-08-13 16:10:14 -07002153 ValueAnimator bounceAnim = new PropertyListBuilder().alpha(1).scale(1).build(v)
2154 .setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
Winson Chungc9168342013-06-26 14:54:55 -07002155 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002156 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002157 return bounceAnim;
2158 }
2159
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002160 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002161 * Add the icons for all apps.
2162 *
2163 * Implementation of the method from LauncherModel.Callbacks.
2164 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002165 public void bindAllApplications(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002166 mAppsView.getAppsStore().setApps(apps);
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002167
Adam Cohen9211d422014-10-07 18:14:53 -07002168 if (mLauncherCallbacks != null) {
2169 mLauncherCallbacks.bindAllApplications(apps);
2170 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002171 }
2172
2173 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002174 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
2175 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2176 */
2177 @Override
2178 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002179 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002180 }
2181
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002182 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002183 * A package was updated.
2184 *
2185 * Implementation of the method from LauncherModel.Callbacks.
2186 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002187 @Override
2188 public void bindAppsAddedOrUpdated(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002189 mAppsView.getAppsStore().addOrUpdateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002190 }
2191
Sunny Goyal4390ace2014-10-13 11:33:11 -07002192 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002193 public void bindPromiseAppProgressUpdated(PromiseAppInfo app) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002194 mAppsView.getAppsStore().updatePromiseAppProgress(app);
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002195 }
2196
2197 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002198 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002199 mWorkspace.widgetsRestored(widgets);
2200 }
2201
Joe Onorato9c1289c2009-08-17 11:03:03 -04002202 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002203 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002204 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002205 *
2206 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002207 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002208 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002209 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002210 if (!updated.isEmpty()) {
2211 mWorkspace.updateShortcuts(updated);
2212 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002213 }
2214
2215 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002216 * Update the state of a package, typically related to install state.
2217 *
2218 * Implementation of the method from LauncherModel.Callbacks.
2219 */
Sunny Goyale755d462014-07-22 13:48:29 -07002220 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002221 public void bindRestoreItemsChange(HashSet<ItemInfo> updates) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07002222 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07002223 }
2224
2225 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002226 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07002227 * in addition to specific applications to remove, the reason being that
2228 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002229 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07002230 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002231 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07002232 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002233 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002234 mWorkspace.removeItemsByMatcher(matcher);
2235 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002236 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002237
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002238 @Override
2239 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002240 mAppsView.getAppsStore().removeApps(appInfos);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002241 }
Joe Onoratobe386092009-11-17 17:32:16 -08002242
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002243 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002244 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
2245 mPopupDataProvider.setAllWidgets(allWidgets);
Tony Wickham26b17462017-03-20 17:12:24 -07002246 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
2247 if (topView != null) {
2248 topView.onWidgetsBound();
2249 }
2250 }
2251
Tony Wickham86222d22017-03-29 15:30:43 -07002252 /**
2253 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
2254 * refreshes the widgets and shortcuts associated with the given package/user
2255 */
2256 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07002257 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002258 }
2259
Winson Chung80baf5a2010-08-09 16:03:15 -07002260 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002261 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08002262 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002263 @Override
2264 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
2265 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002266
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002267 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
2268 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07002269 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002270 writer.println(prefix + " Homescreen " + i);
2271
2272 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
2273 for (int j = 0; j < layout.getChildCount(); j++) {
2274 Object tag = layout.getChildAt(j).getTag();
2275 if (tag != null) {
2276 writer.println(prefix + " " + tag.toString());
2277 }
2278 }
2279 }
2280
2281 writer.println(prefix + " Hotseat");
2282 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07002283 for (int j = 0; j < layout.getChildCount(); j++) {
2284 Object tag = layout.getChildAt(j).getTag();
2285 if (tag != null) {
2286 writer.println(prefix + " " + tag.toString());
2287 }
Adam Cohen4caf2982013-08-20 18:54:31 -07002288 }
Sunny Goyala1365452015-10-01 15:46:24 -07002289 }
2290
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002291 writer.println(prefix + "Misc:");
2292 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
2293 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
2294 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
Sunny Goyale43d00d2018-05-14 14:23:18 -07002295 writer.println(" mRotationHelper: " + mRotationHelper);
2296 dumpMisc(writer);
Sunny Goyal2bba1902018-02-27 12:20:50 -08002297
2298 try {
2299 FileLog.flushAll(writer);
2300 } catch (Exception e) {
2301 // Ignore
2302 }
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002303
2304 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002305
Adam Cohen9211d422014-10-07 18:14:53 -07002306 if (mLauncherCallbacks != null) {
2307 mLauncherCallbacks.dump(prefix, fd, writer, args);
2308 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002309 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002310
Sunny Goyal66b24572016-09-21 15:57:55 -07002311 @Override
2312 @TargetApi(Build.VERSION_CODES.N)
2313 public void onProvideKeyboardShortcuts(
2314 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
2315
2316 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002317 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002318 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
2319 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
Vadim Tryshev74c261c2018-06-12 13:44:43 -07002320 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.widget_button_text),
2321 KeyEvent.KEYCODE_W, KeyEvent.META_CTRL_ON));
Sunny Goyal66b24572016-09-21 15:57:55 -07002322 }
Vadim Tryshev3455f8d2018-05-11 20:02:20 -07002323 final View currentFocus = getCurrentFocus();
2324 if (currentFocus != null) {
2325 if (new CustomActionsPopup(this, currentFocus).canShow()) {
2326 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
2327 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
2328 }
2329 if (currentFocus.getTag() instanceof ItemInfo
2330 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
2331 shortcutInfos.add(new KeyboardShortcutInfo(
2332 getString(R.string.shortcuts_menu_with_notifications_description),
2333 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
2334 }
Sunny Goyal66b24572016-09-21 15:57:55 -07002335 }
2336 if (!shortcutInfos.isEmpty()) {
2337 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
2338 }
2339
2340 super.onProvideKeyboardShortcuts(data, menu, deviceId);
2341 }
2342
2343 @Override
2344 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
2345 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
2346 switch (keyCode) {
2347 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002348 if (isInState(NORMAL)) {
2349 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07002350 return true;
2351 }
2352 break;
2353 case KeyEvent.KEYCODE_S: {
2354 View focusedView = getCurrentFocus();
2355 if (focusedView instanceof BubbleTextView
2356 && focusedView.getTag() instanceof ItemInfo
2357 && mAccessibilityDelegate.performAction(focusedView,
2358 (ItemInfo) focusedView.getTag(),
2359 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal00ac9202017-11-09 15:24:06 -08002360 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07002361 return true;
2362 }
2363 break;
2364 }
2365 case KeyEvent.KEYCODE_O:
2366 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
2367 return true;
2368 }
2369 break;
Vadim Tryshev74c261c2018-06-12 13:44:43 -07002370 case KeyEvent.KEYCODE_W:
2371 if (isInState(NORMAL)) {
2372 OptionsPopupView.openWidgets(this);
2373 return true;
2374 }
2375 break;
Sunny Goyal66b24572016-09-21 15:57:55 -07002376 }
2377 }
2378 return super.onKeyShortcut(keyCode, event);
2379 }
2380
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002381 @Override
2382 public boolean onKeyUp(int keyCode, KeyEvent event) {
2383 if (keyCode == KeyEvent.KEYCODE_MENU) {
2384 // KEYCODE_MENU is sent by some tests, for example
2385 // LauncherJankTests#testWidgetsContainerFling. Don't just remove its handling.
2386 if (!mDragController.isDragging() && !mWorkspace.isSwitchingState() &&
2387 isInState(NORMAL)) {
2388 // Close any open floating views.
2389 AbstractFloatingView.closeAllOpenViews(this);
2390
2391 // Setting the touch point to (-1, -1) will show the options popup in the center of
2392 // the screen.
Sunny Goyal2fd7a8b2018-03-30 17:10:13 -07002393 OptionsPopupView.showDefaultOptions(this, -1, -1);
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002394 }
2395 return true;
2396 }
2397 return super.onKeyUp(keyCode, event);
2398 }
2399
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002400 public static Launcher getLauncher(Context context) {
Sunny Goyal87b5eb62018-07-03 15:53:39 -07002401 return (Launcher) fromContext(context);
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002402 }
2403
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002404 /**
2405 * Callback for listening for onResume
2406 */
2407 public interface OnResumeCallback {
2408
2409 void onLauncherResume();
2410 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002411}