blob: 5da4944d4e91b240ba60090cbd947369bc6c07d4 [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 Goyalc99cb172017-10-19 16:15:09 -070021import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_NOSENSOR;
22import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Sunny Goyal076839c2017-10-30 13:52:20 -070023import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_BY_PUBLISHER;
24import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_LOCKED_USER;
25import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_QUIET_USER;
26import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_SAFEMODE;
27import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_SUSPENDED;
Sunny Goyal4c7f2152017-10-17 17:17:16 -070028import static com.android.launcher3.LauncherAnimUtils.SPRING_LOADED_EXIT_DELAY;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -070029import static com.android.launcher3.LauncherState.ALL_APPS;
30import static com.android.launcher3.LauncherState.NORMAL;
31import static com.android.launcher3.LauncherState.OVERVIEW;
Sunny Goyalaeb16432017-10-16 11:46:41 -070032import static com.android.launcher3.logging.LoggerUtils.newContainerTarget;
Sunny Goyal326403e2017-10-02 12:45:10 -070033
Sunny Goyal28c6b962015-10-12 11:42:05 -070034import android.Manifest;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070035import android.animation.Animator;
Jon Miranda2d89ea82017-05-04 11:47:53 -070036import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070037import android.animation.AnimatorSet;
Jon Miranda2d89ea82017-05-04 11:47:53 -070038import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070039import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070040import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000041import android.annotation.TargetApi;
Chris Wren40c5ed32014-06-24 18:24:23 -040042import android.app.AlertDialog;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080043import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070044import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080046import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080047import android.content.ComponentCallbacks2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.Context;
Andrew Sappersteinabef55a2016-06-19 12:49:00 -070049import android.content.ContextWrapper;
Chris Wren40c5ed32014-06-24 18:24:23 -040050import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070052import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070053import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070054import android.content.SharedPreferences;
Sunny Goyal745bad92016-05-02 10:54:12 -070055import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056import android.content.pm.PackageManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080057import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070058import android.database.sqlite.SQLiteDatabase;
Jon Mirandacc42c5b2016-10-27 17:15:27 -070059import android.graphics.Point;
Sunny Goyal02424b22018-01-19 11:24:32 -080060import android.graphics.PointF;
Michael Jurkaaf442092010-06-10 17:01:57 -070061import android.graphics.Rect;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070062import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020063import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064import android.os.Bundle;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070065import android.os.Parcelable;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080066import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070067import android.os.StrictMode;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070068import android.os.UserHandle;
Tony Wickham86222d22017-03-29 15:30:43 -070069import android.support.annotation.Nullable;
Michael Jurkaa33411c2012-06-14 16:18:21 -070070import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070072import android.util.Log;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070073import android.util.SparseArray;
Sunny Goyal7c8a65e2017-12-22 11:11:33 -080074import android.view.ActionMode;
Adam Cohen96d30a12013-07-16 18:13:21 -070075import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070076import android.view.HapticFeedbackConstants;
77import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070078import android.view.KeyboardShortcutGroup;
79import android.view.KeyboardShortcutInfo;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080080import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -070081import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070082import android.view.MotionEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070083import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080084import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070085import android.view.ViewGroup;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080086import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070087import android.view.animation.OvershootInterpolator;
Adam Cohen96d30a12013-07-16 18:13:21 -070088import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070089
Sunny Goyal651077b2014-06-30 14:15:31 -070090import com.android.launcher3.DropTarget.DragObject;
Sunny Goyala7ce1662016-05-31 15:01:35 -070091import com.android.launcher3.LauncherSettings.Favorites;
Jon Mirandac476d6e2017-05-04 16:30:01 -070092import com.android.launcher3.Workspace.ItemOperator;
Sunny Goyalae502842016-06-17 08:43:56 -070093import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070094import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070095import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal7ede6112017-12-05 15:11:21 -080096import com.android.launcher3.allapps.DiscoveryBounce;
Sunny Goyalffe83f12014-08-14 17:39:34 -070097import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010098import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -070099import com.android.launcher3.compat.LauncherAppsCompatVO;
Tony Wickhame0c33232016-02-08 11:37:04 -0800100import com.android.launcher3.config.FeatureFlags;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700101import com.android.launcher3.dragndrop.DragController;
102import com.android.launcher3.dragndrop.DragLayer;
Sunny Goyal94b510c2016-08-16 15:36:48 -0700103import com.android.launcher3.dragndrop.DragOptions;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700104import com.android.launcher3.dragndrop.DragView;
Mario Bertschler27288382017-05-24 15:35:09 -0700105import com.android.launcher3.dynamicui.WallpaperColorInfo;
Sunny Goyal26119432016-02-18 22:09:23 +0000106import com.android.launcher3.folder.Folder;
107import com.android.launcher3.folder.FolderIcon;
Sunny Goyal66b24572016-09-21 15:57:55 -0700108import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700109import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700110import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700111import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyala535ae42017-02-27 10:07:13 -0800112import com.android.launcher3.model.ModelWriter;
Sunny Goyala535ae42017-02-27 10:07:13 -0800113import com.android.launcher3.notification.NotificationListener;
Tony Wickham540913e2017-01-23 11:47:51 -0800114import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -0800115import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700116import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyalf8088ee2017-11-10 14:52:00 -0800117import com.android.launcher3.states.InternalStateHandler;
Sunny Goyala5ace712017-11-15 17:12:51 -0800118import com.android.launcher3.uioverrides.UiFactory;
Jon Mirandac6cf4932017-02-07 17:12:36 -0800119import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530120import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
121import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
Sunny Goyalaeb16432017-10-16 11:46:41 -0700122import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyal2100c782016-08-22 16:00:03 -0700123import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700124import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700125import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700126import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700127import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800128import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700129import com.android.launcher3.util.PendingRequestArgs;
Sunny Goyal8392c822017-06-20 10:03:56 -0700130import com.android.launcher3.util.SystemUiController;
Mario Bertschlera6936942017-05-31 14:48:19 -0700131import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700132import com.android.launcher3.util.Thunk;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700133import com.android.launcher3.util.TraceHelper;
Sunny Goyal326403e2017-10-02 12:45:10 -0700134import com.android.launcher3.util.UiThreadHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700135import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyal29947f02017-12-18 13:49:44 -0800136import com.android.launcher3.widget.LauncherAppWidgetHostView;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800137import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700138import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal29947f02017-12-18 13:49:44 -0800139import com.android.launcher3.widget.PendingAppWidgetHostView;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800140import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700141import com.android.launcher3.widget.WidgetHostViewLoader;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700142import com.android.launcher3.widget.WidgetListRowEntry;
143import com.android.launcher3.widget.WidgetsFullSheet;
Sunny Goyal952e63d2017-08-16 04:59:08 -0700144import com.android.launcher3.widget.custom.CustomWidgetParser;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700145
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700146import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700147import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700148import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700149import java.util.Collection;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700150import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700151import java.util.List;
Tony Wickham010d2552017-01-20 08:15:28 -0800152import java.util.Set;
Tony2917a8b2017-07-31 23:29:42 -0700153import java.util.concurrent.Executor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700154
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155/**
156 * Default launcher application.
157 */
Sunny Goyal27835952017-01-13 12:15:53 -0800158public class Launcher extends BaseActivity
Adam Cohen79d90c52016-04-22 13:29:20 -0700159 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
160 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Mario Bertschler27288382017-05-24 15:35:09 -0700161 WallpaperColorInfo.OnThemeChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700162 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700163 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164
Winson Chunga2413752012-04-03 14:22:34 -0700165 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700166
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700168 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700169
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700170 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700171 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172
Michael Jurka8b805b12012-04-18 14:23:14 -0700173 private static final int REQUEST_BIND_APPWIDGET = 11;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700174 private static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
175 private static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700176
Jon Mirandac476d6e2017-05-04 16:30:01 -0700177 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700178
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700179 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
180
Mathew Inwood876a8462013-06-14 14:12:41 +0100181 /**
182 * IntentStarter uses request codes starting with this. This must be greater than all activity
183 * request codes used internally.
184 */
185 protected static final int REQUEST_LAST = 100;
186
Winson Chung2672ff92012-05-04 16:22:30 -0700187 // The Intent extra that defines whether to ignore the launch animation
188 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400189 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700190
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191 // Type: int
192 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700193 // Type: int
194 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700195 // Type: PendingRequestArgs
196 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
197 // Type: ActivityResultInfo
198 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700199 // Type: SparseArray<Parcelable>
200 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800201
Sunny Goyal7c8a65e2017-12-22 11:11:33 -0800202 // When starting an action mode, setting this tag will cause the action mode to be cancelled
203 // automatically when user interacts with the launcher.
204 public static final Object AUTO_CANCEL_ACTION_MODE = new Object();
205
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700206 private LauncherStateManager mStateManager;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700207
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700208 private boolean mIsSafeModeEnabled;
209
Adam Cohenad4e15c2013-10-17 16:21:35 -0700210 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211
Winson Chunga2413752012-04-03 14:22:34 -0700212 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700213 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
214 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
215 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700216
Jon Miranda54441f52018-01-24 15:38:25 -0800217 private LauncherAppTransitionManager mAppTransitionManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800218 private Configuration mOldConfig;
Jon Miranda54441f52018-01-24 15:38:25 -0800219
Adam Cohen091440a2015-03-18 14:16:05 -0700220 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700221 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700222 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800223 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700224
Sunny Goyalffe83f12014-08-14 17:39:34 -0700225 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700226 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700227
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700228 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700229
Sunny Goyal316490e2015-06-02 09:38:28 -0700230 @Thunk Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700231
Sunny Goyal47328fd2016-05-25 18:56:41 -0700232 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700233
234 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700235 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700236 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700237
Winson Chung8ae41982017-11-10 11:42:13 -0800238 // UI and state for the overview panel
239 private ViewGroup mOverviewPanel;
240
Adam Cohen091440a2015-03-18 14:16:05 -0700241 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400242
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700243 private OnResumeCallback mOnResumeCallback;
244
Sunny Goyal527c7d32015-08-28 15:19:36 -0700245 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700246
Joe Onorato9c1289c2009-08-17 11:03:03 -0400247 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800248 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800249 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700250 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400251
Jon Miranda2d89ea82017-05-04 11:47:53 -0700252 private ObjectAnimator mScrimAnimator;
Jon Miranda7fda2852017-07-19 16:07:01 -0700253 private boolean mShouldFadeInScrim;
Jon Miranda2d89ea82017-05-04 11:47:53 -0700254
Tony Wickham010d2552017-01-20 08:15:28 -0800255 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700256
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800257 private int mSynchronouslyBoundPage = PagedView.INVALID_PAGE;
Adam Cohen1462de32012-07-24 22:34:36 -0700258
Winson Chung46353de2012-02-16 14:05:10 -0800259 // We only want to get the SharedPreferences once since it does an FS stat each time we get
260 // it from the context.
261 private SharedPreferences mSharedPrefs;
262
Sunny Goyal2100c782016-08-22 16:00:03 -0700263 // Activity result which needs to be processed after workspace has loaded.
264 private ActivityResultInfo mPendingActivityResult;
265 /**
266 * Holds extra information required to handle a result from an external call, like
267 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
268 */
269 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800270
Sunny Goyal02424b22018-01-19 11:24:32 -0800271 private final PointF mLastDispatchTouchEvent = new PointF();
Jon Miranda379198e2016-09-23 08:54:40 -0700272
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700273 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700274 private boolean mRotationEnabled = false;
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700275 private boolean mAppLaunchSuccess;
276
Sunny Goyal745bad92016-05-02 10:54:12 -0700277 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7c8a65e2017-12-22 11:11:33 -0800278 private ActionMode mCurrentActionMode;
Sunny Goyal7779d622015-06-11 16:18:39 -0700279
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800280 @Override
281 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700282 if (DEBUG_STRICT_MODE) {
283 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
284 .detectDiskReads()
285 .detectDiskWrites()
286 .detectNetwork() // or .detectAll() for all detectable problems
287 .penaltyLog()
288 .build());
289 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
290 .detectLeakedSqlLiteObjects()
291 .detectLeakedClosableObjects()
292 .penaltyLog()
293 .penaltyDeath()
294 .build());
295 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700296 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700297
Adam Cohen9211d422014-10-07 18:14:53 -0700298 if (mLauncherCallbacks != null) {
299 mLauncherCallbacks.preOnCreate();
300 }
301
Mario Bertschler27288382017-05-24 15:35:09 -0700302 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
303 wallpaperColorInfo.setOnThemeChangeListener(this);
Mario Bertschlera6936942017-05-31 14:48:19 -0700304 overrideTheme(wallpaperColorInfo.isDark(), wallpaperColorInfo.supportsDarkText());
Mario Bertschler27288382017-05-24 15:35:09 -0700305
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800306 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700307 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400308
Sunny Goyal87f784c2017-01-11 10:48:34 -0800309 LauncherAppState app = LauncherAppState.getInstance(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800310 mOldConfig = new Configuration(getResources().getConfiguration());
311 initDeviceProfile(app.getInvariantDeviceProfile());
Winson Chung6e1c0d32013-10-25 15:24:24 -0700312
Sunny Goyalf7258242015-10-19 16:59:07 -0700313 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700314 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800315 mModel = app.setLauncher(this);
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800316 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout());
Joe Onorato0589f0f2010-02-08 13:44:00 -0800317 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700318 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700319
Joe Onorato41a12d22009-10-31 18:30:00 -0400320 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700321 mAllAppsController = new AllAppsTransitionController(this);
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800322 mStateManager = new LauncherStateManager(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700323
Sunny Goyalffe83f12014-08-14 17:39:34 -0700324 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700325
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700326 mAppWidgetHost = new LauncherAppWidgetHost(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700327 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700328
Sunny Goyal60820d72017-05-09 12:40:11 -0700329 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700330
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800331 setupViews();
Tony Wickham010d2552017-01-20 08:15:28 -0800332 mPopupDataProvider = new PopupDataProvider(this);
333
Sunny Goyald3864fa2017-11-14 15:06:36 -0800334 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
335 // In case we are on a device with locked rotation, we should look at preferences to check
336 // if the user has specifically allowed rotation.
337 if (!mRotationEnabled) {
338 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
339 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
340 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
341 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800342
Sunny Goyald3864fa2017-11-14 15:06:36 -0800343 boolean internalStateHandled = InternalStateHandler.handleCreate(this, getIntent());
344 if (internalStateHandled) {
345 // Temporarily enable the rotation
346 mRotationEnabled = true;
347
348 if (savedInstanceState != null) {
349 // InternalStateHandler has already set the appropriate state.
350 // We dont need to do anything.
351 savedInstanceState.remove(RUNTIME_STATE);
352 }
353 }
354 restoreState(savedInstanceState);
Winson Chungb63b44c2017-11-10 17:54:44 -0800355
Sunny Goyal2100c782016-08-22 16:00:03 -0700356 // We only load the page synchronously if the user rotates (or triggers a
357 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700358 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
359 if (savedInstanceState != null) {
360 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
361 }
Sunny Goyald3864fa2017-11-14 15:06:36 -0800362
Sunny Goyalfe770c92016-09-27 14:38:58 -0700363 if (!mModel.startLoader(currentScreen)) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800364 if (!internalStateHandled) {
365 // If we are not binding synchronously, show a fade in animation when
366 // the first page bind completes.
367 mDragLayer.setAlpha(0);
368 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700369 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700370 // Pages bound synchronously.
371 mWorkspace.setCurrentPage(currentScreen);
372
Sunny Goyal2100c782016-08-22 16:00:03 -0700373 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800374 }
375
376 // For handling default keys
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800377 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800378
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700379 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
380 // we want the screen to auto-rotate based on the current orientation
Sunny Goyalc99cb172017-10-19 16:15:09 -0700381 setRequestedOrientation(mRotationEnabled
382 ? SCREEN_ORIENTATION_UNSPECIFIED : SCREEN_ORIENTATION_NOSENSOR);
Winson Chungaf40f202013-09-18 18:26:31 -0700383
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800384 setContentView(mLauncherView);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800385 getRootView().dispatchInsets();
Mario Bertschlera6936942017-05-31 14:48:19 -0700386
Jon Miranda7fda2852017-07-19 16:07:01 -0700387 // Listen for broadcasts
388 IntentFilter filter = new IntentFilter();
389 filter.addAction(Intent.ACTION_SCREEN_OFF);
390 filter.addAction(Intent.ACTION_USER_PRESENT); // When the device wakes up + keyguard is gone
391 registerReceiver(mReceiver, filter);
392 mShouldFadeInScrim = true;
Sunny Goyala616d2b2017-06-12 13:54:01 -0700393
Sunny Goyal8392c822017-06-20 10:03:56 -0700394 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
395 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700396
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800397 mAppTransitionManager = LauncherAppTransitionManager.newInstance(this);
Jon Miranda7853bdb2018-01-22 17:32:30 -0800398
Sunny Goyal14b32402017-07-31 10:03:28 -0700399 if (mLauncherCallbacks != null) {
400 mLauncherCallbacks.onCreate(savedInstanceState);
401 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700402
403 TraceHelper.endSection("Launcher-onCreate");
Adam Cohen9211d422014-10-07 18:14:53 -0700404 }
405
Sunny Goyal7779d622015-06-11 16:18:39 -0700406 @Override
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800407 public void onConfigurationChanged(Configuration newConfig) {
408 int diff = newConfig.diff(mOldConfig);
409 if ((diff & (CONFIG_ORIENTATION | CONFIG_SCREEN_SIZE)) != 0) {
410 mUserEventDispatcher = null;
411 initDeviceProfile(mDeviceProfile.inv);
Sunny Goyalfde55052018-02-01 14:46:13 -0800412 dispatchDeviceProfileChanged();
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800413
414 getRootView().dispatchInsets();
415 getStateManager().reapplyState();
416
417 // TODO: We can probably avoid rebind when only screen size changed.
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800418 int currentPage = mWorkspace.getNextPage();
419 if (mModel.startLoader(currentPage)) {
420 mWorkspace.setCurrentPage(currentPage);
421 setWorkspaceLoading(true);
422 }
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800423 }
424
425 mOldConfig.setTo(newConfig);
426 super.onConfigurationChanged(newConfig);
427 }
428
429 private void initDeviceProfile(InvariantDeviceProfile idp) {
430 // Load configuration-specific DeviceProfile
431 mDeviceProfile = idp.getDeviceProfile(this);
432 if (isInMultiWindowModeCompat()) {
433 Display display = getWindowManager().getDefaultDisplay();
434 Point mwSize = new Point();
435 display.getSize(mwSize);
436 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
437 }
438 }
439
440 @Override
Mario Bertschler27288382017-05-24 15:35:09 -0700441 public void onThemeChanged() {
442 recreate();
443 }
444
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700445 public LauncherStateManager getStateManager() {
446 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700447 }
448
Tonydc76f8e2018-02-02 21:45:39 -0600449 public LauncherAppTransitionManager getAppTransitionManager() {
450 return mAppTransitionManager;
451 }
452
Mario Bertschlera6936942017-05-31 14:48:19 -0700453 protected void overrideTheme(boolean isDark, boolean supportsDarkText) {
Mario Bertschler27288382017-05-24 15:35:09 -0700454 if (isDark) {
455 setTheme(R.style.LauncherThemeDark);
Mario Bertschlera6936942017-05-31 14:48:19 -0700456 } else if (supportsDarkText) {
457 setTheme(R.style.LauncherThemeDarkText);
Mario Bertschler27288382017-05-24 15:35:09 -0700458 }
459 }
460
461 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700462 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800463 return mLauncherView.findViewById(id);
464 }
465
466 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700467 public void onAppWidgetHostReset() {
468 if (mAppWidgetHost != null) {
469 mAppWidgetHost.startListening();
470 }
471 }
472
Adam Cohen9211d422014-10-07 18:14:53 -0700473 private LauncherCallbacks mLauncherCallbacks;
474
Sunny Goyal32554d12015-12-03 15:31:25 -0800475 /**
476 * Call this after onCreate to set or clear overlay.
477 */
478 public void setLauncherOverlay(LauncherOverlay overlay) {
479 if (overlay != null) {
480 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
481 }
482 mWorkspace.setLauncherOverlay(overlay);
483 }
484
Adam Cohen9211d422014-10-07 18:14:53 -0700485 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
486 mLauncherCallbacks = callbacks;
487 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700488 }
489
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700490 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700491 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700492 if (mLauncherCallbacks != null) {
493 mLauncherCallbacks.onLauncherProviderChange();
494 }
495 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700496
Hyunyoung Song3f471442015-04-08 19:01:34 -0700497 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700498 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
499 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700500 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700501 }
502
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000503 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700504 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
505 // This cast is safe as long as the id < 0x00FFFFFF
506 // Since we jail all the dynamically generated views, there should be no clashes
507 // with any other views.
508 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000509 }
510
Tony Wickham010d2552017-01-20 08:15:28 -0800511 public PopupDataProvider getPopupDataProvider() {
512 return mPopupDataProvider;
513 }
514
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000515 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700516 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
517 * a configuration step, this allows the proper animations to run after other transitions.
518 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700519 private long completeAdd(
520 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
521 long screenId = info.screenId;
522 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700523 // When the screen id represents an actual screen (as opposed to a rank) we make sure
524 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700525 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700526 }
Adam Cohendb364c32014-05-20 14:23:40 -0700527
Sunny Goyal2100c782016-08-22 16:00:03 -0700528 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800529 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700530 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700531 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800532 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700533 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700534 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700535 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700536 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700537 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700538 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700539 int widgetId = appWidgetId;
540 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700541 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700542 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700543 // Since the view was just bound, also launch the configure activity if needed
544 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
545 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800546 if (provider != null) {
547 new WidgetAddFlowHandler(provider)
548 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700549 }
550 }
551 break;
552 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800553 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700554
Adam Cohendb364c32014-05-20 14:23:40 -0700555 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800556 }
557
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800558 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700559 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700560 if (isWorkspaceLoading()) {
561 // process the result once the workspace has loaded.
562 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
563 return;
564 }
565 mPendingActivityResult = null;
566
Winson Chunge341d302013-08-16 14:31:00 -0700567 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700568 final PendingRequestArgs requestArgs = mPendingRequestArgs;
569 setWaitingForResult(null);
570 if (requestArgs == null) {
571 return;
572 }
573
574 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700575
Adam Cohenad4e15c2013-10-17 16:21:35 -0700576 Runnable exitSpringLoaded = new Runnable() {
577 @Override
578 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700579 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700580 }
581 };
582
Michael Jurka8b805b12012-04-18 14:23:14 -0700583 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700584 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700585 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700586 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
587 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700588 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700589 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700590 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700591 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700592 addAppWidgetImpl(
593 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800594 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700595 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700596 }
597 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200598 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700599 if (resultCode == RESULT_OK && isInState(OVERVIEW)) {
Tony Wickhame3054412015-09-09 09:05:25 -0700600 // User could have free-scrolled between pages before picking a wallpaper; make sure
601 // we move to the closest one now.
602 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700603 mStateManager.goToState(NORMAL, false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200604 }
605 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700606 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200607
Adam Cohened66b2b2012-01-23 17:28:51 -0800608 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700609 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700610
Adam Cohened66b2b2012-01-23 17:28:51 -0800611 // We have special handling for widgets
612 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800613 final int appWidgetId;
614 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
615 : -1;
616 if (widgetId < 0) {
617 appWidgetId = pendingAddWidgetId;
618 } else {
619 appWidgetId = widgetId;
620 }
621
Adam Cohenad4e15c2013-10-17 16:21:35 -0700622 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800623 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700624 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
625 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700626 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700627 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700628 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700629 @Override
630 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700631 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700632 }
633 };
Adam Cohendb364c32014-05-20 14:23:40 -0700634
Sunny Goyal2100c782016-08-22 16:00:03 -0700635 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
636 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
637 } else {
638 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
639 // When the screen id represents an actual screen (as opposed to a rank)
640 // we make sure that the drop page actually exists.
641 requestArgs.screenId =
642 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700643 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700644 final CellLayout dropLayout =
645 mWorkspace.getScreenWithId(requestArgs.screenId);
646
647 dropLayout.setDropPending(true);
648 final Runnable onComplete = new Runnable() {
649 @Override
650 public void run() {
651 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
652 dropLayout.setDropPending(false);
653 }
654 };
655 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
656 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700657 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800658 return;
659 }
660
Sunny Goyald478c832016-04-01 12:04:16 -0700661 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
662 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700663 if (resultCode == RESULT_OK) {
664 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700665 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700666 }
667 // Leave the widget in the pending state if the user canceled the configure.
668 return;
669 }
670
Sunny Goyalaad90582015-07-09 14:22:50 -0700671 if (requestCode == REQUEST_CREATE_SHORTCUT) {
672 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700673 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
674 completeAdd(requestCode, data, -1, requestArgs);
675 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
676 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
677
Sunny Goyalaad90582015-07-09 14:22:50 -0700678 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700679 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
680 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800681 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800682 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800683 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800684 }
685
686 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700687 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800688 final int requestCode, final int resultCode, final Intent data) {
689 handleActivityResult(requestCode, resultCode, data);
690 if (mLauncherCallbacks != null) {
691 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
692 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800693 }
694
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700695 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700696 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600697 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700698 PendingRequestArgs pendingArgs = mPendingRequestArgs;
699 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
700 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
701 setWaitingForResult(null);
702
Sunny Goyal28c6b962015-10-12 11:42:05 -0700703 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700704 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700705 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700706 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700707 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700708 Intent intent = pendingArgs.getPendingIntent();
709
Sunny Goyal28c6b962015-10-12 11:42:05 -0700710 if (grantResults.length > 0
711 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700712 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700713 } else {
714 // TODO: Show a snack bar with link to settings
715 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700716 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700717 }
718 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600719 if (mLauncherCallbacks != null) {
720 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
721 grantResults);
722 }
723 }
724
Adam Cohendb364c32014-05-20 14:23:40 -0700725 /**
726 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
727 *
728 * @param screenId the screen id to check
729 * @return the new screen, or screenId if it exists
730 */
731 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800732 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700733 if (dropLayout == null) {
734 // it's possible that the add screen was removed because it was
735 // empty and a re-bind occurred
736 mWorkspace.addExtraEmptyScreen();
737 return mWorkspace.commitExtraEmptyScreen();
738 } else {
739 return screenId;
740 }
741 }
742
Sunny Goyal2100c782016-08-22 16:00:03 -0700743 @Thunk void completeTwoStageWidgetDrop(
744 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
745 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800746 Runnable onCompleteRunnable = null;
747 int animationType = 0;
748
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700749 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800750 if (resultCode == RESULT_OK) {
751 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
752 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800753 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700754 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800755 onCompleteRunnable = new Runnable() {
756 @Override
757 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700758 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700759 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800760 }
761 };
762 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800763 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800764 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800765 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700766 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700767 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700768 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
769 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700770 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700771 // The animated view may be null in the case of a rotation during widget configuration
772 onCompleteRunnable.run();
773 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800774 }
775
776 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700777 protected void onStop() {
778 super.onStop();
779 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700780
781 if (mLauncherCallbacks != null) {
782 mLauncherCallbacks.onStop();
783 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800784 mAppWidgetHost.setListenIfResumed(false);
Tony Wickham010d2552017-01-20 08:15:28 -0800785
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700786 if (!mAppLaunchSuccess) {
787 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
Sunny Goyalea609262017-10-25 15:47:38 -0700788 mStateManager.getState().containerType);
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700789 }
Tony Wickham010d2552017-01-20 08:15:28 -0800790 NotificationListener.removeNotificationsChangedListener();
Sunny Goyalaf161fd2018-02-05 16:34:57 -0800791 getStateManager().moveToRestState();
Michael Jurkacd496d72013-04-11 11:32:45 -0700792 }
793
794 @Override
795 protected void onStart() {
796 super.onStart();
797 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700798
799 if (mLauncherCallbacks != null) {
800 mLauncherCallbacks.onStart();
801 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800802 mAppWidgetHost.setListenIfResumed(true);
Sunny Goyalcac002d2018-01-04 09:51:45 -0800803 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
Jon Miranda2d89ea82017-05-04 11:47:53 -0700804
Jon Mirandade43a712018-01-18 11:35:10 -0800805 if (mShouldFadeInScrim && mLauncherView.getBackground() != null) {
Jon Miranda2d89ea82017-05-04 11:47:53 -0700806 if (mScrimAnimator != null) {
807 mScrimAnimator.cancel();
808 }
Jon Mirandade43a712018-01-18 11:35:10 -0800809 mLauncherView.getBackground().setAlpha(0);
810 mScrimAnimator = ObjectAnimator.ofInt(mLauncherView.getBackground(),
Jon Miranda2d89ea82017-05-04 11:47:53 -0700811 LauncherAnimUtils.DRAWABLE_ALPHA, 0, 255);
812 mScrimAnimator.addListener(new AnimatorListenerAdapter() {
813 @Override
814 public void onAnimationEnd(Animator animation) {
815 mScrimAnimator = null;
816 }
817 });
818 mScrimAnimator.setDuration(600);
819 mScrimAnimator.setStartDelay(getWindow().getTransitionBackgroundFadeDuration());
820 mScrimAnimator.start();
821 }
Jon Miranda7fda2852017-07-19 16:07:01 -0700822 mShouldFadeInScrim = false;
Michael Jurkacd496d72013-04-11 11:32:45 -0700823 }
824
825 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800826 protected void onResume() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700827 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800828 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700829 TraceHelper.partitionSection("ON_RESUME", "superCall");
830
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700831 mAppLaunchSuccess = false;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700832 getUserEventDispatcher().resetElapsedSessionMillis();
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700833 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700834 // Process any items that were added while Launcher was away.
835 InstallShortcutReceiver.disableAndFlushInstallQueue(
836 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700837
Sunny Goyala474a9b2017-05-04 16:47:11 -0700838 // Refresh shortcuts if the permission changed.
839 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700840
Sunny Goyal7ede6112017-12-05 15:11:21 -0800841 DiscoveryBounce.showIfNeeded(this);
Adam Cohen9211d422014-10-07 18:14:53 -0700842 if (mLauncherCallbacks != null) {
843 mLauncherCallbacks.onResume();
844 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800845
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700846 TraceHelper.endSection("ON_RESUME");
Adam Cohen06dff352012-06-01 17:17:08 -0700847 }
848
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800849 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700850 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700851 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700852 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700853
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700854 super.onPause();
Joe Onorato24b6fd82009-11-12 13:47:09 -0800855 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700856 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700857
Adam Cohen9211d422014-10-07 18:14:53 -0700858 if (mLauncherCallbacks != null) {
859 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700860 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700861 }
862
Adam Cohenc2d6e892014-10-16 09:49:24 -0700863 public interface LauncherOverlay {
864
865 /**
866 * Touch interaction leading to overscroll has begun
867 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700868 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700869
870 /**
871 * Touch interaction related to overscroll has ended
872 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700873 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700874
875 /**
876 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
877 * screen (or in the case of RTL, the rightmost screen).
878 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700879 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700880
881 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800882 * Called when the launcher is ready to use the overlay
883 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700884 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700885 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700886 }
887
888 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700889 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700890 }
891
892 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
893
Sunny Goyalc86df472016-02-25 09:19:38 -0800894 public void onScrollChanged(float progress) {
895 if (mWorkspace != null) {
896 mWorkspace.onOverlayScrollChanged(progress);
897 }
898 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700899 }
900
Sunny Goyal16764582017-11-06 16:00:34 -0800901 public boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -0700902 if (mLauncherCallbacks != null) {
903 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700904 } else {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800905 // On O and above we there is always some setting present settings (add icon to
906 // home screen or icon badging). On earlier APIs we will have the allow rotation
907 // setting, on devices with a locked orientation,
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700908 return Utilities.ATLEAST_OREO || !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -0700909 }
Jorim Jaggid017f882014-01-14 17:08:48 -0800910 }
911
Sunny Goyalf9403d92017-10-18 10:55:56 -0700912 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -0700913 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700914 }
915
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800916 /**
917 * Restores the previous state, if it exists.
918 *
919 * @param savedState The previous state.
920 */
921 private void restoreState(Bundle savedState) {
922 if (savedState == null) {
923 return;
924 }
925
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700926 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -0700927 LauncherState[] stateValues = LauncherState.values();
928 LauncherState state = stateValues[stateOrdinal];
Sunny Goyalbc683e92017-12-06 10:25:07 -0800929 if (!state.disableRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700930 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800931 }
932
Sunny Goyal2100c782016-08-22 16:00:03 -0700933 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
934 if (requestArgs != null) {
935 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800936 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700937
938 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700939
940 SparseArray<Parcelable> widgetsState =
941 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
942 if (widgetsState != null) {
943 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
944 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800945 }
946
947 /**
948 * Finds all the views we need and configure them properly.
949 */
950 private void setupViews() {
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800951 mDragLayer = findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700952 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -0700953 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700954 mWorkspace.initParentViews(mDragLayer);
Sunny Goyal16764582017-11-06 16:00:34 -0800955 mOverviewPanel = findViewById(R.id.overview_panel);
Craig Mautner360310b2012-10-26 15:13:08 -0700956
Sunny Goyal784f9c32016-03-23 16:56:54 -0700957 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
958 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
959 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800960
Winson Chung4c98d922011-05-31 16:50:48 -0700961 // Setup the drag layer
Sunny Goyal7ede6112017-12-05 15:11:21 -0800962 mDragLayer.setup(this, mDragController);
Winson Chung4c98d922011-05-31 16:50:48 -0700963
Winson Chung3d503fb2011-07-13 17:25:49 -0700964 // Setup the hotseat
965 mHotseat = (Hotseat) findViewById(R.id.hotseat);
966 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -0700967 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -0700968 }
969
Winson Chung4c98d922011-05-31 16:50:48 -0700970 // Setup the workspace
971 mWorkspace.setHapticFeedbackEnabled(false);
972 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700973 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -0700974 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
975 // default state, otherwise we will update to the wrong offsets in RTL
976 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700977 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700978 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700979
Tony Wickham34d2c912015-09-11 09:27:58 -0700980 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700981 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700982
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700983 // Setup Apps
984 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -0700985
Winson Chung3d503fb2011-07-13 17:25:49 -0700986 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -0700987 mDragController.setMoveTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700988 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -0400989
Hyunyoung Songd725f642017-08-17 22:26:35 -0700990 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800991 }
992
993 /**
994 * Creates a view representing a shortcut.
995 *
996 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800997 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800998 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700999 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001000 }
1001
1002 /**
1003 * Creates a view representing a shortcut inflated from the specified resource.
1004 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001005 * @param parent The group the shortcut belongs to.
1006 * @param info The data structure describing the shortcut.
1007 *
1008 * @return A View inflated from layoutResId.
1009 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001010 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -08001011 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
1012 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001013 favorite.applyFromShortcutInfo(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001014 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001015 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001016 return favorite;
1017 }
1018
1019 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -07001020 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001021 *
1022 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001023 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001024 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001025 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001026 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
1027 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001028 return;
1029 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001030
Jon Mirandac476d6e2017-05-04 16:30:01 -07001031 int[] cellXY = mTmpAddItemCellCoordinates;
1032 CellLayout layout = getCellLayout(container, screenId);
1033
Sunny Goyal782f0c92017-01-19 10:27:54 -08001034 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001035 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001036 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
1037 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001038 }
1039
1040 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001041 // Legacy shortcuts are only supported for primary profile.
1042 info = Process.myUserHandle().equals(args.user)
1043 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001044
Sunny Goyalb57645f2017-03-20 13:57:28 -07001045 if (info == null) {
1046 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1047 return;
1048 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001049 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001050 // The app is trying to add a shortcut without sufficient permissions
1051 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1052 return;
1053 }
1054 }
1055
Jon Mirandac476d6e2017-05-04 16:30:01 -07001056 if (container < 0) {
1057 // Adding a shortcut to the Workspace.
1058 final View view = createShortcut(info);
1059 boolean foundCellSpan = false;
1060 // First we check if we already know the exact location where we want to add this item.
1061 if (cellX >= 0 && cellY >= 0) {
1062 cellXY[0] = cellX;
1063 cellXY[1] = cellY;
1064 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001065
Jon Mirandac476d6e2017-05-04 16:30:01 -07001066 // If appropriate, either create a folder or add to an existing folder
1067 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001068 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001069 return;
1070 }
1071 DragObject dragObject = new DragObject();
1072 dragObject.dragInfo = info;
1073 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1074 true)) {
1075 return;
1076 }
1077 } else {
1078 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1079 }
1080
1081 if (!foundCellSpan) {
1082 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001083 return;
1084 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001085
1086 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1087 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001088 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001089 // Adding a shortcut to a Folder.
Sunny Goyale29897f2017-07-20 10:09:42 -07001090 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001091 if (folderIcon != null) {
1092 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1093 folderInfo.add(info, args.rank, false);
1094 } else {
Sunny Goyale29897f2017-07-20 10:09:42 -07001095 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001096 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001097 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001098 }
1099
Sunny Goyale29897f2017-07-20 10:09:42 -07001100 public FolderIcon findFolderIcon(final long folderIconId) {
1101 return (FolderIcon) mWorkspace.getFirstMatch(new ItemOperator() {
1102 @Override
1103 public boolean evaluate(ItemInfo info, View view) {
1104 return info != null && info.id == folderIconId;
1105 }
1106 });
1107 }
1108
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001109 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001110 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001111 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001112 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001113 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001114 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001115 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1116
Adam Cohened66b2b2012-01-23 17:28:51 -08001117 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001118 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001119 }
Romain Guycbb89e42009-06-08 15:52:54 -07001120
Adam Cohen59400422014-03-05 18:07:04 -08001121 LauncherAppWidgetInfo launcherInfo;
1122 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001123 launcherInfo.spanX = itemInfo.spanX;
1124 launcherInfo.spanY = itemInfo.spanY;
1125 launcherInfo.minSpanX = itemInfo.minSpanX;
1126 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001127 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001128
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001129 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001130 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001131
Sunny Goyal2100c782016-08-22 16:00:03 -07001132 if (hostView == null) {
1133 // Perform actual inflation because we're live
1134 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001135 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001136 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301137 prepareAppWidget(hostView, launcherInfo);
1138 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001139 }
Romain Guycbb89e42009-06-08 15:52:54 -07001140
Sunny Goyald5462aa2016-11-22 16:52:39 +05301141 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001142 hostView.setTag(item);
1143 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001144 hostView.setFocusable(true);
1145 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001146 }
1147
Adam Cohended9f8d2010-11-03 13:25:16 -07001148 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1149 @Override
1150 public void onReceive(Context context, Intent intent) {
1151 final String action = intent.getAction();
1152 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001153 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001154 // processing a multi-step drop
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001155 if (mAppsView != null && mPendingRequestArgs == null) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001156 mStateManager.goToState(NORMAL);
Winson Chung785d2eb2011-04-14 16:08:02 -07001157 }
Jon Miranda7fda2852017-07-19 16:07:01 -07001158 mShouldFadeInScrim = true;
1159 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1160 // ACTION_USER_PRESENT is sent after onStart/onResume. This covers the case where
1161 // the user unlocked and the Launcher is not in the foreground.
1162 mShouldFadeInScrim = false;
Adam Cohended9f8d2010-11-03 13:25:16 -07001163 }
1164 }
1165 };
1166
Tony Wickham010d2552017-01-20 08:15:28 -08001167 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
Sunny Goyal29947f02017-12-18 13:49:44 -08001168 mWorkspace.updateIconBadges(updatedBadges);
1169 mAppsView.updateIconBadges(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001170
Sunny Goyal29947f02017-12-18 13:49:44 -08001171 PopupContainerWithArrow popup = PopupContainerWithArrow.getOpen(Launcher.this);
1172 if (popup != null) {
1173 popup.updateNotificationHeader(updatedBadges);
Tony Wickham010d2552017-01-20 08:15:28 -08001174 }
1175 }
1176
Adam Cohended9f8d2010-11-03 13:25:16 -07001177 @Override
1178 public void onAttachedToWindow() {
1179 super.onAttachedToWindow();
1180
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001181 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyalc86df472016-02-25 09:19:38 -08001182 if (mLauncherCallbacks != null) {
1183 mLauncherCallbacks.onAttachedToWindow();
1184 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001185 }
1186
1187 @Override
1188 public void onDetachedFromWindow() {
1189 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001190
1191 if (mLauncherCallbacks != null) {
1192 mLauncherCallbacks.onDetachedFromWindow();
1193 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001194 }
1195
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001196 public AllAppsTransitionController getAllAppsController() {
1197 return mAllAppsController;
1198 }
1199
Winson Chung1a341002018-01-17 10:00:23 -08001200 public LauncherRootView getRootView() {
1201 return (LauncherRootView) mLauncherView;
1202 }
1203
Winson Chunga6945242014-01-08 14:04:34 -08001204 public DragLayer getDragLayer() {
1205 return mDragLayer;
1206 }
1207
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001208 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001209 return mAppsView;
1210 }
1211
Winson Chunga6945242014-01-08 14:04:34 -08001212 public Workspace getWorkspace() {
1213 return mWorkspace;
1214 }
1215
1216 public Hotseat getHotseat() {
1217 return mHotseat;
1218 }
1219
Winson Chung8ae41982017-11-10 11:42:13 -08001220 public <T extends ViewGroup> T getOverviewPanel() {
1221 return (T) mOverviewPanel;
Winson Chunga6945242014-01-08 14:04:34 -08001222 }
1223
Sunny Goyal47328fd2016-05-25 18:56:41 -07001224 public DropTargetBar getDropTargetBar() {
1225 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001226 }
1227
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001228 public LauncherAppWidgetHost getAppWidgetHost() {
1229 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001230 }
Romain Guycbb89e42009-06-08 15:52:54 -07001231
Winson Chunga9abd0e2010-10-27 17:18:37 -07001232 public LauncherModel getModel() {
1233 return mModel;
1234 }
1235
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001236 public ModelWriter getModelWriter() {
1237 return mModelWriter;
1238 }
1239
Adam Cohen79d90c52016-04-22 13:29:20 -07001240 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001241 return mSharedPrefs;
1242 }
1243
Sunny Goyalf8d56fc2018-01-31 15:18:11 -08001244 public int getOrientation() { return mOldConfig.orientation; }
Jon Miranda6bed3502017-09-19 14:43:17 -07001245
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001246 @Override
1247 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001248 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001249 super.onNewIntent(intent);
1250
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001251 boolean alreadyOnHome = hasWindowFocus() && ((intent.getFlags() &
Winson15f8b172015-08-19 11:02:39 -07001252 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1253 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001254
1255 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001256 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001257 && AbstractFloatingView.getTopOpenView(this) == null;
Winson15f8b172015-08-19 11:02:39 -07001258 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001259 boolean internalStateHandled = InternalStateHandler
Sunny Goyal8b264562018-02-03 23:00:13 -08001260 .handleNewIntent(this, intent, isStarted());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001261
Winson15f8b172015-08-19 11:02:39 -07001262 if (isActionMain) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001263 if (!internalStateHandled) {
1264 // Note: There should be at most one log per method call. This is enforced
1265 // implicitly by using if-else statements.
1266 UserEventDispatcher ued = getUserEventDispatcher();
1267 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
1268 if (topOpenView != null) {
1269 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
1270 } else if (alreadyOnHome) {
1271 Target target = newContainerTarget(mStateManager.getState().containerType);
1272 target.pageIndex = mWorkspace.getCurrentPage();
1273 ued.logActionCommand(Action.Command.HOME_INTENT, target);
1274 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001275
Sunny Goyald3864fa2017-11-14 15:06:36 -08001276 // In all these cases, only animate if we're already on home
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001277 AbstractFloatingView.closeAllOpenViews(this, isStarted());
Jon Mirandafeba90f2016-10-06 10:53:29 -07001278
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001279 mStateManager.goToState(NORMAL);
Sunny Goyald3864fa2017-11-14 15:06:36 -08001280
1281 // Reset the apps view
1282 if (!alreadyOnHome && mAppsView != null) {
1283 mAppsView.reset();
1284 }
1285
1286 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()) {
1287 mWorkspace.post(mWorkspace::moveToDefaultScreen);
1288 }
1289 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001290
1291 final View v = getWindow().peekDecorView();
1292 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001293 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001294 }
1295
Adam Cohen9211d422014-10-07 18:14:53 -07001296 if (mLauncherCallbacks != null) {
1297 mLauncherCallbacks.onHomeIntent();
1298 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001299 }
Winson15f8b172015-08-19 11:02:39 -07001300
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001301 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001302 }
1303
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001304 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001305 public void onRestoreInstanceState(Bundle state) {
1306 super.onRestoreInstanceState(state);
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001307 mWorkspace.restoreInstanceStateForChild(mSynchronouslyBoundPage);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001308 }
1309
1310 @Override
1311 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001312 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001313 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001314
Adam Cohen21cd0022013-10-09 18:57:02 -07001315 }
Sunny Goyalea609262017-10-25 15:47:38 -07001316 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001317
1318
1319 AbstractFloatingView widgets = AbstractFloatingView
1320 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1321 if (widgets != null) {
1322 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1323 widgets.saveHierarchyState(widgetsState);
1324 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1325 } else {
1326 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1327 }
1328
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001329 // We close any open folders and shortcut containers since they will not be re-opened,
1330 // and we need to make sure this state is reflected.
1331 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001332
Sunny Goyal2100c782016-08-22 16:00:03 -07001333 if (mPendingRequestArgs != null) {
1334 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1335 }
1336 if (mPendingActivityResult != null) {
1337 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001338 }
1339
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001340 super.onSaveInstanceState(outState);
1341
Adam Cohen9211d422014-10-07 18:14:53 -07001342 if (mLauncherCallbacks != null) {
1343 mLauncherCallbacks.onSaveInstanceState(outState);
1344 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001345 }
1346
1347 @Override
1348 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001349 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001350
Sunny Goyal5cc7bbd2017-06-12 09:50:39 -07001351 unregisterReceiver(mReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001352 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001353
Winson Chungcd2b0142011-06-08 16:02:26 -07001354 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001355 // It's possible to receive onDestroy after a new Launcher activity has
1356 // been created. In this case, don't interfere with the new Launcher.
1357 if (mModel.isCurrentCallbacks(this)) {
1358 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001359 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001360 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001361
Sunny Goyal745bad92016-05-02 10:54:12 -07001362 if (mRotationPrefChangeHandler != null) {
1363 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1364 }
1365
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001366 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001367 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001368 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001369 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001370 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001371
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001372 TextKeyListener.getInstance().release();
Mario Bertschler27288382017-05-24 15:35:09 -07001373 WallpaperColorInfo.getInstance(this).setOnThemeChangeListener(null);
1374
Michael Jurka2ecf9952012-06-18 12:52:28 -07001375 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001376
Tony2917a8b2017-07-31 23:29:42 -07001377 clearPendingBinds();
1378
Adam Cohen9211d422014-10-07 18:14:53 -07001379 if (mLauncherCallbacks != null) {
1380 mLauncherCallbacks.onDestroy();
1381 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001382 }
1383
Sunny Goyalae502842016-06-17 08:43:56 -07001384 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1385 return mAccessibilityDelegate;
1386 }
1387
Adam Cohena9cf38f2011-05-02 15:36:58 -07001388 public DragController getDragController() {
1389 return mDragController;
1390 }
1391
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001392 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001393 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001394 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001395 }
1396
1397 @Override
1398 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1399 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001400 try {
1401 super.startIntentSenderForResult(intent, requestCode,
1402 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1403 } catch (IntentSender.SendIntentException e) {
1404 throw new ActivityNotFoundException();
1405 }
1406 }
1407
Winson Chung70d72102011-08-12 11:24:35 -07001408 /**
1409 * Indicates that we want global search for this activity by setting the globalSearch
1410 * argument for {@link #startSearch} to true.
1411 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001412 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001413 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001414 Bundle appSearchData, boolean globalSearch) {
1415 if (appSearchData == null) {
1416 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001417 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001418 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001419
Sunny Goyal6f28e712016-09-13 14:19:29 -07001420 if (mLauncherCallbacks == null ||
1421 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1422 // Starting search from the callbacks failed. Start the default global search.
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001423 super.startSearch(initialQuery, selectInitialQuery, appSearchData, true);
Ian Parkinson047036e2014-09-01 15:40:53 +01001424 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001425
1426 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001427 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001428 }
1429
Joe Onorato9c1289c2009-08-17 11:03:03 -04001430 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001431 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001432 }
1433
Adam Cohen517a7f52014-03-01 12:12:59 -08001434 public boolean isWorkspaceLoading() {
1435 return mWorkspaceLoading;
1436 }
1437
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001438 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001439 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001440 }
1441
Sunny Goyal04a324a2017-01-20 21:08:59 -08001442 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001443 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001444 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001445
Sunny Goyal2100c782016-08-22 16:00:03 -07001446 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001447 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001448 if (LOGD) {
1449 Log.d(TAG, "Adding widget from drop");
1450 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001451 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001452 }
1453
Sunny Goyal2100c782016-08-22 16:00:03 -07001454 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001455 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1456 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1457 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001458
Adam Cohenad4e15c2013-10-17 16:21:35 -07001459 Runnable onComplete = new Runnable() {
1460 @Override
1461 public void run() {
1462 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001463 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001464 }
1465 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001466 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001467 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001468 }
1469 }
Romain Guycbb89e42009-06-08 15:52:54 -07001470
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001471 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1472 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001473 info.container = container;
1474 info.screenId = screenId;
1475 if (cell != null) {
1476 info.cellX = cell[0];
1477 info.cellY = cell[1];
1478 }
1479 info.spanX = spanX;
1480 info.spanY = spanY;
1481
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001482 switch (info.itemType) {
1483 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1484 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001485 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001486 break;
1487 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001488 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001489 break;
1490 default:
1491 throw new IllegalStateException("Unknown item type: " + info.itemType);
1492 }
1493 }
1494
Adam Cohenfbba09b2011-07-18 21:43:05 -07001495 /**
1496 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001497 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001498 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001499 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1500 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001501 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1502 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1503 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001504 }
1505
Adam Cohenfbba09b2011-07-18 21:43:05 -07001506 /**
1507 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001508 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001509 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001510 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001511 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001512 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001513 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001514 // In the case where we've prebound the widget, we remove it from the DragLayer
1515 if (LOGD) {
1516 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1517 }
1518 getDragLayer().removeView(hostView);
1519
Adam Cohened66b2b2012-01-23 17:28:51 -08001520 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001521 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001522
1523 // Clear the boundWidget so that it doesn't get destroyed.
1524 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001525 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001526 // In this case, we either need to start an activity to get permission to bind
1527 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001528 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1529 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1530 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1531 this, info.componentName);
1532 } else {
1533 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1534 }
Adam Cohendd70d662012-10-04 16:53:44 -07001535 Bundle options = info.bindOptions;
1536
Sunny Goyalffe83f12014-08-14 17:39:34 -07001537 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1538 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001539 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001540 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001541 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001542 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001543 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001544 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001545 }
1546
Adam Cohendcd297f2013-06-18 13:13:40 -07001547 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001548 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001549 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001550 folderInfo.title = getText(R.string.folder_name);
1551
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001552 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001553 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001554
1555 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001556 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301557 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001558 // Force measure the new folder icon
1559 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1560 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001561 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001562 }
Romain Guycbb89e42009-06-08 15:52:54 -07001563
Winsonc0b52fe2015-09-09 16:38:15 -07001564 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001565 * Unbinds the view for the specified item, and removes the item and all its children.
1566 *
1567 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001568 * @param itemInfo the {@link ItemInfo} for this view.
1569 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001570 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001571 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001572 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001573 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001574 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1575 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001576 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001577 } else {
1578 mWorkspace.removeWorkspaceItem(v);
1579 }
Winsonc0b52fe2015-09-09 16:38:15 -07001580 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001581 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001582 }
1583 } else if (itemInfo instanceof FolderInfo) {
1584 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001585 if (v instanceof FolderIcon) {
1586 ((FolderIcon) v).removeListeners();
1587 }
Winsonc0b52fe2015-09-09 16:38:15 -07001588 mWorkspace.removeWorkspaceItem(v);
1589 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001590 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001591 }
1592 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1593 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001594 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001595 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07001596 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001597 }
1598 } else {
1599 return false;
1600 }
1601 return true;
1602 }
1603
1604 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07001605 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07001606 */
Sunny Goyal56c73602015-09-25 12:55:01 -07001607 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07001608 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07001609 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07001610 // Deleting an app widget ID is a void call but writes to disk before returning
1611 // to the caller...
1612 new AsyncTask<Void, Void, Void>() {
1613 public Void doInBackground(Void ... args) {
1614 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
1615 return null;
1616 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07001617 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07001618 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001619 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001620 }
1621
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001622 @Override
1623 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001624 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001625 }
1626
Joe Onorato88ec0992009-11-19 13:16:06 -08001627 @Override
1628 public void onBackPressed() {
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08001629 if (finishAutoCancelActionMode()) {
1630 return;
1631 }
Adam Cohen9211d422014-10-07 18:14:53 -07001632 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1633 return;
1634 }
1635
Sunny Goyal45478022015-06-08 16:52:41 -07001636 if (mDragController.isDragging()) {
1637 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001638 return;
1639 }
1640
Jon Mirandafeba90f2016-10-06 10:53:29 -07001641 // Note: There should be at most one log per method call. This is enforced implicitly
1642 // by using if-else statements.
1643 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001644 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
1645 if (topView != null) {
Sunny Goyal37920962017-09-28 13:43:24 -07001646 topView.onBackPressed();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001647 } else if (!isInState(NORMAL)) {
Sunny Goyal49bcf342018-01-11 13:59:53 -08001648 LauncherState lastState = mStateManager.getLastState();
Sunny Goyalea609262017-10-25 15:47:38 -07001649 ued.logActionCommand(Action.Command.BACK, mStateManager.getState().containerType);
Sunny Goyal49bcf342018-01-11 13:59:53 -08001650 mStateManager.goToState(lastState);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001651 } else {
1652 // Back button is a no-op here, but give at least some feedback for the button press
1653 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001654 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001655 }
1656
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001657 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001658 * Launches the intent referred by the clicked shortcut.
1659 *
1660 * @param v The view representing the clicked shortcut.
1661 */
1662 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001663 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1664 // view has detached (it's possible for this to happen if the view is removed mid touch).
1665 if (v.getWindowToken() == null) {
1666 return;
1667 }
1668
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001669 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001670 return;
1671 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001672
Adam Cohen1697b792013-09-17 19:08:21 -07001673 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001674 if (isInState(OVERVIEW)) {
Jon Miranda2e61fba2017-02-24 09:12:59 -08001675 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1676 LauncherLogProto.Action.Direction.NONE,
1677 LauncherLogProto.ContainerType.OVERVIEW, mWorkspace.getCurrentPage());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001678 mStateManager.goToState(NORMAL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001679 }
1680 return;
1681 }
1682
1683 if (v instanceof CellLayout) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001684 if (isInState(OVERVIEW)) {
Jon Mirandac6cf4932017-02-07 17:12:36 -08001685 int page = mWorkspace.indexOfChild(v);
1686 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1687 LauncherLogProto.Action.Direction.NONE,
1688 LauncherLogProto.ContainerType.OVERVIEW, page);
1689 mWorkspace.snapToPageFromOverView(page);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001690 mStateManager.goToState(NORMAL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001691 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07001692 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001693 }
1694
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001695 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001696 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001697 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001698 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001699 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001700 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001701 }
Sunny Goyal508da152014-08-14 10:53:27 -07001702 } else if (tag instanceof AppInfo) {
1703 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07001704 } else if (tag instanceof LauncherAppWidgetInfo) {
1705 if (v instanceof PendingAppWidgetHostView) {
1706 onClickPendingWidget((PendingAppWidgetHostView) v);
1707 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001708 }
1709 }
1710
Sunny Goyal70660032015-05-14 00:07:08 -07001711 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07001712 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07001713 return false;
1714 }
1715
Michael Jurkaaf442092010-06-10 17:01:57 -07001716 /**
Sunny Goyalff572272014-07-23 13:58:07 -07001717 * Event handler for the app widget view which has not fully restored.
1718 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001719 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07001720 if (mIsSafeModeEnabled) {
1721 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
1722 return;
1723 }
1724
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001725 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07001726 if (v.isReadyForClickSetup()) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001727 LauncherAppWidgetProviderInfo appWidgetInfo =
1728 mAppWidgetManager.findProvider(info.providerName, info.user);
1729 if (appWidgetInfo == null) {
1730 return;
1731 }
1732 WidgetAddFlowHandler addFlowHandler = new WidgetAddFlowHandler(appWidgetInfo);
1733
Sunny Goyald478c832016-04-01 12:04:16 -07001734 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
1735 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
1736 // This should not happen, as we make sure that an Id is allocated during bind.
1737 return;
1738 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001739 addFlowHandler.startBindFlow(this, info.appWidgetId, info,
1740 REQUEST_BIND_PENDING_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -07001741 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001742 addFlowHandler.startConfigActivity(this, info, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07001743 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001744 } else {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001745 final String packageName = info.providerName.getPackageName();
Sunny Goyale6e72002017-01-12 16:55:36 -08001746 onClickPendingAppItem(v, packageName, info.installProgress >= 0);
Sunny Goyalff572272014-07-23 13:58:07 -07001747 }
1748 }
1749
Sunny Goyale6e72002017-01-12 16:55:36 -08001750 private void onClickPendingAppItem(final View v, final String packageName,
1751 boolean downloadStarted) {
1752 if (downloadStarted) {
1753 // If the download has started, simply direct to the market app.
1754 startMarketIntentForPackage(v, packageName);
1755 return;
1756 }
Sunny Goyale03b8122014-10-08 09:55:24 -07001757 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001758 .setTitle(R.string.abandoned_promises_title)
1759 .setMessage(R.string.abandoned_promise_explanation)
Sunny Goyale6e72002017-01-12 16:55:36 -08001760 .setPositiveButton(R.string.abandoned_search, new DialogInterface.OnClickListener() {
1761 @Override
1762 public void onClick(DialogInterface dialogInterface, int i) {
1763 startMarketIntentForPackage(v, packageName);
1764 }
1765 })
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001766 .setNeutralButton(R.string.abandoned_clean_this,
1767 new DialogInterface.OnClickListener() {
1768 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001769 final UserHandle user = Process.myUserHandle();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001770 mWorkspace.removeAbandonedPromise(packageName, user);
1771 }
1772 })
1773 .create().show();
Sunny Goyale6e72002017-01-12 16:55:36 -08001774 }
1775
1776 private void startMarketIntentForPackage(View v, String packageName) {
1777 ItemInfo item = (ItemInfo) v.getTag();
Sunny Goyal67e75e22018-01-22 15:14:00 -08001778 Intent intent = new PackageManagerHelper(v.getContext()).getMarketIntent(packageName);
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001779 startActivitySafely(v, intent, item);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001780 }
1781
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001782 /**
1783 * Event handler for an app shortcut click.
1784 *
1785 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
1786 */
Chris Wren40c5ed32014-06-24 18:24:23 -04001787 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001788 if (LOGD) Log.d(TAG, "onClickAppShortcut");
1789 Object tag = v.getTag();
1790 if (!(tag instanceof ShortcutInfo)) {
1791 throw new IllegalArgumentException("Input must be a Shortcut");
1792 }
1793
1794 // Open shortcut
1795 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001796
Sunny Goyal076839c2017-10-30 13:52:20 -07001797 if (shortcut.isDisabled()) {
Tony Makf6308652017-12-02 16:56:22 +00001798 final int disabledFlags = shortcut.runtimeStatusFlags & ShortcutInfo.FLAG_DISABLED_MASK;
1799 if ((disabledFlags &
Sunny Goyal076839c2017-10-30 13:52:20 -07001800 ~FLAG_DISABLED_SUSPENDED &
1801 ~FLAG_DISABLED_QUIET_USER) == 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001802 // If the app is only disabled because of the above flags, launch activity anyway.
1803 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00001804 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07001805 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
1806 // Use a message specific to this shortcut, if it has one.
1807 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
1808 return;
1809 }
1810 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00001811 int error = R.string.activity_not_available;
Sunny Goyal076839c2017-10-30 13:52:20 -07001812 if ((shortcut.runtimeStatusFlags & FLAG_DISABLED_SAFEMODE) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00001813 error = R.string.safemode_shortcut_error;
Sunny Goyal076839c2017-10-30 13:52:20 -07001814 } else if ((shortcut.runtimeStatusFlags & FLAG_DISABLED_BY_PUBLISHER) != 0 ||
1815 (shortcut.runtimeStatusFlags & FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07001816 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00001817 }
1818 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
1819 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001820 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001821 }
1822
Chris Wren40c5ed32014-06-24 18:24:23 -04001823 // Check for abandoned promise
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -07001824 if ((v instanceof BubbleTextView) && shortcut.hasPromiseIconUi()) {
Sunny Goyale6e72002017-01-12 16:55:36 -08001825 String packageName = shortcut.intent.getComponent() != null ?
1826 shortcut.intent.getComponent().getPackageName() : shortcut.intent.getPackage();
1827 if (!TextUtils.isEmpty(packageName)) {
1828 onClickPendingAppItem(v, packageName,
1829 shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE));
1830 return;
1831 }
Chris Wren40c5ed32014-06-24 18:24:23 -04001832 }
1833
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001834 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07001835 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04001836 }
1837
Sunny Goyala7ce1662016-05-31 15:01:35 -07001838 private void startAppShortcutOrInfoActivity(View v) {
1839 ItemInfo item = (ItemInfo) v.getTag();
Mario Bertschler08ffaae2017-03-20 11:30:27 -07001840 Intent intent;
1841 if (item instanceof PromiseAppInfo) {
1842 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) item;
Sunny Goyal67e75e22018-01-22 15:14:00 -08001843 intent = promiseAppInfo.getMarketIntent(this);
Mario Bertschler08ffaae2017-03-20 11:30:27 -07001844 } else {
1845 intent = item.getIntent();
1846 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07001847 if (intent == null) {
1848 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07001849 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001850 startActivitySafely(v, intent, item);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001851 }
1852
1853 /**
1854 * Event handler for a folder icon click.
1855 *
1856 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
1857 */
1858 protected void onClickFolderIcon(View v) {
1859 if (LOGD) Log.d(TAG, "onClickFolder");
1860 if (!(v instanceof FolderIcon)){
1861 throw new IllegalArgumentException("Input must be a FolderIcon");
1862 }
1863
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001864 Folder folder = ((FolderIcon) v).getFolder();
1865 if (!folder.isOpen() && !folder.isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001866 // Open the requested folder
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001867 folder.animateOpen();
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001868 }
Michael Jurka5130e402011-10-13 04:55:35 -07001869 }
1870
Sandeep Siddharthad8058372014-01-28 10:41:15 -08001871 /**
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001872 * Event handler for the wallpaper picker button that appears after a long press
1873 * on the home screen.
1874 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001875 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07001876 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07001877 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
1878 return;
1879 }
1880
Tony Wickham785f7a52015-08-31 17:28:32 -07001881 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
1882 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal2100c782016-08-22 16:00:03 -07001883 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001884 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001885 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001886
1887 String pickerPackage = getString(R.string.wallpaper_picker_package);
Tony85efb4b2017-06-02 13:06:10 -07001888 boolean hasTargetPackage = !TextUtils.isEmpty(pickerPackage);
1889 if (hasTargetPackage) {
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001890 intent.setPackage(pickerPackage);
1891 }
1892
Sunny Goyala5ace712017-11-15 17:12:51 -08001893 final Bundle launchOptions;
1894 if (v != null) {
1895 intent.setSourceBounds(getViewBounds(v));
1896 // If there is no target package, use the default intent chooser animation
Jon Miranda17940f02018-01-18 14:46:34 -08001897 launchOptions = hasTargetPackage
1898 ? getActivityLaunchOptions(v, isInMultiWindowModeCompat())
1899 : null;
Sunny Goyala5ace712017-11-15 17:12:51 -08001900 } else {
1901 launchOptions = null;
1902 }
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001903 try {
Sunny Goyala5ace712017-11-15 17:12:51 -08001904 startActivityForResult(intent, REQUEST_PICK_WALLPAPER, launchOptions);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001905 } catch (ActivityNotFoundException e) {
1906 setWaitingForResult(null);
1907 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1908 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001909 }
1910
Sunny Goyala7ce1662016-05-31 15:01:35 -07001911 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001912 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001913 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
1914 try {
1915 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
1916 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
1917 // is enabled by default on NYC.
1918 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
1919 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001920
1921 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
1922 String id = ((ShortcutInfo) info).getDeepShortcutId();
1923 String packageName = intent.getPackage();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301924 DeepShortcutManager.getInstance(this).startShortcut(
Sunny Goyal9994b2b2016-06-23 14:17:24 -07001925 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001926 } else {
1927 // Could be launching some bookkeeping activity
1928 startActivity(intent, optsBundle);
1929 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07001930 } finally {
1931 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01001932 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001933 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001934 // Due to legacy reasons, direct call shortcuts require Launchers to have the
1935 // corresponding permission. Show the appropriate permission prompt if that
1936 // is the case.
1937 if (intent.getComponent() == null
1938 && Intent.ACTION_CALL.equals(intent.getAction())
1939 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
1940 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001941
1942 setWaitingForResult(PendingRequestArgs
1943 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07001944 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
1945 REQUEST_PERMISSION_CALL_PHONE);
1946 } else {
1947 // No idea why this was thrown.
1948 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07001949 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001950 }
1951 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001952
Sunny Goyalfe770c92016-09-27 14:38:58 -07001953 @TargetApi(Build.VERSION_CODES.M)
Jon Miranda17940f02018-01-18 14:46:34 -08001954 public Bundle getActivityLaunchOptions(View v, boolean useDefaultLaunchOptions) {
1955 return useDefaultLaunchOptions
Jon Miranda54441f52018-01-24 15:38:25 -08001956 ? mAppTransitionManager.getDefaultActivityLaunchOptions(this, v)
1957 : mAppTransitionManager.getActivityLaunchOptions(this, v);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001958 }
1959
Tonye3c59252017-05-02 21:32:27 -07001960 public Rect getViewBounds(View v) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001961 int[] pos = new int[2];
1962 v.getLocationOnScreen(pos);
1963 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
1964 }
1965
Sunny Goyala7ce1662016-05-31 15:01:35 -07001966 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07001967 mAppLaunchSuccess = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001968 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
1969 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07001970 return mAppLaunchSuccess;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001971 }
Jon Miranda17940f02018-01-18 14:46:34 -08001972
1973 boolean isShortcut = Utilities.ATLEAST_MARSHMALLOW
1974 && (item instanceof ShortcutInfo)
1975 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
1976 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
1977 && !((ShortcutInfo) item).isPromise();
1978
Sunny Goyala7ce1662016-05-31 15:01:35 -07001979 // Only launch using the new animation if the shortcut has not opted out (this is a
1980 // private contract between launcher and may be ignored in the future).
1981 boolean useLaunchAnimation = (v != null) &&
1982 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Jon Miranda17940f02018-01-18 14:46:34 -08001983 Bundle optsBundle = useLaunchAnimation
1984 ? getActivityLaunchOptions(v, isShortcut || isInMultiWindowModeCompat())
1985 : null;
Sunny Goyala7ce1662016-05-31 15:01:35 -07001986
Sunny Goyal24bb66a2017-03-21 15:12:01 -07001987 UserHandle user = item == null ? null : item.user;
Sunny Goyala7ce1662016-05-31 15:01:35 -07001988
1989 // Prepare intent
1990 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1991 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001992 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07001993 }
1994 try {
Jon Miranda17940f02018-01-18 14:46:34 -08001995 if (isShortcut) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001996 // Shortcuts need some special checks due to legacy reasons.
1997 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001998 } else if (user == null || user.equals(Process.myUserHandle())) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001999 // Could be launching some bookkeeping activity
2000 startActivity(intent, optsBundle);
2001 } else {
2002 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2003 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2004 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002005
2006 if (v instanceof BubbleTextView) {
2007 // This is set to the view that launched the activity that navigated the user away
2008 // from launcher. Since there is no callback for when the activity has finished
2009 // launching, enable the press state and keep this reference to reset the press
2010 // state when we return to launcher.
2011 BubbleTextView btv = (BubbleTextView) v;
2012 btv.setStayPressed(true);
2013 setOnResumeCallback(btv);
2014 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002015 mAppLaunchSuccess = true;
2016 getUserEventDispatcher().logAppLaunch(v, intent); // TODO for discovered apps b/35802115
Sunny Goyala7ce1662016-05-31 15:01:35 -07002017 } catch (ActivityNotFoundException|SecurityException e) {
2018 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2019 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2020 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002021 return mAppLaunchSuccess;
Michael Jurka86a720e2012-05-09 11:23:23 -07002022 }
2023
Tony Wickhamdadb3042016-02-24 11:07:00 -08002024 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07002025 public boolean dispatchTouchEvent(MotionEvent ev) {
Sunny Goyal02424b22018-01-19 11:24:32 -08002026 mLastDispatchTouchEvent.set(ev.getX(), ev.getY());
Jon Miranda379198e2016-09-23 08:54:40 -07002027 return super.dispatchTouchEvent(ev);
2028 }
2029
2030 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002031 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002032 if (!isDraggingEnabled()) return false;
2033 if (isWorkspaceLocked()) return false;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002034 if (!isInState(NORMAL) && !isInState(OVERVIEW)) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002035
Jon Miranda51f037d2016-11-07 08:37:20 -08002036 boolean ignoreLongPressToOverview =
Sunny Goyal02424b22018-01-19 11:24:32 -08002037 mDeviceProfile.shouldIgnoreLongPressToOverview(mLastDispatchTouchEvent.x);
Jon Miranda379198e2016-09-23 08:54:40 -07002038
Adam Cohen1697b792013-09-17 19:08:21 -07002039 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002040 if (!isInState(OVERVIEW)) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002041 if (!mWorkspace.isTouchActive() && !ignoreLongPressToOverview) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302042 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2043 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002044 mWorkspace.getCurrentPage());
Sunny Goyal02424b22018-01-19 11:24:32 -08002045 UiFactory.onWorkspaceLongPress(this, mLastDispatchTouchEvent);
Adam Cohen93c97562013-09-26 13:48:01 -07002046 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2047 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2048 return true;
2049 } else {
2050 return false;
2051 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002052 } else {
2053 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002054 }
Adam Cohen1697b792013-09-17 19:08:21 -07002055 }
2056
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002057 CellLayout.CellInfo longClickCellInfo = null;
2058 View itemUnderLongClick = null;
2059 if (v.getTag() instanceof ItemInfo) {
2060 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002061 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002062 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07002063 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002064 }
2065
Winson Chung3d503fb2011-07-13 17:25:49 -07002066 // The hotseat touch handling does not go through Workspace, and we always allow long press
2067 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002068 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002069 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002070 // User long pressed on empty space
Sunny Goyalc99cb172017-10-19 16:15:09 -07002071 if (mWorkspace.isPageRearrangeEnabled()) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002072 mWorkspace.startReordering(v);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302073 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2074 Action.Direction.NONE, ContainerType.OVERVIEW);
Adam Cohend3ecce92013-09-16 14:58:00 -07002075 } else {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002076 if (ignoreLongPressToOverview) {
Jon Miranda379198e2016-09-23 08:54:40 -07002077 return false;
2078 }
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302079 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2080 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002081 mWorkspace.getCurrentPage());
Sunny Goyal02424b22018-01-19 11:24:32 -08002082 UiFactory.onWorkspaceLongPress(this, mLastDispatchTouchEvent);
Adam Cohendedbd962013-07-11 14:21:49 -07002083 }
Jon Miranda379198e2016-09-23 08:54:40 -07002084 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2085 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002086 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07002087 final boolean isAllAppsButton =
2088 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
2089 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
2090 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05002091 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002092 // User long pressed on an item
Tony Wickham10236d62016-09-28 12:49:25 -07002093 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002094 }
2095 }
2096 }
2097 return true;
2098 }
2099
Winson Chung3d503fb2011-07-13 17:25:49 -07002100 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08002101 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07002102 return mHotseat != null && layout != null &&
2103 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2104 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002105
Winson Chung3d503fb2011-07-13 17:25:49 -07002106 /**
2107 * Returns the CellLayout of the specified container at the specified screen.
2108 */
Sunny Goyal92820592015-03-02 11:31:35 -08002109 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002110 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2111 if (mHotseat != null) {
2112 return mHotseat.getLayout();
2113 } else {
2114 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002115 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002116 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002117 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002118 }
2119 }
2120
Michael Jurkae326f182011-11-21 14:05:46 -08002121 @Override
2122 public void onTrimMemory(int level) {
2123 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07002124 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2125 // The widget preview db can result in holding onto over
2126 // 3MB of memory for caching which isn't necessary.
2127 SQLiteDatabase.releaseMemory();
2128
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08002129 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07002130 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08002131 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01002132 if (mLauncherCallbacks != null) {
2133 mLauncherCallbacks.onTrimMemory(level);
2134 }
Michael Jurkae326f182011-11-21 14:05:46 -08002135 }
2136
Michael Jurkad7c28052012-04-27 15:43:36 -07002137 @Override
2138 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07002139 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07002140 final List<CharSequence> text = event.getText();
2141 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07002142 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002143 // TODO: When can workspace be null?
2144 text.add(mWorkspace == null
2145 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07002146 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07002147 return result;
2148 }
2149
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002150 public void setOnResumeCallback(OnResumeCallback callback) {
2151 if (mOnResumeCallback != null) {
2152 mOnResumeCallback.onLauncherResume();
2153 }
2154 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07002155 }
2156
Michael Jurka7607c2f2013-04-03 14:33:19 -07002157 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002158 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002159 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002160 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002161 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002162 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002163 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002164 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07002165 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07002166 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002167 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002168
Joe Onorato9c1289c2009-08-17 11:03:03 -04002169 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07002170 * Clear any pending bind callbacks. This is called when is loader is planning to
2171 * perform a full rebind from scratch.
2172 */
2173 @Override
2174 public void clearPendingBinds() {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002175 if (mPendingExecutor != null) {
2176 mPendingExecutor.markCompleted();
2177 mPendingExecutor = null;
2178 }
2179 }
2180
2181 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002182 * Refreshes the shortcuts shown on the workspace.
2183 *
2184 * Implementation of the method from LauncherModel.Callbacks.
2185 */
2186 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002187 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002188 // Floating panels (except the full widget sheet) are associated with individual icons. If
2189 // we are starting a fresh bind, close all such panels as all the icons are about
2190 // to go away.
2191 AbstractFloatingView.closeOpenViews(this, true,
Sunny Goyal7ede6112017-12-05 15:11:21 -08002192 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_REBIND_SAFE);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08002193
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002194 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08002195
Winson Chungd64d1762013-08-20 14:37:16 -07002196 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07002197 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07002198 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07002199
Winson Chung3d503fb2011-07-13 17:25:49 -07002200 if (mHotseat != null) {
2201 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002202 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002203 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002204 }
2205
Adam Cohendcd297f2013-06-18 13:13:40 -07002206 @Override
2207 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002208 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002209 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
2210 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002211 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
2212 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002213 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002214 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
2215 // If there are no screens, we need to have an empty screen
2216 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07002217 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002218 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07002219
Winsonc7d2e832016-07-28 12:24:55 -07002220 // After we have added all the screens, if the wallpaper was locked to the default state,
2221 // then notify to indicate that it can be released and a proper wallpaper offset can be
2222 // computed before the next layout
2223 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07002224 }
2225
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002226 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002227 int count = orderedScreenIds.size();
2228 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002229 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002230 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002231 // No need to bind the first screen, as its always bound.
2232 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
2233 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002234 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002235 }
2236
Sunny Goyalb23980c2017-08-17 07:45:25 -07002237 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002238 public void bindAppsAdded(ArrayList<Long> newScreens, ArrayList<ItemInfo> addNotAnimated,
2239 ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07002240 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08002241 if (newScreens != null) {
2242 bindAddScreens(newScreens);
2243 }
Winson Chungd64d1762013-08-20 14:37:16 -07002244
2245 // We add the items without animation on non-visible pages, and with
2246 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08002247 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002248 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002249 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002250 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002251 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07002252 }
Winson Chungc58497e2013-09-03 17:48:37 -07002253
Winson Chung87412982013-10-03 18:34:14 -07002254 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07002255 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002256 }
2257
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002258 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002259 * Bind the items start-end from the list.
2260 *
2261 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002262 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07002263 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07002264 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002265 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07002266 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002267 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07002268 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07002269 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002270 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07002271 int end = items.size();
2272 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002273 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002274
2275 // Short circuit if we are loading dock items for a configuration which has no dock
2276 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2277 mHotseat == null) {
2278 continue;
2279 }
2280
Sunny Goyal639e9062015-08-19 19:17:06 -07002281 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002282 switch (item.itemType) {
2283 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2284 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08002285 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07002286 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07002287 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002288 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002289 }
2290 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07002291 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002292 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002293 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002294 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002295 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07002296 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2297 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07002298 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002299 if (view == null) {
2300 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002301 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08002302 break;
2303 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002304 default:
2305 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002306 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002307
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002308 /*
2309 * Remove colliding items.
2310 */
2311 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
2312 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
2313 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
2314 View v = cl.getChildAt(item.cellX, item.cellY);
2315 Object tag = v.getTag();
2316 String desc = "Collision while binding workspace item: " + item
2317 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08002318 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002319 throw (new RuntimeException(desc));
2320 } else {
2321 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002322 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002323 continue;
2324 }
2325 }
2326 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302327 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07002328 if (animateIcons) {
2329 // Animate all the applications up now
2330 view.setAlpha(0f);
2331 view.setScaleX(0f);
2332 view.setScaleY(0f);
2333 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08002334 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07002335 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002336 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07002337
Winson Chung997a9232013-07-24 15:33:46 -07002338 if (animateIcons) {
2339 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08002340 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07002341 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08002342 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07002343 final Runnable startBounceAnimRunnable = new Runnable() {
2344 public void run() {
2345 anim.playTogether(bounceAnims);
2346 anim.start();
2347 }
2348 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08002349 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07002350 // We post the animation slightly delayed to prevent slowdowns
2351 // when we are loading right after we return to launcher.
2352 mWorkspace.postDelayed(new Runnable() {
2353 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00002354 if (mWorkspace != null) {
2355 mWorkspace.snapToPage(newScreenIndex);
2356 mWorkspace.postDelayed(startBounceAnimRunnable,
2357 NEW_APPS_ANIMATION_DELAY);
2358 }
Winson Chung94d67682013-09-25 16:29:40 -07002359 }
2360 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07002361 } else {
2362 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07002363 }
2364 }
Winson Chung64359a52013-07-08 17:17:08 -07002365 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002366 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002367 }
2368
Joe Onorato9c1289c2009-08-17 11:03:03 -04002369 /**
2370 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002371 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07002372 public void bindAppWidget(LauncherAppWidgetInfo item) {
2373 View view = inflateAppWidget(item);
2374 if (view != null) {
2375 mWorkspace.addInScreen(view, item);
2376 mWorkspace.requestLayout();
2377 }
2378 }
2379
2380 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002381 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302382 PendingAppWidgetHostView view =
2383 new PendingAppWidgetHostView(this, item, mIconCache, true);
2384 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002385 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002386 }
2387
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002388 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002389
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002390 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08002391
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002392 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2393 // If the provider is not ready, bind as a pending widget.
2394 appWidgetInfo = null;
2395 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2396 // The widget id is not valid. Try to find the widget based on the provider info.
2397 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
2398 } else {
2399 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
2400 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002401
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002402 // If the provider is ready, but the width is not yet restored, try to restore it.
2403 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
2404 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07002405 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002406 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
2407 + " belongs to component " + item.providerName
2408 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002409 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002410 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002411 }
Sunny Goyalff572272014-07-23 13:58:07 -07002412
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002413 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002414 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07002415 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2416 // Id has not been allocated yet. Allocate a new id.
2417 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
2418 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07002419
Sunny Goyald478c832016-04-01 12:04:16 -07002420 // Also try to bind the widget. If the bind fails, the user will be shown
2421 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08002422 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07002423 pendingInfo.spanX = item.spanX;
2424 pendingInfo.spanY = item.spanY;
2425 pendingInfo.minSpanX = item.minSpanX;
2426 pendingInfo.minSpanY = item.minSpanY;
2427 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07002428
2429 boolean isDirectConfig =
2430 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2431 if (isDirectConfig && item.bindOptions != null) {
2432 Bundle newOptions = item.bindOptions.getExtras();
2433 if (options != null) {
2434 newOptions.putAll(options);
2435 }
2436 options = newOptions;
2437 }
Sunny Goyald478c832016-04-01 12:04:16 -07002438 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2439 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002440
Sunny Goyal86df1382016-08-10 15:03:22 -07002441 // We tried to bind once. If we were not able to bind, we would need to
2442 // go through the permission dialog, which means we cannot skip the config
2443 // activity.
2444 item.bindOptions = null;
2445 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2446
Sunny Goyald478c832016-04-01 12:04:16 -07002447 // Bind succeeded
2448 if (success) {
2449 // If the widget has a configure activity, it is still needs to set it up,
2450 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002451 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002452 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2453 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002454 }
Sunny Goyald478c832016-04-01 12:04:16 -07002455
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002456 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002457 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002458 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002459 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002460 // The widget was marked as UI not ready, but there is no configure activity to
2461 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002462 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002463 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002464 }
Sunny Goyalff572272014-07-23 13:58:07 -07002465 }
2466
Sunny Goyald5462aa2016-11-22 16:52:39 +05302467 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002468 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002469 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002470 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002471 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07002472 deleteWidgetInfo(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002473 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002474 }
2475
Sunny Goyal233ee962015-08-03 13:05:01 -07002476 item.minSpanX = appWidgetInfo.minSpanX;
2477 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302478 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002479 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302480 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002481 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302482 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002483
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002484 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002485 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002486 }
2487
Sunny Goyalff572272014-07-23 13:58:07 -07002488 /**
2489 * Restores a pending widget.
2490 *
2491 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002492 */
Sunny Goyald478c832016-04-01 12:04:16 -07002493 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002494 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2495 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2496 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002497 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002498 }
2499
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002500 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002501 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002502 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2503 info.pendingItemInfo = null;
2504 }
Sunny Goyalff572272014-07-23 13:58:07 -07002505
Sunny Goyalba47faa2017-10-04 16:50:57 -07002506 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002507 view.reInflate();
Sunny Goyalba47faa2017-10-04 16:50:57 -07002508 }
2509
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002510 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002511 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002512 }
2513
Adam Cohen1462de32012-07-24 22:34:36 -07002514 public void onPageBoundSynchronously(int page) {
Sunny Goyalf4aceab2017-12-06 16:16:31 -08002515 mSynchronouslyBoundPage = page;
Adam Cohen1462de32012-07-24 22:34:36 -07002516 }
2517
Sunny Goyal527c7d32015-08-28 15:19:36 -07002518 @Override
2519 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2520 if (mPendingExecutor != null) {
2521 mPendingExecutor.markCompleted();
2522 }
2523 mPendingExecutor = executor;
2524 executor.attachTo(this);
2525 }
2526
2527 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2528 if (mPendingExecutor == executor) {
2529 mPendingExecutor = null;
2530 }
2531 }
2532
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002533 @Override
2534 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002535 if (mDragLayer.getAlpha() < 1) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002536 mDragLayer.animate().alpha(1).withEndAction(
2537 executor == null ? null : executor::onLoadAnimationCompleted).start();
2538 } else if (executor != null) {
2539 executor.onLoadAnimationCompleted();
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002540 }
2541 }
2542
Joe Onorato9c1289c2009-08-17 11:03:03 -04002543 /**
2544 * Callback saying that there aren't any more items to bind.
2545 *
2546 * Implementation of the method from LauncherModel.Callbacks.
2547 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002548 public void finishBindingItems() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002549 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002550 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002551
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002552 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002553
Sunny Goyal2100c782016-08-22 16:00:03 -07002554 if (mPendingActivityResult != null) {
2555 handleActivityResult(mPendingActivityResult.requestCode,
2556 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2557 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002558 }
2559
Sunny Goyala474a9b2017-05-04 16:47:11 -07002560 InstallShortcutReceiver.disableAndFlushInstallQueue(
2561 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002562
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002563 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002564 }
2565
Winson Chunga2413752012-04-03 14:22:34 -07002566 private boolean canRunNewAppsAnimation() {
2567 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002568 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002569 }
2570
Winson Chungc9168342013-06-26 14:54:55 -07002571 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002572 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07002573 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
2574 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002575 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002576 return bounceAnim;
2577 }
2578
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002579 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002580 * Add the icons for all apps.
2581 *
2582 * Implementation of the method from LauncherModel.Callbacks.
2583 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002584 public void bindAllApplications(ArrayList<AppInfo> apps) {
Winson Chungb745afb2015-03-02 11:51:23 -08002585 if (mAppsView != null) {
Tony2917a8b2017-07-31 23:29:42 -07002586 Executor pendingExecutor = getPendingExecutor();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002587 if (pendingExecutor != null && !isInState(ALL_APPS)) {
Tony2917a8b2017-07-31 23:29:42 -07002588 // Wait until the fade in animation has finished before setting all apps list.
Sunny Goyal29947f02017-12-18 13:49:44 -08002589 pendingExecutor.execute(() -> bindAllApplications(apps));
Tony2917a8b2017-07-31 23:29:42 -07002590 return;
2591 }
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002592
Winson Chungb745afb2015-03-02 11:51:23 -08002593 mAppsView.setApps(apps);
2594 }
Adam Cohen9211d422014-10-07 18:14:53 -07002595 if (mLauncherCallbacks != null) {
2596 mLauncherCallbacks.bindAllApplications(apps);
2597 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002598 }
2599
2600 /**
Tony2917a8b2017-07-31 23:29:42 -07002601 * Returns an Executor that will run after the launcher is first drawn (including after the
2602 * initial fade in animation). Returns null if the first draw has already occurred.
2603 */
2604 public @Nullable Executor getPendingExecutor() {
2605 return mPendingExecutor != null && mPendingExecutor.canQueue() ? mPendingExecutor : null;
2606 }
2607
2608 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002609 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
2610 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2611 */
2612 @Override
2613 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002614 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002615 }
2616
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002617 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002618 * A package was updated.
2619 *
2620 * Implementation of the method from LauncherModel.Callbacks.
2621 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002622 @Override
2623 public void bindAppsAddedOrUpdated(ArrayList<AppInfo> apps) {
Winson Chungb745afb2015-03-02 11:51:23 -08002624 if (mAppsView != null) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002625 mAppsView.addOrUpdateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07002626 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002627 }
2628
Sunny Goyal4390ace2014-10-13 11:33:11 -07002629 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002630 public void bindPromiseAppProgressUpdated(PromiseAppInfo app) {
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002631 if (mAppsView != null) {
2632 mAppsView.updatePromiseAppProgress(app);
2633 }
2634 }
2635
2636 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002637 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002638 mWorkspace.widgetsRestored(widgets);
2639 }
2640
Joe Onorato9c1289c2009-08-17 11:03:03 -04002641 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002642 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002643 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002644 *
2645 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002646 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002647 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002648 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002649 if (!updated.isEmpty()) {
2650 mWorkspace.updateShortcuts(updated);
2651 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002652 }
2653
2654 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002655 * Update the state of a package, typically related to install state.
2656 *
2657 * Implementation of the method from LauncherModel.Callbacks.
2658 */
Sunny Goyale755d462014-07-22 13:48:29 -07002659 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002660 public void bindRestoreItemsChange(HashSet<ItemInfo> updates) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07002661 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07002662 }
2663
2664 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002665 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07002666 * in addition to specific applications to remove, the reason being that
2667 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002668 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07002669 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002670 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07002671 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002672 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002673 mWorkspace.removeItemsByMatcher(matcher);
2674 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002675 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002676
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002677 @Override
2678 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
Winson Chungdf95eb12013-10-16 14:57:07 -07002679 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08002680 if (mAppsView != null) {
2681 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07002682 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002683 }
Joe Onoratobe386092009-11-17 17:32:16 -08002684
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002685 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002686 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
2687 mPopupDataProvider.setAllWidgets(allWidgets);
Tony Wickham26b17462017-03-20 17:12:24 -07002688 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
2689 if (topView != null) {
2690 topView.onWidgetsBound();
2691 }
2692 }
2693
Tony Wickham86222d22017-03-29 15:30:43 -07002694 /**
2695 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
2696 * refreshes the widgets and shortcuts associated with the given package/user
2697 */
2698 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07002699 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002700 }
2701
Sunny Goyalc99cb172017-10-19 16:15:09 -07002702 public boolean isRotationEnabled () {
2703 return mRotationEnabled;
Winson Chung400438b2011-01-16 17:53:48 -08002704 }
2705
Winson Chung80baf5a2010-08-09 16:03:15 -07002706 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002707 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08002708 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002709 @Override
2710 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
2711 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002712
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002713 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
2714 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07002715 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002716 writer.println(prefix + " Homescreen " + i);
2717
2718 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
2719 for (int j = 0; j < layout.getChildCount(); j++) {
2720 Object tag = layout.getChildAt(j).getTag();
2721 if (tag != null) {
2722 writer.println(prefix + " " + tag.toString());
2723 }
2724 }
2725 }
2726
2727 writer.println(prefix + " Hotseat");
2728 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07002729 for (int j = 0; j < layout.getChildCount(); j++) {
2730 Object tag = layout.getChildAt(j).getTag();
2731 if (tag != null) {
2732 writer.println(prefix + " " + tag.toString());
2733 }
Adam Cohen4caf2982013-08-20 18:54:31 -07002734 }
Sunny Goyala1365452015-10-01 15:46:24 -07002735
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002736 try {
2737 FileLog.flushAll(writer);
2738 } catch (Exception e) {
2739 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07002740 }
2741 }
2742
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002743 writer.println(prefix + "Misc:");
2744 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
2745 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
2746 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
2747
2748 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002749
Adam Cohen9211d422014-10-07 18:14:53 -07002750 if (mLauncherCallbacks != null) {
2751 mLauncherCallbacks.dump(prefix, fd, writer, args);
2752 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002753 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002754
Sunny Goyal66b24572016-09-21 15:57:55 -07002755 @Override
2756 @TargetApi(Build.VERSION_CODES.N)
2757 public void onProvideKeyboardShortcuts(
2758 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
2759
2760 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002761 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002762 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
2763 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
2764 }
2765 View currentFocus = getCurrentFocus();
2766 if (new CustomActionsPopup(this, currentFocus).canShow()) {
2767 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
2768 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
2769 }
Tony18c4aa42017-05-10 21:23:57 -05002770 if (currentFocus.getTag() instanceof ItemInfo
2771 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002772 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
2773 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
2774 }
2775 if (!shortcutInfos.isEmpty()) {
2776 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
2777 }
2778
2779 super.onProvideKeyboardShortcuts(data, menu, deviceId);
2780 }
2781
2782 @Override
2783 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
2784 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
2785 switch (keyCode) {
2786 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002787 if (isInState(NORMAL)) {
2788 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07002789 return true;
2790 }
2791 break;
2792 case KeyEvent.KEYCODE_S: {
2793 View focusedView = getCurrentFocus();
2794 if (focusedView instanceof BubbleTextView
2795 && focusedView.getTag() instanceof ItemInfo
2796 && mAccessibilityDelegate.performAction(focusedView,
2797 (ItemInfo) focusedView.getTag(),
2798 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal00ac9202017-11-09 15:24:06 -08002799 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07002800 return true;
2801 }
2802 break;
2803 }
2804 case KeyEvent.KEYCODE_O:
2805 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
2806 return true;
2807 }
2808 break;
2809 }
2810 }
2811 return super.onKeyShortcut(keyCode, event);
2812 }
2813
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002814 @Override
2815 public boolean onKeyUp(int keyCode, KeyEvent event) {
2816 if (keyCode == KeyEvent.KEYCODE_MENU) {
2817 // KEYCODE_MENU is sent by some tests, for example
2818 // LauncherJankTests#testWidgetsContainerFling. Don't just remove its handling.
2819 if (!mDragController.isDragging() && !mWorkspace.isSwitchingState() &&
2820 isInState(NORMAL)) {
2821 // Close any open floating views.
2822 AbstractFloatingView.closeAllOpenViews(this);
2823
2824 // Setting the touch point to (-1, -1) will show the options popup in the center of
2825 // the screen.
2826 mLastDispatchTouchEvent.set(-1, -1);
2827 UiFactory.onWorkspaceLongPress(this, mLastDispatchTouchEvent);
2828 }
2829 return true;
2830 }
2831 return super.onKeyUp(keyCode, event);
2832 }
2833
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002834 public static Launcher getLauncher(Context context) {
2835 if (context instanceof Launcher) {
2836 return (Launcher) context;
2837 }
2838 return ((Launcher) ((ContextWrapper) context).getBaseContext());
2839 }
2840
Sunny Goyal5a81c382017-03-20 15:08:06 -07002841 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener {
Sunny Goyal745bad92016-05-02 10:54:12 -07002842
2843 @Override
2844 public void onSharedPreferenceChanged(
2845 SharedPreferences sharedPreferences, String key) {
2846 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
Sunny Goyale13d4642017-05-22 15:58:42 -07002847 // Recreate the activity so that it initializes the rotation preference again.
2848 recreate();
Sunny Goyal745bad92016-05-02 10:54:12 -07002849 }
2850 }
Sunny Goyal745bad92016-05-02 10:54:12 -07002851 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002852
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08002853 @Override
2854 public void onActionModeStarted(ActionMode mode) {
2855 super.onActionModeStarted(mode);
2856 mCurrentActionMode = mode;
2857 }
2858
2859 @Override
2860 public void onActionModeFinished(ActionMode mode) {
2861 super.onActionModeFinished(mode);
2862 mCurrentActionMode = null;
2863 }
2864
2865 public boolean finishAutoCancelActionMode() {
2866 if (mCurrentActionMode != null && AUTO_CANCEL_ACTION_MODE == mCurrentActionMode.getTag()) {
2867 mCurrentActionMode.finish();
2868 return true;
2869 }
2870 return false;
2871 }
2872
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002873 /**
2874 * Callback for listening for onResume
2875 */
2876 public interface OnResumeCallback {
2877
2878 void onLauncherResume();
2879 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002880}