blob: b4f9409d534ecde2f28a5f05878bd766d8b4eb20 [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
Sunny Goyal28c6b962015-10-12 11:42:05 -070028import android.Manifest;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070029import android.animation.Animator;
Jon Miranda2d89ea82017-05-04 11:47:53 -070030import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070031import android.animation.AnimatorSet;
Jon Miranda2d89ea82017-05-04 11:47:53 -070032import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070033import android.animation.ValueAnimator;
Adam Cohen0b395352014-06-09 22:54:36 +000034import android.annotation.TargetApi;
Tony Wickham005df0b2018-02-13 11:28:12 -080035import android.app.ActivityOptions;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080036import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070037import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080039import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080040import android.content.ComponentCallbacks2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Context;
Andrew Sappersteinabef55a2016-06-19 12:49:00 -070042import android.content.ContextWrapper;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070044import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070045import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070046import android.content.SharedPreferences;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.content.pm.PackageManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080048import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070049import android.database.sqlite.SQLiteDatabase;
Jon Mirandacc42c5b2016-10-27 17:15:27 -070050import android.graphics.Point;
Michael Jurkaaf442092010-06-10 17:01:57 -070051import android.graphics.Rect;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070052import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020053import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054import android.os.Bundle;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070055import android.os.Parcelable;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080056import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070057import android.os.StrictMode;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070058import android.os.UserHandle;
Tony Wickham86222d22017-03-29 15:30:43 -070059import android.support.annotation.Nullable;
Michael Jurkaa33411c2012-06-14 16:18:21 -070060import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070062import android.util.Log;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070063import android.util.SparseArray;
Sunny Goyal7c8a65e2017-12-22 11:11:33 -080064import android.view.ActionMode;
Adam Cohen96d30a12013-07-16 18:13:21 -070065import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070066import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070067import android.view.KeyboardShortcutGroup;
68import android.view.KeyboardShortcutInfo;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080069import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -070070import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070071import android.view.View;
Adam Cohen96d30a12013-07-16 18:13:21 -070072import android.view.ViewGroup;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080073import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070074import android.view.animation.OvershootInterpolator;
Adam Cohen96d30a12013-07-16 18:13:21 -070075import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070076
Sunny Goyal651077b2014-06-30 14:15:31 -070077import com.android.launcher3.DropTarget.DragObject;
Sunny Goyala7ce1662016-05-31 15:01:35 -070078import com.android.launcher3.LauncherSettings.Favorites;
Jon Mirandac476d6e2017-05-04 16:30:01 -070079import com.android.launcher3.Workspace.ItemOperator;
Sunny Goyalae502842016-06-17 08:43:56 -070080import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070081import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070082import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal7ede6112017-12-05 15:11:21 -080083import com.android.launcher3.allapps.DiscoveryBounce;
Sunny Goyalffe83f12014-08-14 17:39:34 -070084import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010085import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -070086import com.android.launcher3.compat.LauncherAppsCompatVO;
Tony Wickhame0c33232016-02-08 11:37:04 -080087import com.android.launcher3.config.FeatureFlags;
Vadim Tryshevfedca432015-08-19 17:55:02 -070088import com.android.launcher3.dragndrop.DragController;
89import com.android.launcher3.dragndrop.DragLayer;
90import com.android.launcher3.dragndrop.DragView;
Mario Bertschler27288382017-05-24 15:35:09 -070091import com.android.launcher3.dynamicui.WallpaperColorInfo;
Sunny Goyal26119432016-02-18 22:09:23 +000092import com.android.launcher3.folder.FolderIcon;
Sunny Goyal66b24572016-09-21 15:57:55 -070093import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -070094import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -070095import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -070096import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyala535ae42017-02-27 10:07:13 -080097import com.android.launcher3.model.ModelWriter;
Sunny Goyala535ae42017-02-27 10:07:13 -080098import com.android.launcher3.notification.NotificationListener;
Tony Wickham540913e2017-01-23 11:47:51 -080099import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -0800100import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700101import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyalf8088ee2017-11-10 14:52:00 -0800102import com.android.launcher3.states.InternalStateHandler;
Sunny Goyal623eddd2018-03-02 12:24:41 -0800103import com.android.launcher3.states.RotationHelper;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800104import com.android.launcher3.touch.ItemClickHandler;
Sunny Goyala5ace712017-11-15 17:12:51 -0800105import com.android.launcher3.uioverrides.UiFactory;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530106import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
107import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
Sunny Goyalaeb16432017-10-16 11:46:41 -0700108import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyal2100c782016-08-22 16:00:03 -0700109import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700110import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700111import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700112import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700113import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800114import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700115import com.android.launcher3.util.PendingRequestArgs;
Sunny Goyal8392c822017-06-20 10:03:56 -0700116import com.android.launcher3.util.SystemUiController;
Mario Bertschlera6936942017-05-31 14:48:19 -0700117import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700118import com.android.launcher3.util.Thunk;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700119import com.android.launcher3.util.TraceHelper;
Sunny Goyal326403e2017-10-02 12:45:10 -0700120import com.android.launcher3.util.UiThreadHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700121import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800122import com.android.launcher3.views.OptionsPopupView;
Sunny Goyal29947f02017-12-18 13:49:44 -0800123import com.android.launcher3.widget.LauncherAppWidgetHostView;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800124import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700125import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal29947f02017-12-18 13:49:44 -0800126import com.android.launcher3.widget.PendingAppWidgetHostView;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800127import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700128import com.android.launcher3.widget.WidgetHostViewLoader;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700129import com.android.launcher3.widget.WidgetListRowEntry;
130import com.android.launcher3.widget.WidgetsFullSheet;
Sunny Goyal952e63d2017-08-16 04:59:08 -0700131import com.android.launcher3.widget.custom.CustomWidgetParser;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700132
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700133import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700134import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700135import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700136import java.util.Collection;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700137import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700138import java.util.List;
Tony Wickham010d2552017-01-20 08:15:28 -0800139import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700140
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141/**
142 * Default launcher application.
143 */
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800144public class Launcher extends BaseActivity implements LauncherExterns, LauncherModel.Callbacks,
145 LauncherProviderChangeListener, WallpaperColorInfo.OnThemeChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700146 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700147 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148
Winson Chunga2413752012-04-03 14:22:34 -0700149 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700150
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700152 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700153
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700154 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700155 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156
Michael Jurka8b805b12012-04-18 14:23:14 -0700157 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800158 public static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
159 public static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700160
Jon Mirandac476d6e2017-05-04 16:30:01 -0700161 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700162
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700163 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
164
Mathew Inwood876a8462013-06-14 14:12:41 +0100165 /**
166 * IntentStarter uses request codes starting with this. This must be greater than all activity
167 * request codes used internally.
168 */
169 protected static final int REQUEST_LAST = 100;
170
Winson Chung2672ff92012-05-04 16:22:30 -0700171 // The Intent extra that defines whether to ignore the launch animation
172 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400173 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700174
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 // Type: int
176 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700177 // Type: int
178 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700179 // Type: PendingRequestArgs
180 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
181 // Type: ActivityResultInfo
182 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700183 // Type: SparseArray<Parcelable>
184 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185
Sunny Goyal7c8a65e2017-12-22 11:11:33 -0800186 // When starting an action mode, setting this tag will cause the action mode to be cancelled
187 // automatically when user interacts with the launcher.
188 public static final Object AUTO_CANCEL_ACTION_MODE = new Object();
189
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700190 private LauncherStateManager mStateManager;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700191
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700192 private boolean mIsSafeModeEnabled;
193
Adam Cohenad4e15c2013-10-17 16:21:35 -0700194 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800195
Winson Chunga2413752012-04-03 14:22:34 -0700196 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700197 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
198 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
199 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700200
Jon Miranda54441f52018-01-24 15:38:25 -0800201 private LauncherAppTransitionManager mAppTransitionManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800202 private Configuration mOldConfig;
Jon Miranda54441f52018-01-24 15:38:25 -0800203
Adam Cohen091440a2015-03-18 14:16:05 -0700204 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700205 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700206 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800207 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700208
Sunny Goyalffe83f12014-08-14 17:39:34 -0700209 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700210 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700211
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700212 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700213
Sunny Goyal316490e2015-06-02 09:38:28 -0700214 @Thunk Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700215
Sunny Goyal47328fd2016-05-25 18:56:41 -0700216 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700217
218 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700219 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700220 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700221
Winson Chung8ae41982017-11-10 11:42:13 -0800222 // UI and state for the overview panel
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800223 private View mOverviewPanel;
Winson Chung8ae41982017-11-10 11:42:13 -0800224
Adam Cohen091440a2015-03-18 14:16:05 -0700225 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400226
Sunny Goyal60876ac2018-02-20 14:21:20 -0800227 private OnStartCallback mOnStartCallback;
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700228 private OnResumeCallback mOnResumeCallback;
229
Sunny Goyal527c7d32015-08-28 15:19:36 -0700230 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700231
Joe Onorato9c1289c2009-08-17 11:03:03 -0400232 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800233 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800234 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700235 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400236
Jon Miranda2d89ea82017-05-04 11:47:53 -0700237 private ObjectAnimator mScrimAnimator;
Jon Miranda7fda2852017-07-19 16:07:01 -0700238 private boolean mShouldFadeInScrim;
Jon Miranda2d89ea82017-05-04 11:47:53 -0700239
Tony Wickham010d2552017-01-20 08:15:28 -0800240 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700241
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800242 private int mSynchronouslyBoundPage = PagedView.INVALID_PAGE;
Adam Cohen1462de32012-07-24 22:34:36 -0700243
Winson Chung46353de2012-02-16 14:05:10 -0800244 // We only want to get the SharedPreferences once since it does an FS stat each time we get
245 // it from the context.
246 private SharedPreferences mSharedPrefs;
247
Sunny Goyal2100c782016-08-22 16:00:03 -0700248 // Activity result which needs to be processed after workspace has loaded.
249 private ActivityResultInfo mPendingActivityResult;
250 /**
251 * Holds extra information required to handle a result from an external call, like
252 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
253 */
254 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800255
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700256 public ViewGroupFocusHelper mFocusHandler;
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700257 private boolean mAppLaunchSuccess;
258
Sunny Goyal623eddd2018-03-02 12:24:41 -0800259 private RotationHelper mRotationHelper;
Sunny Goyal7c8a65e2017-12-22 11:11:33 -0800260 private ActionMode mCurrentActionMode;
Sunny Goyal7779d622015-06-11 16:18:39 -0700261
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800262 @Override
263 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700264 if (DEBUG_STRICT_MODE) {
265 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
266 .detectDiskReads()
267 .detectDiskWrites()
268 .detectNetwork() // or .detectAll() for all detectable problems
269 .penaltyLog()
270 .build());
271 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
272 .detectLeakedSqlLiteObjects()
273 .detectLeakedClosableObjects()
274 .penaltyLog()
275 .penaltyDeath()
276 .build());
277 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700278 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700279
Mario Bertschler27288382017-05-24 15:35:09 -0700280 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
281 wallpaperColorInfo.setOnThemeChangeListener(this);
Mario Bertschlera6936942017-05-31 14:48:19 -0700282 overrideTheme(wallpaperColorInfo.isDark(), wallpaperColorInfo.supportsDarkText());
Mario Bertschler27288382017-05-24 15:35:09 -0700283
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800284 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700285 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400286
Sunny Goyal87f784c2017-01-11 10:48:34 -0800287 LauncherAppState app = LauncherAppState.getInstance(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800288 mOldConfig = new Configuration(getResources().getConfiguration());
Sunny Goyal0e7724c2018-02-22 13:22:21 -0800289 mModel = app.setLauncher(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800290 initDeviceProfile(app.getInvariantDeviceProfile());
Winson Chung6e1c0d32013-10-25 15:24:24 -0700291
Sunny Goyalf7258242015-10-19 16:59:07 -0700292 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700293 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800294 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700295 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700296
Joe Onorato41a12d22009-10-31 18:30:00 -0400297 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700298 mAllAppsController = new AllAppsTransitionController(this);
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800299 mStateManager = new LauncherStateManager(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700300
Sunny Goyalffe83f12014-08-14 17:39:34 -0700301 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700302
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700303 mAppWidgetHost = new LauncherAppWidgetHost(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700304 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700305
Sunny Goyal60820d72017-05-09 12:40:11 -0700306 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700307
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800308 setupViews();
Tony Wickham010d2552017-01-20 08:15:28 -0800309 mPopupDataProvider = new PopupDataProvider(this);
310
Sunny Goyal623eddd2018-03-02 12:24:41 -0800311 mRotationHelper = new RotationHelper(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800312
Sunny Goyald3864fa2017-11-14 15:06:36 -0800313 boolean internalStateHandled = InternalStateHandler.handleCreate(this, getIntent());
314 if (internalStateHandled) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800315 if (savedInstanceState != null) {
316 // InternalStateHandler has already set the appropriate state.
317 // We dont need to do anything.
318 savedInstanceState.remove(RUNTIME_STATE);
319 }
320 }
321 restoreState(savedInstanceState);
Winson Chungb63b44c2017-11-10 17:54:44 -0800322
Sunny Goyal2100c782016-08-22 16:00:03 -0700323 // We only load the page synchronously if the user rotates (or triggers a
324 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700325 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
326 if (savedInstanceState != null) {
327 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
328 }
Sunny Goyald3864fa2017-11-14 15:06:36 -0800329
Sunny Goyalfe770c92016-09-27 14:38:58 -0700330 if (!mModel.startLoader(currentScreen)) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800331 if (!internalStateHandled) {
332 // If we are not binding synchronously, show a fade in animation when
333 // the first page bind completes.
334 mDragLayer.setAlpha(0);
335 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700336 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700337 // Pages bound synchronously.
338 mWorkspace.setCurrentPage(currentScreen);
339
Sunny Goyal2100c782016-08-22 16:00:03 -0700340 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800341 }
342
343 // For handling default keys
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800344 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800345
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800346 setContentView(mLauncherView);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800347 getRootView().dispatchInsets();
Mario Bertschlera6936942017-05-31 14:48:19 -0700348
Jon Miranda7fda2852017-07-19 16:07:01 -0700349 // Listen for broadcasts
350 IntentFilter filter = new IntentFilter();
351 filter.addAction(Intent.ACTION_SCREEN_OFF);
352 filter.addAction(Intent.ACTION_USER_PRESENT); // When the device wakes up + keyguard is gone
353 registerReceiver(mReceiver, filter);
354 mShouldFadeInScrim = true;
Sunny Goyala616d2b2017-06-12 13:54:01 -0700355
Sunny Goyal8392c822017-06-20 10:03:56 -0700356 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
357 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700358
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800359 mAppTransitionManager = LauncherAppTransitionManager.newInstance(this);
Jon Miranda7853bdb2018-01-22 17:32:30 -0800360
Sunny Goyal14b32402017-07-31 10:03:28 -0700361 if (mLauncherCallbacks != null) {
362 mLauncherCallbacks.onCreate(savedInstanceState);
363 }
Sunny Goyal623eddd2018-03-02 12:24:41 -0800364 mRotationHelper.initialize();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700365
366 TraceHelper.endSection("Launcher-onCreate");
Adam Cohen9211d422014-10-07 18:14:53 -0700367 }
368
Sunny Goyal7779d622015-06-11 16:18:39 -0700369 @Override
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800370 public void onConfigurationChanged(Configuration newConfig) {
371 int diff = newConfig.diff(mOldConfig);
372 if ((diff & (CONFIG_ORIENTATION | CONFIG_SCREEN_SIZE)) != 0) {
373 mUserEventDispatcher = null;
374 initDeviceProfile(mDeviceProfile.inv);
Sunny Goyal2bba1902018-02-27 12:20:50 -0800375 FileLog.d(TAG, "Config changed, my orientation=" +
376 getResources().getConfiguration().orientation +
377 ", new orientation=" + newConfig.orientation +
378 ", old orientation=" + mOldConfig.orientation +
379 ", isTransposed=" + mDeviceProfile.isVerticalBarLayout());
Sunny Goyalfde55052018-02-01 14:46:13 -0800380 dispatchDeviceProfileChanged();
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800381
382 getRootView().dispatchInsets();
383 getStateManager().reapplyState();
384
385 // TODO: We can probably avoid rebind when only screen size changed.
Sunny Goyal605bcf32018-03-02 15:16:12 -0800386 rebindModel();
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800387 }
388
389 mOldConfig.setTo(newConfig);
390 super.onConfigurationChanged(newConfig);
391 }
392
Sunny Goyal605bcf32018-03-02 15:16:12 -0800393 @Override
394 public void rebindModel() {
395 int currentPage = mWorkspace.getNextPage();
396 if (mModel.startLoader(currentPage)) {
397 mWorkspace.setCurrentPage(currentPage);
398 setWorkspaceLoading(true);
399 }
400 }
401
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800402 private void initDeviceProfile(InvariantDeviceProfile idp) {
403 // Load configuration-specific DeviceProfile
404 mDeviceProfile = idp.getDeviceProfile(this);
405 if (isInMultiWindowModeCompat()) {
406 Display display = getWindowManager().getDefaultDisplay();
407 Point mwSize = new Point();
408 display.getSize(mwSize);
409 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
410 }
Sunny Goyal605bcf32018-03-02 15:16:12 -0800411 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout(), true);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800412 }
413
Sunny Goyal623eddd2018-03-02 12:24:41 -0800414 public RotationHelper getRotationHelper() {
415 return mRotationHelper;
416 }
417
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800418 @Override
Mario Bertschler27288382017-05-24 15:35:09 -0700419 public void onThemeChanged() {
420 recreate();
421 }
422
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700423 public LauncherStateManager getStateManager() {
424 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700425 }
426
Tonydc76f8e2018-02-02 21:45:39 -0600427 public LauncherAppTransitionManager getAppTransitionManager() {
428 return mAppTransitionManager;
429 }
430
Mario Bertschlera6936942017-05-31 14:48:19 -0700431 protected void overrideTheme(boolean isDark, boolean supportsDarkText) {
Mario Bertschler27288382017-05-24 15:35:09 -0700432 if (isDark) {
433 setTheme(R.style.LauncherThemeDark);
Mario Bertschlera6936942017-05-31 14:48:19 -0700434 } else if (supportsDarkText) {
435 setTheme(R.style.LauncherThemeDarkText);
Mario Bertschler27288382017-05-24 15:35:09 -0700436 }
437 }
438
439 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700440 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800441 return mLauncherView.findViewById(id);
442 }
443
444 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700445 public void onAppWidgetHostReset() {
446 if (mAppWidgetHost != null) {
447 mAppWidgetHost.startListening();
448 }
449 }
450
Adam Cohen9211d422014-10-07 18:14:53 -0700451 private LauncherCallbacks mLauncherCallbacks;
452
Sunny Goyal32554d12015-12-03 15:31:25 -0800453 /**
454 * Call this after onCreate to set or clear overlay.
455 */
456 public void setLauncherOverlay(LauncherOverlay overlay) {
457 if (overlay != null) {
458 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
459 }
460 mWorkspace.setLauncherOverlay(overlay);
461 }
462
Adam Cohen9211d422014-10-07 18:14:53 -0700463 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
464 mLauncherCallbacks = callbacks;
465 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700466 }
467
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700468 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700469 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700470 if (mLauncherCallbacks != null) {
471 mLauncherCallbacks.onLauncherProviderChange();
472 }
473 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700474
Hyunyoung Song3f471442015-04-08 19:01:34 -0700475 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700476 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
477 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700478 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700479 }
480
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000481 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700482 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
483 // This cast is safe as long as the id < 0x00FFFFFF
484 // Since we jail all the dynamically generated views, there should be no clashes
485 // with any other views.
486 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000487 }
488
Tony Wickham010d2552017-01-20 08:15:28 -0800489 public PopupDataProvider getPopupDataProvider() {
490 return mPopupDataProvider;
491 }
492
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000493 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700494 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
495 * a configuration step, this allows the proper animations to run after other transitions.
496 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700497 private long completeAdd(
498 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
499 long screenId = info.screenId;
500 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700501 // When the screen id represents an actual screen (as opposed to a rank) we make sure
502 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700503 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700504 }
Adam Cohendb364c32014-05-20 14:23:40 -0700505
Sunny Goyal2100c782016-08-22 16:00:03 -0700506 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800507 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700508 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700509 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800510 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700511 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700512 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700513 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700514 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700515 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700516 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700517 int widgetId = appWidgetId;
518 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700519 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700520 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700521 // Since the view was just bound, also launch the configure activity if needed
522 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
523 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800524 if (provider != null) {
525 new WidgetAddFlowHandler(provider)
526 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700527 }
528 }
529 break;
530 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800531 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700532
Adam Cohendb364c32014-05-20 14:23:40 -0700533 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800534 }
535
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800536 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700537 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700538 if (isWorkspaceLoading()) {
539 // process the result once the workspace has loaded.
540 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
541 return;
542 }
543 mPendingActivityResult = null;
544
Winson Chunge341d302013-08-16 14:31:00 -0700545 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700546 final PendingRequestArgs requestArgs = mPendingRequestArgs;
547 setWaitingForResult(null);
548 if (requestArgs == null) {
549 return;
550 }
551
552 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700553
Adam Cohenad4e15c2013-10-17 16:21:35 -0700554 Runnable exitSpringLoaded = new Runnable() {
555 @Override
556 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700557 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700558 }
559 };
560
Michael Jurka8b805b12012-04-18 14:23:14 -0700561 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700562 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700563 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700564 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
565 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700566 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700567 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700568 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700569 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700570 addAppWidgetImpl(
571 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800572 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700573 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700574 }
575 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200576 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700577 if (resultCode == RESULT_OK && isInState(OVERVIEW)) {
Tony Wickhame3054412015-09-09 09:05:25 -0700578 // User could have free-scrolled between pages before picking a wallpaper; make sure
579 // we move to the closest one now.
580 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700581 mStateManager.goToState(NORMAL, false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200582 }
583 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700584 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200585
Adam Cohened66b2b2012-01-23 17:28:51 -0800586 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700587 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700588
Adam Cohened66b2b2012-01-23 17:28:51 -0800589 // We have special handling for widgets
590 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800591 final int appWidgetId;
592 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
593 : -1;
594 if (widgetId < 0) {
595 appWidgetId = pendingAddWidgetId;
596 } else {
597 appWidgetId = widgetId;
598 }
599
Adam Cohenad4e15c2013-10-17 16:21:35 -0700600 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800601 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700602 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
603 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700604 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700605 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700606 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700607 @Override
608 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700609 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700610 }
611 };
Adam Cohendb364c32014-05-20 14:23:40 -0700612
Sunny Goyal2100c782016-08-22 16:00:03 -0700613 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
614 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
615 } else {
616 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
617 // When the screen id represents an actual screen (as opposed to a rank)
618 // we make sure that the drop page actually exists.
619 requestArgs.screenId =
620 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700621 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700622 final CellLayout dropLayout =
623 mWorkspace.getScreenWithId(requestArgs.screenId);
624
625 dropLayout.setDropPending(true);
626 final Runnable onComplete = new Runnable() {
627 @Override
628 public void run() {
629 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
630 dropLayout.setDropPending(false);
631 }
632 };
633 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
634 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700635 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800636 return;
637 }
638
Sunny Goyald478c832016-04-01 12:04:16 -0700639 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
640 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700641 if (resultCode == RESULT_OK) {
642 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700643 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700644 }
645 // Leave the widget in the pending state if the user canceled the configure.
646 return;
647 }
648
Sunny Goyalaad90582015-07-09 14:22:50 -0700649 if (requestCode == REQUEST_CREATE_SHORTCUT) {
650 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700651 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
652 completeAdd(requestCode, data, -1, requestArgs);
653 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
654 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
655
Sunny Goyalaad90582015-07-09 14:22:50 -0700656 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700657 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
658 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800659 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800661 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800662 }
663
664 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700665 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800666 final int requestCode, final int resultCode, final Intent data) {
667 handleActivityResult(requestCode, resultCode, data);
668 if (mLauncherCallbacks != null) {
669 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
670 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800671 }
672
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700673 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700674 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600675 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700676 PendingRequestArgs pendingArgs = mPendingRequestArgs;
677 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
678 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
679 setWaitingForResult(null);
680
Sunny Goyal28c6b962015-10-12 11:42:05 -0700681 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700682 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700683 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700684 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700685 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700686 Intent intent = pendingArgs.getPendingIntent();
687
Sunny Goyal28c6b962015-10-12 11:42:05 -0700688 if (grantResults.length > 0
689 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700690 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700691 } else {
692 // TODO: Show a snack bar with link to settings
693 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700694 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700695 }
696 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600697 if (mLauncherCallbacks != null) {
698 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
699 grantResults);
700 }
701 }
702
Adam Cohendb364c32014-05-20 14:23:40 -0700703 /**
704 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
705 *
706 * @param screenId the screen id to check
707 * @return the new screen, or screenId if it exists
708 */
709 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800710 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700711 if (dropLayout == null) {
712 // it's possible that the add screen was removed because it was
713 // empty and a re-bind occurred
714 mWorkspace.addExtraEmptyScreen();
715 return mWorkspace.commitExtraEmptyScreen();
716 } else {
717 return screenId;
718 }
719 }
720
Sunny Goyal2100c782016-08-22 16:00:03 -0700721 @Thunk void completeTwoStageWidgetDrop(
722 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
723 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800724 Runnable onCompleteRunnable = null;
725 int animationType = 0;
726
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700727 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800728 if (resultCode == RESULT_OK) {
729 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
730 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800731 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700732 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800733 onCompleteRunnable = new Runnable() {
734 @Override
735 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700736 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700737 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800738 }
739 };
740 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800741 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800742 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800743 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700744 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700745 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700746 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
747 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700748 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700749 // The animated view may be null in the case of a rotation during widget configuration
750 onCompleteRunnable.run();
751 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800752 }
753
754 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700755 protected void onStop() {
756 super.onStop();
757 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700758
759 if (mLauncherCallbacks != null) {
760 mLauncherCallbacks.onStop();
761 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800762 mAppWidgetHost.setListenIfResumed(false);
Tony Wickham010d2552017-01-20 08:15:28 -0800763
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700764 if (!mAppLaunchSuccess) {
765 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -0800766 mStateManager.getState().containerType, -1);
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700767 }
Tony Wickham010d2552017-01-20 08:15:28 -0800768 NotificationListener.removeNotificationsChangedListener();
Sunny Goyalaf161fd2018-02-05 16:34:57 -0800769 getStateManager().moveToRestState();
Michael Jurkacd496d72013-04-11 11:32:45 -0700770 }
771
772 @Override
773 protected void onStart() {
774 super.onStart();
775 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700776
Sunny Goyal60876ac2018-02-20 14:21:20 -0800777 if (mOnStartCallback != null) {
778 mOnStartCallback.onLauncherStart(this);
779 mOnStartCallback = null;
780 }
Adam Cohen9211d422014-10-07 18:14:53 -0700781 if (mLauncherCallbacks != null) {
782 mLauncherCallbacks.onStart();
783 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800784 mAppWidgetHost.setListenIfResumed(true);
Sunny Goyalcac002d2018-01-04 09:51:45 -0800785 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
Jon Miranda2d89ea82017-05-04 11:47:53 -0700786
Jon Mirandade43a712018-01-18 11:35:10 -0800787 if (mShouldFadeInScrim && mLauncherView.getBackground() != null) {
Jon Miranda2d89ea82017-05-04 11:47:53 -0700788 if (mScrimAnimator != null) {
789 mScrimAnimator.cancel();
790 }
Jon Mirandade43a712018-01-18 11:35:10 -0800791 mLauncherView.getBackground().setAlpha(0);
792 mScrimAnimator = ObjectAnimator.ofInt(mLauncherView.getBackground(),
Jon Miranda2d89ea82017-05-04 11:47:53 -0700793 LauncherAnimUtils.DRAWABLE_ALPHA, 0, 255);
794 mScrimAnimator.addListener(new AnimatorListenerAdapter() {
795 @Override
796 public void onAnimationEnd(Animator animation) {
797 mScrimAnimator = null;
798 }
799 });
800 mScrimAnimator.setDuration(600);
801 mScrimAnimator.setStartDelay(getWindow().getTransitionBackgroundFadeDuration());
802 mScrimAnimator.start();
803 }
Jon Miranda7fda2852017-07-19 16:07:01 -0700804 mShouldFadeInScrim = false;
Winson Chung62a70be2018-02-23 15:10:05 -0800805 UiFactory.onStart(this);
Michael Jurkacd496d72013-04-11 11:32:45 -0700806 }
807
808 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800809 protected void onResume() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700810 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800811 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700812 TraceHelper.partitionSection("ON_RESUME", "superCall");
813
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700814 mAppLaunchSuccess = false;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700815 getUserEventDispatcher().resetElapsedSessionMillis();
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700816 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700817 // Process any items that were added while Launcher was away.
818 InstallShortcutReceiver.disableAndFlushInstallQueue(
819 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700820
Sunny Goyala474a9b2017-05-04 16:47:11 -0700821 // Refresh shortcuts if the permission changed.
822 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700823
Sunny Goyal7ede6112017-12-05 15:11:21 -0800824 DiscoveryBounce.showIfNeeded(this);
Adam Cohen9211d422014-10-07 18:14:53 -0700825 if (mLauncherCallbacks != null) {
826 mLauncherCallbacks.onResume();
827 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800828
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700829 TraceHelper.endSection("ON_RESUME");
Adam Cohen06dff352012-06-01 17:17:08 -0700830 }
831
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800832 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700833 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700834 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700835 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700836
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700837 super.onPause();
Joe Onorato24b6fd82009-11-12 13:47:09 -0800838 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700839 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700840
Adam Cohen9211d422014-10-07 18:14:53 -0700841 if (mLauncherCallbacks != null) {
842 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700843 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700844 }
845
Winson Chungd7823942018-02-16 03:23:47 +0000846 @Override
847 public void onWindowFocusChanged(boolean hasFocus) {
848 super.onWindowFocusChanged(hasFocus);
849 mStateManager.onWindowFocusChanged();
850 }
851
Adam Cohenc2d6e892014-10-16 09:49:24 -0700852 public interface LauncherOverlay {
853
854 /**
855 * Touch interaction leading to overscroll has begun
856 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700857 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700858
859 /**
860 * Touch interaction related to overscroll has ended
861 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700862 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700863
864 /**
865 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
866 * screen (or in the case of RTL, the rightmost screen).
867 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700868 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700869
870 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800871 * Called when the launcher is ready to use the overlay
872 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700873 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700874 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700875 }
876
877 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700878 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700879 }
880
881 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
882
Sunny Goyalc86df472016-02-25 09:19:38 -0800883 public void onScrollChanged(float progress) {
884 if (mWorkspace != null) {
885 mWorkspace.onOverlayScrollChanged(progress);
886 }
887 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700888 }
889
Sunny Goyal16764582017-11-06 16:00:34 -0800890 public boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -0700891 if (mLauncherCallbacks != null) {
892 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700893 } else {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800894 // On O and above we there is always some setting present settings (add icon to
895 // home screen or icon badging). On earlier APIs we will have the allow rotation
896 // setting, on devices with a locked orientation,
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700897 return Utilities.ATLEAST_OREO || !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -0700898 }
Jorim Jaggid017f882014-01-14 17:08:48 -0800899 }
900
Sunny Goyalf9403d92017-10-18 10:55:56 -0700901 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -0700902 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700903 }
904
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800905 /**
906 * Restores the previous state, if it exists.
907 *
908 * @param savedState The previous state.
909 */
910 private void restoreState(Bundle savedState) {
911 if (savedState == null) {
912 return;
913 }
914
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700915 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -0700916 LauncherState[] stateValues = LauncherState.values();
917 LauncherState state = stateValues[stateOrdinal];
Sunny Goyalbc683e92017-12-06 10:25:07 -0800918 if (!state.disableRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700919 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800920 }
921
Sunny Goyal2100c782016-08-22 16:00:03 -0700922 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
923 if (requestArgs != null) {
924 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800925 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700926
927 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700928
929 SparseArray<Parcelable> widgetsState =
930 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
931 if (widgetsState != null) {
932 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
933 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800934 }
935
936 /**
937 * Finds all the views we need and configure them properly.
938 */
939 private void setupViews() {
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800940 mDragLayer = findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700941 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -0700942 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700943 mWorkspace.initParentViews(mDragLayer);
Sunny Goyal16764582017-11-06 16:00:34 -0800944 mOverviewPanel = findViewById(R.id.overview_panel);
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800945 mHotseat = findViewById(R.id.hotseat);
Craig Mautner360310b2012-10-26 15:13:08 -0700946
Sunny Goyal784f9c32016-03-23 16:56:54 -0700947 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
948 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
949 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800950
Winson Chung4c98d922011-05-31 16:50:48 -0700951 // Setup the drag layer
Sunny Goyal7ede6112017-12-05 15:11:21 -0800952 mDragLayer.setup(this, mDragController);
Winson Chung4c98d922011-05-31 16:50:48 -0700953
Hyunyoung Song645764e2016-06-06 14:19:02 -0700954 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -0700955 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
956 // default state, otherwise we will update to the wrong offsets in RTL
957 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700958 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700959 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700960
Tony Wickham34d2c912015-09-11 09:27:58 -0700961 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700962 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700963
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700964 // Setup Apps
965 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -0700966
Winson Chung3d503fb2011-07-13 17:25:49 -0700967 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -0700968 mDragController.setMoveTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700969 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -0400970
Sunny Goyal6639a5d2018-02-28 15:09:36 -0800971 mAllAppsController.setupViews(mAppsView, mHotseat);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800972 }
973
974 /**
975 * Creates a view representing a shortcut.
976 *
977 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800978 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800979 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700980 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800981 }
982
983 /**
984 * Creates a view representing a shortcut inflated from the specified resource.
985 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800986 * @param parent The group the shortcut belongs to.
987 * @param info The data structure describing the shortcut.
988 *
989 * @return A View inflated from layoutResId.
990 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700991 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -0800992 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
993 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800994 favorite.applyFromShortcutInfo(info);
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800995 favorite.setOnClickListener(ItemClickHandler.INSTANCE);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700996 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800997 return favorite;
998 }
999
1000 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -07001001 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001002 *
1003 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001004 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001005 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001006 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001007 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
1008 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001009 return;
1010 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001011
Jon Mirandac476d6e2017-05-04 16:30:01 -07001012 int[] cellXY = mTmpAddItemCellCoordinates;
1013 CellLayout layout = getCellLayout(container, screenId);
1014
Sunny Goyal782f0c92017-01-19 10:27:54 -08001015 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001016 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001017 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
1018 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001019 }
1020
1021 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001022 // Legacy shortcuts are only supported for primary profile.
1023 info = Process.myUserHandle().equals(args.user)
1024 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001025
Sunny Goyalb57645f2017-03-20 13:57:28 -07001026 if (info == null) {
1027 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1028 return;
1029 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001030 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001031 // The app is trying to add a shortcut without sufficient permissions
1032 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1033 return;
1034 }
1035 }
1036
Jon Mirandac476d6e2017-05-04 16:30:01 -07001037 if (container < 0) {
1038 // Adding a shortcut to the Workspace.
1039 final View view = createShortcut(info);
1040 boolean foundCellSpan = false;
1041 // First we check if we already know the exact location where we want to add this item.
1042 if (cellX >= 0 && cellY >= 0) {
1043 cellXY[0] = cellX;
1044 cellXY[1] = cellY;
1045 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001046
Jon Mirandac476d6e2017-05-04 16:30:01 -07001047 // If appropriate, either create a folder or add to an existing folder
1048 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001049 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001050 return;
1051 }
1052 DragObject dragObject = new DragObject();
1053 dragObject.dragInfo = info;
1054 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1055 true)) {
1056 return;
1057 }
1058 } else {
1059 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1060 }
1061
1062 if (!foundCellSpan) {
1063 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001064 return;
1065 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001066
1067 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1068 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001069 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001070 // Adding a shortcut to a Folder.
Sunny Goyale29897f52017-07-20 10:09:42 -07001071 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001072 if (folderIcon != null) {
1073 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1074 folderInfo.add(info, args.rank, false);
1075 } else {
Sunny Goyale29897f52017-07-20 10:09:42 -07001076 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001077 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001078 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001079 }
1080
Sunny Goyale29897f52017-07-20 10:09:42 -07001081 public FolderIcon findFolderIcon(final long folderIconId) {
1082 return (FolderIcon) mWorkspace.getFirstMatch(new ItemOperator() {
1083 @Override
1084 public boolean evaluate(ItemInfo info, View view) {
1085 return info != null && info.id == folderIconId;
1086 }
1087 });
1088 }
1089
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001091 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001092 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001093 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001094 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001095 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001096 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1097
Adam Cohened66b2b2012-01-23 17:28:51 -08001098 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001099 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001100 }
Romain Guycbb89e42009-06-08 15:52:54 -07001101
Adam Cohen59400422014-03-05 18:07:04 -08001102 LauncherAppWidgetInfo launcherInfo;
1103 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001104 launcherInfo.spanX = itemInfo.spanX;
1105 launcherInfo.spanY = itemInfo.spanY;
1106 launcherInfo.minSpanX = itemInfo.minSpanX;
1107 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001108 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001109
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001110 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001111 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001112
Sunny Goyal2100c782016-08-22 16:00:03 -07001113 if (hostView == null) {
1114 // Perform actual inflation because we're live
1115 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001116 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001117 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301118 prepareAppWidget(hostView, launcherInfo);
1119 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001120 }
Romain Guycbb89e42009-06-08 15:52:54 -07001121
Sunny Goyald5462aa2016-11-22 16:52:39 +05301122 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001123 hostView.setTag(item);
1124 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001125 hostView.setFocusable(true);
1126 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001127 }
1128
Adam Cohended9f8d2010-11-03 13:25:16 -07001129 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1130 @Override
1131 public void onReceive(Context context, Intent intent) {
1132 final String action = intent.getAction();
1133 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001134 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001135 // processing a multi-step drop
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001136 if (mAppsView != null && mPendingRequestArgs == null) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001137 mStateManager.goToState(NORMAL);
Winson Chung785d2eb2011-04-14 16:08:02 -07001138 }
Jon Miranda7fda2852017-07-19 16:07:01 -07001139 mShouldFadeInScrim = true;
1140 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1141 // ACTION_USER_PRESENT is sent after onStart/onResume. This covers the case where
1142 // the user unlocked and the Launcher is not in the foreground.
1143 mShouldFadeInScrim = false;
Adam Cohended9f8d2010-11-03 13:25:16 -07001144 }
1145 }
1146 };
1147
Tony Wickham010d2552017-01-20 08:15:28 -08001148 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
Sunny Goyal29947f02017-12-18 13:49:44 -08001149 mWorkspace.updateIconBadges(updatedBadges);
Sunny Goyal60180b02018-02-07 12:56:30 -08001150 mAppsView.getAppsStore().updateIconBadges(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001151
Sunny Goyal29947f02017-12-18 13:49:44 -08001152 PopupContainerWithArrow popup = PopupContainerWithArrow.getOpen(Launcher.this);
1153 if (popup != null) {
1154 popup.updateNotificationHeader(updatedBadges);
Tony Wickham010d2552017-01-20 08:15:28 -08001155 }
1156 }
1157
Adam Cohended9f8d2010-11-03 13:25:16 -07001158 @Override
1159 public void onAttachedToWindow() {
1160 super.onAttachedToWindow();
1161
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001162 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyalc86df472016-02-25 09:19:38 -08001163 if (mLauncherCallbacks != null) {
1164 mLauncherCallbacks.onAttachedToWindow();
1165 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001166 }
1167
1168 @Override
1169 public void onDetachedFromWindow() {
1170 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001171
1172 if (mLauncherCallbacks != null) {
1173 mLauncherCallbacks.onDetachedFromWindow();
1174 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001175 }
1176
Sunny Goyal3bac2c22018-02-07 14:44:05 -08001177 public void onQuickstepGestureStarted(boolean isVisible) {
1178 if (mLauncherCallbacks != null) {
1179 mLauncherCallbacks.onQuickstepGestureStarted(isVisible);
1180 }
1181 }
1182
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001183 public AllAppsTransitionController getAllAppsController() {
1184 return mAllAppsController;
1185 }
1186
Winson Chung1a341002018-01-17 10:00:23 -08001187 public LauncherRootView getRootView() {
1188 return (LauncherRootView) mLauncherView;
1189 }
1190
Winson Chunga6945242014-01-08 14:04:34 -08001191 public DragLayer getDragLayer() {
1192 return mDragLayer;
1193 }
1194
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001195 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001196 return mAppsView;
1197 }
1198
Winson Chunga6945242014-01-08 14:04:34 -08001199 public Workspace getWorkspace() {
1200 return mWorkspace;
1201 }
1202
1203 public Hotseat getHotseat() {
1204 return mHotseat;
1205 }
1206
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001207 public <T extends View> T getOverviewPanel() {
Winson Chung8ae41982017-11-10 11:42:13 -08001208 return (T) mOverviewPanel;
Winson Chunga6945242014-01-08 14:04:34 -08001209 }
1210
Sunny Goyal47328fd2016-05-25 18:56:41 -07001211 public DropTargetBar getDropTargetBar() {
1212 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001213 }
1214
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001215 public LauncherAppWidgetHost getAppWidgetHost() {
1216 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001217 }
Romain Guycbb89e42009-06-08 15:52:54 -07001218
Winson Chunga9abd0e2010-10-27 17:18:37 -07001219 public LauncherModel getModel() {
1220 return mModel;
1221 }
1222
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001223 public ModelWriter getModelWriter() {
1224 return mModelWriter;
1225 }
1226
Adam Cohen79d90c52016-04-22 13:29:20 -07001227 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001228 return mSharedPrefs;
1229 }
1230
Sunny Goyalf8d56fc2018-01-31 15:18:11 -08001231 public int getOrientation() { return mOldConfig.orientation; }
Jon Miranda6bed3502017-09-19 14:43:17 -07001232
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001233 @Override
1234 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001235 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001236 super.onNewIntent(intent);
1237
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001238 boolean alreadyOnHome = hasWindowFocus() && ((intent.getFlags() &
Winson15f8b172015-08-19 11:02:39 -07001239 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1240 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001241
1242 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001243 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001244 && AbstractFloatingView.getTopOpenView(this) == null;
Winson15f8b172015-08-19 11:02:39 -07001245 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001246 boolean internalStateHandled = InternalStateHandler
Sunny Goyal8b264562018-02-03 23:00:13 -08001247 .handleNewIntent(this, intent, isStarted());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001248
Winson15f8b172015-08-19 11:02:39 -07001249 if (isActionMain) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001250 if (!internalStateHandled) {
1251 // Note: There should be at most one log per method call. This is enforced
1252 // implicitly by using if-else statements.
1253 UserEventDispatcher ued = getUserEventDispatcher();
1254 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
1255 if (topOpenView != null) {
1256 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
1257 } else if (alreadyOnHome) {
1258 Target target = newContainerTarget(mStateManager.getState().containerType);
1259 target.pageIndex = mWorkspace.getCurrentPage();
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001260 ued.logActionCommand(Action.Command.HOME_INTENT, target,
1261 newContainerTarget(ContainerType.WORKSPACE));
Sunny Goyald3864fa2017-11-14 15:06:36 -08001262 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001263
Sunny Goyald3864fa2017-11-14 15:06:36 -08001264 // In all these cases, only animate if we're already on home
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001265 AbstractFloatingView.closeAllOpenViews(this, isStarted());
Jon Mirandafeba90f2016-10-06 10:53:29 -07001266
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001267 mStateManager.goToState(NORMAL);
Sunny Goyald3864fa2017-11-14 15:06:36 -08001268
1269 // Reset the apps view
1270 if (!alreadyOnHome && mAppsView != null) {
1271 mAppsView.reset();
1272 }
1273
1274 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()) {
1275 mWorkspace.post(mWorkspace::moveToDefaultScreen);
1276 }
1277 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001278
1279 final View v = getWindow().peekDecorView();
1280 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001281 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001282 }
1283
Adam Cohen9211d422014-10-07 18:14:53 -07001284 if (mLauncherCallbacks != null) {
1285 mLauncherCallbacks.onHomeIntent();
1286 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001287 }
Winson15f8b172015-08-19 11:02:39 -07001288
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001289 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001290 }
1291
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001292 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001293 public void onRestoreInstanceState(Bundle state) {
1294 super.onRestoreInstanceState(state);
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001295 mWorkspace.restoreInstanceStateForChild(mSynchronouslyBoundPage);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001296 }
1297
1298 @Override
1299 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001300 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001301 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001302
Adam Cohen21cd0022013-10-09 18:57:02 -07001303 }
Sunny Goyalea609262017-10-25 15:47:38 -07001304 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001305
1306
1307 AbstractFloatingView widgets = AbstractFloatingView
1308 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1309 if (widgets != null) {
1310 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1311 widgets.saveHierarchyState(widgetsState);
1312 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1313 } else {
1314 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1315 }
1316
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001317 // We close any open folders and shortcut containers since they will not be re-opened,
1318 // and we need to make sure this state is reflected.
1319 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001320
Sunny Goyal2100c782016-08-22 16:00:03 -07001321 if (mPendingRequestArgs != null) {
1322 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1323 }
1324 if (mPendingActivityResult != null) {
1325 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001326 }
1327
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001328 super.onSaveInstanceState(outState);
1329
Adam Cohen9211d422014-10-07 18:14:53 -07001330 if (mLauncherCallbacks != null) {
1331 mLauncherCallbacks.onSaveInstanceState(outState);
1332 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001333 }
1334
1335 @Override
1336 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001337 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001338
Sunny Goyal5cc7bbd2017-06-12 09:50:39 -07001339 unregisterReceiver(mReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001340 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001341
Winson Chungcd2b0142011-06-08 16:02:26 -07001342 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001343 // It's possible to receive onDestroy after a new Launcher activity has
1344 // been created. In this case, don't interfere with the new Launcher.
1345 if (mModel.isCurrentCallbacks(this)) {
1346 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001347 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001348 }
Sunny Goyal623eddd2018-03-02 12:24:41 -08001349 mRotationHelper.destroy();
Sunny Goyal745bad92016-05-02 10:54:12 -07001350
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001351 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001352 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001353 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001354 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001355 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001356
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001357 TextKeyListener.getInstance().release();
Mario Bertschler27288382017-05-24 15:35:09 -07001358 WallpaperColorInfo.getInstance(this).setOnThemeChangeListener(null);
1359
Michael Jurka2ecf9952012-06-18 12:52:28 -07001360 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001361
Tony2917a8b2017-07-31 23:29:42 -07001362 clearPendingBinds();
1363
Adam Cohen9211d422014-10-07 18:14:53 -07001364 if (mLauncherCallbacks != null) {
1365 mLauncherCallbacks.onDestroy();
1366 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001367 }
1368
Sunny Goyalae502842016-06-17 08:43:56 -07001369 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1370 return mAccessibilityDelegate;
1371 }
1372
Adam Cohena9cf38f2011-05-02 15:36:58 -07001373 public DragController getDragController() {
1374 return mDragController;
1375 }
1376
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001377 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001378 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001379 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001380 }
1381
1382 @Override
1383 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1384 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001385 try {
1386 super.startIntentSenderForResult(intent, requestCode,
1387 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1388 } catch (IntentSender.SendIntentException e) {
1389 throw new ActivityNotFoundException();
1390 }
1391 }
1392
Winson Chung70d72102011-08-12 11:24:35 -07001393 /**
1394 * Indicates that we want global search for this activity by setting the globalSearch
1395 * argument for {@link #startSearch} to true.
1396 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001397 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001398 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001399 Bundle appSearchData, boolean globalSearch) {
1400 if (appSearchData == null) {
1401 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001402 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001403 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001404
Sunny Goyal6f28e712016-09-13 14:19:29 -07001405 if (mLauncherCallbacks == null ||
1406 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1407 // Starting search from the callbacks failed. Start the default global search.
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001408 super.startSearch(initialQuery, selectInitialQuery, appSearchData, true);
Ian Parkinson047036e2014-09-01 15:40:53 +01001409 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001410
1411 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001412 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001413 }
1414
Joe Onorato9c1289c2009-08-17 11:03:03 -04001415 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001416 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001417 }
1418
Adam Cohen517a7f52014-03-01 12:12:59 -08001419 public boolean isWorkspaceLoading() {
1420 return mWorkspaceLoading;
1421 }
1422
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001423 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001424 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001425 }
1426
Sunny Goyal04a324a2017-01-20 21:08:59 -08001427 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001428 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001429 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001430
Sunny Goyal2100c782016-08-22 16:00:03 -07001431 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001432 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001433 if (LOGD) {
1434 Log.d(TAG, "Adding widget from drop");
1435 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001436 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001437 }
1438
Sunny Goyal2100c782016-08-22 16:00:03 -07001439 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001440 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1441 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1442 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001443
Adam Cohenad4e15c2013-10-17 16:21:35 -07001444 Runnable onComplete = new Runnable() {
1445 @Override
1446 public void run() {
1447 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001448 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001449 }
1450 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001451 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001452 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001453 }
1454 }
Romain Guycbb89e42009-06-08 15:52:54 -07001455
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001456 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1457 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001458 info.container = container;
1459 info.screenId = screenId;
1460 if (cell != null) {
1461 info.cellX = cell[0];
1462 info.cellY = cell[1];
1463 }
1464 info.spanX = spanX;
1465 info.spanY = spanY;
1466
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001467 switch (info.itemType) {
1468 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1469 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001470 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001471 break;
1472 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001473 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001474 break;
1475 default:
1476 throw new IllegalStateException("Unknown item type: " + info.itemType);
1477 }
1478 }
1479
Adam Cohenfbba09b2011-07-18 21:43:05 -07001480 /**
1481 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001482 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001483 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001484 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1485 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001486 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1487 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1488 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001489 }
1490
Adam Cohenfbba09b2011-07-18 21:43:05 -07001491 /**
1492 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001493 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001494 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001495 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001496 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001497 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001498 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001499 // In the case where we've prebound the widget, we remove it from the DragLayer
1500 if (LOGD) {
1501 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1502 }
1503 getDragLayer().removeView(hostView);
1504
Adam Cohened66b2b2012-01-23 17:28:51 -08001505 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001506 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001507
1508 // Clear the boundWidget so that it doesn't get destroyed.
1509 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001510 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001511 // In this case, we either need to start an activity to get permission to bind
1512 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001513 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1514 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1515 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1516 this, info.componentName);
1517 } else {
1518 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1519 }
Adam Cohendd70d662012-10-04 16:53:44 -07001520 Bundle options = info.bindOptions;
1521
Sunny Goyalffe83f12014-08-14 17:39:34 -07001522 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1523 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001524 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001525 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001526 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001527 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001528 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001529 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001530 }
1531
Adam Cohendcd297f2013-06-18 13:13:40 -07001532 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001533 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001534 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001535 folderInfo.title = getText(R.string.folder_name);
1536
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001537 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001538 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001539
1540 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001541 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301542 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001543 // Force measure the new folder icon
1544 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1545 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001546 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001547 }
Romain Guycbb89e42009-06-08 15:52:54 -07001548
Winsonc0b52fe2015-09-09 16:38:15 -07001549 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001550 * Unbinds the view for the specified item, and removes the item and all its children.
1551 *
1552 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001553 * @param itemInfo the {@link ItemInfo} for this view.
1554 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001555 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001556 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001557 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001558 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001559 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1560 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001561 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001562 } else {
1563 mWorkspace.removeWorkspaceItem(v);
1564 }
Winsonc0b52fe2015-09-09 16:38:15 -07001565 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001566 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001567 }
1568 } else if (itemInfo instanceof FolderInfo) {
1569 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001570 if (v instanceof FolderIcon) {
1571 ((FolderIcon) v).removeListeners();
1572 }
Winsonc0b52fe2015-09-09 16:38:15 -07001573 mWorkspace.removeWorkspaceItem(v);
1574 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001575 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001576 }
1577 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1578 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001579 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001580 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07001581 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001582 }
1583 } else {
1584 return false;
1585 }
1586 return true;
1587 }
1588
1589 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07001590 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07001591 */
Sunny Goyal56c73602015-09-25 12:55:01 -07001592 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07001593 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07001594 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07001595 // Deleting an app widget ID is a void call but writes to disk before returning
1596 // to the caller...
1597 new AsyncTask<Void, Void, Void>() {
1598 public Void doInBackground(Void ... args) {
1599 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
1600 return null;
1601 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07001602 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07001603 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001604 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001605 }
1606
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001607 @Override
1608 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001609 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001610 }
1611
Joe Onorato88ec0992009-11-19 13:16:06 -08001612 @Override
1613 public void onBackPressed() {
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08001614 if (finishAutoCancelActionMode()) {
1615 return;
1616 }
Adam Cohen9211d422014-10-07 18:14:53 -07001617 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1618 return;
1619 }
1620
Sunny Goyal45478022015-06-08 16:52:41 -07001621 if (mDragController.isDragging()) {
1622 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001623 return;
1624 }
1625
Jon Mirandafeba90f2016-10-06 10:53:29 -07001626 // Note: There should be at most one log per method call. This is enforced implicitly
1627 // by using if-else statements.
1628 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001629 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
1630 if (topView != null) {
Sunny Goyal37920962017-09-28 13:43:24 -07001631 topView.onBackPressed();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001632 } else if (!isInState(NORMAL)) {
Sunny Goyal49bcf342018-01-11 13:59:53 -08001633 LauncherState lastState = mStateManager.getLastState();
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001634 ued.logActionCommand(Action.Command.BACK, mStateManager.getState().containerType,
1635 lastState.containerType);
Sunny Goyal49bcf342018-01-11 13:59:53 -08001636 mStateManager.goToState(lastState);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001637 } else {
1638 // Back button is a no-op here, but give at least some feedback for the button press
1639 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001640 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001641 }
1642
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001643 /**
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001644 * Event handler for the wallpaper picker button that appears after a long press
1645 * on the home screen.
1646 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001647 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07001648 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07001649 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
1650 return;
1651 }
1652
Tony Wickham785f7a52015-08-31 17:28:32 -07001653 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
1654 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal2100c782016-08-22 16:00:03 -07001655 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001656 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001657 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001658
1659 String pickerPackage = getString(R.string.wallpaper_picker_package);
Tony85efb4b2017-06-02 13:06:10 -07001660 boolean hasTargetPackage = !TextUtils.isEmpty(pickerPackage);
1661 if (hasTargetPackage) {
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001662 intent.setPackage(pickerPackage);
1663 }
1664
Sunny Goyala5ace712017-11-15 17:12:51 -08001665 final Bundle launchOptions;
1666 if (v != null) {
1667 intent.setSourceBounds(getViewBounds(v));
1668 // If there is no target package, use the default intent chooser animation
Jon Miranda17940f02018-01-18 14:46:34 -08001669 launchOptions = hasTargetPackage
Tony Wickham005df0b2018-02-13 11:28:12 -08001670 ? getActivityLaunchOptionsAsBundle(v, isInMultiWindowModeCompat())
Jon Miranda17940f02018-01-18 14:46:34 -08001671 : null;
Sunny Goyala5ace712017-11-15 17:12:51 -08001672 } else {
1673 launchOptions = null;
1674 }
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001675 try {
Sunny Goyala5ace712017-11-15 17:12:51 -08001676 startActivityForResult(intent, REQUEST_PICK_WALLPAPER, launchOptions);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001677 } catch (ActivityNotFoundException e) {
1678 setWaitingForResult(null);
1679 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1680 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001681 }
1682
Sunny Goyala7ce1662016-05-31 15:01:35 -07001683 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001684 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001685 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
1686 try {
1687 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
1688 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
1689 // is enabled by default on NYC.
1690 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
1691 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001692
1693 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
1694 String id = ((ShortcutInfo) info).getDeepShortcutId();
1695 String packageName = intent.getPackage();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301696 DeepShortcutManager.getInstance(this).startShortcut(
Sunny Goyal9994b2b2016-06-23 14:17:24 -07001697 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001698 } else {
1699 // Could be launching some bookkeeping activity
1700 startActivity(intent, optsBundle);
1701 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07001702 } finally {
1703 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01001704 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001705 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001706 // Due to legacy reasons, direct call shortcuts require Launchers to have the
1707 // corresponding permission. Show the appropriate permission prompt if that
1708 // is the case.
1709 if (intent.getComponent() == null
1710 && Intent.ACTION_CALL.equals(intent.getAction())
1711 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
1712 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001713
1714 setWaitingForResult(PendingRequestArgs
1715 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07001716 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
1717 REQUEST_PERMISSION_CALL_PHONE);
1718 } else {
1719 // No idea why this was thrown.
1720 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07001721 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001722 }
1723 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001724
Tony Wickham005df0b2018-02-13 11:28:12 -08001725 public Bundle getActivityLaunchOptionsAsBundle(View v, boolean useDefaultLaunchOptions) {
1726 ActivityOptions activityOptions = getActivityLaunchOptions(v, useDefaultLaunchOptions);
1727 return activityOptions == null ? null : activityOptions.toBundle();
1728 }
1729
Sunny Goyalfe770c92016-09-27 14:38:58 -07001730 @TargetApi(Build.VERSION_CODES.M)
Tony Wickham005df0b2018-02-13 11:28:12 -08001731 public ActivityOptions getActivityLaunchOptions(View v, boolean useDefaultLaunchOptions) {
Jon Miranda17940f02018-01-18 14:46:34 -08001732 return useDefaultLaunchOptions
Jon Miranda54441f52018-01-24 15:38:25 -08001733 ? mAppTransitionManager.getDefaultActivityLaunchOptions(this, v)
1734 : mAppTransitionManager.getActivityLaunchOptions(this, v);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001735 }
1736
Tonye3c59252017-05-02 21:32:27 -07001737 public Rect getViewBounds(View v) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001738 int[] pos = new int[2];
1739 v.getLocationOnScreen(pos);
1740 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
1741 }
1742
Sunny Goyala7ce1662016-05-31 15:01:35 -07001743 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07001744 mAppLaunchSuccess = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001745 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
1746 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07001747 return mAppLaunchSuccess;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001748 }
Jon Miranda17940f02018-01-18 14:46:34 -08001749
Sunny Goyala7ce1662016-05-31 15:01:35 -07001750 // Only launch using the new animation if the shortcut has not opted out (this is a
1751 // private contract between launcher and may be ignored in the future).
1752 boolean useLaunchAnimation = (v != null) &&
1753 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Jon Miranda17940f02018-01-18 14:46:34 -08001754 Bundle optsBundle = useLaunchAnimation
Tony Wickham005df0b2018-02-13 11:28:12 -08001755 ? getActivityLaunchOptionsAsBundle(v, isInMultiWindowModeCompat())
Jon Miranda17940f02018-01-18 14:46:34 -08001756 : null;
Sunny Goyala7ce1662016-05-31 15:01:35 -07001757
Sunny Goyal24bb66a2017-03-21 15:12:01 -07001758 UserHandle user = item == null ? null : item.user;
Sunny Goyala7ce1662016-05-31 15:01:35 -07001759
1760 // Prepare intent
1761 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1762 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001763 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07001764 }
1765 try {
Jon Miranda84e71bf2018-02-13 15:36:20 -08001766 boolean isShortcut = Utilities.ATLEAST_MARSHMALLOW
1767 && (item instanceof ShortcutInfo)
1768 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
1769 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
1770 && !((ShortcutInfo) item).isPromise();
Jon Miranda17940f02018-01-18 14:46:34 -08001771 if (isShortcut) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001772 // Shortcuts need some special checks due to legacy reasons.
1773 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001774 } else if (user == null || user.equals(Process.myUserHandle())) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001775 // Could be launching some bookkeeping activity
1776 startActivity(intent, optsBundle);
1777 } else {
1778 LauncherAppsCompat.getInstance(this).startActivityForProfile(
1779 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
1780 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001781
1782 if (v instanceof BubbleTextView) {
1783 // This is set to the view that launched the activity that navigated the user away
1784 // from launcher. Since there is no callback for when the activity has finished
1785 // launching, enable the press state and keep this reference to reset the press
1786 // state when we return to launcher.
1787 BubbleTextView btv = (BubbleTextView) v;
1788 btv.setStayPressed(true);
1789 setOnResumeCallback(btv);
1790 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07001791 mAppLaunchSuccess = true;
1792 getUserEventDispatcher().logAppLaunch(v, intent); // TODO for discovered apps b/35802115
Sunny Goyala7ce1662016-05-31 15:01:35 -07001793 } catch (ActivityNotFoundException|SecurityException e) {
1794 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1795 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
1796 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07001797 return mAppLaunchSuccess;
Michael Jurka86a720e2012-05-09 11:23:23 -07001798 }
1799
Winson Chung3d503fb2011-07-13 17:25:49 -07001800 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001801 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07001802 return mHotseat != null && layout != null &&
1803 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
1804 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001805
Winson Chung3d503fb2011-07-13 17:25:49 -07001806 /**
1807 * Returns the CellLayout of the specified container at the specified screen.
1808 */
Sunny Goyal92820592015-03-02 11:31:35 -08001809 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001810 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1811 if (mHotseat != null) {
1812 return mHotseat.getLayout();
1813 } else {
1814 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08001815 }
Winson Chung3d503fb2011-07-13 17:25:49 -07001816 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08001817 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08001818 }
1819 }
1820
Michael Jurkae326f182011-11-21 14:05:46 -08001821 @Override
1822 public void onTrimMemory(int level) {
1823 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07001824 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
1825 // The widget preview db can result in holding onto over
1826 // 3MB of memory for caching which isn't necessary.
1827 SQLiteDatabase.releaseMemory();
1828
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08001829 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07001830 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08001831 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01001832 if (mLauncherCallbacks != null) {
1833 mLauncherCallbacks.onTrimMemory(level);
1834 }
Winson Chung62a70be2018-02-23 15:10:05 -08001835 UiFactory.onTrimMemory(this, level);
Michael Jurkae326f182011-11-21 14:05:46 -08001836 }
1837
Michael Jurkad7c28052012-04-27 15:43:36 -07001838 @Override
1839 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07001840 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07001841 final List<CharSequence> text = event.getText();
1842 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07001843 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001844 // TODO: When can workspace be null?
1845 text.add(mWorkspace == null
1846 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07001847 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07001848 return result;
1849 }
1850
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001851 public void setOnResumeCallback(OnResumeCallback callback) {
1852 if (mOnResumeCallback != null) {
1853 mOnResumeCallback.onLauncherResume();
1854 }
1855 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07001856 }
1857
Sunny Goyal60876ac2018-02-20 14:21:20 -08001858 public void setOnStartCallback(OnStartCallback callback) {
1859 mOnStartCallback = callback;
1860 }
1861
Michael Jurka7607c2f2013-04-03 14:33:19 -07001862 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001863 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001864 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07001865 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001866 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07001867 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001868 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07001869 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07001870 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07001871 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001872 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001873
Joe Onorato9c1289c2009-08-17 11:03:03 -04001874 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07001875 * Clear any pending bind callbacks. This is called when is loader is planning to
1876 * perform a full rebind from scratch.
1877 */
1878 @Override
1879 public void clearPendingBinds() {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001880 if (mPendingExecutor != null) {
1881 mPendingExecutor.markCompleted();
1882 mPendingExecutor = null;
1883 }
1884 }
1885
1886 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001887 * Refreshes the shortcuts shown on the workspace.
1888 *
1889 * Implementation of the method from LauncherModel.Callbacks.
1890 */
1891 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001892 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001893 // Floating panels (except the full widget sheet) are associated with individual icons. If
1894 // we are starting a fresh bind, close all such panels as all the icons are about
1895 // to go away.
1896 AbstractFloatingView.closeOpenViews(this, true,
Sunny Goyal7ede6112017-12-05 15:11:21 -08001897 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_REBIND_SAFE);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08001898
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001899 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08001900
Winson Chungd64d1762013-08-20 14:37:16 -07001901 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07001902 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07001903 mWorkspace.removeAllWorkspaceScreens();
Sunny Goyalc11fac32018-02-27 19:20:15 -08001904 mAppWidgetHost.clearViews();
Winson Chungd64d1762013-08-20 14:37:16 -07001905
Winson Chung3d503fb2011-07-13 17:25:49 -07001906 if (mHotseat != null) {
1907 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001908 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001909 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001910 }
1911
Adam Cohendcd297f2013-06-18 13:13:40 -07001912 @Override
1913 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001914 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001915 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
1916 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001917 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
1918 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001919 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001920 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
1921 // If there are no screens, we need to have an empty screen
1922 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07001923 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001924 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07001925
Winsonc7d2e832016-07-28 12:24:55 -07001926 // After we have added all the screens, if the wallpaper was locked to the default state,
1927 // then notify to indicate that it can be released and a proper wallpaper offset can be
1928 // computed before the next layout
1929 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07001930 }
1931
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001932 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001933 int count = orderedScreenIds.size();
1934 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001935 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001936 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001937 // No need to bind the first screen, as its always bound.
1938 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
1939 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001940 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001941 }
1942
Sunny Goyalb23980c2017-08-17 07:45:25 -07001943 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08001944 public void bindAppsAdded(ArrayList<Long> newScreens, ArrayList<ItemInfo> addNotAnimated,
1945 ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07001946 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08001947 if (newScreens != null) {
1948 bindAddScreens(newScreens);
1949 }
Winson Chungd64d1762013-08-20 14:37:16 -07001950
1951 // We add the items without animation on non-visible pages, and with
1952 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08001953 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001954 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001955 }
Adam Cohen76a47a12014-02-05 11:47:43 -08001956 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001957 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07001958 }
Winson Chungc58497e2013-09-03 17:48:37 -07001959
Winson Chung87412982013-10-03 18:34:14 -07001960 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07001961 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001962 }
1963
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001964 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001965 * Bind the items start-end from the list.
1966 *
1967 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001968 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07001969 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07001970 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001971 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07001972 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001973 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07001974 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001975 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001976 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07001977 int end = items.size();
1978 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001979 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07001980
1981 // Short circuit if we are loading dock items for a configuration which has no dock
1982 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
1983 mHotseat == null) {
1984 continue;
1985 }
1986
Sunny Goyal639e9062015-08-19 19:17:06 -07001987 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001988 switch (item.itemType) {
1989 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1990 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08001991 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001992 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07001993 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001994 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001995 }
1996 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07001997 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001998 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001999 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002000 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002001 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07002002 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2003 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07002004 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002005 if (view == null) {
2006 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002007 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08002008 break;
2009 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002010 default:
2011 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002012 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002013
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002014 /*
2015 * Remove colliding items.
2016 */
2017 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
2018 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
2019 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
2020 View v = cl.getChildAt(item.cellX, item.cellY);
2021 Object tag = v.getTag();
2022 String desc = "Collision while binding workspace item: " + item
2023 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08002024 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002025 throw (new RuntimeException(desc));
2026 } else {
2027 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002028 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002029 continue;
2030 }
2031 }
2032 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302033 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07002034 if (animateIcons) {
2035 // Animate all the applications up now
2036 view.setAlpha(0f);
2037 view.setScaleX(0f);
2038 view.setScaleY(0f);
2039 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08002040 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07002041 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002042 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07002043
Winson Chung997a9232013-07-24 15:33:46 -07002044 if (animateIcons) {
2045 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08002046 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07002047 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08002048 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07002049 final Runnable startBounceAnimRunnable = new Runnable() {
2050 public void run() {
2051 anim.playTogether(bounceAnims);
2052 anim.start();
2053 }
2054 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08002055 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07002056 // We post the animation slightly delayed to prevent slowdowns
2057 // when we are loading right after we return to launcher.
2058 mWorkspace.postDelayed(new Runnable() {
2059 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00002060 if (mWorkspace != null) {
2061 mWorkspace.snapToPage(newScreenIndex);
2062 mWorkspace.postDelayed(startBounceAnimRunnable,
2063 NEW_APPS_ANIMATION_DELAY);
2064 }
Winson Chung94d67682013-09-25 16:29:40 -07002065 }
2066 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07002067 } else {
2068 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07002069 }
2070 }
Winson Chung64359a52013-07-08 17:17:08 -07002071 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002072 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002073 }
2074
Joe Onorato9c1289c2009-08-17 11:03:03 -04002075 /**
2076 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002077 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07002078 public void bindAppWidget(LauncherAppWidgetInfo item) {
2079 View view = inflateAppWidget(item);
2080 if (view != null) {
2081 mWorkspace.addInScreen(view, item);
2082 mWorkspace.requestLayout();
2083 }
2084 }
2085
2086 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002087 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302088 PendingAppWidgetHostView view =
2089 new PendingAppWidgetHostView(this, item, mIconCache, true);
2090 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002091 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002092 }
2093
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002094 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002095
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002096 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08002097
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002098 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2099 // If the provider is not ready, bind as a pending widget.
2100 appWidgetInfo = null;
2101 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2102 // The widget id is not valid. Try to find the widget based on the provider info.
2103 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
2104 } else {
2105 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
2106 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002107
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002108 // If the provider is ready, but the width is not yet restored, try to restore it.
2109 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
2110 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07002111 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002112 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
2113 + " belongs to component " + item.providerName
2114 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002115 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002116 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002117 }
Sunny Goyalff572272014-07-23 13:58:07 -07002118
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002119 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002120 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07002121 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2122 // Id has not been allocated yet. Allocate a new id.
2123 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
2124 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07002125
Sunny Goyald478c832016-04-01 12:04:16 -07002126 // Also try to bind the widget. If the bind fails, the user will be shown
2127 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08002128 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07002129 pendingInfo.spanX = item.spanX;
2130 pendingInfo.spanY = item.spanY;
2131 pendingInfo.minSpanX = item.minSpanX;
2132 pendingInfo.minSpanY = item.minSpanY;
2133 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07002134
2135 boolean isDirectConfig =
2136 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2137 if (isDirectConfig && item.bindOptions != null) {
2138 Bundle newOptions = item.bindOptions.getExtras();
2139 if (options != null) {
2140 newOptions.putAll(options);
2141 }
2142 options = newOptions;
2143 }
Sunny Goyald478c832016-04-01 12:04:16 -07002144 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2145 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002146
Sunny Goyal86df1382016-08-10 15:03:22 -07002147 // We tried to bind once. If we were not able to bind, we would need to
2148 // go through the permission dialog, which means we cannot skip the config
2149 // activity.
2150 item.bindOptions = null;
2151 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2152
Sunny Goyald478c832016-04-01 12:04:16 -07002153 // Bind succeeded
2154 if (success) {
2155 // If the widget has a configure activity, it is still needs to set it up,
2156 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002157 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002158 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2159 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002160 }
Sunny Goyald478c832016-04-01 12:04:16 -07002161
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002162 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002163 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002164 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002165 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002166 // The widget was marked as UI not ready, but there is no configure activity to
2167 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002168 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002169 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002170 }
Sunny Goyalff572272014-07-23 13:58:07 -07002171 }
2172
Sunny Goyald5462aa2016-11-22 16:52:39 +05302173 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002174 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002175 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002176 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002177 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07002178 deleteWidgetInfo(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002179 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002180 }
2181
Sunny Goyal233ee962015-08-03 13:05:01 -07002182 item.minSpanX = appWidgetInfo.minSpanX;
2183 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302184 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002185 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302186 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002187 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302188 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002189
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002190 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002191 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002192 }
2193
Sunny Goyalff572272014-07-23 13:58:07 -07002194 /**
2195 * Restores a pending widget.
2196 *
2197 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002198 */
Sunny Goyald478c832016-04-01 12:04:16 -07002199 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002200 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2201 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2202 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002203 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002204 }
2205
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002206 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002207 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002208 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2209 info.pendingItemInfo = null;
2210 }
Sunny Goyalff572272014-07-23 13:58:07 -07002211
Sunny Goyalba47faa2017-10-04 16:50:57 -07002212 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002213 view.reInflate();
Sunny Goyalba47faa2017-10-04 16:50:57 -07002214 }
2215
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002216 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002217 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002218 }
2219
Adam Cohen1462de32012-07-24 22:34:36 -07002220 public void onPageBoundSynchronously(int page) {
Sunny Goyalf4aceab2017-12-06 16:16:31 -08002221 mSynchronouslyBoundPage = page;
Adam Cohen1462de32012-07-24 22:34:36 -07002222 }
2223
Sunny Goyal527c7d32015-08-28 15:19:36 -07002224 @Override
2225 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2226 if (mPendingExecutor != null) {
2227 mPendingExecutor.markCompleted();
2228 }
2229 mPendingExecutor = executor;
Sunny Goyal60180b02018-02-07 12:56:30 -08002230 if (!isInState(ALL_APPS)) {
2231 mAppsView.getAppsStore().setDeferUpdates(true);
2232 mPendingExecutor.execute(() -> mAppsView.getAppsStore().setDeferUpdates(false));
2233 }
2234
Sunny Goyal527c7d32015-08-28 15:19:36 -07002235 executor.attachTo(this);
2236 }
2237
2238 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2239 if (mPendingExecutor == executor) {
2240 mPendingExecutor = null;
2241 }
2242 }
2243
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002244 @Override
2245 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002246 if (mDragLayer.getAlpha() < 1) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002247 mDragLayer.animate().alpha(1).withEndAction(
2248 executor == null ? null : executor::onLoadAnimationCompleted).start();
2249 } else if (executor != null) {
2250 executor.onLoadAnimationCompleted();
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002251 }
2252 }
2253
Joe Onorato9c1289c2009-08-17 11:03:03 -04002254 /**
2255 * Callback saying that there aren't any more items to bind.
2256 *
2257 * Implementation of the method from LauncherModel.Callbacks.
2258 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002259 public void finishBindingItems() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002260 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002261 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002262
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002263 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002264
Sunny Goyal2100c782016-08-22 16:00:03 -07002265 if (mPendingActivityResult != null) {
2266 handleActivityResult(mPendingActivityResult.requestCode,
2267 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2268 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002269 }
2270
Sunny Goyala474a9b2017-05-04 16:47:11 -07002271 InstallShortcutReceiver.disableAndFlushInstallQueue(
2272 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002273
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002274 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002275 }
2276
Winson Chunga2413752012-04-03 14:22:34 -07002277 private boolean canRunNewAppsAnimation() {
2278 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002279 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002280 }
2281
Winson Chungc9168342013-06-26 14:54:55 -07002282 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002283 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07002284 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
2285 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002286 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002287 return bounceAnim;
2288 }
2289
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002290 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002291 * Add the icons for all apps.
2292 *
2293 * Implementation of the method from LauncherModel.Callbacks.
2294 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002295 public void bindAllApplications(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002296 mAppsView.getAppsStore().setApps(apps);
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002297
Adam Cohen9211d422014-10-07 18:14:53 -07002298 if (mLauncherCallbacks != null) {
2299 mLauncherCallbacks.bindAllApplications(apps);
2300 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002301 }
2302
2303 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002304 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
2305 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2306 */
2307 @Override
2308 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002309 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002310 }
2311
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002312 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002313 * A package was updated.
2314 *
2315 * Implementation of the method from LauncherModel.Callbacks.
2316 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002317 @Override
2318 public void bindAppsAddedOrUpdated(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002319 mAppsView.getAppsStore().addOrUpdateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002320 }
2321
Sunny Goyal4390ace2014-10-13 11:33:11 -07002322 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002323 public void bindPromiseAppProgressUpdated(PromiseAppInfo app) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002324 mAppsView.getAppsStore().updatePromiseAppProgress(app);
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002325 }
2326
2327 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002328 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002329 mWorkspace.widgetsRestored(widgets);
2330 }
2331
Joe Onorato9c1289c2009-08-17 11:03:03 -04002332 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002333 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002334 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002335 *
2336 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002337 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002338 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002339 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002340 if (!updated.isEmpty()) {
2341 mWorkspace.updateShortcuts(updated);
2342 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002343 }
2344
2345 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002346 * Update the state of a package, typically related to install state.
2347 *
2348 * Implementation of the method from LauncherModel.Callbacks.
2349 */
Sunny Goyale755d462014-07-22 13:48:29 -07002350 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002351 public void bindRestoreItemsChange(HashSet<ItemInfo> updates) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07002352 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07002353 }
2354
2355 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002356 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07002357 * in addition to specific applications to remove, the reason being that
2358 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002359 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07002360 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002361 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07002362 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002363 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002364 mWorkspace.removeItemsByMatcher(matcher);
2365 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002366 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002367
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002368 @Override
2369 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002370 mAppsView.getAppsStore().removeApps(appInfos);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002371 }
Joe Onoratobe386092009-11-17 17:32:16 -08002372
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002373 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002374 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
2375 mPopupDataProvider.setAllWidgets(allWidgets);
Tony Wickham26b17462017-03-20 17:12:24 -07002376 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
2377 if (topView != null) {
2378 topView.onWidgetsBound();
2379 }
2380 }
2381
Tony Wickham86222d22017-03-29 15:30:43 -07002382 /**
2383 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
2384 * refreshes the widgets and shortcuts associated with the given package/user
2385 */
2386 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07002387 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002388 }
2389
Winson Chung80baf5a2010-08-09 16:03:15 -07002390 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002391 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08002392 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002393 @Override
2394 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
2395 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002396
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002397 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
2398 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07002399 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002400 writer.println(prefix + " Homescreen " + i);
2401
2402 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
2403 for (int j = 0; j < layout.getChildCount(); j++) {
2404 Object tag = layout.getChildAt(j).getTag();
2405 if (tag != null) {
2406 writer.println(prefix + " " + tag.toString());
2407 }
2408 }
2409 }
2410
2411 writer.println(prefix + " Hotseat");
2412 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07002413 for (int j = 0; j < layout.getChildCount(); j++) {
2414 Object tag = layout.getChildAt(j).getTag();
2415 if (tag != null) {
2416 writer.println(prefix + " " + tag.toString());
2417 }
Adam Cohen4caf2982013-08-20 18:54:31 -07002418 }
Sunny Goyala1365452015-10-01 15:46:24 -07002419 }
2420
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002421 writer.println(prefix + "Misc:");
2422 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
2423 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
2424 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
Sunny Goyal2bba1902018-02-27 12:20:50 -08002425 writer.println(" deviceProfile isTransposed=" + getDeviceProfile().isVerticalBarLayout());
2426 writer.println(" orientation=" + getResources().getConfiguration().orientation);
2427
2428 try {
2429 FileLog.flushAll(writer);
2430 } catch (Exception e) {
2431 // Ignore
2432 }
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002433
2434 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002435
Adam Cohen9211d422014-10-07 18:14:53 -07002436 if (mLauncherCallbacks != null) {
2437 mLauncherCallbacks.dump(prefix, fd, writer, args);
2438 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002439 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002440
Sunny Goyal66b24572016-09-21 15:57:55 -07002441 @Override
2442 @TargetApi(Build.VERSION_CODES.N)
2443 public void onProvideKeyboardShortcuts(
2444 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
2445
2446 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002447 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002448 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
2449 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
2450 }
2451 View currentFocus = getCurrentFocus();
2452 if (new CustomActionsPopup(this, currentFocus).canShow()) {
2453 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
2454 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
2455 }
Tony18c4aa42017-05-10 21:23:57 -05002456 if (currentFocus.getTag() instanceof ItemInfo
2457 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002458 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
2459 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
2460 }
2461 if (!shortcutInfos.isEmpty()) {
2462 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
2463 }
2464
2465 super.onProvideKeyboardShortcuts(data, menu, deviceId);
2466 }
2467
2468 @Override
2469 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
2470 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
2471 switch (keyCode) {
2472 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002473 if (isInState(NORMAL)) {
2474 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07002475 return true;
2476 }
2477 break;
2478 case KeyEvent.KEYCODE_S: {
2479 View focusedView = getCurrentFocus();
2480 if (focusedView instanceof BubbleTextView
2481 && focusedView.getTag() instanceof ItemInfo
2482 && mAccessibilityDelegate.performAction(focusedView,
2483 (ItemInfo) focusedView.getTag(),
2484 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal00ac9202017-11-09 15:24:06 -08002485 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07002486 return true;
2487 }
2488 break;
2489 }
2490 case KeyEvent.KEYCODE_O:
2491 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
2492 return true;
2493 }
2494 break;
2495 }
2496 }
2497 return super.onKeyShortcut(keyCode, event);
2498 }
2499
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002500 @Override
2501 public boolean onKeyUp(int keyCode, KeyEvent event) {
2502 if (keyCode == KeyEvent.KEYCODE_MENU) {
2503 // KEYCODE_MENU is sent by some tests, for example
2504 // LauncherJankTests#testWidgetsContainerFling. Don't just remove its handling.
2505 if (!mDragController.isDragging() && !mWorkspace.isSwitchingState() &&
2506 isInState(NORMAL)) {
2507 // Close any open floating views.
2508 AbstractFloatingView.closeAllOpenViews(this);
2509
2510 // Setting the touch point to (-1, -1) will show the options popup in the center of
2511 // the screen.
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08002512 OptionsPopupView.show(this, -1, -1);
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002513 }
2514 return true;
2515 }
2516 return super.onKeyUp(keyCode, event);
2517 }
2518
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002519 public static Launcher getLauncher(Context context) {
2520 if (context instanceof Launcher) {
2521 return (Launcher) context;
2522 }
2523 return ((Launcher) ((ContextWrapper) context).getBaseContext());
2524 }
2525
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08002526 @Override
2527 public void onActionModeStarted(ActionMode mode) {
2528 super.onActionModeStarted(mode);
2529 mCurrentActionMode = mode;
2530 }
2531
2532 @Override
2533 public void onActionModeFinished(ActionMode mode) {
2534 super.onActionModeFinished(mode);
2535 mCurrentActionMode = null;
2536 }
2537
2538 public boolean finishAutoCancelActionMode() {
2539 if (mCurrentActionMode != null && AUTO_CANCEL_ACTION_MODE == mCurrentActionMode.getTag()) {
2540 mCurrentActionMode.finish();
2541 return true;
2542 }
2543 return false;
2544 }
2545
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002546 /**
2547 * Callback for listening for onResume
2548 */
2549 public interface OnResumeCallback {
2550
2551 void onLauncherResume();
2552 }
Sunny Goyal60876ac2018-02-20 14:21:20 -08002553
2554 /**
2555 * Callback for listening for onStart
2556 */
2557 public interface OnStartCallback {
2558
2559 void onLauncherStart(Launcher launcher);
2560 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002561}