blob: 0bbb90c452d3e9b0e630b7d8e922df2b0bd9303a [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;
Michael Jurkaaf442092010-06-10 17:01:57 -070050import android.graphics.Rect;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070051import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020052import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053import android.os.Bundle;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070054import android.os.Parcelable;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080055import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070056import android.os.StrictMode;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070057import android.os.UserHandle;
Tony Wickham86222d22017-03-29 15:30:43 -070058import android.support.annotation.Nullable;
Michael Jurkaa33411c2012-06-14 16:18:21 -070059import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070061import android.util.Log;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070062import android.util.SparseArray;
Sunny Goyal7c8a65e2017-12-22 11:11:33 -080063import android.view.ActionMode;
Adam Cohen96d30a12013-07-16 18:13:21 -070064import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070065import android.view.KeyboardShortcutGroup;
66import android.view.KeyboardShortcutInfo;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080067import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -070068import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070069import android.view.View;
Adam Cohen96d30a12013-07-16 18:13:21 -070070import android.view.ViewGroup;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080071import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070072import android.view.animation.OvershootInterpolator;
Adam Cohen96d30a12013-07-16 18:13:21 -070073import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070074
Sunny Goyal651077b2014-06-30 14:15:31 -070075import com.android.launcher3.DropTarget.DragObject;
Sunny Goyala7ce1662016-05-31 15:01:35 -070076import com.android.launcher3.LauncherSettings.Favorites;
Jon Mirandac476d6e2017-05-04 16:30:01 -070077import com.android.launcher3.Workspace.ItemOperator;
Sunny Goyalae502842016-06-17 08:43:56 -070078import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070079import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070080import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal7ede6112017-12-05 15:11:21 -080081import com.android.launcher3.allapps.DiscoveryBounce;
Sunny Goyalffe83f12014-08-14 17:39:34 -070082import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010083import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -070084import com.android.launcher3.compat.LauncherAppsCompatVO;
Tony Wickhame0c33232016-02-08 11:37:04 -080085import com.android.launcher3.config.FeatureFlags;
Vadim Tryshevfedca432015-08-19 17:55:02 -070086import com.android.launcher3.dragndrop.DragController;
87import com.android.launcher3.dragndrop.DragLayer;
88import com.android.launcher3.dragndrop.DragView;
Mario Bertschler27288382017-05-24 15:35:09 -070089import com.android.launcher3.dynamicui.WallpaperColorInfo;
Sunny Goyal26119432016-02-18 22:09:23 +000090import com.android.launcher3.folder.FolderIcon;
Sunny Goyal66b24572016-09-21 15:57:55 -070091import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -070092import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -070093import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -070094import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyala535ae42017-02-27 10:07:13 -080095import com.android.launcher3.model.ModelWriter;
Sunny Goyala535ae42017-02-27 10:07:13 -080096import com.android.launcher3.notification.NotificationListener;
Tony Wickham540913e2017-01-23 11:47:51 -080097import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -080098import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -070099import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyalf8088ee2017-11-10 14:52:00 -0800100import com.android.launcher3.states.InternalStateHandler;
Sunny Goyal623eddd2018-03-02 12:24:41 -0800101import com.android.launcher3.states.RotationHelper;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800102import com.android.launcher3.touch.ItemClickHandler;
Sunny Goyala5ace712017-11-15 17:12:51 -0800103import com.android.launcher3.uioverrides.UiFactory;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530104import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
105import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
Sunny Goyalaeb16432017-10-16 11:46:41 -0700106import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyal2100c782016-08-22 16:00:03 -0700107import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700108import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700109import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700110import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700111import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800112import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700113import com.android.launcher3.util.PendingRequestArgs;
Sunny Goyal8392c822017-06-20 10:03:56 -0700114import com.android.launcher3.util.SystemUiController;
Mario Bertschlera6936942017-05-31 14:48:19 -0700115import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700116import com.android.launcher3.util.Thunk;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700117import com.android.launcher3.util.TraceHelper;
Sunny Goyal326403e2017-10-02 12:45:10 -0700118import com.android.launcher3.util.UiThreadHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700119import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800120import com.android.launcher3.views.OptionsPopupView;
Sunny Goyal29947f02017-12-18 13:49:44 -0800121import com.android.launcher3.widget.LauncherAppWidgetHostView;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800122import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700123import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal29947f02017-12-18 13:49:44 -0800124import com.android.launcher3.widget.PendingAppWidgetHostView;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800125import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700126import com.android.launcher3.widget.WidgetHostViewLoader;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700127import com.android.launcher3.widget.WidgetListRowEntry;
128import com.android.launcher3.widget.WidgetsFullSheet;
Sunny Goyal952e63d2017-08-16 04:59:08 -0700129import com.android.launcher3.widget.custom.CustomWidgetParser;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700130
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700131import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700132import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700133import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700134import java.util.Collection;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700135import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700136import java.util.List;
Tony Wickham010d2552017-01-20 08:15:28 -0800137import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700138
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139/**
140 * Default launcher application.
141 */
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800142public class Launcher extends BaseActivity implements LauncherExterns, LauncherModel.Callbacks,
143 LauncherProviderChangeListener, WallpaperColorInfo.OnThemeChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700144 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700145 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146
Winson Chunga2413752012-04-03 14:22:34 -0700147 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700148
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700150 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700151
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700152 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700153 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154
Michael Jurka8b805b12012-04-18 14:23:14 -0700155 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800156 public static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
157 public static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700158
Jon Mirandac476d6e2017-05-04 16:30:01 -0700159 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700160
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700161 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
162
Mathew Inwood876a8462013-06-14 14:12:41 +0100163 /**
164 * IntentStarter uses request codes starting with this. This must be greater than all activity
165 * request codes used internally.
166 */
167 protected static final int REQUEST_LAST = 100;
168
Winson Chung2672ff92012-05-04 16:22:30 -0700169 // The Intent extra that defines whether to ignore the launch animation
170 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400171 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700172
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173 // Type: int
174 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700175 // Type: int
176 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700177 // Type: PendingRequestArgs
178 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
179 // Type: ActivityResultInfo
180 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700181 // Type: SparseArray<Parcelable>
182 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183
Sunny Goyal7c8a65e2017-12-22 11:11:33 -0800184 // When starting an action mode, setting this tag will cause the action mode to be cancelled
185 // automatically when user interacts with the launcher.
186 public static final Object AUTO_CANCEL_ACTION_MODE = new Object();
187
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700188 private LauncherStateManager mStateManager;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700189
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700190 private boolean mIsSafeModeEnabled;
191
Adam Cohenad4e15c2013-10-17 16:21:35 -0700192 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193
Winson Chunga2413752012-04-03 14:22:34 -0700194 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700195 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
196 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
197 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700198
Jon Miranda54441f52018-01-24 15:38:25 -0800199 private LauncherAppTransitionManager mAppTransitionManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800200 private Configuration mOldConfig;
Jon Miranda54441f52018-01-24 15:38:25 -0800201
Adam Cohen091440a2015-03-18 14:16:05 -0700202 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700203 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700204 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800205 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700206
Sunny Goyalffe83f12014-08-14 17:39:34 -0700207 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700208 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700209
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700210 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700211
Sunny Goyal316490e2015-06-02 09:38:28 -0700212 @Thunk Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700213
Sunny Goyal47328fd2016-05-25 18:56:41 -0700214 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700215
216 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700217 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700218 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700219
Winson Chung8ae41982017-11-10 11:42:13 -0800220 // UI and state for the overview panel
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800221 private View mOverviewPanel;
Winson Chung8ae41982017-11-10 11:42:13 -0800222
Adam Cohen091440a2015-03-18 14:16:05 -0700223 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400224
Sunny Goyal60876ac2018-02-20 14:21:20 -0800225 private OnStartCallback mOnStartCallback;
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700226 private OnResumeCallback mOnResumeCallback;
227
Sunny Goyal527c7d32015-08-28 15:19:36 -0700228 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700229
Joe Onorato9c1289c2009-08-17 11:03:03 -0400230 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800231 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800232 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700233 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400234
Jon Miranda2d89ea82017-05-04 11:47:53 -0700235 private ObjectAnimator mScrimAnimator;
Jon Miranda7fda2852017-07-19 16:07:01 -0700236 private boolean mShouldFadeInScrim;
Jon Miranda2d89ea82017-05-04 11:47:53 -0700237
Tony Wickham010d2552017-01-20 08:15:28 -0800238 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700239
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800240 private int mSynchronouslyBoundPage = PagedView.INVALID_PAGE;
Adam Cohen1462de32012-07-24 22:34:36 -0700241
Winson Chung46353de2012-02-16 14:05:10 -0800242 // We only want to get the SharedPreferences once since it does an FS stat each time we get
243 // it from the context.
244 private SharedPreferences mSharedPrefs;
245
Sunny Goyal2100c782016-08-22 16:00:03 -0700246 // Activity result which needs to be processed after workspace has loaded.
247 private ActivityResultInfo mPendingActivityResult;
248 /**
249 * Holds extra information required to handle a result from an external call, like
250 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
251 */
252 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800253
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700254 public ViewGroupFocusHelper mFocusHandler;
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700255 private boolean mAppLaunchSuccess;
256
Sunny Goyal623eddd2018-03-02 12:24:41 -0800257 private RotationHelper mRotationHelper;
Sunny Goyal7c8a65e2017-12-22 11:11:33 -0800258 private ActionMode mCurrentActionMode;
Sunny Goyal7779d622015-06-11 16:18:39 -0700259
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260 @Override
261 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700262 if (DEBUG_STRICT_MODE) {
263 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
264 .detectDiskReads()
265 .detectDiskWrites()
266 .detectNetwork() // or .detectAll() for all detectable problems
267 .penaltyLog()
268 .build());
269 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
270 .detectLeakedSqlLiteObjects()
271 .detectLeakedClosableObjects()
272 .penaltyLog()
273 .penaltyDeath()
274 .build());
275 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700276 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700277
Mario Bertschler27288382017-05-24 15:35:09 -0700278 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
279 wallpaperColorInfo.setOnThemeChangeListener(this);
Mario Bertschlera6936942017-05-31 14:48:19 -0700280 overrideTheme(wallpaperColorInfo.isDark(), wallpaperColorInfo.supportsDarkText());
Mario Bertschler27288382017-05-24 15:35:09 -0700281
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800282 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700283 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400284
Sunny Goyal87f784c2017-01-11 10:48:34 -0800285 LauncherAppState app = LauncherAppState.getInstance(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800286 mOldConfig = new Configuration(getResources().getConfiguration());
Sunny Goyal0e7724c2018-02-22 13:22:21 -0800287 mModel = app.setLauncher(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800288 initDeviceProfile(app.getInvariantDeviceProfile());
Winson Chung6e1c0d32013-10-25 15:24:24 -0700289
Sunny Goyalf7258242015-10-19 16:59:07 -0700290 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700291 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800292 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700293 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700294
Joe Onorato41a12d22009-10-31 18:30:00 -0400295 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700296 mAllAppsController = new AllAppsTransitionController(this);
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800297 mStateManager = new LauncherStateManager(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700298
Sunny Goyalffe83f12014-08-14 17:39:34 -0700299 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700300
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700301 mAppWidgetHost = new LauncherAppWidgetHost(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700302 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700303
Sunny Goyal60820d72017-05-09 12:40:11 -0700304 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700305
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800306 setupViews();
Tony Wickham010d2552017-01-20 08:15:28 -0800307 mPopupDataProvider = new PopupDataProvider(this);
308
Sunny Goyal623eddd2018-03-02 12:24:41 -0800309 mRotationHelper = new RotationHelper(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800310
Sunny Goyald3864fa2017-11-14 15:06:36 -0800311 boolean internalStateHandled = InternalStateHandler.handleCreate(this, getIntent());
312 if (internalStateHandled) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800313 if (savedInstanceState != null) {
314 // InternalStateHandler has already set the appropriate state.
315 // We dont need to do anything.
316 savedInstanceState.remove(RUNTIME_STATE);
317 }
318 }
319 restoreState(savedInstanceState);
Winson Chungb63b44c2017-11-10 17:54:44 -0800320
Sunny Goyal2100c782016-08-22 16:00:03 -0700321 // We only load the page synchronously if the user rotates (or triggers a
322 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700323 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
324 if (savedInstanceState != null) {
325 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
326 }
Sunny Goyald3864fa2017-11-14 15:06:36 -0800327
Sunny Goyalfe770c92016-09-27 14:38:58 -0700328 if (!mModel.startLoader(currentScreen)) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800329 if (!internalStateHandled) {
330 // If we are not binding synchronously, show a fade in animation when
331 // the first page bind completes.
332 mDragLayer.setAlpha(0);
333 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700334 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700335 // Pages bound synchronously.
336 mWorkspace.setCurrentPage(currentScreen);
337
Sunny Goyal2100c782016-08-22 16:00:03 -0700338 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800339 }
340
341 // For handling default keys
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800342 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800343
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800344 setContentView(mLauncherView);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800345 getRootView().dispatchInsets();
Mario Bertschlera6936942017-05-31 14:48:19 -0700346
Jon Miranda7fda2852017-07-19 16:07:01 -0700347 // Listen for broadcasts
348 IntentFilter filter = new IntentFilter();
349 filter.addAction(Intent.ACTION_SCREEN_OFF);
350 filter.addAction(Intent.ACTION_USER_PRESENT); // When the device wakes up + keyguard is gone
351 registerReceiver(mReceiver, filter);
352 mShouldFadeInScrim = true;
Sunny Goyala616d2b2017-06-12 13:54:01 -0700353
Sunny Goyal8392c822017-06-20 10:03:56 -0700354 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
355 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700356
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800357 mAppTransitionManager = LauncherAppTransitionManager.newInstance(this);
Jon Miranda7853bdb2018-01-22 17:32:30 -0800358
Sunny Goyal14b32402017-07-31 10:03:28 -0700359 if (mLauncherCallbacks != null) {
360 mLauncherCallbacks.onCreate(savedInstanceState);
361 }
Sunny Goyal623eddd2018-03-02 12:24:41 -0800362 mRotationHelper.initialize();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700363
364 TraceHelper.endSection("Launcher-onCreate");
Adam Cohen9211d422014-10-07 18:14:53 -0700365 }
366
Sunny Goyal7779d622015-06-11 16:18:39 -0700367 @Override
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800368 public void onConfigurationChanged(Configuration newConfig) {
369 int diff = newConfig.diff(mOldConfig);
370 if ((diff & (CONFIG_ORIENTATION | CONFIG_SCREEN_SIZE)) != 0) {
371 mUserEventDispatcher = null;
372 initDeviceProfile(mDeviceProfile.inv);
Sunny Goyal2bba1902018-02-27 12:20:50 -0800373 FileLog.d(TAG, "Config changed, my orientation=" +
374 getResources().getConfiguration().orientation +
375 ", new orientation=" + newConfig.orientation +
376 ", old orientation=" + mOldConfig.orientation +
377 ", isTransposed=" + mDeviceProfile.isVerticalBarLayout());
Sunny Goyalfde55052018-02-01 14:46:13 -0800378 dispatchDeviceProfileChanged();
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800379
380 getRootView().dispatchInsets();
381 getStateManager().reapplyState();
382
383 // TODO: We can probably avoid rebind when only screen size changed.
Sunny Goyal605bcf32018-03-02 15:16:12 -0800384 rebindModel();
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800385 }
386
387 mOldConfig.setTo(newConfig);
388 super.onConfigurationChanged(newConfig);
389 }
390
Sunny Goyal605bcf32018-03-02 15:16:12 -0800391 @Override
392 public void rebindModel() {
393 int currentPage = mWorkspace.getNextPage();
394 if (mModel.startLoader(currentPage)) {
395 mWorkspace.setCurrentPage(currentPage);
396 setWorkspaceLoading(true);
397 }
398 }
399
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800400 private void initDeviceProfile(InvariantDeviceProfile idp) {
401 // Load configuration-specific DeviceProfile
Sunny Goyalf633ef52018-03-13 09:57:05 -0700402 setDeviceProfile(idp.getDeviceProfile(this));
Sunny Goyal605bcf32018-03-02 15:16:12 -0800403 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout(), true);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800404 }
405
Sunny Goyal623eddd2018-03-02 12:24:41 -0800406 public RotationHelper getRotationHelper() {
407 return mRotationHelper;
408 }
409
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800410 @Override
Mario Bertschler27288382017-05-24 15:35:09 -0700411 public void onThemeChanged() {
412 recreate();
413 }
414
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700415 public LauncherStateManager getStateManager() {
416 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700417 }
418
Tonydc76f8e2018-02-02 21:45:39 -0600419 public LauncherAppTransitionManager getAppTransitionManager() {
420 return mAppTransitionManager;
421 }
422
Mario Bertschlera6936942017-05-31 14:48:19 -0700423 protected void overrideTheme(boolean isDark, boolean supportsDarkText) {
Mario Bertschler27288382017-05-24 15:35:09 -0700424 if (isDark) {
425 setTheme(R.style.LauncherThemeDark);
Mario Bertschlera6936942017-05-31 14:48:19 -0700426 } else if (supportsDarkText) {
427 setTheme(R.style.LauncherThemeDarkText);
Mario Bertschler27288382017-05-24 15:35:09 -0700428 }
429 }
430
431 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700432 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800433 return mLauncherView.findViewById(id);
434 }
435
436 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700437 public void onAppWidgetHostReset() {
438 if (mAppWidgetHost != null) {
439 mAppWidgetHost.startListening();
440 }
441 }
442
Adam Cohen9211d422014-10-07 18:14:53 -0700443 private LauncherCallbacks mLauncherCallbacks;
444
Sunny Goyal32554d12015-12-03 15:31:25 -0800445 /**
446 * Call this after onCreate to set or clear overlay.
447 */
448 public void setLauncherOverlay(LauncherOverlay overlay) {
449 if (overlay != null) {
450 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
451 }
452 mWorkspace.setLauncherOverlay(overlay);
453 }
454
Adam Cohen9211d422014-10-07 18:14:53 -0700455 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
456 mLauncherCallbacks = callbacks;
457 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700458 }
459
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700460 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700461 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700462 if (mLauncherCallbacks != null) {
463 mLauncherCallbacks.onLauncherProviderChange();
464 }
465 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700466
Hyunyoung Song3f471442015-04-08 19:01:34 -0700467 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700468 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
469 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700470 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700471 }
472
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000473 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700474 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
475 // This cast is safe as long as the id < 0x00FFFFFF
476 // Since we jail all the dynamically generated views, there should be no clashes
477 // with any other views.
478 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000479 }
480
Tony Wickham010d2552017-01-20 08:15:28 -0800481 public PopupDataProvider getPopupDataProvider() {
482 return mPopupDataProvider;
483 }
484
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000485 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700486 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
487 * a configuration step, this allows the proper animations to run after other transitions.
488 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700489 private long completeAdd(
490 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
491 long screenId = info.screenId;
492 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700493 // When the screen id represents an actual screen (as opposed to a rank) we make sure
494 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700495 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700496 }
Adam Cohendb364c32014-05-20 14:23:40 -0700497
Sunny Goyal2100c782016-08-22 16:00:03 -0700498 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800499 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700500 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700501 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800502 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700503 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700504 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700505 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700506 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700507 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700508 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700509 int widgetId = appWidgetId;
510 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700511 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700512 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700513 // Since the view was just bound, also launch the configure activity if needed
514 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
515 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800516 if (provider != null) {
517 new WidgetAddFlowHandler(provider)
518 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700519 }
520 }
521 break;
522 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800523 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700524
Adam Cohendb364c32014-05-20 14:23:40 -0700525 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800526 }
527
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800528 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700529 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700530 if (isWorkspaceLoading()) {
531 // process the result once the workspace has loaded.
532 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
533 return;
534 }
535 mPendingActivityResult = null;
536
Winson Chunge341d302013-08-16 14:31:00 -0700537 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700538 final PendingRequestArgs requestArgs = mPendingRequestArgs;
539 setWaitingForResult(null);
540 if (requestArgs == null) {
541 return;
542 }
543
544 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700545
Adam Cohenad4e15c2013-10-17 16:21:35 -0700546 Runnable exitSpringLoaded = new Runnable() {
547 @Override
548 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700549 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700550 }
551 };
552
Michael Jurka8b805b12012-04-18 14:23:14 -0700553 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700554 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700555 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700556 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
557 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700558 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700559 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700560 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700561 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700562 addAppWidgetImpl(
563 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800564 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700565 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700566 }
567 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200568 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700569 if (resultCode == RESULT_OK && isInState(OVERVIEW)) {
Tony Wickhame3054412015-09-09 09:05:25 -0700570 // User could have free-scrolled between pages before picking a wallpaper; make sure
571 // we move to the closest one now.
572 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700573 mStateManager.goToState(NORMAL, false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200574 }
575 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700576 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200577
Adam Cohened66b2b2012-01-23 17:28:51 -0800578 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700579 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700580
Adam Cohened66b2b2012-01-23 17:28:51 -0800581 // We have special handling for widgets
582 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800583 final int appWidgetId;
584 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
585 : -1;
586 if (widgetId < 0) {
587 appWidgetId = pendingAddWidgetId;
588 } else {
589 appWidgetId = widgetId;
590 }
591
Adam Cohenad4e15c2013-10-17 16:21:35 -0700592 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800593 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700594 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
595 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700596 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700597 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700598 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700599 @Override
600 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700601 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700602 }
603 };
Adam Cohendb364c32014-05-20 14:23:40 -0700604
Sunny Goyal2100c782016-08-22 16:00:03 -0700605 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
606 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
607 } else {
608 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
609 // When the screen id represents an actual screen (as opposed to a rank)
610 // we make sure that the drop page actually exists.
611 requestArgs.screenId =
612 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700613 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700614 final CellLayout dropLayout =
615 mWorkspace.getScreenWithId(requestArgs.screenId);
616
617 dropLayout.setDropPending(true);
618 final Runnable onComplete = new Runnable() {
619 @Override
620 public void run() {
621 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
622 dropLayout.setDropPending(false);
623 }
624 };
625 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
626 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700627 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800628 return;
629 }
630
Sunny Goyald478c832016-04-01 12:04:16 -0700631 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
632 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700633 if (resultCode == RESULT_OK) {
634 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700635 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700636 }
637 // Leave the widget in the pending state if the user canceled the configure.
638 return;
639 }
640
Sunny Goyalaad90582015-07-09 14:22:50 -0700641 if (requestCode == REQUEST_CREATE_SHORTCUT) {
642 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700643 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
644 completeAdd(requestCode, data, -1, requestArgs);
645 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
646 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
647
Sunny Goyalaad90582015-07-09 14:22:50 -0700648 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700649 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
650 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800651 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800652 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800653 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800654 }
655
656 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700657 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800658 final int requestCode, final int resultCode, final Intent data) {
659 handleActivityResult(requestCode, resultCode, data);
660 if (mLauncherCallbacks != null) {
661 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
662 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800663 }
664
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700665 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700666 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600667 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700668 PendingRequestArgs pendingArgs = mPendingRequestArgs;
669 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
670 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
671 setWaitingForResult(null);
672
Sunny Goyal28c6b962015-10-12 11:42:05 -0700673 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700674 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700675 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700676 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700677 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700678 Intent intent = pendingArgs.getPendingIntent();
679
Sunny Goyal28c6b962015-10-12 11:42:05 -0700680 if (grantResults.length > 0
681 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700682 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700683 } else {
684 // TODO: Show a snack bar with link to settings
685 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700686 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700687 }
688 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600689 if (mLauncherCallbacks != null) {
690 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
691 grantResults);
692 }
693 }
694
Adam Cohendb364c32014-05-20 14:23:40 -0700695 /**
696 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
697 *
698 * @param screenId the screen id to check
699 * @return the new screen, or screenId if it exists
700 */
701 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800702 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700703 if (dropLayout == null) {
704 // it's possible that the add screen was removed because it was
705 // empty and a re-bind occurred
706 mWorkspace.addExtraEmptyScreen();
707 return mWorkspace.commitExtraEmptyScreen();
708 } else {
709 return screenId;
710 }
711 }
712
Sunny Goyal2100c782016-08-22 16:00:03 -0700713 @Thunk void completeTwoStageWidgetDrop(
714 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
715 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800716 Runnable onCompleteRunnable = null;
717 int animationType = 0;
718
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700719 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800720 if (resultCode == RESULT_OK) {
721 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
722 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800723 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700724 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800725 onCompleteRunnable = new Runnable() {
726 @Override
727 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700728 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700729 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800730 }
731 };
732 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800733 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800734 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800735 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700736 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700737 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700738 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
739 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700740 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700741 // The animated view may be null in the case of a rotation during widget configuration
742 onCompleteRunnable.run();
743 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800744 }
745
746 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700747 protected void onStop() {
748 super.onStop();
749 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700750
751 if (mLauncherCallbacks != null) {
752 mLauncherCallbacks.onStop();
753 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800754 mAppWidgetHost.setListenIfResumed(false);
Tony Wickham010d2552017-01-20 08:15:28 -0800755
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700756 if (!mAppLaunchSuccess) {
757 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -0800758 mStateManager.getState().containerType, -1);
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700759 }
Tony Wickham010d2552017-01-20 08:15:28 -0800760 NotificationListener.removeNotificationsChangedListener();
Sunny Goyalaf161fd2018-02-05 16:34:57 -0800761 getStateManager().moveToRestState();
Michael Jurkacd496d72013-04-11 11:32:45 -0700762 }
763
764 @Override
765 protected void onStart() {
766 super.onStart();
767 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700768
Sunny Goyal60876ac2018-02-20 14:21:20 -0800769 if (mOnStartCallback != null) {
770 mOnStartCallback.onLauncherStart(this);
771 mOnStartCallback = null;
772 }
Adam Cohen9211d422014-10-07 18:14:53 -0700773 if (mLauncherCallbacks != null) {
774 mLauncherCallbacks.onStart();
775 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800776 mAppWidgetHost.setListenIfResumed(true);
Sunny Goyalcac002d2018-01-04 09:51:45 -0800777 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
Jon Miranda2d89ea82017-05-04 11:47:53 -0700778
Jon Mirandade43a712018-01-18 11:35:10 -0800779 if (mShouldFadeInScrim && mLauncherView.getBackground() != null) {
Jon Miranda2d89ea82017-05-04 11:47:53 -0700780 if (mScrimAnimator != null) {
781 mScrimAnimator.cancel();
782 }
Jon Mirandade43a712018-01-18 11:35:10 -0800783 mLauncherView.getBackground().setAlpha(0);
784 mScrimAnimator = ObjectAnimator.ofInt(mLauncherView.getBackground(),
Jon Miranda2d89ea82017-05-04 11:47:53 -0700785 LauncherAnimUtils.DRAWABLE_ALPHA, 0, 255);
786 mScrimAnimator.addListener(new AnimatorListenerAdapter() {
787 @Override
788 public void onAnimationEnd(Animator animation) {
789 mScrimAnimator = null;
790 }
791 });
792 mScrimAnimator.setDuration(600);
793 mScrimAnimator.setStartDelay(getWindow().getTransitionBackgroundFadeDuration());
794 mScrimAnimator.start();
795 }
Jon Miranda7fda2852017-07-19 16:07:01 -0700796 mShouldFadeInScrim = false;
Winson Chung62a70be2018-02-23 15:10:05 -0800797 UiFactory.onStart(this);
Michael Jurkacd496d72013-04-11 11:32:45 -0700798 }
799
800 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800801 protected void onResume() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700802 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800803 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700804 TraceHelper.partitionSection("ON_RESUME", "superCall");
805
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700806 mAppLaunchSuccess = false;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700807 getUserEventDispatcher().resetElapsedSessionMillis();
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700808 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700809 // Process any items that were added while Launcher was away.
810 InstallShortcutReceiver.disableAndFlushInstallQueue(
811 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700812
Sunny Goyala474a9b2017-05-04 16:47:11 -0700813 // Refresh shortcuts if the permission changed.
814 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700815
Sunny Goyal7ede6112017-12-05 15:11:21 -0800816 DiscoveryBounce.showIfNeeded(this);
Adam Cohen9211d422014-10-07 18:14:53 -0700817 if (mLauncherCallbacks != null) {
818 mLauncherCallbacks.onResume();
819 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800820
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700821 TraceHelper.endSection("ON_RESUME");
Adam Cohen06dff352012-06-01 17:17:08 -0700822 }
823
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800824 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700825 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700826 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700827 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700828
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700829 super.onPause();
Joe Onorato24b6fd82009-11-12 13:47:09 -0800830 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700831 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700832
Adam Cohen9211d422014-10-07 18:14:53 -0700833 if (mLauncherCallbacks != null) {
834 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700835 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700836 }
837
Winson Chungd7823942018-02-16 03:23:47 +0000838 @Override
839 public void onWindowFocusChanged(boolean hasFocus) {
840 super.onWindowFocusChanged(hasFocus);
841 mStateManager.onWindowFocusChanged();
842 }
843
Adam Cohenc2d6e892014-10-16 09:49:24 -0700844 public interface LauncherOverlay {
845
846 /**
847 * Touch interaction leading to overscroll has begun
848 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700849 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700850
851 /**
852 * Touch interaction related to overscroll has ended
853 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700854 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700855
856 /**
857 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
858 * screen (or in the case of RTL, the rightmost screen).
859 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700860 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700861
862 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800863 * Called when the launcher is ready to use the overlay
864 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700865 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700866 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700867 }
868
869 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700870 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700871 }
872
873 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
874
Sunny Goyalc86df472016-02-25 09:19:38 -0800875 public void onScrollChanged(float progress) {
876 if (mWorkspace != null) {
877 mWorkspace.onOverlayScrollChanged(progress);
878 }
879 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700880 }
881
Sunny Goyal16764582017-11-06 16:00:34 -0800882 public boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -0700883 if (mLauncherCallbacks != null) {
884 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700885 } else {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800886 // On O and above we there is always some setting present settings (add icon to
887 // home screen or icon badging). On earlier APIs we will have the allow rotation
888 // setting, on devices with a locked orientation,
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700889 return Utilities.ATLEAST_OREO || !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -0700890 }
Jorim Jaggid017f882014-01-14 17:08:48 -0800891 }
892
Sunny Goyalf9403d92017-10-18 10:55:56 -0700893 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -0700894 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700895 }
896
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800897 /**
898 * Restores the previous state, if it exists.
899 *
900 * @param savedState The previous state.
901 */
902 private void restoreState(Bundle savedState) {
903 if (savedState == null) {
904 return;
905 }
906
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700907 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -0700908 LauncherState[] stateValues = LauncherState.values();
909 LauncherState state = stateValues[stateOrdinal];
Sunny Goyalbc683e92017-12-06 10:25:07 -0800910 if (!state.disableRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700911 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800912 }
913
Sunny Goyal2100c782016-08-22 16:00:03 -0700914 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
915 if (requestArgs != null) {
916 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800917 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700918
919 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700920
921 SparseArray<Parcelable> widgetsState =
922 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
923 if (widgetsState != null) {
924 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
925 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800926 }
927
928 /**
929 * Finds all the views we need and configure them properly.
930 */
931 private void setupViews() {
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800932 mDragLayer = findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700933 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -0700934 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700935 mWorkspace.initParentViews(mDragLayer);
Sunny Goyal16764582017-11-06 16:00:34 -0800936 mOverviewPanel = findViewById(R.id.overview_panel);
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800937 mHotseat = findViewById(R.id.hotseat);
Craig Mautner360310b2012-10-26 15:13:08 -0700938
Sunny Goyal784f9c32016-03-23 16:56:54 -0700939 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
940 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
941 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800942
Winson Chung4c98d922011-05-31 16:50:48 -0700943 // Setup the drag layer
Sunny Goyal7ede6112017-12-05 15:11:21 -0800944 mDragLayer.setup(this, mDragController);
Winson Chung4c98d922011-05-31 16:50:48 -0700945
Hyunyoung Song645764e2016-06-06 14:19:02 -0700946 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -0700947 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
948 // default state, otherwise we will update to the wrong offsets in RTL
949 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700950 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700951 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700952
Tony Wickham34d2c912015-09-11 09:27:58 -0700953 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700954 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700955
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700956 // Setup Apps
957 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -0700958
Winson Chung3d503fb2011-07-13 17:25:49 -0700959 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -0700960 mDragController.setMoveTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700961 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -0400962
Sunny Goyal6639a5d2018-02-28 15:09:36 -0800963 mAllAppsController.setupViews(mAppsView, mHotseat);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800964 }
965
966 /**
967 * Creates a view representing a shortcut.
968 *
969 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800970 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800971 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700972 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800973 }
974
975 /**
976 * Creates a view representing a shortcut inflated from the specified resource.
977 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800978 * @param parent The group the shortcut belongs to.
979 * @param info The data structure describing the shortcut.
980 *
981 * @return A View inflated from layoutResId.
982 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700983 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -0800984 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
985 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800986 favorite.applyFromShortcutInfo(info);
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800987 favorite.setOnClickListener(ItemClickHandler.INSTANCE);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700988 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800989 return favorite;
990 }
991
992 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -0700993 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800994 *
995 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800996 */
Adam Cohendcd297f2013-06-18 13:13:40 -0700997 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700998 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -0700999 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
1000 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001001 return;
1002 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001003
Jon Mirandac476d6e2017-05-04 16:30:01 -07001004 int[] cellXY = mTmpAddItemCellCoordinates;
1005 CellLayout layout = getCellLayout(container, screenId);
1006
Sunny Goyal782f0c92017-01-19 10:27:54 -08001007 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001008 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001009 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
1010 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001011 }
1012
1013 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001014 // Legacy shortcuts are only supported for primary profile.
1015 info = Process.myUserHandle().equals(args.user)
1016 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001017
Sunny Goyalb57645f2017-03-20 13:57:28 -07001018 if (info == null) {
1019 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1020 return;
1021 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001022 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001023 // The app is trying to add a shortcut without sufficient permissions
1024 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1025 return;
1026 }
1027 }
1028
Jon Mirandac476d6e2017-05-04 16:30:01 -07001029 if (container < 0) {
1030 // Adding a shortcut to the Workspace.
1031 final View view = createShortcut(info);
1032 boolean foundCellSpan = false;
1033 // First we check if we already know the exact location where we want to add this item.
1034 if (cellX >= 0 && cellY >= 0) {
1035 cellXY[0] = cellX;
1036 cellXY[1] = cellY;
1037 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001038
Jon Mirandac476d6e2017-05-04 16:30:01 -07001039 // If appropriate, either create a folder or add to an existing folder
1040 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001041 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001042 return;
1043 }
1044 DragObject dragObject = new DragObject();
1045 dragObject.dragInfo = info;
1046 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1047 true)) {
1048 return;
1049 }
1050 } else {
1051 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1052 }
1053
1054 if (!foundCellSpan) {
1055 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001056 return;
1057 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001058
1059 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1060 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001061 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001062 // Adding a shortcut to a Folder.
Sunny Goyale29897f2017-07-20 10:09:42 -07001063 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001064 if (folderIcon != null) {
1065 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1066 folderInfo.add(info, args.rank, false);
1067 } else {
Sunny Goyale29897f2017-07-20 10:09:42 -07001068 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001069 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001070 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001071 }
1072
Sunny Goyale29897f2017-07-20 10:09:42 -07001073 public FolderIcon findFolderIcon(final long folderIconId) {
1074 return (FolderIcon) mWorkspace.getFirstMatch(new ItemOperator() {
1075 @Override
1076 public boolean evaluate(ItemInfo info, View view) {
1077 return info != null && info.id == folderIconId;
1078 }
1079 });
1080 }
1081
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001082 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001083 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001084 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001085 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001086 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001087 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001088 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1089
Adam Cohened66b2b2012-01-23 17:28:51 -08001090 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001091 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001092 }
Romain Guycbb89e42009-06-08 15:52:54 -07001093
Adam Cohen59400422014-03-05 18:07:04 -08001094 LauncherAppWidgetInfo launcherInfo;
1095 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001096 launcherInfo.spanX = itemInfo.spanX;
1097 launcherInfo.spanY = itemInfo.spanY;
1098 launcherInfo.minSpanX = itemInfo.minSpanX;
1099 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001100 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001101
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001102 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001103 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001104
Sunny Goyal2100c782016-08-22 16:00:03 -07001105 if (hostView == null) {
1106 // Perform actual inflation because we're live
1107 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001108 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001109 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301110 prepareAppWidget(hostView, launcherInfo);
1111 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001112 }
Romain Guycbb89e42009-06-08 15:52:54 -07001113
Sunny Goyald5462aa2016-11-22 16:52:39 +05301114 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001115 hostView.setTag(item);
1116 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001117 hostView.setFocusable(true);
1118 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001119 }
1120
Adam Cohended9f8d2010-11-03 13:25:16 -07001121 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1122 @Override
1123 public void onReceive(Context context, Intent intent) {
1124 final String action = intent.getAction();
1125 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001126 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001127 // processing a multi-step drop
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001128 if (mAppsView != null && mPendingRequestArgs == null) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001129 mStateManager.goToState(NORMAL);
Winson Chung785d2eb2011-04-14 16:08:02 -07001130 }
Jon Miranda7fda2852017-07-19 16:07:01 -07001131 mShouldFadeInScrim = true;
1132 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1133 // ACTION_USER_PRESENT is sent after onStart/onResume. This covers the case where
1134 // the user unlocked and the Launcher is not in the foreground.
1135 mShouldFadeInScrim = false;
Adam Cohended9f8d2010-11-03 13:25:16 -07001136 }
1137 }
1138 };
1139
Tony Wickham010d2552017-01-20 08:15:28 -08001140 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
Sunny Goyal29947f02017-12-18 13:49:44 -08001141 mWorkspace.updateIconBadges(updatedBadges);
Sunny Goyal60180b02018-02-07 12:56:30 -08001142 mAppsView.getAppsStore().updateIconBadges(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001143
Sunny Goyal29947f02017-12-18 13:49:44 -08001144 PopupContainerWithArrow popup = PopupContainerWithArrow.getOpen(Launcher.this);
1145 if (popup != null) {
1146 popup.updateNotificationHeader(updatedBadges);
Tony Wickham010d2552017-01-20 08:15:28 -08001147 }
1148 }
1149
Adam Cohended9f8d2010-11-03 13:25:16 -07001150 @Override
1151 public void onAttachedToWindow() {
1152 super.onAttachedToWindow();
1153
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001154 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyalc86df472016-02-25 09:19:38 -08001155 if (mLauncherCallbacks != null) {
1156 mLauncherCallbacks.onAttachedToWindow();
1157 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001158 }
1159
1160 @Override
1161 public void onDetachedFromWindow() {
1162 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001163
1164 if (mLauncherCallbacks != null) {
1165 mLauncherCallbacks.onDetachedFromWindow();
1166 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001167 }
1168
Sunny Goyal3bac2c22018-02-07 14:44:05 -08001169 public void onQuickstepGestureStarted(boolean isVisible) {
1170 if (mLauncherCallbacks != null) {
1171 mLauncherCallbacks.onQuickstepGestureStarted(isVisible);
1172 }
1173 }
1174
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001175 public AllAppsTransitionController getAllAppsController() {
1176 return mAllAppsController;
1177 }
1178
Winson Chung1a341002018-01-17 10:00:23 -08001179 public LauncherRootView getRootView() {
1180 return (LauncherRootView) mLauncherView;
1181 }
1182
Winson Chunga6945242014-01-08 14:04:34 -08001183 public DragLayer getDragLayer() {
1184 return mDragLayer;
1185 }
1186
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001187 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001188 return mAppsView;
1189 }
1190
Winson Chunga6945242014-01-08 14:04:34 -08001191 public Workspace getWorkspace() {
1192 return mWorkspace;
1193 }
1194
1195 public Hotseat getHotseat() {
1196 return mHotseat;
1197 }
1198
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001199 public <T extends View> T getOverviewPanel() {
Winson Chung8ae41982017-11-10 11:42:13 -08001200 return (T) mOverviewPanel;
Winson Chunga6945242014-01-08 14:04:34 -08001201 }
1202
Sunny Goyal47328fd2016-05-25 18:56:41 -07001203 public DropTargetBar getDropTargetBar() {
1204 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001205 }
1206
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001207 public LauncherAppWidgetHost getAppWidgetHost() {
1208 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001209 }
Romain Guycbb89e42009-06-08 15:52:54 -07001210
Winson Chunga9abd0e2010-10-27 17:18:37 -07001211 public LauncherModel getModel() {
1212 return mModel;
1213 }
1214
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001215 public ModelWriter getModelWriter() {
1216 return mModelWriter;
1217 }
1218
Adam Cohen79d90c52016-04-22 13:29:20 -07001219 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001220 return mSharedPrefs;
1221 }
1222
Sunny Goyalf8d56fc2018-01-31 15:18:11 -08001223 public int getOrientation() { return mOldConfig.orientation; }
Jon Miranda6bed3502017-09-19 14:43:17 -07001224
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001225 @Override
1226 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001227 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001228 super.onNewIntent(intent);
1229
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001230 boolean alreadyOnHome = hasWindowFocus() && ((intent.getFlags() &
Winson15f8b172015-08-19 11:02:39 -07001231 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1232 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001233
1234 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001235 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001236 && AbstractFloatingView.getTopOpenView(this) == null;
Winson15f8b172015-08-19 11:02:39 -07001237 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001238 boolean internalStateHandled = InternalStateHandler
Sunny Goyal8b264562018-02-03 23:00:13 -08001239 .handleNewIntent(this, intent, isStarted());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001240
Winson15f8b172015-08-19 11:02:39 -07001241 if (isActionMain) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001242 if (!internalStateHandled) {
1243 // Note: There should be at most one log per method call. This is enforced
1244 // implicitly by using if-else statements.
1245 UserEventDispatcher ued = getUserEventDispatcher();
1246 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
1247 if (topOpenView != null) {
1248 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
1249 } else if (alreadyOnHome) {
1250 Target target = newContainerTarget(mStateManager.getState().containerType);
1251 target.pageIndex = mWorkspace.getCurrentPage();
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001252 ued.logActionCommand(Action.Command.HOME_INTENT, target,
1253 newContainerTarget(ContainerType.WORKSPACE));
Sunny Goyald3864fa2017-11-14 15:06:36 -08001254 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001255
Sunny Goyald3864fa2017-11-14 15:06:36 -08001256 // In all these cases, only animate if we're already on home
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001257 AbstractFloatingView.closeAllOpenViews(this, isStarted());
Jon Mirandafeba90f2016-10-06 10:53:29 -07001258
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001259 mStateManager.goToState(NORMAL);
Sunny Goyald3864fa2017-11-14 15:06:36 -08001260
1261 // Reset the apps view
1262 if (!alreadyOnHome && mAppsView != null) {
1263 mAppsView.reset();
1264 }
1265
1266 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()) {
1267 mWorkspace.post(mWorkspace::moveToDefaultScreen);
1268 }
1269 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001270
1271 final View v = getWindow().peekDecorView();
1272 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001273 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001274 }
1275
Adam Cohen9211d422014-10-07 18:14:53 -07001276 if (mLauncherCallbacks != null) {
1277 mLauncherCallbacks.onHomeIntent();
1278 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001279 }
Winson15f8b172015-08-19 11:02:39 -07001280
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001281 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001282 }
1283
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001284 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001285 public void onRestoreInstanceState(Bundle state) {
1286 super.onRestoreInstanceState(state);
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001287 mWorkspace.restoreInstanceStateForChild(mSynchronouslyBoundPage);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001288 }
1289
1290 @Override
1291 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001292 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001293 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001294
Adam Cohen21cd0022013-10-09 18:57:02 -07001295 }
Sunny Goyalea609262017-10-25 15:47:38 -07001296 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001297
1298
1299 AbstractFloatingView widgets = AbstractFloatingView
1300 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1301 if (widgets != null) {
1302 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1303 widgets.saveHierarchyState(widgetsState);
1304 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1305 } else {
1306 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1307 }
1308
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001309 // We close any open folders and shortcut containers since they will not be re-opened,
1310 // and we need to make sure this state is reflected.
1311 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001312
Sunny Goyal2100c782016-08-22 16:00:03 -07001313 if (mPendingRequestArgs != null) {
1314 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1315 }
1316 if (mPendingActivityResult != null) {
1317 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001318 }
1319
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001320 super.onSaveInstanceState(outState);
1321
Adam Cohen9211d422014-10-07 18:14:53 -07001322 if (mLauncherCallbacks != null) {
1323 mLauncherCallbacks.onSaveInstanceState(outState);
1324 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001325 }
1326
1327 @Override
1328 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001329 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001330
Sunny Goyal5cc7bbd2017-06-12 09:50:39 -07001331 unregisterReceiver(mReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001332 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001333
Winson Chungcd2b0142011-06-08 16:02:26 -07001334 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001335 // It's possible to receive onDestroy after a new Launcher activity has
1336 // been created. In this case, don't interfere with the new Launcher.
1337 if (mModel.isCurrentCallbacks(this)) {
1338 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001339 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001340 }
Sunny Goyal623eddd2018-03-02 12:24:41 -08001341 mRotationHelper.destroy();
Sunny Goyal745bad92016-05-02 10:54:12 -07001342
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001343 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001344 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001345 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001346 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001347 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001348
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001349 TextKeyListener.getInstance().release();
Mario Bertschler27288382017-05-24 15:35:09 -07001350 WallpaperColorInfo.getInstance(this).setOnThemeChangeListener(null);
1351
Michael Jurka2ecf9952012-06-18 12:52:28 -07001352 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001353
Tony2917a8b2017-07-31 23:29:42 -07001354 clearPendingBinds();
1355
Adam Cohen9211d422014-10-07 18:14:53 -07001356 if (mLauncherCallbacks != null) {
1357 mLauncherCallbacks.onDestroy();
1358 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001359 }
1360
Sunny Goyalae502842016-06-17 08:43:56 -07001361 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1362 return mAccessibilityDelegate;
1363 }
1364
Adam Cohena9cf38f2011-05-02 15:36:58 -07001365 public DragController getDragController() {
1366 return mDragController;
1367 }
1368
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001369 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001370 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001371 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001372 }
1373
1374 @Override
1375 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1376 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001377 try {
1378 super.startIntentSenderForResult(intent, requestCode,
1379 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1380 } catch (IntentSender.SendIntentException e) {
1381 throw new ActivityNotFoundException();
1382 }
1383 }
1384
Winson Chung70d72102011-08-12 11:24:35 -07001385 /**
1386 * Indicates that we want global search for this activity by setting the globalSearch
1387 * argument for {@link #startSearch} to true.
1388 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001389 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001390 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001391 Bundle appSearchData, boolean globalSearch) {
1392 if (appSearchData == null) {
1393 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001394 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001395 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001396
Sunny Goyal6f28e712016-09-13 14:19:29 -07001397 if (mLauncherCallbacks == null ||
1398 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1399 // Starting search from the callbacks failed. Start the default global search.
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001400 super.startSearch(initialQuery, selectInitialQuery, appSearchData, true);
Ian Parkinson047036e2014-09-01 15:40:53 +01001401 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001402
1403 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001404 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001405 }
1406
Joe Onorato9c1289c2009-08-17 11:03:03 -04001407 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001408 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001409 }
1410
Adam Cohen517a7f52014-03-01 12:12:59 -08001411 public boolean isWorkspaceLoading() {
1412 return mWorkspaceLoading;
1413 }
1414
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001415 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001416 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001417 }
1418
Sunny Goyal04a324a2017-01-20 21:08:59 -08001419 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001420 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001421 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001422
Sunny Goyal2100c782016-08-22 16:00:03 -07001423 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001424 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001425 if (LOGD) {
1426 Log.d(TAG, "Adding widget from drop");
1427 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001428 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001429 }
1430
Sunny Goyal2100c782016-08-22 16:00:03 -07001431 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001432 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1433 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1434 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001435
Adam Cohenad4e15c2013-10-17 16:21:35 -07001436 Runnable onComplete = new Runnable() {
1437 @Override
1438 public void run() {
1439 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001440 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001441 }
1442 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001443 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001444 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001445 }
1446 }
Romain Guycbb89e42009-06-08 15:52:54 -07001447
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001448 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1449 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001450 info.container = container;
1451 info.screenId = screenId;
1452 if (cell != null) {
1453 info.cellX = cell[0];
1454 info.cellY = cell[1];
1455 }
1456 info.spanX = spanX;
1457 info.spanY = spanY;
1458
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001459 switch (info.itemType) {
1460 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1461 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001462 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001463 break;
1464 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001465 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001466 break;
1467 default:
1468 throw new IllegalStateException("Unknown item type: " + info.itemType);
1469 }
1470 }
1471
Adam Cohenfbba09b2011-07-18 21:43:05 -07001472 /**
1473 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001474 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001475 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001476 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1477 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001478 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1479 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1480 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001481 }
1482
Adam Cohenfbba09b2011-07-18 21:43:05 -07001483 /**
1484 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001485 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001486 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001487 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001488 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001489 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001490 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001491 // In the case where we've prebound the widget, we remove it from the DragLayer
1492 if (LOGD) {
1493 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1494 }
1495 getDragLayer().removeView(hostView);
1496
Adam Cohened66b2b2012-01-23 17:28:51 -08001497 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001498 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001499
1500 // Clear the boundWidget so that it doesn't get destroyed.
1501 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001502 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001503 // In this case, we either need to start an activity to get permission to bind
1504 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001505 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1506 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1507 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1508 this, info.componentName);
1509 } else {
1510 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1511 }
Adam Cohendd70d662012-10-04 16:53:44 -07001512 Bundle options = info.bindOptions;
1513
Sunny Goyalffe83f12014-08-14 17:39:34 -07001514 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1515 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001516 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001517 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001518 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001519 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001520 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001521 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001522 }
1523
Adam Cohendcd297f2013-06-18 13:13:40 -07001524 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001525 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001526 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001527 folderInfo.title = getText(R.string.folder_name);
1528
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001529 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001530 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001531
1532 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001533 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301534 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001535 // Force measure the new folder icon
1536 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1537 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001538 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001539 }
Romain Guycbb89e42009-06-08 15:52:54 -07001540
Winsonc0b52fe2015-09-09 16:38:15 -07001541 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001542 * Unbinds the view for the specified item, and removes the item and all its children.
1543 *
1544 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001545 * @param itemInfo the {@link ItemInfo} for this view.
1546 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001547 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001548 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001549 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001550 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001551 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1552 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001553 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001554 } else {
1555 mWorkspace.removeWorkspaceItem(v);
1556 }
Winsonc0b52fe2015-09-09 16:38:15 -07001557 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001558 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001559 }
1560 } else if (itemInfo instanceof FolderInfo) {
1561 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001562 if (v instanceof FolderIcon) {
1563 ((FolderIcon) v).removeListeners();
1564 }
Winsonc0b52fe2015-09-09 16:38:15 -07001565 mWorkspace.removeWorkspaceItem(v);
1566 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001567 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001568 }
1569 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1570 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001571 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001572 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07001573 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001574 }
1575 } else {
1576 return false;
1577 }
1578 return true;
1579 }
1580
1581 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07001582 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07001583 */
Sunny Goyal56c73602015-09-25 12:55:01 -07001584 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07001585 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07001586 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07001587 // Deleting an app widget ID is a void call but writes to disk before returning
1588 // to the caller...
1589 new AsyncTask<Void, Void, Void>() {
1590 public Void doInBackground(Void ... args) {
1591 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
1592 return null;
1593 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07001594 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07001595 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001596 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001597 }
1598
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001599 @Override
1600 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001601 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001602 }
1603
Joe Onorato88ec0992009-11-19 13:16:06 -08001604 @Override
1605 public void onBackPressed() {
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08001606 if (finishAutoCancelActionMode()) {
1607 return;
1608 }
Adam Cohen9211d422014-10-07 18:14:53 -07001609 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1610 return;
1611 }
1612
Sunny Goyal45478022015-06-08 16:52:41 -07001613 if (mDragController.isDragging()) {
1614 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001615 return;
1616 }
1617
Jon Mirandafeba90f2016-10-06 10:53:29 -07001618 // Note: There should be at most one log per method call. This is enforced implicitly
1619 // by using if-else statements.
1620 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001621 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
1622 if (topView != null) {
Sunny Goyal37920962017-09-28 13:43:24 -07001623 topView.onBackPressed();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001624 } else if (!isInState(NORMAL)) {
Sunny Goyal49bcf342018-01-11 13:59:53 -08001625 LauncherState lastState = mStateManager.getLastState();
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001626 ued.logActionCommand(Action.Command.BACK, mStateManager.getState().containerType,
1627 lastState.containerType);
Sunny Goyal49bcf342018-01-11 13:59:53 -08001628 mStateManager.goToState(lastState);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001629 } else {
1630 // Back button is a no-op here, but give at least some feedback for the button press
1631 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001632 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001633 }
1634
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001635 /**
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001636 * Event handler for the wallpaper picker button that appears after a long press
1637 * on the home screen.
1638 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001639 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07001640 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07001641 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
1642 return;
1643 }
1644
Tony Wickham785f7a52015-08-31 17:28:32 -07001645 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
1646 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal2100c782016-08-22 16:00:03 -07001647 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001648 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001649 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001650
1651 String pickerPackage = getString(R.string.wallpaper_picker_package);
Tony85efb4b2017-06-02 13:06:10 -07001652 boolean hasTargetPackage = !TextUtils.isEmpty(pickerPackage);
1653 if (hasTargetPackage) {
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001654 intent.setPackage(pickerPackage);
1655 }
1656
Sunny Goyala5ace712017-11-15 17:12:51 -08001657 final Bundle launchOptions;
1658 if (v != null) {
1659 intent.setSourceBounds(getViewBounds(v));
1660 // If there is no target package, use the default intent chooser animation
Jon Miranda17940f02018-01-18 14:46:34 -08001661 launchOptions = hasTargetPackage
Tony Wickham005df0b2018-02-13 11:28:12 -08001662 ? getActivityLaunchOptionsAsBundle(v, isInMultiWindowModeCompat())
Jon Miranda17940f02018-01-18 14:46:34 -08001663 : null;
Sunny Goyala5ace712017-11-15 17:12:51 -08001664 } else {
1665 launchOptions = null;
1666 }
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001667 try {
Sunny Goyala5ace712017-11-15 17:12:51 -08001668 startActivityForResult(intent, REQUEST_PICK_WALLPAPER, launchOptions);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001669 } catch (ActivityNotFoundException e) {
1670 setWaitingForResult(null);
1671 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1672 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001673 }
1674
Sunny Goyala7ce1662016-05-31 15:01:35 -07001675 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001676 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001677 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
1678 try {
1679 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
1680 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
1681 // is enabled by default on NYC.
1682 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
1683 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001684
1685 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
1686 String id = ((ShortcutInfo) info).getDeepShortcutId();
1687 String packageName = intent.getPackage();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301688 DeepShortcutManager.getInstance(this).startShortcut(
Sunny Goyal9994b2b2016-06-23 14:17:24 -07001689 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001690 } else {
1691 // Could be launching some bookkeeping activity
1692 startActivity(intent, optsBundle);
1693 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07001694 } finally {
1695 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01001696 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001697 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001698 // Due to legacy reasons, direct call shortcuts require Launchers to have the
1699 // corresponding permission. Show the appropriate permission prompt if that
1700 // is the case.
1701 if (intent.getComponent() == null
1702 && Intent.ACTION_CALL.equals(intent.getAction())
1703 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
1704 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001705
1706 setWaitingForResult(PendingRequestArgs
1707 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07001708 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
1709 REQUEST_PERMISSION_CALL_PHONE);
1710 } else {
1711 // No idea why this was thrown.
1712 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07001713 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001714 }
1715 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001716
Tony Wickham005df0b2018-02-13 11:28:12 -08001717 public Bundle getActivityLaunchOptionsAsBundle(View v, boolean useDefaultLaunchOptions) {
1718 ActivityOptions activityOptions = getActivityLaunchOptions(v, useDefaultLaunchOptions);
1719 return activityOptions == null ? null : activityOptions.toBundle();
1720 }
1721
Sunny Goyalfe770c92016-09-27 14:38:58 -07001722 @TargetApi(Build.VERSION_CODES.M)
Tony Wickham005df0b2018-02-13 11:28:12 -08001723 public ActivityOptions getActivityLaunchOptions(View v, boolean useDefaultLaunchOptions) {
Jon Miranda17940f02018-01-18 14:46:34 -08001724 return useDefaultLaunchOptions
Jon Miranda54441f52018-01-24 15:38:25 -08001725 ? mAppTransitionManager.getDefaultActivityLaunchOptions(this, v)
1726 : mAppTransitionManager.getActivityLaunchOptions(this, v);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001727 }
1728
Tonye3c59252017-05-02 21:32:27 -07001729 public Rect getViewBounds(View v) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001730 int[] pos = new int[2];
1731 v.getLocationOnScreen(pos);
1732 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
1733 }
1734
Sunny Goyala7ce1662016-05-31 15:01:35 -07001735 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07001736 mAppLaunchSuccess = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001737 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
1738 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07001739 return mAppLaunchSuccess;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001740 }
Jon Miranda17940f02018-01-18 14:46:34 -08001741
Sunny Goyala7ce1662016-05-31 15:01:35 -07001742 // Only launch using the new animation if the shortcut has not opted out (this is a
1743 // private contract between launcher and may be ignored in the future).
1744 boolean useLaunchAnimation = (v != null) &&
1745 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Jon Miranda17940f02018-01-18 14:46:34 -08001746 Bundle optsBundle = useLaunchAnimation
Tony Wickham005df0b2018-02-13 11:28:12 -08001747 ? getActivityLaunchOptionsAsBundle(v, isInMultiWindowModeCompat())
Jon Miranda17940f02018-01-18 14:46:34 -08001748 : null;
Sunny Goyala7ce1662016-05-31 15:01:35 -07001749
Sunny Goyal24bb66a2017-03-21 15:12:01 -07001750 UserHandle user = item == null ? null : item.user;
Sunny Goyala7ce1662016-05-31 15:01:35 -07001751
1752 // Prepare intent
1753 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1754 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001755 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07001756 }
1757 try {
Jon Miranda84e71bf2018-02-13 15:36:20 -08001758 boolean isShortcut = Utilities.ATLEAST_MARSHMALLOW
1759 && (item instanceof ShortcutInfo)
1760 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
1761 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
1762 && !((ShortcutInfo) item).isPromise();
Jon Miranda17940f02018-01-18 14:46:34 -08001763 if (isShortcut) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001764 // Shortcuts need some special checks due to legacy reasons.
1765 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001766 } else if (user == null || user.equals(Process.myUserHandle())) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001767 // Could be launching some bookkeeping activity
1768 startActivity(intent, optsBundle);
1769 } else {
1770 LauncherAppsCompat.getInstance(this).startActivityForProfile(
1771 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
1772 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001773
1774 if (v instanceof BubbleTextView) {
1775 // This is set to the view that launched the activity that navigated the user away
1776 // from launcher. Since there is no callback for when the activity has finished
1777 // launching, enable the press state and keep this reference to reset the press
1778 // state when we return to launcher.
1779 BubbleTextView btv = (BubbleTextView) v;
1780 btv.setStayPressed(true);
1781 setOnResumeCallback(btv);
1782 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07001783 mAppLaunchSuccess = true;
1784 getUserEventDispatcher().logAppLaunch(v, intent); // TODO for discovered apps b/35802115
Sunny Goyala7ce1662016-05-31 15:01:35 -07001785 } catch (ActivityNotFoundException|SecurityException e) {
1786 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1787 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
1788 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07001789 return mAppLaunchSuccess;
Michael Jurka86a720e2012-05-09 11:23:23 -07001790 }
1791
Winson Chung3d503fb2011-07-13 17:25:49 -07001792 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001793 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07001794 return mHotseat != null && layout != null &&
1795 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
1796 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001797
Winson Chung3d503fb2011-07-13 17:25:49 -07001798 /**
1799 * Returns the CellLayout of the specified container at the specified screen.
1800 */
Sunny Goyal92820592015-03-02 11:31:35 -08001801 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001802 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1803 if (mHotseat != null) {
1804 return mHotseat.getLayout();
1805 } else {
1806 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08001807 }
Winson Chung3d503fb2011-07-13 17:25:49 -07001808 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08001809 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08001810 }
1811 }
1812
Michael Jurkae326f182011-11-21 14:05:46 -08001813 @Override
1814 public void onTrimMemory(int level) {
1815 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07001816 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
1817 // The widget preview db can result in holding onto over
1818 // 3MB of memory for caching which isn't necessary.
1819 SQLiteDatabase.releaseMemory();
1820
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08001821 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07001822 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08001823 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01001824 if (mLauncherCallbacks != null) {
1825 mLauncherCallbacks.onTrimMemory(level);
1826 }
Winson Chung62a70be2018-02-23 15:10:05 -08001827 UiFactory.onTrimMemory(this, level);
Michael Jurkae326f182011-11-21 14:05:46 -08001828 }
1829
Michael Jurkad7c28052012-04-27 15:43:36 -07001830 @Override
1831 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07001832 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07001833 final List<CharSequence> text = event.getText();
1834 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07001835 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001836 // TODO: When can workspace be null?
1837 text.add(mWorkspace == null
1838 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07001839 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07001840 return result;
1841 }
1842
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001843 public void setOnResumeCallback(OnResumeCallback callback) {
1844 if (mOnResumeCallback != null) {
1845 mOnResumeCallback.onLauncherResume();
1846 }
1847 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07001848 }
1849
Sunny Goyal60876ac2018-02-20 14:21:20 -08001850 public void setOnStartCallback(OnStartCallback callback) {
1851 mOnStartCallback = callback;
1852 }
1853
Michael Jurka7607c2f2013-04-03 14:33:19 -07001854 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001855 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001856 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07001857 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001858 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07001859 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001860 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07001861 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07001862 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07001863 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001864 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001865
Joe Onorato9c1289c2009-08-17 11:03:03 -04001866 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07001867 * Clear any pending bind callbacks. This is called when is loader is planning to
1868 * perform a full rebind from scratch.
1869 */
1870 @Override
1871 public void clearPendingBinds() {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001872 if (mPendingExecutor != null) {
1873 mPendingExecutor.markCompleted();
1874 mPendingExecutor = null;
1875 }
1876 }
1877
1878 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001879 * Refreshes the shortcuts shown on the workspace.
1880 *
1881 * Implementation of the method from LauncherModel.Callbacks.
1882 */
1883 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001884 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001885 // Floating panels (except the full widget sheet) are associated with individual icons. If
1886 // we are starting a fresh bind, close all such panels as all the icons are about
1887 // to go away.
1888 AbstractFloatingView.closeOpenViews(this, true,
Sunny Goyal7ede6112017-12-05 15:11:21 -08001889 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_REBIND_SAFE);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08001890
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001891 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08001892
Winson Chungd64d1762013-08-20 14:37:16 -07001893 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07001894 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07001895 mWorkspace.removeAllWorkspaceScreens();
Sunny Goyalc11fac32018-02-27 19:20:15 -08001896 mAppWidgetHost.clearViews();
Winson Chungd64d1762013-08-20 14:37:16 -07001897
Winson Chung3d503fb2011-07-13 17:25:49 -07001898 if (mHotseat != null) {
1899 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001900 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001901 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001902 }
1903
Adam Cohendcd297f2013-06-18 13:13:40 -07001904 @Override
1905 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001906 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001907 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
1908 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001909 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
1910 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001911 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001912 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
1913 // If there are no screens, we need to have an empty screen
1914 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07001915 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001916 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07001917
Winsonc7d2e832016-07-28 12:24:55 -07001918 // After we have added all the screens, if the wallpaper was locked to the default state,
1919 // then notify to indicate that it can be released and a proper wallpaper offset can be
1920 // computed before the next layout
1921 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07001922 }
1923
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001924 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001925 int count = orderedScreenIds.size();
1926 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001927 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001928 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001929 // No need to bind the first screen, as its always bound.
1930 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
1931 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001932 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001933 }
1934
Sunny Goyalb23980c2017-08-17 07:45:25 -07001935 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08001936 public void bindAppsAdded(ArrayList<Long> newScreens, ArrayList<ItemInfo> addNotAnimated,
1937 ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07001938 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08001939 if (newScreens != null) {
1940 bindAddScreens(newScreens);
1941 }
Winson Chungd64d1762013-08-20 14:37:16 -07001942
1943 // We add the items without animation on non-visible pages, and with
1944 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08001945 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001946 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001947 }
Adam Cohen76a47a12014-02-05 11:47:43 -08001948 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07001949 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07001950 }
Winson Chungc58497e2013-09-03 17:48:37 -07001951
Winson Chung87412982013-10-03 18:34:14 -07001952 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07001953 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07001954 }
1955
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001956 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001957 * Bind the items start-end from the list.
1958 *
1959 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001960 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07001961 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07001962 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001963 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07001964 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001965 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07001966 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001967 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001968 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07001969 int end = items.size();
1970 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08001971 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07001972
1973 // Short circuit if we are loading dock items for a configuration which has no dock
1974 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
1975 mHotseat == null) {
1976 continue;
1977 }
1978
Sunny Goyal639e9062015-08-19 19:17:06 -07001979 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001980 switch (item.itemType) {
1981 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1982 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08001983 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001984 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07001985 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001986 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001987 }
1988 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07001989 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001990 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001991 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001992 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001993 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07001994 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1995 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07001996 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07001997 if (view == null) {
1998 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08001999 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08002000 break;
2001 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002002 default:
2003 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002004 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002005
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002006 /*
2007 * Remove colliding items.
2008 */
2009 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
2010 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
2011 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
2012 View v = cl.getChildAt(item.cellX, item.cellY);
2013 Object tag = v.getTag();
2014 String desc = "Collision while binding workspace item: " + item
2015 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08002016 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002017 throw (new RuntimeException(desc));
2018 } else {
2019 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002020 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002021 continue;
2022 }
2023 }
2024 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302025 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07002026 if (animateIcons) {
2027 // Animate all the applications up now
2028 view.setAlpha(0f);
2029 view.setScaleX(0f);
2030 view.setScaleY(0f);
2031 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08002032 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07002033 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002034 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07002035
Winson Chung997a9232013-07-24 15:33:46 -07002036 if (animateIcons) {
2037 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08002038 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07002039 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08002040 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07002041 final Runnable startBounceAnimRunnable = new Runnable() {
2042 public void run() {
2043 anim.playTogether(bounceAnims);
2044 anim.start();
2045 }
2046 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08002047 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07002048 // We post the animation slightly delayed to prevent slowdowns
2049 // when we are loading right after we return to launcher.
2050 mWorkspace.postDelayed(new Runnable() {
2051 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00002052 if (mWorkspace != null) {
2053 mWorkspace.snapToPage(newScreenIndex);
2054 mWorkspace.postDelayed(startBounceAnimRunnable,
2055 NEW_APPS_ANIMATION_DELAY);
2056 }
Winson Chung94d67682013-09-25 16:29:40 -07002057 }
2058 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07002059 } else {
2060 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07002061 }
2062 }
Winson Chung64359a52013-07-08 17:17:08 -07002063 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002064 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002065 }
2066
Joe Onorato9c1289c2009-08-17 11:03:03 -04002067 /**
2068 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002069 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07002070 public void bindAppWidget(LauncherAppWidgetInfo item) {
2071 View view = inflateAppWidget(item);
2072 if (view != null) {
2073 mWorkspace.addInScreen(view, item);
2074 mWorkspace.requestLayout();
2075 }
2076 }
2077
2078 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002079 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302080 PendingAppWidgetHostView view =
2081 new PendingAppWidgetHostView(this, item, mIconCache, true);
2082 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002083 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002084 }
2085
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002086 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002087
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002088 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08002089
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002090 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2091 // If the provider is not ready, bind as a pending widget.
2092 appWidgetInfo = null;
2093 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2094 // The widget id is not valid. Try to find the widget based on the provider info.
2095 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
2096 } else {
2097 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
2098 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002099
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002100 // If the provider is ready, but the width is not yet restored, try to restore it.
2101 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
2102 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07002103 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002104 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
2105 + " belongs to component " + item.providerName
2106 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002107 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002108 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002109 }
Sunny Goyalff572272014-07-23 13:58:07 -07002110
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002111 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002112 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07002113 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2114 // Id has not been allocated yet. Allocate a new id.
2115 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
2116 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07002117
Sunny Goyald478c832016-04-01 12:04:16 -07002118 // Also try to bind the widget. If the bind fails, the user will be shown
2119 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08002120 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07002121 pendingInfo.spanX = item.spanX;
2122 pendingInfo.spanY = item.spanY;
2123 pendingInfo.minSpanX = item.minSpanX;
2124 pendingInfo.minSpanY = item.minSpanY;
2125 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07002126
2127 boolean isDirectConfig =
2128 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2129 if (isDirectConfig && item.bindOptions != null) {
2130 Bundle newOptions = item.bindOptions.getExtras();
2131 if (options != null) {
2132 newOptions.putAll(options);
2133 }
2134 options = newOptions;
2135 }
Sunny Goyald478c832016-04-01 12:04:16 -07002136 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2137 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002138
Sunny Goyal86df1382016-08-10 15:03:22 -07002139 // We tried to bind once. If we were not able to bind, we would need to
2140 // go through the permission dialog, which means we cannot skip the config
2141 // activity.
2142 item.bindOptions = null;
2143 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2144
Sunny Goyald478c832016-04-01 12:04:16 -07002145 // Bind succeeded
2146 if (success) {
2147 // If the widget has a configure activity, it is still needs to set it up,
2148 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002149 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002150 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2151 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002152 }
Sunny Goyald478c832016-04-01 12:04:16 -07002153
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002154 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002155 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002156 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002157 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002158 // The widget was marked as UI not ready, but there is no configure activity to
2159 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002160 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002161 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002162 }
Sunny Goyalff572272014-07-23 13:58:07 -07002163 }
2164
Sunny Goyald5462aa2016-11-22 16:52:39 +05302165 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002166 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002167 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002168 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002169 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07002170 deleteWidgetInfo(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002171 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002172 }
2173
Sunny Goyal233ee962015-08-03 13:05:01 -07002174 item.minSpanX = appWidgetInfo.minSpanX;
2175 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302176 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002177 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302178 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002179 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302180 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002181
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002182 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002183 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002184 }
2185
Sunny Goyalff572272014-07-23 13:58:07 -07002186 /**
2187 * Restores a pending widget.
2188 *
2189 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002190 */
Sunny Goyald478c832016-04-01 12:04:16 -07002191 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002192 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2193 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2194 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002195 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002196 }
2197
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002198 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002199 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002200 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2201 info.pendingItemInfo = null;
2202 }
Sunny Goyalff572272014-07-23 13:58:07 -07002203
Sunny Goyalba47faa2017-10-04 16:50:57 -07002204 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002205 view.reInflate();
Sunny Goyalba47faa2017-10-04 16:50:57 -07002206 }
2207
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002208 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002209 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002210 }
2211
Adam Cohen1462de32012-07-24 22:34:36 -07002212 public void onPageBoundSynchronously(int page) {
Sunny Goyalf4aceab2017-12-06 16:16:31 -08002213 mSynchronouslyBoundPage = page;
Adam Cohen1462de32012-07-24 22:34:36 -07002214 }
2215
Sunny Goyal527c7d32015-08-28 15:19:36 -07002216 @Override
2217 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2218 if (mPendingExecutor != null) {
2219 mPendingExecutor.markCompleted();
2220 }
2221 mPendingExecutor = executor;
Sunny Goyal60180b02018-02-07 12:56:30 -08002222 if (!isInState(ALL_APPS)) {
2223 mAppsView.getAppsStore().setDeferUpdates(true);
2224 mPendingExecutor.execute(() -> mAppsView.getAppsStore().setDeferUpdates(false));
2225 }
2226
Sunny Goyal527c7d32015-08-28 15:19:36 -07002227 executor.attachTo(this);
2228 }
2229
2230 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2231 if (mPendingExecutor == executor) {
2232 mPendingExecutor = null;
2233 }
2234 }
2235
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002236 @Override
2237 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002238 if (mDragLayer.getAlpha() < 1) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002239 mDragLayer.animate().alpha(1).withEndAction(
2240 executor == null ? null : executor::onLoadAnimationCompleted).start();
2241 } else if (executor != null) {
2242 executor.onLoadAnimationCompleted();
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002243 }
2244 }
2245
Joe Onorato9c1289c2009-08-17 11:03:03 -04002246 /**
2247 * Callback saying that there aren't any more items to bind.
2248 *
2249 * Implementation of the method from LauncherModel.Callbacks.
2250 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002251 public void finishBindingItems() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002252 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002253 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002254
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002255 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002256
Sunny Goyal2100c782016-08-22 16:00:03 -07002257 if (mPendingActivityResult != null) {
2258 handleActivityResult(mPendingActivityResult.requestCode,
2259 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2260 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002261 }
2262
Sunny Goyala474a9b2017-05-04 16:47:11 -07002263 InstallShortcutReceiver.disableAndFlushInstallQueue(
2264 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002265
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002266 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002267 }
2268
Winson Chunga2413752012-04-03 14:22:34 -07002269 private boolean canRunNewAppsAnimation() {
2270 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002271 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002272 }
2273
Winson Chungc9168342013-06-26 14:54:55 -07002274 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002275 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07002276 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
2277 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002278 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002279 return bounceAnim;
2280 }
2281
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002282 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002283 * Add the icons for all apps.
2284 *
2285 * Implementation of the method from LauncherModel.Callbacks.
2286 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002287 public void bindAllApplications(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002288 mAppsView.getAppsStore().setApps(apps);
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002289
Adam Cohen9211d422014-10-07 18:14:53 -07002290 if (mLauncherCallbacks != null) {
2291 mLauncherCallbacks.bindAllApplications(apps);
2292 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002293 }
2294
2295 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002296 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
2297 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2298 */
2299 @Override
2300 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002301 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002302 }
2303
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002304 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002305 * A package was updated.
2306 *
2307 * Implementation of the method from LauncherModel.Callbacks.
2308 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002309 @Override
2310 public void bindAppsAddedOrUpdated(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002311 mAppsView.getAppsStore().addOrUpdateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002312 }
2313
Sunny Goyal4390ace2014-10-13 11:33:11 -07002314 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002315 public void bindPromiseAppProgressUpdated(PromiseAppInfo app) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002316 mAppsView.getAppsStore().updatePromiseAppProgress(app);
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002317 }
2318
2319 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002320 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002321 mWorkspace.widgetsRestored(widgets);
2322 }
2323
Joe Onorato9c1289c2009-08-17 11:03:03 -04002324 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002325 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002326 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002327 *
2328 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002329 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002330 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002331 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002332 if (!updated.isEmpty()) {
2333 mWorkspace.updateShortcuts(updated);
2334 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002335 }
2336
2337 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002338 * Update the state of a package, typically related to install state.
2339 *
2340 * Implementation of the method from LauncherModel.Callbacks.
2341 */
Sunny Goyale755d462014-07-22 13:48:29 -07002342 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002343 public void bindRestoreItemsChange(HashSet<ItemInfo> updates) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07002344 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07002345 }
2346
2347 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002348 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07002349 * in addition to specific applications to remove, the reason being that
2350 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002351 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07002352 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002353 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07002354 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002355 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002356 mWorkspace.removeItemsByMatcher(matcher);
2357 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002358 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002359
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002360 @Override
2361 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002362 mAppsView.getAppsStore().removeApps(appInfos);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002363 }
Joe Onoratobe386092009-11-17 17:32:16 -08002364
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002365 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002366 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
2367 mPopupDataProvider.setAllWidgets(allWidgets);
Tony Wickham26b17462017-03-20 17:12:24 -07002368 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
2369 if (topView != null) {
2370 topView.onWidgetsBound();
2371 }
2372 }
2373
Tony Wickham86222d22017-03-29 15:30:43 -07002374 /**
2375 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
2376 * refreshes the widgets and shortcuts associated with the given package/user
2377 */
2378 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07002379 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002380 }
2381
Winson Chung80baf5a2010-08-09 16:03:15 -07002382 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002383 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08002384 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002385 @Override
2386 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
2387 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002388
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002389 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
2390 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07002391 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002392 writer.println(prefix + " Homescreen " + i);
2393
2394 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
2395 for (int j = 0; j < layout.getChildCount(); j++) {
2396 Object tag = layout.getChildAt(j).getTag();
2397 if (tag != null) {
2398 writer.println(prefix + " " + tag.toString());
2399 }
2400 }
2401 }
2402
2403 writer.println(prefix + " Hotseat");
2404 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07002405 for (int j = 0; j < layout.getChildCount(); j++) {
2406 Object tag = layout.getChildAt(j).getTag();
2407 if (tag != null) {
2408 writer.println(prefix + " " + tag.toString());
2409 }
Adam Cohen4caf2982013-08-20 18:54:31 -07002410 }
Sunny Goyala1365452015-10-01 15:46:24 -07002411 }
2412
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002413 writer.println(prefix + "Misc:");
2414 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
2415 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
2416 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
Sunny Goyal2bba1902018-02-27 12:20:50 -08002417 writer.println(" deviceProfile isTransposed=" + getDeviceProfile().isVerticalBarLayout());
2418 writer.println(" orientation=" + getResources().getConfiguration().orientation);
2419
2420 try {
2421 FileLog.flushAll(writer);
2422 } catch (Exception e) {
2423 // Ignore
2424 }
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002425
2426 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002427
Adam Cohen9211d422014-10-07 18:14:53 -07002428 if (mLauncherCallbacks != null) {
2429 mLauncherCallbacks.dump(prefix, fd, writer, args);
2430 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002431 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002432
Sunny Goyal66b24572016-09-21 15:57:55 -07002433 @Override
2434 @TargetApi(Build.VERSION_CODES.N)
2435 public void onProvideKeyboardShortcuts(
2436 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
2437
2438 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002439 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002440 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
2441 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
2442 }
2443 View currentFocus = getCurrentFocus();
2444 if (new CustomActionsPopup(this, currentFocus).canShow()) {
2445 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
2446 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
2447 }
Tony18c4aa42017-05-10 21:23:57 -05002448 if (currentFocus.getTag() instanceof ItemInfo
2449 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002450 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
2451 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
2452 }
2453 if (!shortcutInfos.isEmpty()) {
2454 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
2455 }
2456
2457 super.onProvideKeyboardShortcuts(data, menu, deviceId);
2458 }
2459
2460 @Override
2461 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
2462 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
2463 switch (keyCode) {
2464 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002465 if (isInState(NORMAL)) {
2466 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07002467 return true;
2468 }
2469 break;
2470 case KeyEvent.KEYCODE_S: {
2471 View focusedView = getCurrentFocus();
2472 if (focusedView instanceof BubbleTextView
2473 && focusedView.getTag() instanceof ItemInfo
2474 && mAccessibilityDelegate.performAction(focusedView,
2475 (ItemInfo) focusedView.getTag(),
2476 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal00ac9202017-11-09 15:24:06 -08002477 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07002478 return true;
2479 }
2480 break;
2481 }
2482 case KeyEvent.KEYCODE_O:
2483 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
2484 return true;
2485 }
2486 break;
2487 }
2488 }
2489 return super.onKeyShortcut(keyCode, event);
2490 }
2491
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002492 @Override
2493 public boolean onKeyUp(int keyCode, KeyEvent event) {
2494 if (keyCode == KeyEvent.KEYCODE_MENU) {
2495 // KEYCODE_MENU is sent by some tests, for example
2496 // LauncherJankTests#testWidgetsContainerFling. Don't just remove its handling.
2497 if (!mDragController.isDragging() && !mWorkspace.isSwitchingState() &&
2498 isInState(NORMAL)) {
2499 // Close any open floating views.
2500 AbstractFloatingView.closeAllOpenViews(this);
2501
2502 // Setting the touch point to (-1, -1) will show the options popup in the center of
2503 // the screen.
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08002504 OptionsPopupView.show(this, -1, -1);
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002505 }
2506 return true;
2507 }
2508 return super.onKeyUp(keyCode, event);
2509 }
2510
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002511 public static Launcher getLauncher(Context context) {
2512 if (context instanceof Launcher) {
2513 return (Launcher) context;
2514 }
2515 return ((Launcher) ((ContextWrapper) context).getBaseContext());
2516 }
2517
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08002518 @Override
2519 public void onActionModeStarted(ActionMode mode) {
2520 super.onActionModeStarted(mode);
2521 mCurrentActionMode = mode;
2522 }
2523
2524 @Override
2525 public void onActionModeFinished(ActionMode mode) {
2526 super.onActionModeFinished(mode);
2527 mCurrentActionMode = null;
2528 }
2529
2530 public boolean finishAutoCancelActionMode() {
2531 if (mCurrentActionMode != null && AUTO_CANCEL_ACTION_MODE == mCurrentActionMode.getTag()) {
2532 mCurrentActionMode.finish();
2533 return true;
2534 }
2535 return false;
2536 }
2537
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002538 /**
2539 * Callback for listening for onResume
2540 */
2541 public interface OnResumeCallback {
2542
2543 void onLauncherResume();
2544 }
Sunny Goyal60876ac2018-02-20 14:21:20 -08002545
2546 /**
2547 * Callback for listening for onStart
2548 */
2549 public interface OnStartCallback {
2550
2551 void onLauncherStart(Launcher launcher);
2552 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002553}