blob: 9a3e8a24b47306212c6640dcbf9e393e4ef51b65 [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
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080019import static android.content.pm.ActivityInfo.CONFIG_ORIENTATION;
20import static android.content.pm.ActivityInfo.CONFIG_SCREEN_SIZE;
Sunny Goyal623eddd2018-03-02 12:24:41 -080021
Sunny Goyal4c7f2152017-10-17 17:17:16 -070022import static com.android.launcher3.LauncherAnimUtils.SPRING_LOADED_EXIT_DELAY;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -070023import static com.android.launcher3.LauncherState.ALL_APPS;
24import static com.android.launcher3.LauncherState.NORMAL;
25import static com.android.launcher3.LauncherState.OVERVIEW;
Sunny Goyalaeb16432017-10-16 11:46:41 -070026import static com.android.launcher3.logging.LoggerUtils.newContainerTarget;
Sunny Goyal326403e2017-10-02 12:45:10 -070027
Gilles Debunnedd6c9922010-10-25 11:23:41 -070028import android.animation.Animator;
Jon Miranda2d89ea82017-05-04 11:47:53 -070029import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070030import android.animation.AnimatorSet;
Jon Miranda2d89ea82017-05-04 11:47:53 -070031import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070032import android.animation.ValueAnimator;
Adam Cohen0b395352014-06-09 22:54:36 +000033import android.annotation.TargetApi;
Tony Wickham005df0b2018-02-13 11:28:12 -080034import android.app.ActivityOptions;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080035import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070036import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080038import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080039import android.content.ComponentCallbacks2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.Context;
Andrew Sappersteinabef55a2016-06-19 12:49:00 -070041import android.content.ContextWrapper;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070043import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070044import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070045import android.content.SharedPreferences;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.content.pm.PackageManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080047import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070048import android.database.sqlite.SQLiteDatabase;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070049import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020050import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051import android.os.Bundle;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070052import android.os.Parcelable;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080053import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070054import android.os.StrictMode;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070055import android.os.UserHandle;
Tony Wickham86222d22017-03-29 15:30:43 -070056import android.support.annotation.Nullable;
Michael Jurkaa33411c2012-06-14 16:18:21 -070057import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070059import android.util.Log;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070060import android.util.SparseArray;
Adam Cohen96d30a12013-07-16 18:13:21 -070061import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070062import android.view.KeyboardShortcutGroup;
63import android.view.KeyboardShortcutInfo;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080064import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -070065import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070066import android.view.View;
Adam Cohen96d30a12013-07-16 18:13:21 -070067import android.view.ViewGroup;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080068import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070069import android.view.animation.OvershootInterpolator;
Adam Cohen96d30a12013-07-16 18:13:21 -070070import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070071
Sunny Goyal651077b2014-06-30 14:15:31 -070072import com.android.launcher3.DropTarget.DragObject;
Jon Mirandac476d6e2017-05-04 16:30:01 -070073import com.android.launcher3.Workspace.ItemOperator;
Sunny Goyalae502842016-06-17 08:43:56 -070074import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070075import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070076import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal7ede6112017-12-05 15:11:21 -080077import com.android.launcher3.allapps.DiscoveryBounce;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070078import com.android.launcher3.badge.BadgeInfo;
Sunny Goyalffe83f12014-08-14 17:39:34 -070079import com.android.launcher3.compat.AppWidgetManagerCompat;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -070080import com.android.launcher3.compat.LauncherAppsCompatVO;
Tony Wickhame0c33232016-02-08 11:37:04 -080081import com.android.launcher3.config.FeatureFlags;
Vadim Tryshevfedca432015-08-19 17:55:02 -070082import com.android.launcher3.dragndrop.DragController;
83import com.android.launcher3.dragndrop.DragLayer;
84import com.android.launcher3.dragndrop.DragView;
Mario Bertschler27288382017-05-24 15:35:09 -070085import com.android.launcher3.dynamicui.WallpaperColorInfo;
Sunny Goyal26119432016-02-18 22:09:23 +000086import com.android.launcher3.folder.FolderIcon;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070087import com.android.launcher3.folder.FolderIconPreviewVerifier;
Sunny Goyal66b24572016-09-21 15:57:55 -070088import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -070089import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -070090import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -070091import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyala535ae42017-02-27 10:07:13 -080092import com.android.launcher3.model.ModelWriter;
Sunny Goyala535ae42017-02-27 10:07:13 -080093import com.android.launcher3.notification.NotificationListener;
Tony Wickham540913e2017-01-23 11:47:51 -080094import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -080095import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -070096import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyalf8088ee2017-11-10 14:52:00 -080097import com.android.launcher3.states.InternalStateHandler;
Sunny Goyal623eddd2018-03-02 12:24:41 -080098import com.android.launcher3.states.RotationHelper;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -080099import com.android.launcher3.touch.ItemClickHandler;
Sunny Goyala5ace712017-11-15 17:12:51 -0800100import com.android.launcher3.uioverrides.UiFactory;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530101import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
102import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
Sunny Goyalaeb16432017-10-16 11:46:41 -0700103import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyal2100c782016-08-22 16:00:03 -0700104import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700105import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700106import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700107import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700108import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800109import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700110import com.android.launcher3.util.PendingRequestArgs;
Sunny Goyal8392c822017-06-20 10:03:56 -0700111import com.android.launcher3.util.SystemUiController;
Mario Bertschlera6936942017-05-31 14:48:19 -0700112import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700113import com.android.launcher3.util.Thunk;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700114import com.android.launcher3.util.TraceHelper;
Sunny Goyal326403e2017-10-02 12:45:10 -0700115import com.android.launcher3.util.UiThreadHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700116import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800117import com.android.launcher3.views.OptionsPopupView;
Sunny Goyal29947f02017-12-18 13:49:44 -0800118import com.android.launcher3.widget.LauncherAppWidgetHostView;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800119import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700120import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal29947f02017-12-18 13:49:44 -0800121import com.android.launcher3.widget.PendingAppWidgetHostView;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800122import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700123import com.android.launcher3.widget.WidgetHostViewLoader;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700124import com.android.launcher3.widget.WidgetListRowEntry;
125import com.android.launcher3.widget.WidgetsFullSheet;
Sunny Goyal952e63d2017-08-16 04:59:08 -0700126import com.android.launcher3.widget.custom.CustomWidgetParser;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700127
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700128import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700129import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700130import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700131import java.util.Collection;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700132import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700133import java.util.List;
Tony Wickham010d2552017-01-20 08:15:28 -0800134import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700135
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136/**
137 * Default launcher application.
138 */
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700139public class Launcher extends BaseDraggingActivity implements LauncherExterns, LauncherModel.Callbacks,
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800140 LauncherProviderChangeListener, WallpaperColorInfo.OnThemeChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700141 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700142 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143
Winson Chunga2413752012-04-03 14:22:34 -0700144 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700145
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700147 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700148
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700149 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700150 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151
Michael Jurka8b805b12012-04-18 14:23:14 -0700152 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800153 public static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
154 public static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700155
Jon Mirandac476d6e2017-05-04 16:30:01 -0700156 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700157
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700158 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
159
Mathew Inwood876a8462013-06-14 14:12:41 +0100160 /**
161 * IntentStarter uses request codes starting with this. This must be greater than all activity
162 * request codes used internally.
163 */
164 protected static final int REQUEST_LAST = 100;
165
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 // Type: int
167 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700168 // Type: int
169 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700170 // Type: PendingRequestArgs
171 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
172 // Type: ActivityResultInfo
173 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700174 // Type: SparseArray<Parcelable>
175 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700177 private LauncherStateManager mStateManager;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700178
Adam Cohenad4e15c2013-10-17 16:21:35 -0700179 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180
Winson Chunga2413752012-04-03 14:22:34 -0700181 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700182 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
183 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
184 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700185
Jon Miranda54441f52018-01-24 15:38:25 -0800186 private LauncherAppTransitionManager mAppTransitionManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800187 private Configuration mOldConfig;
Jon Miranda54441f52018-01-24 15:38:25 -0800188
Adam Cohen091440a2015-03-18 14:16:05 -0700189 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700190 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700191 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800192 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700193
Sunny Goyalffe83f12014-08-14 17:39:34 -0700194 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700195 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700196
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700197 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700198
Sunny Goyal316490e2015-06-02 09:38:28 -0700199 @Thunk Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700200
Sunny Goyal47328fd2016-05-25 18:56:41 -0700201 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700202
203 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700204 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700205 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700206
Winson Chung8ae41982017-11-10 11:42:13 -0800207 // UI and state for the overview panel
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800208 private View mOverviewPanel;
Winson Chung8ae41982017-11-10 11:42:13 -0800209
Adam Cohen091440a2015-03-18 14:16:05 -0700210 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400211
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700212 private OnResumeCallback mOnResumeCallback;
213
Sunny Goyal527c7d32015-08-28 15:19:36 -0700214 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700215
Joe Onorato9c1289c2009-08-17 11:03:03 -0400216 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800217 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800218 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700219 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400220
Jon Miranda2d89ea82017-05-04 11:47:53 -0700221 private ObjectAnimator mScrimAnimator;
Jon Miranda7fda2852017-07-19 16:07:01 -0700222 private boolean mShouldFadeInScrim;
Jon Miranda2d89ea82017-05-04 11:47:53 -0700223
Tony Wickham010d2552017-01-20 08:15:28 -0800224 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700225
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800226 private int mSynchronouslyBoundPage = PagedView.INVALID_PAGE;
Adam Cohen1462de32012-07-24 22:34:36 -0700227
Winson Chung46353de2012-02-16 14:05:10 -0800228 // We only want to get the SharedPreferences once since it does an FS stat each time we get
229 // it from the context.
230 private SharedPreferences mSharedPrefs;
231
Sunny Goyal2100c782016-08-22 16:00:03 -0700232 // Activity result which needs to be processed after workspace has loaded.
233 private ActivityResultInfo mPendingActivityResult;
234 /**
235 * Holds extra information required to handle a result from an external call, like
236 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
237 */
238 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800239
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700240 public ViewGroupFocusHelper mFocusHandler;
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700241 private boolean mAppLaunchSuccess;
242
Sunny Goyal623eddd2018-03-02 12:24:41 -0800243 private RotationHelper mRotationHelper;
Sunny Goyal7779d622015-06-11 16:18:39 -0700244
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800245 @Override
246 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700247 if (DEBUG_STRICT_MODE) {
248 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
249 .detectDiskReads()
250 .detectDiskWrites()
251 .detectNetwork() // or .detectAll() for all detectable problems
252 .penaltyLog()
253 .build());
254 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
255 .detectLeakedSqlLiteObjects()
256 .detectLeakedClosableObjects()
257 .penaltyLog()
258 .penaltyDeath()
259 .build());
260 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700261 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700262
Mario Bertschler27288382017-05-24 15:35:09 -0700263 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
264 wallpaperColorInfo.setOnThemeChangeListener(this);
Mario Bertschlera6936942017-05-31 14:48:19 -0700265 overrideTheme(wallpaperColorInfo.isDark(), wallpaperColorInfo.supportsDarkText());
Mario Bertschler27288382017-05-24 15:35:09 -0700266
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800267 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700268 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400269
Sunny Goyal87f784c2017-01-11 10:48:34 -0800270 LauncherAppState app = LauncherAppState.getInstance(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800271 mOldConfig = new Configuration(getResources().getConfiguration());
Sunny Goyal0e7724c2018-02-22 13:22:21 -0800272 mModel = app.setLauncher(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800273 initDeviceProfile(app.getInvariantDeviceProfile());
Winson Chung6e1c0d32013-10-25 15:24:24 -0700274
Sunny Goyalf7258242015-10-19 16:59:07 -0700275 mSharedPrefs = Utilities.getPrefs(this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800276 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700277 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700278
Joe Onorato41a12d22009-10-31 18:30:00 -0400279 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700280 mAllAppsController = new AllAppsTransitionController(this);
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800281 mStateManager = new LauncherStateManager(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700282
Sunny Goyalffe83f12014-08-14 17:39:34 -0700283 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700284
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700285 mAppWidgetHost = new LauncherAppWidgetHost(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700286 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700287
Sunny Goyal60820d72017-05-09 12:40:11 -0700288 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700289
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800290 setupViews();
Tony Wickham010d2552017-01-20 08:15:28 -0800291 mPopupDataProvider = new PopupDataProvider(this);
292
Sunny Goyal623eddd2018-03-02 12:24:41 -0800293 mRotationHelper = new RotationHelper(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800294
Sunny Goyald3864fa2017-11-14 15:06:36 -0800295 boolean internalStateHandled = InternalStateHandler.handleCreate(this, getIntent());
296 if (internalStateHandled) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800297 if (savedInstanceState != null) {
298 // InternalStateHandler has already set the appropriate state.
299 // We dont need to do anything.
300 savedInstanceState.remove(RUNTIME_STATE);
301 }
302 }
303 restoreState(savedInstanceState);
Winson Chungb63b44c2017-11-10 17:54:44 -0800304
Sunny Goyal2100c782016-08-22 16:00:03 -0700305 // We only load the page synchronously if the user rotates (or triggers a
306 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700307 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
308 if (savedInstanceState != null) {
309 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
310 }
Sunny Goyald3864fa2017-11-14 15:06:36 -0800311
Sunny Goyalfe770c92016-09-27 14:38:58 -0700312 if (!mModel.startLoader(currentScreen)) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800313 if (!internalStateHandled) {
314 // If we are not binding synchronously, show a fade in animation when
315 // the first page bind completes.
316 mDragLayer.setAlpha(0);
317 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700318 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700319 // Pages bound synchronously.
320 mWorkspace.setCurrentPage(currentScreen);
321
Sunny Goyal2100c782016-08-22 16:00:03 -0700322 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800323 }
324
325 // For handling default keys
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800326 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800327
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800328 setContentView(mLauncherView);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800329 getRootView().dispatchInsets();
Mario Bertschlera6936942017-05-31 14:48:19 -0700330
Jon Miranda7fda2852017-07-19 16:07:01 -0700331 // Listen for broadcasts
332 IntentFilter filter = new IntentFilter();
333 filter.addAction(Intent.ACTION_SCREEN_OFF);
334 filter.addAction(Intent.ACTION_USER_PRESENT); // When the device wakes up + keyguard is gone
335 registerReceiver(mReceiver, filter);
336 mShouldFadeInScrim = true;
Sunny Goyala616d2b2017-06-12 13:54:01 -0700337
Sunny Goyal8392c822017-06-20 10:03:56 -0700338 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
339 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700340
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800341 mAppTransitionManager = LauncherAppTransitionManager.newInstance(this);
Jon Miranda7853bdb2018-01-22 17:32:30 -0800342
Sunny Goyal14b32402017-07-31 10:03:28 -0700343 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
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800352 public void onConfigurationChanged(Configuration newConfig) {
353 int diff = newConfig.diff(mOldConfig);
354 if ((diff & (CONFIG_ORIENTATION | CONFIG_SCREEN_SIZE)) != 0) {
355 mUserEventDispatcher = null;
356 initDeviceProfile(mDeviceProfile.inv);
Sunny Goyal2bba1902018-02-27 12:20:50 -0800357 FileLog.d(TAG, "Config changed, my orientation=" +
358 getResources().getConfiguration().orientation +
359 ", new orientation=" + newConfig.orientation +
360 ", old orientation=" + mOldConfig.orientation +
361 ", isTransposed=" + mDeviceProfile.isVerticalBarLayout());
Sunny Goyalfde55052018-02-01 14:46:13 -0800362 dispatchDeviceProfileChanged();
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800363
364 getRootView().dispatchInsets();
365 getStateManager().reapplyState();
366
367 // TODO: We can probably avoid rebind when only screen size changed.
Sunny Goyal605bcf32018-03-02 15:16:12 -0800368 rebindModel();
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800369 }
370
371 mOldConfig.setTo(newConfig);
372 super.onConfigurationChanged(newConfig);
373 }
374
Sunny Goyal605bcf32018-03-02 15:16:12 -0800375 @Override
376 public void rebindModel() {
377 int currentPage = mWorkspace.getNextPage();
378 if (mModel.startLoader(currentPage)) {
379 mWorkspace.setCurrentPage(currentPage);
380 setWorkspaceLoading(true);
381 }
382 }
383
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800384 private void initDeviceProfile(InvariantDeviceProfile idp) {
385 // Load configuration-specific DeviceProfile
Sunny Goyalf633ef52018-03-13 09:57:05 -0700386 setDeviceProfile(idp.getDeviceProfile(this));
Sunny Goyal605bcf32018-03-02 15:16:12 -0800387 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout(), true);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800388 }
389
Sunny Goyal623eddd2018-03-02 12:24:41 -0800390 public RotationHelper getRotationHelper() {
391 return mRotationHelper;
392 }
393
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800394 @Override
Mario Bertschler27288382017-05-24 15:35:09 -0700395 public void onThemeChanged() {
396 recreate();
397 }
398
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700399 public LauncherStateManager getStateManager() {
400 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700401 }
402
Mario Bertschlera6936942017-05-31 14:48:19 -0700403 protected void overrideTheme(boolean isDark, boolean supportsDarkText) {
Mario Bertschler27288382017-05-24 15:35:09 -0700404 if (isDark) {
405 setTheme(R.style.LauncherThemeDark);
Mario Bertschlera6936942017-05-31 14:48:19 -0700406 } else if (supportsDarkText) {
407 setTheme(R.style.LauncherThemeDarkText);
Mario Bertschler27288382017-05-24 15:35:09 -0700408 }
409 }
410
411 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700412 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800413 return mLauncherView.findViewById(id);
414 }
415
416 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700417 public void onAppWidgetHostReset() {
418 if (mAppWidgetHost != null) {
419 mAppWidgetHost.startListening();
420 }
421 }
422
Adam Cohen9211d422014-10-07 18:14:53 -0700423 private LauncherCallbacks mLauncherCallbacks;
424
Sunny Goyal32554d12015-12-03 15:31:25 -0800425 /**
426 * Call this after onCreate to set or clear overlay.
427 */
428 public void setLauncherOverlay(LauncherOverlay overlay) {
429 if (overlay != null) {
430 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
431 }
432 mWorkspace.setLauncherOverlay(overlay);
433 }
434
Adam Cohen9211d422014-10-07 18:14:53 -0700435 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
436 mLauncherCallbacks = callbacks;
437 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700438 }
439
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700440 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700441 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700442 if (mLauncherCallbacks != null) {
443 mLauncherCallbacks.onLauncherProviderChange();
444 }
445 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700446
Hyunyoung Song3f471442015-04-08 19:01:34 -0700447 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700448 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
449 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700450 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700451 }
452
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000453 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700454 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
455 // This cast is safe as long as the id < 0x00FFFFFF
456 // Since we jail all the dynamically generated views, there should be no clashes
457 // with any other views.
458 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000459 }
460
Tony Wickham010d2552017-01-20 08:15:28 -0800461 public PopupDataProvider getPopupDataProvider() {
462 return mPopupDataProvider;
463 }
464
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700465 @Override
466 public BadgeInfo getBadgeInfoForItem(ItemInfo info) {
467 return mPopupDataProvider.getBadgeInfoForItem(info);
468 }
469
470 @Override
471 public void invalidateParent(ItemInfo info) {
472 FolderIconPreviewVerifier verifier = new FolderIconPreviewVerifier(getDeviceProfile().inv);
473 if (verifier.isItemInPreview(info.rank) && (info.container >= 0)) {
474 View folderIcon = getWorkspace().getHomescreenIconByItemId(info.container);
475 if (folderIcon != null) {
476 folderIcon.invalidate();
477 }
478 }
479 }
480
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000481 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700482 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
483 * a configuration step, this allows the proper animations to run after other transitions.
484 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700485 private long completeAdd(
486 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
487 long screenId = info.screenId;
488 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700489 // When the screen id represents an actual screen (as opposed to a rank) we make sure
490 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700491 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700492 }
Adam Cohendb364c32014-05-20 14:23:40 -0700493
Sunny Goyal2100c782016-08-22 16:00:03 -0700494 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800495 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700496 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700497 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800498 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700499 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700500 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700501 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700502 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700503 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700504 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700505 int widgetId = appWidgetId;
506 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700507 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700508 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700509 // Since the view was just bound, also launch the configure activity if needed
510 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
511 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800512 if (provider != null) {
513 new WidgetAddFlowHandler(provider)
514 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700515 }
516 }
517 break;
518 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800519 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700520
Adam Cohendb364c32014-05-20 14:23:40 -0700521 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800522 }
523
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800524 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700525 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700526 if (isWorkspaceLoading()) {
527 // process the result once the workspace has loaded.
528 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
529 return;
530 }
531 mPendingActivityResult = null;
532
Winson Chunge341d302013-08-16 14:31:00 -0700533 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700534 final PendingRequestArgs requestArgs = mPendingRequestArgs;
535 setWaitingForResult(null);
536 if (requestArgs == null) {
537 return;
538 }
539
540 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700541
Adam Cohenad4e15c2013-10-17 16:21:35 -0700542 Runnable exitSpringLoaded = new Runnable() {
543 @Override
544 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700545 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700546 }
547 };
548
Michael Jurka8b805b12012-04-18 14:23:14 -0700549 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700550 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700551 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700552 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
553 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700554 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700555 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700556 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700557 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700558 addAppWidgetImpl(
559 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800560 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700561 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700562 }
563 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200564 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700565 if (resultCode == RESULT_OK && isInState(OVERVIEW)) {
Tony Wickhame3054412015-09-09 09:05:25 -0700566 // User could have free-scrolled between pages before picking a wallpaper; make sure
567 // we move to the closest one now.
568 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700569 mStateManager.goToState(NORMAL, false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200570 }
571 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700572 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200573
Adam Cohened66b2b2012-01-23 17:28:51 -0800574 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700575 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700576
Adam Cohened66b2b2012-01-23 17:28:51 -0800577 // We have special handling for widgets
578 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800579 final int appWidgetId;
580 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
581 : -1;
582 if (widgetId < 0) {
583 appWidgetId = pendingAddWidgetId;
584 } else {
585 appWidgetId = widgetId;
586 }
587
Adam Cohenad4e15c2013-10-17 16:21:35 -0700588 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800589 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700590 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
591 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700592 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700593 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700594 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700595 @Override
596 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700597 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700598 }
599 };
Adam Cohendb364c32014-05-20 14:23:40 -0700600
Sunny Goyal2100c782016-08-22 16:00:03 -0700601 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
602 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
603 } else {
604 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
605 // When the screen id represents an actual screen (as opposed to a rank)
606 // we make sure that the drop page actually exists.
607 requestArgs.screenId =
608 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700609 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700610 final CellLayout dropLayout =
611 mWorkspace.getScreenWithId(requestArgs.screenId);
612
613 dropLayout.setDropPending(true);
614 final Runnable onComplete = new Runnable() {
615 @Override
616 public void run() {
617 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
618 dropLayout.setDropPending(false);
619 }
620 };
621 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
622 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700623 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800624 return;
625 }
626
Sunny Goyald478c832016-04-01 12:04:16 -0700627 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
628 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700629 if (resultCode == RESULT_OK) {
630 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700631 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700632 }
633 // Leave the widget in the pending state if the user canceled the configure.
634 return;
635 }
636
Sunny Goyalaad90582015-07-09 14:22:50 -0700637 if (requestCode == REQUEST_CREATE_SHORTCUT) {
638 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700639 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
640 completeAdd(requestCode, data, -1, requestArgs);
641 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
642 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
643
Sunny Goyalaad90582015-07-09 14:22:50 -0700644 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700645 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
646 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800647 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800648 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800649 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800650 }
651
652 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700653 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800654 final int requestCode, final int resultCode, final Intent data) {
655 handleActivityResult(requestCode, resultCode, data);
656 if (mLauncherCallbacks != null) {
657 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
658 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800659 }
660
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700661 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700662 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600663 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700664 PendingRequestArgs pendingArgs = mPendingRequestArgs;
665 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
666 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
667 setWaitingForResult(null);
668
Sunny Goyal28c6b962015-10-12 11:42:05 -0700669 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700670 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700671 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700672 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700673 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700674 Intent intent = pendingArgs.getPendingIntent();
675
Sunny Goyal28c6b962015-10-12 11:42:05 -0700676 if (grantResults.length > 0
677 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700678 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700679 } else {
680 // TODO: Show a snack bar with link to settings
681 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700682 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700683 }
684 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600685 if (mLauncherCallbacks != null) {
686 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
687 grantResults);
688 }
689 }
690
Adam Cohendb364c32014-05-20 14:23:40 -0700691 /**
692 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
693 *
694 * @param screenId the screen id to check
695 * @return the new screen, or screenId if it exists
696 */
697 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800698 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700699 if (dropLayout == null) {
700 // it's possible that the add screen was removed because it was
701 // empty and a re-bind occurred
702 mWorkspace.addExtraEmptyScreen();
703 return mWorkspace.commitExtraEmptyScreen();
704 } else {
705 return screenId;
706 }
707 }
708
Sunny Goyal2100c782016-08-22 16:00:03 -0700709 @Thunk void completeTwoStageWidgetDrop(
710 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
711 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800712 Runnable onCompleteRunnable = null;
713 int animationType = 0;
714
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700715 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800716 if (resultCode == RESULT_OK) {
717 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
718 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800719 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700720 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800721 onCompleteRunnable = new Runnable() {
722 @Override
723 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700724 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700725 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800726 }
727 };
728 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800729 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800730 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800731 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700732 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700733 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700734 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
735 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700736 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700737 // The animated view may be null in the case of a rotation during widget configuration
738 onCompleteRunnable.run();
739 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800740 }
741
742 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700743 protected void onStop() {
744 super.onStop();
745 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700746
747 if (mLauncherCallbacks != null) {
748 mLauncherCallbacks.onStop();
749 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800750 mAppWidgetHost.setListenIfResumed(false);
Tony Wickham010d2552017-01-20 08:15:28 -0800751
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700752 if (!mAppLaunchSuccess) {
753 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -0800754 mStateManager.getState().containerType, -1);
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700755 }
Tony Wickham010d2552017-01-20 08:15:28 -0800756 NotificationListener.removeNotificationsChangedListener();
Sunny Goyalaf161fd2018-02-05 16:34:57 -0800757 getStateManager().moveToRestState();
Michael Jurkacd496d72013-04-11 11:32:45 -0700758 }
759
760 @Override
761 protected void onStart() {
762 super.onStart();
763 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700764
765 if (mLauncherCallbacks != null) {
766 mLauncherCallbacks.onStart();
767 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800768 mAppWidgetHost.setListenIfResumed(true);
Sunny Goyalcac002d2018-01-04 09:51:45 -0800769 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
Jon Miranda2d89ea82017-05-04 11:47:53 -0700770
Jon Mirandade43a712018-01-18 11:35:10 -0800771 if (mShouldFadeInScrim && mLauncherView.getBackground() != null) {
Jon Miranda2d89ea82017-05-04 11:47:53 -0700772 if (mScrimAnimator != null) {
773 mScrimAnimator.cancel();
774 }
Jon Mirandade43a712018-01-18 11:35:10 -0800775 mLauncherView.getBackground().setAlpha(0);
776 mScrimAnimator = ObjectAnimator.ofInt(mLauncherView.getBackground(),
Jon Miranda2d89ea82017-05-04 11:47:53 -0700777 LauncherAnimUtils.DRAWABLE_ALPHA, 0, 255);
778 mScrimAnimator.addListener(new AnimatorListenerAdapter() {
779 @Override
780 public void onAnimationEnd(Animator animation) {
781 mScrimAnimator = null;
782 }
783 });
784 mScrimAnimator.setDuration(600);
785 mScrimAnimator.setStartDelay(getWindow().getTransitionBackgroundFadeDuration());
786 mScrimAnimator.start();
787 }
Jon Miranda7fda2852017-07-19 16:07:01 -0700788 mShouldFadeInScrim = false;
Winson Chung62a70be2018-02-23 15:10:05 -0800789 UiFactory.onStart(this);
Michael Jurkacd496d72013-04-11 11:32:45 -0700790 }
791
792 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800793 protected void onResume() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700794 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800795 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700796 TraceHelper.partitionSection("ON_RESUME", "superCall");
797
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700798 mAppLaunchSuccess = false;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700799 getUserEventDispatcher().resetElapsedSessionMillis();
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700800 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700801 // Process any items that were added while Launcher was away.
802 InstallShortcutReceiver.disableAndFlushInstallQueue(
803 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700804
Sunny Goyala474a9b2017-05-04 16:47:11 -0700805 // Refresh shortcuts if the permission changed.
806 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700807
Sunny Goyal7ede6112017-12-05 15:11:21 -0800808 DiscoveryBounce.showIfNeeded(this);
Adam Cohen9211d422014-10-07 18:14:53 -0700809 if (mLauncherCallbacks != null) {
810 mLauncherCallbacks.onResume();
811 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800812
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700813 TraceHelper.endSection("ON_RESUME");
Adam Cohen06dff352012-06-01 17:17:08 -0700814 }
815
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800816 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700817 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700818 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700819 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700820
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700821 super.onPause();
Joe Onorato24b6fd82009-11-12 13:47:09 -0800822 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700823 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700824
Adam Cohen9211d422014-10-07 18:14:53 -0700825 if (mLauncherCallbacks != null) {
826 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700827 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700828 }
829
Winson Chungd7823942018-02-16 03:23:47 +0000830 @Override
831 public void onWindowFocusChanged(boolean hasFocus) {
832 super.onWindowFocusChanged(hasFocus);
833 mStateManager.onWindowFocusChanged();
834 }
835
Adam Cohenc2d6e892014-10-16 09:49:24 -0700836 public interface LauncherOverlay {
837
838 /**
839 * Touch interaction leading to overscroll has begun
840 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700841 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700842
843 /**
844 * Touch interaction related to overscroll has ended
845 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700846 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700847
848 /**
849 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
850 * screen (or in the case of RTL, the rightmost screen).
851 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700852 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700853
854 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800855 * Called when the launcher is ready to use the overlay
856 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700857 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700858 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700859 }
860
861 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700862 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700863 }
864
865 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
866
Sunny Goyalc86df472016-02-25 09:19:38 -0800867 public void onScrollChanged(float progress) {
868 if (mWorkspace != null) {
869 mWorkspace.onOverlayScrollChanged(progress);
870 }
871 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700872 }
873
Sunny Goyal16764582017-11-06 16:00:34 -0800874 public boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -0700875 if (mLauncherCallbacks != null) {
876 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700877 } else {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800878 // On O and above we there is always some setting present settings (add icon to
879 // home screen or icon badging). On earlier APIs we will have the allow rotation
880 // setting, on devices with a locked orientation,
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700881 return Utilities.ATLEAST_OREO || !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -0700882 }
Jorim Jaggid017f882014-01-14 17:08:48 -0800883 }
884
Sunny Goyalf9403d92017-10-18 10:55:56 -0700885 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -0700886 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700887 }
888
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800889 /**
890 * Restores the previous state, if it exists.
891 *
892 * @param savedState The previous state.
893 */
894 private void restoreState(Bundle savedState) {
895 if (savedState == null) {
896 return;
897 }
898
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700899 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -0700900 LauncherState[] stateValues = LauncherState.values();
901 LauncherState state = stateValues[stateOrdinal];
Sunny Goyalbc683e92017-12-06 10:25:07 -0800902 if (!state.disableRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700903 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800904 }
905
Sunny Goyal2100c782016-08-22 16:00:03 -0700906 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
907 if (requestArgs != null) {
908 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800909 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700910
911 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700912
913 SparseArray<Parcelable> widgetsState =
914 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
915 if (widgetsState != null) {
916 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
917 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800918 }
919
920 /**
921 * Finds all the views we need and configure them properly.
922 */
923 private void setupViews() {
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800924 mDragLayer = findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700925 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -0700926 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700927 mWorkspace.initParentViews(mDragLayer);
Sunny Goyal16764582017-11-06 16:00:34 -0800928 mOverviewPanel = findViewById(R.id.overview_panel);
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800929 mHotseat = findViewById(R.id.hotseat);
Craig Mautner360310b2012-10-26 15:13:08 -0700930
Sunny Goyal784f9c32016-03-23 16:56:54 -0700931 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
932 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
933 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800934
Winson Chung4c98d922011-05-31 16:50:48 -0700935 // Setup the drag layer
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700936 mDragLayer.setup(mDragController);
Winson Chung4c98d922011-05-31 16:50:48 -0700937
Hyunyoung Song645764e2016-06-06 14:19:02 -0700938 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -0700939 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
940 // default state, otherwise we will update to the wrong offsets in RTL
941 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700942 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700943 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700944
Tony Wickham34d2c912015-09-11 09:27:58 -0700945 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700946 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700947
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700948 // Setup Apps
949 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -0700950
Winson Chung3d503fb2011-07-13 17:25:49 -0700951 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -0700952 mDragController.setMoveTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700953 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -0400954
Sunny Goyal6639a5d2018-02-28 15:09:36 -0800955 mAllAppsController.setupViews(mAppsView, mHotseat);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800956 }
957
958 /**
959 * Creates a view representing a shortcut.
960 *
961 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800962 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800963 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700964 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800965 }
966
967 /**
968 * Creates a view representing a shortcut inflated from the specified resource.
969 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800970 * @param parent The group the shortcut belongs to.
971 * @param info The data structure describing the shortcut.
972 *
973 * @return A View inflated from layoutResId.
974 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700975 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -0800976 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
977 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800978 favorite.applyFromShortcutInfo(info);
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800979 favorite.setOnClickListener(ItemClickHandler.INSTANCE);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700980 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800981 return favorite;
982 }
983
984 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -0700985 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800986 *
987 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800988 */
Adam Cohendcd297f2013-06-18 13:13:40 -0700989 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700990 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -0700991 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
992 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700993 return;
994 }
Adam Cohen558baaf2011-08-15 15:22:57 -0700995
Jon Mirandac476d6e2017-05-04 16:30:01 -0700996 int[] cellXY = mTmpAddItemCellCoordinates;
997 CellLayout layout = getCellLayout(container, screenId);
998
Sunny Goyal782f0c92017-01-19 10:27:54 -0800999 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001000 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001001 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
1002 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001003 }
1004
1005 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001006 // Legacy shortcuts are only supported for primary profile.
1007 info = Process.myUserHandle().equals(args.user)
1008 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001009
Sunny Goyalb57645f2017-03-20 13:57:28 -07001010 if (info == null) {
1011 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1012 return;
1013 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001014 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001015 // The app is trying to add a shortcut without sufficient permissions
1016 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1017 return;
1018 }
1019 }
1020
Jon Mirandac476d6e2017-05-04 16:30:01 -07001021 if (container < 0) {
1022 // Adding a shortcut to the Workspace.
1023 final View view = createShortcut(info);
1024 boolean foundCellSpan = false;
1025 // First we check if we already know the exact location where we want to add this item.
1026 if (cellX >= 0 && cellY >= 0) {
1027 cellXY[0] = cellX;
1028 cellXY[1] = cellY;
1029 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001030
Jon Mirandac476d6e2017-05-04 16:30:01 -07001031 // If appropriate, either create a folder or add to an existing folder
1032 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001033 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001034 return;
1035 }
1036 DragObject dragObject = new DragObject();
1037 dragObject.dragInfo = info;
1038 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1039 true)) {
1040 return;
1041 }
1042 } else {
1043 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1044 }
1045
1046 if (!foundCellSpan) {
1047 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001048 return;
1049 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001050
1051 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1052 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001053 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001054 // Adding a shortcut to a Folder.
Sunny Goyale29897f52017-07-20 10:09:42 -07001055 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001056 if (folderIcon != null) {
1057 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1058 folderInfo.add(info, args.rank, false);
1059 } else {
Sunny Goyale29897f52017-07-20 10:09:42 -07001060 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001061 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001062 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001063 }
1064
Sunny Goyale29897f52017-07-20 10:09:42 -07001065 public FolderIcon findFolderIcon(final long folderIconId) {
1066 return (FolderIcon) mWorkspace.getFirstMatch(new ItemOperator() {
1067 @Override
1068 public boolean evaluate(ItemInfo info, View view) {
1069 return info != null && info.id == folderIconId;
1070 }
1071 });
1072 }
1073
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001074 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001075 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001076 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001077 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001078 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001079 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001080 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1081
Adam Cohened66b2b2012-01-23 17:28:51 -08001082 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001083 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001084 }
Romain Guycbb89e42009-06-08 15:52:54 -07001085
Adam Cohen59400422014-03-05 18:07:04 -08001086 LauncherAppWidgetInfo launcherInfo;
1087 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001088 launcherInfo.spanX = itemInfo.spanX;
1089 launcherInfo.spanY = itemInfo.spanY;
1090 launcherInfo.minSpanX = itemInfo.minSpanX;
1091 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001092 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001093
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001094 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001095 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001096
Sunny Goyal2100c782016-08-22 16:00:03 -07001097 if (hostView == null) {
1098 // Perform actual inflation because we're live
1099 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001100 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001101 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301102 prepareAppWidget(hostView, launcherInfo);
1103 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001104 }
Romain Guycbb89e42009-06-08 15:52:54 -07001105
Sunny Goyald5462aa2016-11-22 16:52:39 +05301106 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001107 hostView.setTag(item);
1108 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001109 hostView.setFocusable(true);
1110 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001111 }
1112
Adam Cohended9f8d2010-11-03 13:25:16 -07001113 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1114 @Override
1115 public void onReceive(Context context, Intent intent) {
1116 final String action = intent.getAction();
1117 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001118 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001119 // processing a multi-step drop
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001120 if (mAppsView != null && mPendingRequestArgs == null) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001121 mStateManager.goToState(NORMAL);
Winson Chung785d2eb2011-04-14 16:08:02 -07001122 }
Jon Miranda7fda2852017-07-19 16:07:01 -07001123 mShouldFadeInScrim = true;
1124 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1125 // ACTION_USER_PRESENT is sent after onStart/onResume. This covers the case where
1126 // the user unlocked and the Launcher is not in the foreground.
1127 mShouldFadeInScrim = false;
Adam Cohended9f8d2010-11-03 13:25:16 -07001128 }
1129 }
1130 };
1131
Tony Wickham010d2552017-01-20 08:15:28 -08001132 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
Sunny Goyal29947f02017-12-18 13:49:44 -08001133 mWorkspace.updateIconBadges(updatedBadges);
Sunny Goyal60180b02018-02-07 12:56:30 -08001134 mAppsView.getAppsStore().updateIconBadges(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001135
Sunny Goyal29947f02017-12-18 13:49:44 -08001136 PopupContainerWithArrow popup = PopupContainerWithArrow.getOpen(Launcher.this);
1137 if (popup != null) {
1138 popup.updateNotificationHeader(updatedBadges);
Tony Wickham010d2552017-01-20 08:15:28 -08001139 }
1140 }
1141
Adam Cohended9f8d2010-11-03 13:25:16 -07001142 @Override
1143 public void onAttachedToWindow() {
1144 super.onAttachedToWindow();
1145
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001146 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyalc86df472016-02-25 09:19:38 -08001147 if (mLauncherCallbacks != null) {
1148 mLauncherCallbacks.onAttachedToWindow();
1149 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001150 }
1151
1152 @Override
1153 public void onDetachedFromWindow() {
1154 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001155
1156 if (mLauncherCallbacks != null) {
1157 mLauncherCallbacks.onDetachedFromWindow();
1158 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001159 }
1160
Sunny Goyal3bac2c22018-02-07 14:44:05 -08001161 public void onQuickstepGestureStarted(boolean isVisible) {
1162 if (mLauncherCallbacks != null) {
1163 mLauncherCallbacks.onQuickstepGestureStarted(isVisible);
1164 }
1165 }
1166
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001167 public AllAppsTransitionController getAllAppsController() {
1168 return mAllAppsController;
1169 }
1170
Sunny Goyal9d69c8d2018-03-19 13:41:31 -07001171 @Override
Winson Chung1a341002018-01-17 10:00:23 -08001172 public LauncherRootView getRootView() {
1173 return (LauncherRootView) mLauncherView;
1174 }
1175
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001176 @Override
Winson Chunga6945242014-01-08 14:04:34 -08001177 public DragLayer getDragLayer() {
1178 return mDragLayer;
1179 }
1180
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001181 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001182 return mAppsView;
1183 }
1184
Winson Chunga6945242014-01-08 14:04:34 -08001185 public Workspace getWorkspace() {
1186 return mWorkspace;
1187 }
1188
1189 public Hotseat getHotseat() {
1190 return mHotseat;
1191 }
1192
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001193 public <T extends View> T getOverviewPanel() {
Winson Chung8ae41982017-11-10 11:42:13 -08001194 return (T) mOverviewPanel;
Winson Chunga6945242014-01-08 14:04:34 -08001195 }
1196
Sunny Goyal47328fd2016-05-25 18:56:41 -07001197 public DropTargetBar getDropTargetBar() {
1198 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001199 }
1200
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001201 public LauncherAppWidgetHost getAppWidgetHost() {
1202 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001203 }
Romain Guycbb89e42009-06-08 15:52:54 -07001204
Winson Chunga9abd0e2010-10-27 17:18:37 -07001205 public LauncherModel getModel() {
1206 return mModel;
1207 }
1208
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001209 public ModelWriter getModelWriter() {
1210 return mModelWriter;
1211 }
1212
Adam Cohen79d90c52016-04-22 13:29:20 -07001213 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001214 return mSharedPrefs;
1215 }
1216
Sunny Goyalf8d56fc2018-01-31 15:18:11 -08001217 public int getOrientation() { return mOldConfig.orientation; }
Jon Miranda6bed3502017-09-19 14:43:17 -07001218
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001219 @Override
1220 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001221 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001222 super.onNewIntent(intent);
1223
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001224 boolean alreadyOnHome = hasWindowFocus() && ((intent.getFlags() &
Winson15f8b172015-08-19 11:02:39 -07001225 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1226 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001227
1228 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001229 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001230 && AbstractFloatingView.getTopOpenView(this) == null;
Winson15f8b172015-08-19 11:02:39 -07001231 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001232 boolean internalStateHandled = InternalStateHandler
Sunny Goyal8b264562018-02-03 23:00:13 -08001233 .handleNewIntent(this, intent, isStarted());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001234
Winson15f8b172015-08-19 11:02:39 -07001235 if (isActionMain) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001236 if (!internalStateHandled) {
1237 // Note: There should be at most one log per method call. This is enforced
1238 // implicitly by using if-else statements.
1239 UserEventDispatcher ued = getUserEventDispatcher();
1240 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
1241 if (topOpenView != null) {
1242 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
1243 } else if (alreadyOnHome) {
1244 Target target = newContainerTarget(mStateManager.getState().containerType);
1245 target.pageIndex = mWorkspace.getCurrentPage();
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001246 ued.logActionCommand(Action.Command.HOME_INTENT, target,
1247 newContainerTarget(ContainerType.WORKSPACE));
Sunny Goyald3864fa2017-11-14 15:06:36 -08001248 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001249
Sunny Goyald3864fa2017-11-14 15:06:36 -08001250 // In all these cases, only animate if we're already on home
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001251 AbstractFloatingView.closeAllOpenViews(this, isStarted());
Jon Mirandafeba90f2016-10-06 10:53:29 -07001252
Sunny Goyala2467272018-03-16 14:53:05 -07001253 if (!isInState(NORMAL)) {
1254 // Only change state, if not already the same. This prevents cancelling any
1255 // animations running as part of resume
1256 mStateManager.goToState(NORMAL);
1257 }
Sunny Goyald3864fa2017-11-14 15:06:36 -08001258
1259 // Reset the apps view
1260 if (!alreadyOnHome && mAppsView != null) {
1261 mAppsView.reset();
1262 }
1263
1264 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()) {
1265 mWorkspace.post(mWorkspace::moveToDefaultScreen);
1266 }
1267 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001268
1269 final View v = getWindow().peekDecorView();
1270 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001271 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001272 }
1273
Adam Cohen9211d422014-10-07 18:14:53 -07001274 if (mLauncherCallbacks != null) {
1275 mLauncherCallbacks.onHomeIntent();
1276 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001277 }
Winson15f8b172015-08-19 11:02:39 -07001278
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001279 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001280 }
1281
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001282 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001283 public void onRestoreInstanceState(Bundle state) {
1284 super.onRestoreInstanceState(state);
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001285 mWorkspace.restoreInstanceStateForChild(mSynchronouslyBoundPage);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001286 }
1287
1288 @Override
1289 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001290 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001291 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001292
Adam Cohen21cd0022013-10-09 18:57:02 -07001293 }
Sunny Goyalea609262017-10-25 15:47:38 -07001294 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001295
1296
1297 AbstractFloatingView widgets = AbstractFloatingView
1298 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1299 if (widgets != null) {
1300 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1301 widgets.saveHierarchyState(widgetsState);
1302 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1303 } else {
1304 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1305 }
1306
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001307 // We close any open folders and shortcut containers since they will not be re-opened,
1308 // and we need to make sure this state is reflected.
1309 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001310
Sunny Goyal2100c782016-08-22 16:00:03 -07001311 if (mPendingRequestArgs != null) {
1312 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1313 }
1314 if (mPendingActivityResult != null) {
1315 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001316 }
1317
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001318 super.onSaveInstanceState(outState);
1319
Adam Cohen9211d422014-10-07 18:14:53 -07001320 if (mLauncherCallbacks != null) {
1321 mLauncherCallbacks.onSaveInstanceState(outState);
1322 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001323 }
1324
1325 @Override
1326 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001327 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001328
Sunny Goyal5cc7bbd2017-06-12 09:50:39 -07001329 unregisterReceiver(mReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001330 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001331
Winson Chungcd2b0142011-06-08 16:02:26 -07001332 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001333 // It's possible to receive onDestroy after a new Launcher activity has
1334 // been created. In this case, don't interfere with the new Launcher.
1335 if (mModel.isCurrentCallbacks(this)) {
1336 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001337 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001338 }
Sunny Goyal623eddd2018-03-02 12:24:41 -08001339 mRotationHelper.destroy();
Sunny Goyal745bad92016-05-02 10:54:12 -07001340
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001341 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001342 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001343 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001344 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001345 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001346
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001347 TextKeyListener.getInstance().release();
Mario Bertschler27288382017-05-24 15:35:09 -07001348 WallpaperColorInfo.getInstance(this).setOnThemeChangeListener(null);
1349
Michael Jurka2ecf9952012-06-18 12:52:28 -07001350 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001351
Tony2917a8b2017-07-31 23:29:42 -07001352 clearPendingBinds();
1353
Adam Cohen9211d422014-10-07 18:14:53 -07001354 if (mLauncherCallbacks != null) {
1355 mLauncherCallbacks.onDestroy();
1356 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001357 }
1358
Sunny Goyalae502842016-06-17 08:43:56 -07001359 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1360 return mAccessibilityDelegate;
1361 }
1362
Adam Cohena9cf38f2011-05-02 15:36:58 -07001363 public DragController getDragController() {
1364 return mDragController;
1365 }
1366
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001367 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001368 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001369 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001370 }
1371
1372 @Override
1373 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1374 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001375 try {
1376 super.startIntentSenderForResult(intent, requestCode,
1377 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1378 } catch (IntentSender.SendIntentException e) {
1379 throw new ActivityNotFoundException();
1380 }
1381 }
1382
Winson Chung70d72102011-08-12 11:24:35 -07001383 /**
1384 * Indicates that we want global search for this activity by setting the globalSearch
1385 * argument for {@link #startSearch} to true.
1386 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001387 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001388 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001389 Bundle appSearchData, boolean globalSearch) {
1390 if (appSearchData == null) {
1391 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001392 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001393 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001394
Sunny Goyal6f28e712016-09-13 14:19:29 -07001395 if (mLauncherCallbacks == null ||
1396 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1397 // Starting search from the callbacks failed. Start the default global search.
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001398 super.startSearch(initialQuery, selectInitialQuery, appSearchData, true);
Ian Parkinson047036e2014-09-01 15:40:53 +01001399 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001400
1401 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001402 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001403 }
1404
Joe Onorato9c1289c2009-08-17 11:03:03 -04001405 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001406 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001407 }
1408
Adam Cohen517a7f52014-03-01 12:12:59 -08001409 public boolean isWorkspaceLoading() {
1410 return mWorkspaceLoading;
1411 }
1412
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001413 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001414 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001415 }
1416
Sunny Goyal04a324a2017-01-20 21:08:59 -08001417 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001418 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001419 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001420
Sunny Goyal2100c782016-08-22 16:00:03 -07001421 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001422 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001423 if (LOGD) {
1424 Log.d(TAG, "Adding widget from drop");
1425 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001426 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001427 }
1428
Sunny Goyal2100c782016-08-22 16:00:03 -07001429 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001430 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1431 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1432 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001433
Adam Cohenad4e15c2013-10-17 16:21:35 -07001434 Runnable onComplete = new Runnable() {
1435 @Override
1436 public void run() {
1437 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001438 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001439 }
1440 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001441 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001442 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001443 }
1444 }
Romain Guycbb89e42009-06-08 15:52:54 -07001445
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001446 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1447 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001448 info.container = container;
1449 info.screenId = screenId;
1450 if (cell != null) {
1451 info.cellX = cell[0];
1452 info.cellY = cell[1];
1453 }
1454 info.spanX = spanX;
1455 info.spanY = spanY;
1456
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001457 switch (info.itemType) {
1458 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1459 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001460 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001461 break;
1462 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001463 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001464 break;
1465 default:
1466 throw new IllegalStateException("Unknown item type: " + info.itemType);
1467 }
1468 }
1469
Adam Cohenfbba09b2011-07-18 21:43:05 -07001470 /**
1471 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001472 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001473 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001474 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1475 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001476 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1477 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1478 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001479 }
1480
Adam Cohenfbba09b2011-07-18 21:43:05 -07001481 /**
1482 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001483 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001484 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001485 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001486 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001487 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001488 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001489 // In the case where we've prebound the widget, we remove it from the DragLayer
1490 if (LOGD) {
1491 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1492 }
1493 getDragLayer().removeView(hostView);
1494
Adam Cohened66b2b2012-01-23 17:28:51 -08001495 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001496 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001497
1498 // Clear the boundWidget so that it doesn't get destroyed.
1499 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001500 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001501 // In this case, we either need to start an activity to get permission to bind
1502 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001503 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1504 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1505 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1506 this, info.componentName);
1507 } else {
1508 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1509 }
Adam Cohendd70d662012-10-04 16:53:44 -07001510 Bundle options = info.bindOptions;
1511
Sunny Goyalffe83f12014-08-14 17:39:34 -07001512 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1513 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001514 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001515 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001516 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001517 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001518 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001519 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001520 }
1521
Adam Cohendcd297f2013-06-18 13:13:40 -07001522 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001523 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001524 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001525 folderInfo.title = getText(R.string.folder_name);
1526
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001527 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001528 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001529
1530 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001531 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301532 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001533 // Force measure the new folder icon
1534 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1535 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001536 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001537 }
Romain Guycbb89e42009-06-08 15:52:54 -07001538
Winsonc0b52fe2015-09-09 16:38:15 -07001539 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001540 * Unbinds the view for the specified item, and removes the item and all its children.
1541 *
1542 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001543 * @param itemInfo the {@link ItemInfo} for this view.
1544 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001545 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001546 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001547 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001548 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001549 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1550 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001551 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001552 } else {
1553 mWorkspace.removeWorkspaceItem(v);
1554 }
Winsonc0b52fe2015-09-09 16:38:15 -07001555 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001556 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001557 }
1558 } else if (itemInfo instanceof FolderInfo) {
1559 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001560 if (v instanceof FolderIcon) {
1561 ((FolderIcon) v).removeListeners();
1562 }
Winsonc0b52fe2015-09-09 16:38:15 -07001563 mWorkspace.removeWorkspaceItem(v);
1564 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001565 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001566 }
1567 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1568 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001569 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001570 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07001571 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001572 }
1573 } else {
1574 return false;
1575 }
1576 return true;
1577 }
1578
1579 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07001580 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07001581 */
Sunny Goyal56c73602015-09-25 12:55:01 -07001582 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07001583 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07001584 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07001585 // Deleting an app widget ID is a void call but writes to disk before returning
1586 // to the caller...
1587 new AsyncTask<Void, Void, Void>() {
1588 public Void doInBackground(Void ... args) {
1589 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
1590 return null;
1591 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07001592 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07001593 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001594 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001595 }
1596
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001597 @Override
1598 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001599 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001600 }
1601
Joe Onorato88ec0992009-11-19 13:16:06 -08001602 @Override
1603 public void onBackPressed() {
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08001604 if (finishAutoCancelActionMode()) {
1605 return;
1606 }
Adam Cohen9211d422014-10-07 18:14:53 -07001607 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1608 return;
1609 }
1610
Sunny Goyal45478022015-06-08 16:52:41 -07001611 if (mDragController.isDragging()) {
1612 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001613 return;
1614 }
1615
Jon Mirandafeba90f2016-10-06 10:53:29 -07001616 // Note: There should be at most one log per method call. This is enforced implicitly
1617 // by using if-else statements.
1618 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001619 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
1620 if (topView != null) {
Sunny Goyal37920962017-09-28 13:43:24 -07001621 topView.onBackPressed();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001622 } else if (!isInState(NORMAL)) {
Sunny Goyal49bcf342018-01-11 13:59:53 -08001623 LauncherState lastState = mStateManager.getLastState();
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001624 ued.logActionCommand(Action.Command.BACK, mStateManager.getState().containerType,
1625 lastState.containerType);
Sunny Goyal49bcf342018-01-11 13:59:53 -08001626 mStateManager.goToState(lastState);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001627 } else {
1628 // Back button is a no-op here, but give at least some feedback for the button press
1629 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001630 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001631 }
1632
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001633 /**
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001634 * Event handler for the wallpaper picker button that appears after a long press
1635 * on the home screen.
1636 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001637 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07001638 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07001639 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
1640 return;
1641 }
1642
Tony Wickham785f7a52015-08-31 17:28:32 -07001643 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
1644 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal2100c782016-08-22 16:00:03 -07001645 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001646 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001647 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001648
1649 String pickerPackage = getString(R.string.wallpaper_picker_package);
Tony85efb4b2017-06-02 13:06:10 -07001650 boolean hasTargetPackage = !TextUtils.isEmpty(pickerPackage);
1651 if (hasTargetPackage) {
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001652 intent.setPackage(pickerPackage);
1653 }
1654
Sunny Goyala5ace712017-11-15 17:12:51 -08001655 final Bundle launchOptions;
1656 if (v != null) {
1657 intent.setSourceBounds(getViewBounds(v));
1658 // If there is no target package, use the default intent chooser animation
Jon Miranda17940f02018-01-18 14:46:34 -08001659 launchOptions = hasTargetPackage
Tony Wickham005df0b2018-02-13 11:28:12 -08001660 ? getActivityLaunchOptionsAsBundle(v, isInMultiWindowModeCompat())
Jon Miranda17940f02018-01-18 14:46:34 -08001661 : null;
Sunny Goyala5ace712017-11-15 17:12:51 -08001662 } else {
1663 launchOptions = null;
1664 }
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001665 try {
Sunny Goyala5ace712017-11-15 17:12:51 -08001666 startActivityForResult(intent, REQUEST_PICK_WALLPAPER, launchOptions);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001667 } catch (ActivityNotFoundException e) {
1668 setWaitingForResult(null);
1669 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1670 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001671 }
1672
Sunny Goyalfe770c92016-09-27 14:38:58 -07001673 @TargetApi(Build.VERSION_CODES.M)
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001674 @Override
Tony Wickham005df0b2018-02-13 11:28:12 -08001675 public ActivityOptions getActivityLaunchOptions(View v, boolean useDefaultLaunchOptions) {
Jon Miranda17940f02018-01-18 14:46:34 -08001676 return useDefaultLaunchOptions
Jon Miranda54441f52018-01-24 15:38:25 -08001677 ? mAppTransitionManager.getDefaultActivityLaunchOptions(this, v)
1678 : mAppTransitionManager.getActivityLaunchOptions(this, v);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001679 }
1680
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001681 @TargetApi(Build.VERSION_CODES.M)
1682 @Override
1683 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
1684 // Due to legacy reasons, direct call shortcuts require Launchers to have the
1685 // corresponding permission. Show the appropriate permission prompt if that
1686 // is the case.
1687 if (intent.getComponent() == null
1688 && Intent.ACTION_CALL.equals(intent.getAction())
1689 && checkSelfPermission(android.Manifest.permission.CALL_PHONE) !=
1690 PackageManager.PERMISSION_GRANTED) {
1691
1692 setWaitingForResult(PendingRequestArgs
1693 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
1694 requestPermissions(new String[]{android.Manifest.permission.CALL_PHONE},
1695 REQUEST_PERMISSION_CALL_PHONE);
1696 return true;
1697 } else {
1698 return false;
1699 }
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001700 }
1701
Sunny Goyala7ce1662016-05-31 15:01:35 -07001702 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001703 mAppLaunchSuccess = super.startActivitySafely(v, intent, item);
1704 if (mAppLaunchSuccess && v instanceof BubbleTextView) {
1705 // This is set to the view that launched the activity that navigated the user away
1706 // from launcher. Since there is no callback for when the activity has finished
1707 // launching, enable the press state and keep this reference to reset the press
1708 // state when we return to launcher.
1709 BubbleTextView btv = (BubbleTextView) v;
1710 btv.setStayPressed(true);
1711 setOnResumeCallback(btv);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001712 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07001713 return mAppLaunchSuccess;
Michael Jurka86a720e2012-05-09 11:23:23 -07001714 }
1715
Winson Chung3d503fb2011-07-13 17:25:49 -07001716 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001717 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07001718 return mHotseat != null && layout != null &&
1719 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
1720 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001721
Winson Chung3d503fb2011-07-13 17:25:49 -07001722 /**
1723 * Returns the CellLayout of the specified container at the specified screen.
1724 */
Sunny Goyal92820592015-03-02 11:31:35 -08001725 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001726 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1727 if (mHotseat != null) {
1728 return mHotseat.getLayout();
1729 } else {
1730 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08001731 }
Winson Chung3d503fb2011-07-13 17:25:49 -07001732 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08001733 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08001734 }
1735 }
1736
Michael Jurkae326f182011-11-21 14:05:46 -08001737 @Override
1738 public void onTrimMemory(int level) {
1739 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07001740 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
1741 // The widget preview db can result in holding onto over
1742 // 3MB of memory for caching which isn't necessary.
1743 SQLiteDatabase.releaseMemory();
1744
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08001745 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07001746 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08001747 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01001748 if (mLauncherCallbacks != null) {
1749 mLauncherCallbacks.onTrimMemory(level);
1750 }
Winson Chung62a70be2018-02-23 15:10:05 -08001751 UiFactory.onTrimMemory(this, level);
Michael Jurkae326f182011-11-21 14:05:46 -08001752 }
1753
Michael Jurkad7c28052012-04-27 15:43:36 -07001754 @Override
1755 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07001756 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07001757 final List<CharSequence> text = event.getText();
1758 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07001759 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001760 // TODO: When can workspace be null?
1761 text.add(mWorkspace == null
1762 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07001763 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07001764 return result;
1765 }
1766
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001767 public void setOnResumeCallback(OnResumeCallback callback) {
1768 if (mOnResumeCallback != null) {
1769 mOnResumeCallback.onLauncherResume();
1770 }
1771 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07001772 }
1773
Michael Jurka7607c2f2013-04-03 14:33:19 -07001774 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001775 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001776 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07001777 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001778 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07001779 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001780 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07001781 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07001782 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07001783 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001784 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001785
Joe Onorato9c1289c2009-08-17 11:03:03 -04001786 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07001787 * Clear any pending bind callbacks. This is called when is loader is planning to
1788 * perform a full rebind from scratch.
1789 */
1790 @Override
1791 public void clearPendingBinds() {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001792 if (mPendingExecutor != null) {
1793 mPendingExecutor.markCompleted();
1794 mPendingExecutor = null;
1795 }
1796 }
1797
1798 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001799 * Refreshes the shortcuts shown on the workspace.
1800 *
1801 * Implementation of the method from LauncherModel.Callbacks.
1802 */
1803 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001804 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001805 // Floating panels (except the full widget sheet) are associated with individual icons. If
1806 // we are starting a fresh bind, close all such panels as all the icons are about
1807 // to go away.
1808 AbstractFloatingView.closeOpenViews(this, true,
Sunny Goyal7ede6112017-12-05 15:11:21 -08001809 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_REBIND_SAFE);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08001810
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001811 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08001812
Winson Chungd64d1762013-08-20 14:37:16 -07001813 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07001814 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07001815 mWorkspace.removeAllWorkspaceScreens();
Sunny Goyalc11fac32018-02-27 19:20:15 -08001816 mAppWidgetHost.clearViews();
Winson Chungd64d1762013-08-20 14:37:16 -07001817
Winson Chung3d503fb2011-07-13 17:25:49 -07001818 if (mHotseat != null) {
1819 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001820 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001821 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001822 }
1823
Adam Cohendcd297f2013-06-18 13:13:40 -07001824 @Override
1825 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001826 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001827 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
1828 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001829 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
1830 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001831 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001832 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
1833 // If there are no screens, we need to have an empty screen
1834 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07001835 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001836 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07001837
Winsonc7d2e832016-07-28 12:24:55 -07001838 // After we have added all the screens, if the wallpaper was locked to the default state,
1839 // then notify to indicate that it can be released and a proper wallpaper offset can be
1840 // computed before the next layout
1841 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07001842 }
1843
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001844 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001845 int count = orderedScreenIds.size();
1846 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001847 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001848 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001849 // No need to bind the first screen, as its always bound.
1850 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
1851 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001852 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001853 }
1854
Sunny Goyalb23980c2017-08-17 07:45:25 -07001855 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08001856 public void bindAppsAdded(ArrayList<Long> newScreens, ArrayList<ItemInfo> addNotAnimated,
1857 ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07001858 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08001859 if (newScreens != null) {
1860 bindAddScreens(newScreens);
1861 }
Winson Chungd64d1762013-08-20 14:37:16 -07001862
1863 // We add the items without animation on non-visible pages, and with
1864 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08001865 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001866 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001867 }
Adam Cohen76a47a12014-02-05 11:47:43 -08001868 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001869 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07001870 }
Winson Chungc58497e2013-09-03 17:48:37 -07001871
Winson Chung87412982013-10-03 18:34:14 -07001872 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07001873 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001874 }
1875
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001876 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001877 * Bind the items start-end from the list.
1878 *
1879 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001880 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07001881 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07001882 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001883 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07001884 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001885 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07001886 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001887 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001888 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07001889 int end = items.size();
1890 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001891 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07001892
1893 // Short circuit if we are loading dock items for a configuration which has no dock
1894 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
1895 mHotseat == null) {
1896 continue;
1897 }
1898
Sunny Goyal639e9062015-08-19 19:17:06 -07001899 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001900 switch (item.itemType) {
1901 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1902 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08001903 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001904 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07001905 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001906 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001907 }
1908 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07001909 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001910 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001911 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001912 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001913 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07001914 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1915 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07001916 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07001917 if (view == null) {
1918 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001919 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08001920 break;
1921 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001922 default:
1923 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001924 }
Sunny Goyal639e9062015-08-19 19:17:06 -07001925
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001926 /*
1927 * Remove colliding items.
1928 */
1929 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1930 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
1931 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
1932 View v = cl.getChildAt(item.cellX, item.cellY);
1933 Object tag = v.getTag();
1934 String desc = "Collision while binding workspace item: " + item
1935 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08001936 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001937 throw (new RuntimeException(desc));
1938 } else {
1939 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001940 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001941 continue;
1942 }
1943 }
1944 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05301945 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07001946 if (animateIcons) {
1947 // Animate all the applications up now
1948 view.setAlpha(0f);
1949 view.setScaleX(0f);
1950 view.setScaleY(0f);
1951 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08001952 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07001953 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001954 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07001955
Winson Chung997a9232013-07-24 15:33:46 -07001956 if (animateIcons) {
1957 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08001958 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07001959 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08001960 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07001961 final Runnable startBounceAnimRunnable = new Runnable() {
1962 public void run() {
1963 anim.playTogether(bounceAnims);
1964 anim.start();
1965 }
1966 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08001967 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07001968 // We post the animation slightly delayed to prevent slowdowns
1969 // when we are loading right after we return to launcher.
1970 mWorkspace.postDelayed(new Runnable() {
1971 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00001972 if (mWorkspace != null) {
1973 mWorkspace.snapToPage(newScreenIndex);
1974 mWorkspace.postDelayed(startBounceAnimRunnable,
1975 NEW_APPS_ANIMATION_DELAY);
1976 }
Winson Chung94d67682013-09-25 16:29:40 -07001977 }
1978 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07001979 } else {
1980 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07001981 }
1982 }
Winson Chung64359a52013-07-08 17:17:08 -07001983 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001984 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001985 }
1986
Joe Onorato9c1289c2009-08-17 11:03:03 -04001987 /**
1988 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001989 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07001990 public void bindAppWidget(LauncherAppWidgetInfo item) {
1991 View view = inflateAppWidget(item);
1992 if (view != null) {
1993 mWorkspace.addInScreen(view, item);
1994 mWorkspace.requestLayout();
1995 }
1996 }
1997
1998 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001999 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302000 PendingAppWidgetHostView view =
2001 new PendingAppWidgetHostView(this, item, mIconCache, true);
2002 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002003 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002004 }
2005
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002006 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002007
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002008 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08002009
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002010 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2011 // If the provider is not ready, bind as a pending widget.
2012 appWidgetInfo = null;
2013 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2014 // The widget id is not valid. Try to find the widget based on the provider info.
2015 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
2016 } else {
2017 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
2018 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002019
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002020 // If the provider is ready, but the width is not yet restored, try to restore it.
2021 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
2022 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07002023 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002024 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
2025 + " belongs to component " + item.providerName
2026 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002027 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002028 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002029 }
Sunny Goyalff572272014-07-23 13:58:07 -07002030
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002031 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002032 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07002033 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2034 // Id has not been allocated yet. Allocate a new id.
2035 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
2036 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07002037
Sunny Goyald478c832016-04-01 12:04:16 -07002038 // Also try to bind the widget. If the bind fails, the user will be shown
2039 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08002040 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07002041 pendingInfo.spanX = item.spanX;
2042 pendingInfo.spanY = item.spanY;
2043 pendingInfo.minSpanX = item.minSpanX;
2044 pendingInfo.minSpanY = item.minSpanY;
2045 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07002046
2047 boolean isDirectConfig =
2048 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2049 if (isDirectConfig && item.bindOptions != null) {
2050 Bundle newOptions = item.bindOptions.getExtras();
2051 if (options != null) {
2052 newOptions.putAll(options);
2053 }
2054 options = newOptions;
2055 }
Sunny Goyald478c832016-04-01 12:04:16 -07002056 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2057 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002058
Sunny Goyal86df1382016-08-10 15:03:22 -07002059 // We tried to bind once. If we were not able to bind, we would need to
2060 // go through the permission dialog, which means we cannot skip the config
2061 // activity.
2062 item.bindOptions = null;
2063 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2064
Sunny Goyald478c832016-04-01 12:04:16 -07002065 // Bind succeeded
2066 if (success) {
2067 // If the widget has a configure activity, it is still needs to set it up,
2068 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002069 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002070 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2071 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002072 }
Sunny Goyald478c832016-04-01 12:04:16 -07002073
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002074 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002075 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002076 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002077 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002078 // The widget was marked as UI not ready, but there is no configure activity to
2079 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002080 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002081 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002082 }
Sunny Goyalff572272014-07-23 13:58:07 -07002083 }
2084
Sunny Goyald5462aa2016-11-22 16:52:39 +05302085 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002086 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002087 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002088 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002089 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07002090 deleteWidgetInfo(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002091 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002092 }
2093
Sunny Goyal233ee962015-08-03 13:05:01 -07002094 item.minSpanX = appWidgetInfo.minSpanX;
2095 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302096 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002097 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302098 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002099 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302100 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002101
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002102 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002103 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002104 }
2105
Sunny Goyalff572272014-07-23 13:58:07 -07002106 /**
2107 * Restores a pending widget.
2108 *
2109 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002110 */
Sunny Goyald478c832016-04-01 12:04:16 -07002111 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002112 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2113 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2114 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002115 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002116 }
2117
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002118 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002119 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002120 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2121 info.pendingItemInfo = null;
2122 }
Sunny Goyalff572272014-07-23 13:58:07 -07002123
Sunny Goyalba47faa2017-10-04 16:50:57 -07002124 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002125 view.reInflate();
Sunny Goyalba47faa2017-10-04 16:50:57 -07002126 }
2127
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002128 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002129 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002130 }
2131
Adam Cohen1462de32012-07-24 22:34:36 -07002132 public void onPageBoundSynchronously(int page) {
Sunny Goyalf4aceab2017-12-06 16:16:31 -08002133 mSynchronouslyBoundPage = page;
Adam Cohen1462de32012-07-24 22:34:36 -07002134 }
2135
Sunny Goyal527c7d32015-08-28 15:19:36 -07002136 @Override
2137 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2138 if (mPendingExecutor != null) {
2139 mPendingExecutor.markCompleted();
2140 }
2141 mPendingExecutor = executor;
Sunny Goyal60180b02018-02-07 12:56:30 -08002142 if (!isInState(ALL_APPS)) {
2143 mAppsView.getAppsStore().setDeferUpdates(true);
2144 mPendingExecutor.execute(() -> mAppsView.getAppsStore().setDeferUpdates(false));
2145 }
2146
Sunny Goyal527c7d32015-08-28 15:19:36 -07002147 executor.attachTo(this);
2148 }
2149
2150 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2151 if (mPendingExecutor == executor) {
2152 mPendingExecutor = null;
2153 }
2154 }
2155
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002156 @Override
2157 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002158 if (mDragLayer.getAlpha() < 1) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002159 mDragLayer.animate().alpha(1).withEndAction(
2160 executor == null ? null : executor::onLoadAnimationCompleted).start();
2161 } else if (executor != null) {
2162 executor.onLoadAnimationCompleted();
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002163 }
2164 }
2165
Joe Onorato9c1289c2009-08-17 11:03:03 -04002166 /**
2167 * Callback saying that there aren't any more items to bind.
2168 *
2169 * Implementation of the method from LauncherModel.Callbacks.
2170 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002171 public void finishBindingItems() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002172 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002173 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002174
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002175 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002176
Sunny Goyal2100c782016-08-22 16:00:03 -07002177 if (mPendingActivityResult != null) {
2178 handleActivityResult(mPendingActivityResult.requestCode,
2179 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2180 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002181 }
2182
Sunny Goyala474a9b2017-05-04 16:47:11 -07002183 InstallShortcutReceiver.disableAndFlushInstallQueue(
2184 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002185
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002186 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002187 }
2188
Winson Chunga2413752012-04-03 14:22:34 -07002189 private boolean canRunNewAppsAnimation() {
2190 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002191 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002192 }
2193
Winson Chungc9168342013-06-26 14:54:55 -07002194 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002195 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07002196 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
2197 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002198 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002199 return bounceAnim;
2200 }
2201
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002202 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002203 * Add the icons for all apps.
2204 *
2205 * Implementation of the method from LauncherModel.Callbacks.
2206 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002207 public void bindAllApplications(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002208 mAppsView.getAppsStore().setApps(apps);
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002209
Adam Cohen9211d422014-10-07 18:14:53 -07002210 if (mLauncherCallbacks != null) {
2211 mLauncherCallbacks.bindAllApplications(apps);
2212 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002213 }
2214
2215 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002216 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
2217 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2218 */
2219 @Override
2220 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002221 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002222 }
2223
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002224 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002225 * A package was updated.
2226 *
2227 * Implementation of the method from LauncherModel.Callbacks.
2228 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002229 @Override
2230 public void bindAppsAddedOrUpdated(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002231 mAppsView.getAppsStore().addOrUpdateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002232 }
2233
Sunny Goyal4390ace2014-10-13 11:33:11 -07002234 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002235 public void bindPromiseAppProgressUpdated(PromiseAppInfo app) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002236 mAppsView.getAppsStore().updatePromiseAppProgress(app);
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002237 }
2238
2239 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002240 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002241 mWorkspace.widgetsRestored(widgets);
2242 }
2243
Joe Onorato9c1289c2009-08-17 11:03:03 -04002244 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002245 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002246 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002247 *
2248 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002249 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002250 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002251 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002252 if (!updated.isEmpty()) {
2253 mWorkspace.updateShortcuts(updated);
2254 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002255 }
2256
2257 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002258 * Update the state of a package, typically related to install state.
2259 *
2260 * Implementation of the method from LauncherModel.Callbacks.
2261 */
Sunny Goyale755d462014-07-22 13:48:29 -07002262 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002263 public void bindRestoreItemsChange(HashSet<ItemInfo> updates) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07002264 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07002265 }
2266
2267 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002268 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07002269 * in addition to specific applications to remove, the reason being that
2270 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002271 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07002272 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002273 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07002274 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002275 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002276 mWorkspace.removeItemsByMatcher(matcher);
2277 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002278 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002279
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002280 @Override
2281 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002282 mAppsView.getAppsStore().removeApps(appInfos);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002283 }
Joe Onoratobe386092009-11-17 17:32:16 -08002284
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002285 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002286 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
2287 mPopupDataProvider.setAllWidgets(allWidgets);
Tony Wickham26b17462017-03-20 17:12:24 -07002288 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
2289 if (topView != null) {
2290 topView.onWidgetsBound();
2291 }
2292 }
2293
Tony Wickham86222d22017-03-29 15:30:43 -07002294 /**
2295 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
2296 * refreshes the widgets and shortcuts associated with the given package/user
2297 */
2298 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07002299 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002300 }
2301
Winson Chung80baf5a2010-08-09 16:03:15 -07002302 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002303 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08002304 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002305 @Override
2306 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
2307 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002308
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002309 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
2310 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07002311 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002312 writer.println(prefix + " Homescreen " + i);
2313
2314 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
2315 for (int j = 0; j < layout.getChildCount(); j++) {
2316 Object tag = layout.getChildAt(j).getTag();
2317 if (tag != null) {
2318 writer.println(prefix + " " + tag.toString());
2319 }
2320 }
2321 }
2322
2323 writer.println(prefix + " Hotseat");
2324 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07002325 for (int j = 0; j < layout.getChildCount(); j++) {
2326 Object tag = layout.getChildAt(j).getTag();
2327 if (tag != null) {
2328 writer.println(prefix + " " + tag.toString());
2329 }
Adam Cohen4caf2982013-08-20 18:54:31 -07002330 }
Sunny Goyala1365452015-10-01 15:46:24 -07002331 }
2332
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002333 writer.println(prefix + "Misc:");
2334 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
2335 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
2336 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
Sunny Goyal2bba1902018-02-27 12:20:50 -08002337 writer.println(" deviceProfile isTransposed=" + getDeviceProfile().isVerticalBarLayout());
2338 writer.println(" orientation=" + getResources().getConfiguration().orientation);
2339
2340 try {
2341 FileLog.flushAll(writer);
2342 } catch (Exception e) {
2343 // Ignore
2344 }
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002345
2346 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002347
Adam Cohen9211d422014-10-07 18:14:53 -07002348 if (mLauncherCallbacks != null) {
2349 mLauncherCallbacks.dump(prefix, fd, writer, args);
2350 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002351 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002352
Sunny Goyal66b24572016-09-21 15:57:55 -07002353 @Override
2354 @TargetApi(Build.VERSION_CODES.N)
2355 public void onProvideKeyboardShortcuts(
2356 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
2357
2358 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002359 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002360 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
2361 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
2362 }
2363 View currentFocus = getCurrentFocus();
2364 if (new CustomActionsPopup(this, currentFocus).canShow()) {
2365 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
2366 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
2367 }
Tony18c4aa42017-05-10 21:23:57 -05002368 if (currentFocus.getTag() instanceof ItemInfo
2369 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002370 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
2371 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
2372 }
2373 if (!shortcutInfos.isEmpty()) {
2374 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
2375 }
2376
2377 super.onProvideKeyboardShortcuts(data, menu, deviceId);
2378 }
2379
2380 @Override
2381 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
2382 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
2383 switch (keyCode) {
2384 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002385 if (isInState(NORMAL)) {
2386 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07002387 return true;
2388 }
2389 break;
2390 case KeyEvent.KEYCODE_S: {
2391 View focusedView = getCurrentFocus();
2392 if (focusedView instanceof BubbleTextView
2393 && focusedView.getTag() instanceof ItemInfo
2394 && mAccessibilityDelegate.performAction(focusedView,
2395 (ItemInfo) focusedView.getTag(),
2396 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal00ac9202017-11-09 15:24:06 -08002397 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07002398 return true;
2399 }
2400 break;
2401 }
2402 case KeyEvent.KEYCODE_O:
2403 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
2404 return true;
2405 }
2406 break;
2407 }
2408 }
2409 return super.onKeyShortcut(keyCode, event);
2410 }
2411
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002412 @Override
2413 public boolean onKeyUp(int keyCode, KeyEvent event) {
2414 if (keyCode == KeyEvent.KEYCODE_MENU) {
2415 // KEYCODE_MENU is sent by some tests, for example
2416 // LauncherJankTests#testWidgetsContainerFling. Don't just remove its handling.
2417 if (!mDragController.isDragging() && !mWorkspace.isSwitchingState() &&
2418 isInState(NORMAL)) {
2419 // Close any open floating views.
2420 AbstractFloatingView.closeAllOpenViews(this);
2421
2422 // Setting the touch point to (-1, -1) will show the options popup in the center of
2423 // the screen.
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08002424 OptionsPopupView.show(this, -1, -1);
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002425 }
2426 return true;
2427 }
2428 return super.onKeyUp(keyCode, event);
2429 }
2430
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002431 public static Launcher getLauncher(Context context) {
2432 if (context instanceof Launcher) {
2433 return (Launcher) context;
2434 }
2435 return ((Launcher) ((ContextWrapper) context).getBaseContext());
2436 }
2437
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002438 /**
2439 * Callback for listening for onResume
2440 */
2441 public interface OnResumeCallback {
2442
2443 void onLauncherResume();
2444 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002445}