blob: a1176f18006c7233429962a4160fde0eb0d59b38 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080019import static android.content.pm.ActivityInfo.CONFIG_ORIENTATION;
20import static android.content.pm.ActivityInfo.CONFIG_SCREEN_SIZE;
Sunny Goyal623eddd2018-03-02 12:24:41 -080021
Sunny Goyal076839c2017-10-30 13:52:20 -070022import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_BY_PUBLISHER;
23import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_LOCKED_USER;
24import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_QUIET_USER;
25import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_SAFEMODE;
26import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_SUSPENDED;
Sunny Goyal4c7f2152017-10-17 17:17:16 -070027import static com.android.launcher3.LauncherAnimUtils.SPRING_LOADED_EXIT_DELAY;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -070028import static com.android.launcher3.LauncherState.ALL_APPS;
29import static com.android.launcher3.LauncherState.NORMAL;
30import static com.android.launcher3.LauncherState.OVERVIEW;
Sunny Goyalaeb16432017-10-16 11:46:41 -070031import static com.android.launcher3.logging.LoggerUtils.newContainerTarget;
Sunny Goyal326403e2017-10-02 12:45:10 -070032
Sunny Goyal28c6b962015-10-12 11:42:05 -070033import android.Manifest;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070034import android.animation.Animator;
Jon Miranda2d89ea82017-05-04 11:47:53 -070035import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070036import android.animation.AnimatorSet;
Jon Miranda2d89ea82017-05-04 11:47:53 -070037import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070038import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070039import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000040import android.annotation.TargetApi;
Tony Wickham005df0b2018-02-13 11:28:12 -080041import android.app.ActivityOptions;
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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055import android.content.pm.PackageManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080056import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070057import android.database.sqlite.SQLiteDatabase;
Jon Mirandacc42c5b2016-10-27 17:15:27 -070058import android.graphics.Point;
Sunny Goyal02424b22018-01-19 11:24:32 -080059import android.graphics.PointF;
Michael Jurkaaf442092010-06-10 17:01:57 -070060import android.graphics.Rect;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070061import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020062import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.os.Bundle;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070064import android.os.Parcelable;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080065import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070066import android.os.StrictMode;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070067import android.os.UserHandle;
Tony Wickham86222d22017-03-29 15:30:43 -070068import android.support.annotation.Nullable;
Michael Jurkaa33411c2012-06-14 16:18:21 -070069import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070071import android.util.Log;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070072import android.util.SparseArray;
Sunny Goyal7c8a65e2017-12-22 11:11:33 -080073import android.view.ActionMode;
Adam Cohen96d30a12013-07-16 18:13:21 -070074import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070075import android.view.HapticFeedbackConstants;
76import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070077import android.view.KeyboardShortcutGroup;
78import android.view.KeyboardShortcutInfo;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080079import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -070080import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070081import android.view.MotionEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070082import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080083import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070084import android.view.ViewGroup;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080085import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070086import android.view.animation.OvershootInterpolator;
Adam Cohen96d30a12013-07-16 18:13:21 -070087import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070088
Sunny Goyal651077b2014-06-30 14:15:31 -070089import com.android.launcher3.DropTarget.DragObject;
Sunny Goyala7ce1662016-05-31 15:01:35 -070090import com.android.launcher3.LauncherSettings.Favorites;
Jon Mirandac476d6e2017-05-04 16:30:01 -070091import com.android.launcher3.Workspace.ItemOperator;
Sunny Goyalae502842016-06-17 08:43:56 -070092import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070093import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070094import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal7ede6112017-12-05 15:11:21 -080095import com.android.launcher3.allapps.DiscoveryBounce;
Sunny Goyalffe83f12014-08-14 17:39:34 -070096import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010097import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -070098import com.android.launcher3.compat.LauncherAppsCompatVO;
Tony Wickhame0c33232016-02-08 11:37:04 -080099import com.android.launcher3.config.FeatureFlags;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700100import com.android.launcher3.dragndrop.DragController;
101import com.android.launcher3.dragndrop.DragLayer;
Sunny Goyal94b510c2016-08-16 15:36:48 -0700102import com.android.launcher3.dragndrop.DragOptions;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700103import com.android.launcher3.dragndrop.DragView;
Mario Bertschler27288382017-05-24 15:35:09 -0700104import com.android.launcher3.dynamicui.WallpaperColorInfo;
Sunny Goyal26119432016-02-18 22:09:23 +0000105import com.android.launcher3.folder.Folder;
106import com.android.launcher3.folder.FolderIcon;
Sunny Goyal66b24572016-09-21 15:57:55 -0700107import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700108import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700109import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700110import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyala535ae42017-02-27 10:07:13 -0800111import com.android.launcher3.model.ModelWriter;
Sunny Goyala535ae42017-02-27 10:07:13 -0800112import com.android.launcher3.notification.NotificationListener;
Tony Wickham540913e2017-01-23 11:47:51 -0800113import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -0800114import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700115import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyalf8088ee2017-11-10 14:52:00 -0800116import com.android.launcher3.states.InternalStateHandler;
Sunny Goyal623eddd2018-03-02 12:24:41 -0800117import com.android.launcher3.states.RotationHelper;
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;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700153
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154/**
155 * Default launcher application.
156 */
Sunny Goyal27835952017-01-13 12:15:53 -0800157public class Launcher extends BaseActivity
Adam Cohen79d90c52016-04-22 13:29:20 -0700158 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
159 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Mario Bertschler27288382017-05-24 15:35:09 -0700160 WallpaperColorInfo.OnThemeChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700161 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700162 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163
Winson Chunga2413752012-04-03 14:22:34 -0700164 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700165
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700167 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700168
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700169 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700170 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171
Michael Jurka8b805b12012-04-18 14:23:14 -0700172 private static final int REQUEST_BIND_APPWIDGET = 11;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700173 private static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
174 private static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700175
Jon Mirandac476d6e2017-05-04 16:30:01 -0700176 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700177
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700178 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
179
Mathew Inwood876a8462013-06-14 14:12:41 +0100180 /**
181 * IntentStarter uses request codes starting with this. This must be greater than all activity
182 * request codes used internally.
183 */
184 protected static final int REQUEST_LAST = 100;
185
Winson Chung2672ff92012-05-04 16:22:30 -0700186 // The Intent extra that defines whether to ignore the launch animation
187 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400188 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700189
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 // Type: int
191 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700192 // Type: int
193 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700194 // Type: PendingRequestArgs
195 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
196 // Type: ActivityResultInfo
197 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700198 // Type: SparseArray<Parcelable>
199 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200
Sunny Goyal7c8a65e2017-12-22 11:11:33 -0800201 // When starting an action mode, setting this tag will cause the action mode to be cancelled
202 // automatically when user interacts with the launcher.
203 public static final Object AUTO_CANCEL_ACTION_MODE = new Object();
204
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700205 private LauncherStateManager mStateManager;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700206
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700207 private boolean mIsSafeModeEnabled;
208
Adam Cohenad4e15c2013-10-17 16:21:35 -0700209 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210
Winson Chunga2413752012-04-03 14:22:34 -0700211 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700212 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
213 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
214 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700215
Jon Miranda54441f52018-01-24 15:38:25 -0800216 private LauncherAppTransitionManager mAppTransitionManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800217 private Configuration mOldConfig;
Jon Miranda54441f52018-01-24 15:38:25 -0800218
Adam Cohen091440a2015-03-18 14:16:05 -0700219 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700220 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700221 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800222 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700223
Sunny Goyalffe83f12014-08-14 17:39:34 -0700224 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700225 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700226
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700227 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700228
Sunny Goyal316490e2015-06-02 09:38:28 -0700229 @Thunk Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700230
Sunny Goyal47328fd2016-05-25 18:56:41 -0700231 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700232
233 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700234 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700235 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700236
Winson Chung8ae41982017-11-10 11:42:13 -0800237 // UI and state for the overview panel
238 private ViewGroup mOverviewPanel;
239
Adam Cohen091440a2015-03-18 14:16:05 -0700240 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400241
Sunny Goyal60876ac2018-02-20 14:21:20 -0800242 private OnStartCallback mOnStartCallback;
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;
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700274 private boolean mAppLaunchSuccess;
275
Sunny Goyal623eddd2018-03-02 12:24:41 -0800276 private RotationHelper mRotationHelper;
Sunny Goyal7c8a65e2017-12-22 11:11:33 -0800277 private ActionMode mCurrentActionMode;
Sunny Goyal7779d622015-06-11 16:18:39 -0700278
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800279 @Override
280 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700281 if (DEBUG_STRICT_MODE) {
282 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
283 .detectDiskReads()
284 .detectDiskWrites()
285 .detectNetwork() // or .detectAll() for all detectable problems
286 .penaltyLog()
287 .build());
288 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
289 .detectLeakedSqlLiteObjects()
290 .detectLeakedClosableObjects()
291 .penaltyLog()
292 .penaltyDeath()
293 .build());
294 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700295 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700296
Mario Bertschler27288382017-05-24 15:35:09 -0700297 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
298 wallpaperColorInfo.setOnThemeChangeListener(this);
Mario Bertschlera6936942017-05-31 14:48:19 -0700299 overrideTheme(wallpaperColorInfo.isDark(), wallpaperColorInfo.supportsDarkText());
Mario Bertschler27288382017-05-24 15:35:09 -0700300
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800301 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700302 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400303
Sunny Goyal87f784c2017-01-11 10:48:34 -0800304 LauncherAppState app = LauncherAppState.getInstance(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800305 mOldConfig = new Configuration(getResources().getConfiguration());
Sunny Goyal0e7724c2018-02-22 13:22:21 -0800306 mModel = app.setLauncher(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800307 initDeviceProfile(app.getInvariantDeviceProfile());
Winson Chung6e1c0d32013-10-25 15:24:24 -0700308
Sunny Goyalf7258242015-10-19 16:59:07 -0700309 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700310 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800311 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700312 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700313
Joe Onorato41a12d22009-10-31 18:30:00 -0400314 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700315 mAllAppsController = new AllAppsTransitionController(this);
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800316 mStateManager = new LauncherStateManager(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700317
Sunny Goyalffe83f12014-08-14 17:39:34 -0700318 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700319
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700320 mAppWidgetHost = new LauncherAppWidgetHost(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700321 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700322
Sunny Goyal60820d72017-05-09 12:40:11 -0700323 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700324
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800325 setupViews();
Tony Wickham010d2552017-01-20 08:15:28 -0800326 mPopupDataProvider = new PopupDataProvider(this);
327
Sunny Goyal623eddd2018-03-02 12:24:41 -0800328 mRotationHelper = new RotationHelper(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800329
Sunny Goyald3864fa2017-11-14 15:06:36 -0800330 boolean internalStateHandled = InternalStateHandler.handleCreate(this, getIntent());
331 if (internalStateHandled) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800332 if (savedInstanceState != null) {
333 // InternalStateHandler has already set the appropriate state.
334 // We dont need to do anything.
335 savedInstanceState.remove(RUNTIME_STATE);
336 }
337 }
338 restoreState(savedInstanceState);
Winson Chungb63b44c2017-11-10 17:54:44 -0800339
Sunny Goyal2100c782016-08-22 16:00:03 -0700340 // We only load the page synchronously if the user rotates (or triggers a
341 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700342 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
343 if (savedInstanceState != null) {
344 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
345 }
Sunny Goyald3864fa2017-11-14 15:06:36 -0800346
Sunny Goyalfe770c92016-09-27 14:38:58 -0700347 if (!mModel.startLoader(currentScreen)) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800348 if (!internalStateHandled) {
349 // If we are not binding synchronously, show a fade in animation when
350 // the first page bind completes.
351 mDragLayer.setAlpha(0);
352 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700353 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700354 // Pages bound synchronously.
355 mWorkspace.setCurrentPage(currentScreen);
356
Sunny Goyal2100c782016-08-22 16:00:03 -0700357 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800358 }
359
360 // For handling default keys
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800361 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800362
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800363 setContentView(mLauncherView);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800364 getRootView().dispatchInsets();
Mario Bertschlera6936942017-05-31 14:48:19 -0700365
Jon Miranda7fda2852017-07-19 16:07:01 -0700366 // Listen for broadcasts
367 IntentFilter filter = new IntentFilter();
368 filter.addAction(Intent.ACTION_SCREEN_OFF);
369 filter.addAction(Intent.ACTION_USER_PRESENT); // When the device wakes up + keyguard is gone
370 registerReceiver(mReceiver, filter);
371 mShouldFadeInScrim = true;
Sunny Goyala616d2b2017-06-12 13:54:01 -0700372
Sunny Goyal8392c822017-06-20 10:03:56 -0700373 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
374 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700375
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800376 mAppTransitionManager = LauncherAppTransitionManager.newInstance(this);
Jon Miranda7853bdb2018-01-22 17:32:30 -0800377
Sunny Goyal14b32402017-07-31 10:03:28 -0700378 if (mLauncherCallbacks != null) {
379 mLauncherCallbacks.onCreate(savedInstanceState);
380 }
Sunny Goyal623eddd2018-03-02 12:24:41 -0800381 mRotationHelper.initialize();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700382
383 TraceHelper.endSection("Launcher-onCreate");
Adam Cohen9211d422014-10-07 18:14:53 -0700384 }
385
Sunny Goyal7779d622015-06-11 16:18:39 -0700386 @Override
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800387 public void onConfigurationChanged(Configuration newConfig) {
388 int diff = newConfig.diff(mOldConfig);
389 if ((diff & (CONFIG_ORIENTATION | CONFIG_SCREEN_SIZE)) != 0) {
390 mUserEventDispatcher = null;
391 initDeviceProfile(mDeviceProfile.inv);
Sunny Goyal2bba1902018-02-27 12:20:50 -0800392 FileLog.d(TAG, "Config changed, my orientation=" +
393 getResources().getConfiguration().orientation +
394 ", new orientation=" + newConfig.orientation +
395 ", old orientation=" + mOldConfig.orientation +
396 ", isTransposed=" + mDeviceProfile.isVerticalBarLayout());
Sunny Goyalfde55052018-02-01 14:46:13 -0800397 dispatchDeviceProfileChanged();
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800398
399 getRootView().dispatchInsets();
400 getStateManager().reapplyState();
401
402 // TODO: We can probably avoid rebind when only screen size changed.
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800403 int currentPage = mWorkspace.getNextPage();
404 if (mModel.startLoader(currentPage)) {
405 mWorkspace.setCurrentPage(currentPage);
406 setWorkspaceLoading(true);
407 }
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800408 }
409
410 mOldConfig.setTo(newConfig);
411 super.onConfigurationChanged(newConfig);
412 }
413
414 private void initDeviceProfile(InvariantDeviceProfile idp) {
415 // Load configuration-specific DeviceProfile
416 mDeviceProfile = idp.getDeviceProfile(this);
417 if (isInMultiWindowModeCompat()) {
418 Display display = getWindowManager().getDefaultDisplay();
419 Point mwSize = new Point();
420 display.getSize(mwSize);
421 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
422 }
Sunny Goyal0e7724c2018-02-22 13:22:21 -0800423 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout());
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800424 }
425
Sunny Goyal623eddd2018-03-02 12:24:41 -0800426 public RotationHelper getRotationHelper() {
427 return mRotationHelper;
428 }
429
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800430 @Override
Mario Bertschler27288382017-05-24 15:35:09 -0700431 public void onThemeChanged() {
432 recreate();
433 }
434
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700435 public LauncherStateManager getStateManager() {
436 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700437 }
438
Tonydc76f8e2018-02-02 21:45:39 -0600439 public LauncherAppTransitionManager getAppTransitionManager() {
440 return mAppTransitionManager;
441 }
442
Mario Bertschlera6936942017-05-31 14:48:19 -0700443 protected void overrideTheme(boolean isDark, boolean supportsDarkText) {
Mario Bertschler27288382017-05-24 15:35:09 -0700444 if (isDark) {
445 setTheme(R.style.LauncherThemeDark);
Mario Bertschlera6936942017-05-31 14:48:19 -0700446 } else if (supportsDarkText) {
447 setTheme(R.style.LauncherThemeDarkText);
Mario Bertschler27288382017-05-24 15:35:09 -0700448 }
449 }
450
451 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700452 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800453 return mLauncherView.findViewById(id);
454 }
455
456 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700457 public void onAppWidgetHostReset() {
458 if (mAppWidgetHost != null) {
459 mAppWidgetHost.startListening();
460 }
461 }
462
Adam Cohen9211d422014-10-07 18:14:53 -0700463 private LauncherCallbacks mLauncherCallbacks;
464
Sunny Goyal32554d12015-12-03 15:31:25 -0800465 /**
466 * Call this after onCreate to set or clear overlay.
467 */
468 public void setLauncherOverlay(LauncherOverlay overlay) {
469 if (overlay != null) {
470 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
471 }
472 mWorkspace.setLauncherOverlay(overlay);
473 }
474
Adam Cohen9211d422014-10-07 18:14:53 -0700475 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
476 mLauncherCallbacks = callbacks;
477 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700478 }
479
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700480 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700481 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700482 if (mLauncherCallbacks != null) {
483 mLauncherCallbacks.onLauncherProviderChange();
484 }
485 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700486
Hyunyoung Song3f471442015-04-08 19:01:34 -0700487 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700488 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
489 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700490 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700491 }
492
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000493 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700494 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
495 // This cast is safe as long as the id < 0x00FFFFFF
496 // Since we jail all the dynamically generated views, there should be no clashes
497 // with any other views.
498 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000499 }
500
Tony Wickham010d2552017-01-20 08:15:28 -0800501 public PopupDataProvider getPopupDataProvider() {
502 return mPopupDataProvider;
503 }
504
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000505 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700506 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
507 * a configuration step, this allows the proper animations to run after other transitions.
508 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700509 private long completeAdd(
510 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
511 long screenId = info.screenId;
512 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700513 // When the screen id represents an actual screen (as opposed to a rank) we make sure
514 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700515 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700516 }
Adam Cohendb364c32014-05-20 14:23:40 -0700517
Sunny Goyal2100c782016-08-22 16:00:03 -0700518 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800519 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700520 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700521 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800522 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700523 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700524 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700525 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700526 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700527 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700528 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700529 int widgetId = appWidgetId;
530 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700531 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700532 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700533 // Since the view was just bound, also launch the configure activity if needed
534 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
535 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800536 if (provider != null) {
537 new WidgetAddFlowHandler(provider)
538 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700539 }
540 }
541 break;
542 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800543 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700544
Adam Cohendb364c32014-05-20 14:23:40 -0700545 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800546 }
547
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800548 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700549 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700550 if (isWorkspaceLoading()) {
551 // process the result once the workspace has loaded.
552 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
553 return;
554 }
555 mPendingActivityResult = null;
556
Winson Chunge341d302013-08-16 14:31:00 -0700557 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700558 final PendingRequestArgs requestArgs = mPendingRequestArgs;
559 setWaitingForResult(null);
560 if (requestArgs == null) {
561 return;
562 }
563
564 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700565
Adam Cohenad4e15c2013-10-17 16:21:35 -0700566 Runnable exitSpringLoaded = new Runnable() {
567 @Override
568 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700569 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700570 }
571 };
572
Michael Jurka8b805b12012-04-18 14:23:14 -0700573 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700574 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700575 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700576 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
577 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700578 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700579 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700580 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700581 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700582 addAppWidgetImpl(
583 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800584 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700585 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700586 }
587 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200588 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700589 if (resultCode == RESULT_OK && isInState(OVERVIEW)) {
Tony Wickhame3054412015-09-09 09:05:25 -0700590 // User could have free-scrolled between pages before picking a wallpaper; make sure
591 // we move to the closest one now.
592 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700593 mStateManager.goToState(NORMAL, false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200594 }
595 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700596 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200597
Adam Cohened66b2b2012-01-23 17:28:51 -0800598 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700599 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700600
Adam Cohened66b2b2012-01-23 17:28:51 -0800601 // We have special handling for widgets
602 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800603 final int appWidgetId;
604 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
605 : -1;
606 if (widgetId < 0) {
607 appWidgetId = pendingAddWidgetId;
608 } else {
609 appWidgetId = widgetId;
610 }
611
Adam Cohenad4e15c2013-10-17 16:21:35 -0700612 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800613 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700614 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
615 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700616 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700617 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700618 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700619 @Override
620 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700621 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700622 }
623 };
Adam Cohendb364c32014-05-20 14:23:40 -0700624
Sunny Goyal2100c782016-08-22 16:00:03 -0700625 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
626 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
627 } else {
628 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
629 // When the screen id represents an actual screen (as opposed to a rank)
630 // we make sure that the drop page actually exists.
631 requestArgs.screenId =
632 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700633 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700634 final CellLayout dropLayout =
635 mWorkspace.getScreenWithId(requestArgs.screenId);
636
637 dropLayout.setDropPending(true);
638 final Runnable onComplete = new Runnable() {
639 @Override
640 public void run() {
641 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
642 dropLayout.setDropPending(false);
643 }
644 };
645 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
646 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700647 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800648 return;
649 }
650
Sunny Goyald478c832016-04-01 12:04:16 -0700651 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
652 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700653 if (resultCode == RESULT_OK) {
654 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700655 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700656 }
657 // Leave the widget in the pending state if the user canceled the configure.
658 return;
659 }
660
Sunny Goyalaad90582015-07-09 14:22:50 -0700661 if (requestCode == REQUEST_CREATE_SHORTCUT) {
662 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700663 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
664 completeAdd(requestCode, data, -1, requestArgs);
665 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
666 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
667
Sunny Goyalaad90582015-07-09 14:22:50 -0700668 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700669 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
670 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800671 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800672 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800673 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800674 }
675
676 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700677 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800678 final int requestCode, final int resultCode, final Intent data) {
679 handleActivityResult(requestCode, resultCode, data);
680 if (mLauncherCallbacks != null) {
681 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
682 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800683 }
684
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700685 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700686 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600687 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700688 PendingRequestArgs pendingArgs = mPendingRequestArgs;
689 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
690 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
691 setWaitingForResult(null);
692
Sunny Goyal28c6b962015-10-12 11:42:05 -0700693 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700694 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700695 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700696 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700697 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700698 Intent intent = pendingArgs.getPendingIntent();
699
Sunny Goyal28c6b962015-10-12 11:42:05 -0700700 if (grantResults.length > 0
701 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700702 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700703 } else {
704 // TODO: Show a snack bar with link to settings
705 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700706 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700707 }
708 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600709 if (mLauncherCallbacks != null) {
710 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
711 grantResults);
712 }
713 }
714
Adam Cohendb364c32014-05-20 14:23:40 -0700715 /**
716 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
717 *
718 * @param screenId the screen id to check
719 * @return the new screen, or screenId if it exists
720 */
721 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800722 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700723 if (dropLayout == null) {
724 // it's possible that the add screen was removed because it was
725 // empty and a re-bind occurred
726 mWorkspace.addExtraEmptyScreen();
727 return mWorkspace.commitExtraEmptyScreen();
728 } else {
729 return screenId;
730 }
731 }
732
Sunny Goyal2100c782016-08-22 16:00:03 -0700733 @Thunk void completeTwoStageWidgetDrop(
734 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
735 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800736 Runnable onCompleteRunnable = null;
737 int animationType = 0;
738
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700739 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800740 if (resultCode == RESULT_OK) {
741 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
742 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800743 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700744 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800745 onCompleteRunnable = new Runnable() {
746 @Override
747 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700748 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700749 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800750 }
751 };
752 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800753 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800754 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800755 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700756 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700757 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700758 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
759 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700760 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700761 // The animated view may be null in the case of a rotation during widget configuration
762 onCompleteRunnable.run();
763 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800764 }
765
766 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700767 protected void onStop() {
768 super.onStop();
769 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700770
771 if (mLauncherCallbacks != null) {
772 mLauncherCallbacks.onStop();
773 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800774 mAppWidgetHost.setListenIfResumed(false);
Tony Wickham010d2552017-01-20 08:15:28 -0800775
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700776 if (!mAppLaunchSuccess) {
777 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -0800778 mStateManager.getState().containerType, -1);
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700779 }
Tony Wickham010d2552017-01-20 08:15:28 -0800780 NotificationListener.removeNotificationsChangedListener();
Sunny Goyalaf161fd2018-02-05 16:34:57 -0800781 getStateManager().moveToRestState();
Michael Jurkacd496d72013-04-11 11:32:45 -0700782 }
783
784 @Override
785 protected void onStart() {
786 super.onStart();
787 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700788
Sunny Goyal60876ac2018-02-20 14:21:20 -0800789 if (mOnStartCallback != null) {
790 mOnStartCallback.onLauncherStart(this);
791 mOnStartCallback = null;
792 }
Adam Cohen9211d422014-10-07 18:14:53 -0700793 if (mLauncherCallbacks != null) {
794 mLauncherCallbacks.onStart();
795 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800796 mAppWidgetHost.setListenIfResumed(true);
Sunny Goyalcac002d2018-01-04 09:51:45 -0800797 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
Jon Miranda2d89ea82017-05-04 11:47:53 -0700798
Jon Mirandade43a712018-01-18 11:35:10 -0800799 if (mShouldFadeInScrim && mLauncherView.getBackground() != null) {
Jon Miranda2d89ea82017-05-04 11:47:53 -0700800 if (mScrimAnimator != null) {
801 mScrimAnimator.cancel();
802 }
Jon Mirandade43a712018-01-18 11:35:10 -0800803 mLauncherView.getBackground().setAlpha(0);
804 mScrimAnimator = ObjectAnimator.ofInt(mLauncherView.getBackground(),
Jon Miranda2d89ea82017-05-04 11:47:53 -0700805 LauncherAnimUtils.DRAWABLE_ALPHA, 0, 255);
806 mScrimAnimator.addListener(new AnimatorListenerAdapter() {
807 @Override
808 public void onAnimationEnd(Animator animation) {
809 mScrimAnimator = null;
810 }
811 });
812 mScrimAnimator.setDuration(600);
813 mScrimAnimator.setStartDelay(getWindow().getTransitionBackgroundFadeDuration());
814 mScrimAnimator.start();
815 }
Jon Miranda7fda2852017-07-19 16:07:01 -0700816 mShouldFadeInScrim = false;
Winson Chung62a70be2018-02-23 15:10:05 -0800817 UiFactory.onStart(this);
Michael Jurkacd496d72013-04-11 11:32:45 -0700818 }
819
820 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800821 protected void onResume() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700822 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800823 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700824 TraceHelper.partitionSection("ON_RESUME", "superCall");
825
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700826 mAppLaunchSuccess = false;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700827 getUserEventDispatcher().resetElapsedSessionMillis();
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700828 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700829 // Process any items that were added while Launcher was away.
830 InstallShortcutReceiver.disableAndFlushInstallQueue(
831 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700832
Sunny Goyala474a9b2017-05-04 16:47:11 -0700833 // Refresh shortcuts if the permission changed.
834 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700835
Sunny Goyal7ede6112017-12-05 15:11:21 -0800836 DiscoveryBounce.showIfNeeded(this);
Adam Cohen9211d422014-10-07 18:14:53 -0700837 if (mLauncherCallbacks != null) {
838 mLauncherCallbacks.onResume();
839 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800840
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700841 TraceHelper.endSection("ON_RESUME");
Adam Cohen06dff352012-06-01 17:17:08 -0700842 }
843
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800844 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700845 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700846 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700847 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700848
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700849 super.onPause();
Joe Onorato24b6fd82009-11-12 13:47:09 -0800850 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700851 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700852
Adam Cohen9211d422014-10-07 18:14:53 -0700853 if (mLauncherCallbacks != null) {
854 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700855 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700856 }
857
Winson Chungd7823942018-02-16 03:23:47 +0000858 @Override
859 public void onWindowFocusChanged(boolean hasFocus) {
860 super.onWindowFocusChanged(hasFocus);
861 mStateManager.onWindowFocusChanged();
862 }
863
Adam Cohenc2d6e892014-10-16 09:49:24 -0700864 public interface LauncherOverlay {
865
866 /**
867 * Touch interaction leading to overscroll has begun
868 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700869 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700870
871 /**
872 * Touch interaction related to overscroll has ended
873 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700874 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700875
876 /**
877 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
878 * screen (or in the case of RTL, the rightmost screen).
879 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700880 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700881
882 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800883 * Called when the launcher is ready to use the overlay
884 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700885 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700886 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700887 }
888
889 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700890 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700891 }
892
893 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
894
Sunny Goyalc86df472016-02-25 09:19:38 -0800895 public void onScrollChanged(float progress) {
896 if (mWorkspace != null) {
897 mWorkspace.onOverlayScrollChanged(progress);
898 }
899 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700900 }
901
Sunny Goyal16764582017-11-06 16:00:34 -0800902 public boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -0700903 if (mLauncherCallbacks != null) {
904 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700905 } else {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800906 // On O and above we there is always some setting present settings (add icon to
907 // home screen or icon badging). On earlier APIs we will have the allow rotation
908 // setting, on devices with a locked orientation,
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700909 return Utilities.ATLEAST_OREO || !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -0700910 }
Jorim Jaggid017f882014-01-14 17:08:48 -0800911 }
912
Sunny Goyalf9403d92017-10-18 10:55:56 -0700913 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -0700914 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700915 }
916
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800917 /**
918 * Restores the previous state, if it exists.
919 *
920 * @param savedState The previous state.
921 */
922 private void restoreState(Bundle savedState) {
923 if (savedState == null) {
924 return;
925 }
926
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700927 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -0700928 LauncherState[] stateValues = LauncherState.values();
929 LauncherState state = stateValues[stateOrdinal];
Sunny Goyalbc683e92017-12-06 10:25:07 -0800930 if (!state.disableRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700931 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800932 }
933
Sunny Goyal2100c782016-08-22 16:00:03 -0700934 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
935 if (requestArgs != null) {
936 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800937 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700938
939 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700940
941 SparseArray<Parcelable> widgetsState =
942 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
943 if (widgetsState != null) {
944 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
945 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800946 }
947
948 /**
949 * Finds all the views we need and configure them properly.
950 */
951 private void setupViews() {
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800952 mDragLayer = findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700953 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -0700954 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700955 mWorkspace.initParentViews(mDragLayer);
Sunny Goyal16764582017-11-06 16:00:34 -0800956 mOverviewPanel = findViewById(R.id.overview_panel);
Craig Mautner360310b2012-10-26 15:13:08 -0700957
Sunny Goyal784f9c32016-03-23 16:56:54 -0700958 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
959 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
960 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800961
Winson Chung4c98d922011-05-31 16:50:48 -0700962 // Setup the drag layer
Sunny Goyal7ede6112017-12-05 15:11:21 -0800963 mDragLayer.setup(this, mDragController);
Winson Chung4c98d922011-05-31 16:50:48 -0700964
Winson Chung3d503fb2011-07-13 17:25:49 -0700965 // Setup the hotseat
966 mHotseat = (Hotseat) findViewById(R.id.hotseat);
967 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -0700968 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -0700969 }
970
Winson Chung4c98d922011-05-31 16:50:48 -0700971 // Setup the workspace
972 mWorkspace.setHapticFeedbackEnabled(false);
973 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700974 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -0700975 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
976 // default state, otherwise we will update to the wrong offsets in RTL
977 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700978 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700979 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700980
Tony Wickham34d2c912015-09-11 09:27:58 -0700981 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700982 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700983
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700984 // Setup Apps
985 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -0700986
Winson Chung3d503fb2011-07-13 17:25:49 -0700987 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -0700988 mDragController.setMoveTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700989 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -0400990
Sunny Goyal6639a5d2018-02-28 15:09:36 -0800991 mAllAppsController.setupViews(mAppsView, mHotseat);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800992 }
993
994 /**
995 * Creates a view representing a shortcut.
996 *
997 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800998 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800999 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001000 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001001 }
1002
1003 /**
1004 * Creates a view representing a shortcut inflated from the specified resource.
1005 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001006 * @param parent The group the shortcut belongs to.
1007 * @param info The data structure describing the shortcut.
1008 *
1009 * @return A View inflated from layoutResId.
1010 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001011 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -08001012 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
1013 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001014 favorite.applyFromShortcutInfo(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001015 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001016 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001017 return favorite;
1018 }
1019
1020 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -07001021 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001022 *
1023 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001024 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001025 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001026 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001027 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
1028 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001029 return;
1030 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001031
Jon Mirandac476d6e2017-05-04 16:30:01 -07001032 int[] cellXY = mTmpAddItemCellCoordinates;
1033 CellLayout layout = getCellLayout(container, screenId);
1034
Sunny Goyal782f0c92017-01-19 10:27:54 -08001035 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001036 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001037 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
1038 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001039 }
1040
1041 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001042 // Legacy shortcuts are only supported for primary profile.
1043 info = Process.myUserHandle().equals(args.user)
1044 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001045
Sunny Goyalb57645f2017-03-20 13:57:28 -07001046 if (info == null) {
1047 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1048 return;
1049 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001050 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001051 // The app is trying to add a shortcut without sufficient permissions
1052 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1053 return;
1054 }
1055 }
1056
Jon Mirandac476d6e2017-05-04 16:30:01 -07001057 if (container < 0) {
1058 // Adding a shortcut to the Workspace.
1059 final View view = createShortcut(info);
1060 boolean foundCellSpan = false;
1061 // First we check if we already know the exact location where we want to add this item.
1062 if (cellX >= 0 && cellY >= 0) {
1063 cellXY[0] = cellX;
1064 cellXY[1] = cellY;
1065 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001066
Jon Mirandac476d6e2017-05-04 16:30:01 -07001067 // If appropriate, either create a folder or add to an existing folder
1068 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001069 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001070 return;
1071 }
1072 DragObject dragObject = new DragObject();
1073 dragObject.dragInfo = info;
1074 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1075 true)) {
1076 return;
1077 }
1078 } else {
1079 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1080 }
1081
1082 if (!foundCellSpan) {
1083 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001084 return;
1085 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001086
1087 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1088 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001089 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001090 // Adding a shortcut to a Folder.
Sunny Goyale29897f52017-07-20 10:09:42 -07001091 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001092 if (folderIcon != null) {
1093 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1094 folderInfo.add(info, args.rank, false);
1095 } else {
Sunny Goyale29897f52017-07-20 10:09:42 -07001096 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001097 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001098 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001099 }
1100
Sunny Goyale29897f52017-07-20 10:09:42 -07001101 public FolderIcon findFolderIcon(final long folderIconId) {
1102 return (FolderIcon) mWorkspace.getFirstMatch(new ItemOperator() {
1103 @Override
1104 public boolean evaluate(ItemInfo info, View view) {
1105 return info != null && info.id == folderIconId;
1106 }
1107 });
1108 }
1109
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001110 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001111 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001112 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001113 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001114 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001115 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001116 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1117
Adam Cohened66b2b2012-01-23 17:28:51 -08001118 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001119 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001120 }
Romain Guycbb89e42009-06-08 15:52:54 -07001121
Adam Cohen59400422014-03-05 18:07:04 -08001122 LauncherAppWidgetInfo launcherInfo;
1123 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001124 launcherInfo.spanX = itemInfo.spanX;
1125 launcherInfo.spanY = itemInfo.spanY;
1126 launcherInfo.minSpanX = itemInfo.minSpanX;
1127 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001128 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001129
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001130 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001131 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001132
Sunny Goyal2100c782016-08-22 16:00:03 -07001133 if (hostView == null) {
1134 // Perform actual inflation because we're live
1135 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001136 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001137 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301138 prepareAppWidget(hostView, launcherInfo);
1139 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001140 }
Romain Guycbb89e42009-06-08 15:52:54 -07001141
Sunny Goyald5462aa2016-11-22 16:52:39 +05301142 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001143 hostView.setTag(item);
1144 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001145 hostView.setFocusable(true);
1146 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001147 }
1148
Adam Cohended9f8d2010-11-03 13:25:16 -07001149 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1150 @Override
1151 public void onReceive(Context context, Intent intent) {
1152 final String action = intent.getAction();
1153 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001154 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001155 // processing a multi-step drop
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001156 if (mAppsView != null && mPendingRequestArgs == null) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001157 mStateManager.goToState(NORMAL);
Winson Chung785d2eb2011-04-14 16:08:02 -07001158 }
Jon Miranda7fda2852017-07-19 16:07:01 -07001159 mShouldFadeInScrim = true;
1160 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1161 // ACTION_USER_PRESENT is sent after onStart/onResume. This covers the case where
1162 // the user unlocked and the Launcher is not in the foreground.
1163 mShouldFadeInScrim = false;
Adam Cohended9f8d2010-11-03 13:25:16 -07001164 }
1165 }
1166 };
1167
Tony Wickham010d2552017-01-20 08:15:28 -08001168 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
Sunny Goyal29947f02017-12-18 13:49:44 -08001169 mWorkspace.updateIconBadges(updatedBadges);
Sunny Goyal60180b02018-02-07 12:56:30 -08001170 mAppsView.getAppsStore().updateIconBadges(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001171
Sunny Goyal29947f02017-12-18 13:49:44 -08001172 PopupContainerWithArrow popup = PopupContainerWithArrow.getOpen(Launcher.this);
1173 if (popup != null) {
1174 popup.updateNotificationHeader(updatedBadges);
Tony Wickham010d2552017-01-20 08:15:28 -08001175 }
1176 }
1177
Adam Cohended9f8d2010-11-03 13:25:16 -07001178 @Override
1179 public void onAttachedToWindow() {
1180 super.onAttachedToWindow();
1181
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001182 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyalc86df472016-02-25 09:19:38 -08001183 if (mLauncherCallbacks != null) {
1184 mLauncherCallbacks.onAttachedToWindow();
1185 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001186 }
1187
1188 @Override
1189 public void onDetachedFromWindow() {
1190 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001191
1192 if (mLauncherCallbacks != null) {
1193 mLauncherCallbacks.onDetachedFromWindow();
1194 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001195 }
1196
Sunny Goyal3bac2c22018-02-07 14:44:05 -08001197 public void onQuickstepGestureStarted(boolean isVisible) {
1198 if (mLauncherCallbacks != null) {
1199 mLauncherCallbacks.onQuickstepGestureStarted(isVisible);
1200 }
1201 }
1202
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001203 public AllAppsTransitionController getAllAppsController() {
1204 return mAllAppsController;
1205 }
1206
Winson Chung1a341002018-01-17 10:00:23 -08001207 public LauncherRootView getRootView() {
1208 return (LauncherRootView) mLauncherView;
1209 }
1210
Winson Chunga6945242014-01-08 14:04:34 -08001211 public DragLayer getDragLayer() {
1212 return mDragLayer;
1213 }
1214
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001215 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001216 return mAppsView;
1217 }
1218
Winson Chunga6945242014-01-08 14:04:34 -08001219 public Workspace getWorkspace() {
1220 return mWorkspace;
1221 }
1222
1223 public Hotseat getHotseat() {
1224 return mHotseat;
1225 }
1226
Winson Chung8ae41982017-11-10 11:42:13 -08001227 public <T extends ViewGroup> T getOverviewPanel() {
1228 return (T) mOverviewPanel;
Winson Chunga6945242014-01-08 14:04:34 -08001229 }
1230
Sunny Goyal47328fd2016-05-25 18:56:41 -07001231 public DropTargetBar getDropTargetBar() {
1232 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001233 }
1234
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001235 public LauncherAppWidgetHost getAppWidgetHost() {
1236 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001237 }
Romain Guycbb89e42009-06-08 15:52:54 -07001238
Winson Chunga9abd0e2010-10-27 17:18:37 -07001239 public LauncherModel getModel() {
1240 return mModel;
1241 }
1242
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001243 public ModelWriter getModelWriter() {
1244 return mModelWriter;
1245 }
1246
Adam Cohen79d90c52016-04-22 13:29:20 -07001247 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001248 return mSharedPrefs;
1249 }
1250
Sunny Goyalf8d56fc2018-01-31 15:18:11 -08001251 public int getOrientation() { return mOldConfig.orientation; }
Jon Miranda6bed3502017-09-19 14:43:17 -07001252
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001253 @Override
1254 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001255 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001256 super.onNewIntent(intent);
1257
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001258 boolean alreadyOnHome = hasWindowFocus() && ((intent.getFlags() &
Winson15f8b172015-08-19 11:02:39 -07001259 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1260 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001261
1262 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001263 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001264 && AbstractFloatingView.getTopOpenView(this) == null;
Winson15f8b172015-08-19 11:02:39 -07001265 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001266 boolean internalStateHandled = InternalStateHandler
Sunny Goyal8b264562018-02-03 23:00:13 -08001267 .handleNewIntent(this, intent, isStarted());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001268
Winson15f8b172015-08-19 11:02:39 -07001269 if (isActionMain) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001270 if (!internalStateHandled) {
1271 // Note: There should be at most one log per method call. This is enforced
1272 // implicitly by using if-else statements.
1273 UserEventDispatcher ued = getUserEventDispatcher();
1274 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
1275 if (topOpenView != null) {
1276 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
1277 } else if (alreadyOnHome) {
1278 Target target = newContainerTarget(mStateManager.getState().containerType);
1279 target.pageIndex = mWorkspace.getCurrentPage();
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001280 ued.logActionCommand(Action.Command.HOME_INTENT, target,
1281 newContainerTarget(ContainerType.WORKSPACE));
Sunny Goyald3864fa2017-11-14 15:06:36 -08001282 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001283
Sunny Goyald3864fa2017-11-14 15:06:36 -08001284 // In all these cases, only animate if we're already on home
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001285 AbstractFloatingView.closeAllOpenViews(this, isStarted());
Jon Mirandafeba90f2016-10-06 10:53:29 -07001286
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001287 mStateManager.goToState(NORMAL);
Sunny Goyald3864fa2017-11-14 15:06:36 -08001288
1289 // Reset the apps view
1290 if (!alreadyOnHome && mAppsView != null) {
1291 mAppsView.reset();
1292 }
1293
1294 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()) {
1295 mWorkspace.post(mWorkspace::moveToDefaultScreen);
1296 }
1297 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001298
1299 final View v = getWindow().peekDecorView();
1300 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001301 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001302 }
1303
Adam Cohen9211d422014-10-07 18:14:53 -07001304 if (mLauncherCallbacks != null) {
1305 mLauncherCallbacks.onHomeIntent();
1306 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001307 }
Winson15f8b172015-08-19 11:02:39 -07001308
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001309 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001310 }
1311
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001312 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001313 public void onRestoreInstanceState(Bundle state) {
1314 super.onRestoreInstanceState(state);
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001315 mWorkspace.restoreInstanceStateForChild(mSynchronouslyBoundPage);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001316 }
1317
1318 @Override
1319 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001320 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001321 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001322
Adam Cohen21cd0022013-10-09 18:57:02 -07001323 }
Sunny Goyalea609262017-10-25 15:47:38 -07001324 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001325
1326
1327 AbstractFloatingView widgets = AbstractFloatingView
1328 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1329 if (widgets != null) {
1330 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1331 widgets.saveHierarchyState(widgetsState);
1332 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1333 } else {
1334 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1335 }
1336
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001337 // We close any open folders and shortcut containers since they will not be re-opened,
1338 // and we need to make sure this state is reflected.
1339 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001340
Sunny Goyal2100c782016-08-22 16:00:03 -07001341 if (mPendingRequestArgs != null) {
1342 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1343 }
1344 if (mPendingActivityResult != null) {
1345 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001346 }
1347
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001348 super.onSaveInstanceState(outState);
1349
Adam Cohen9211d422014-10-07 18:14:53 -07001350 if (mLauncherCallbacks != null) {
1351 mLauncherCallbacks.onSaveInstanceState(outState);
1352 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001353 }
1354
1355 @Override
1356 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001357 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001358
Sunny Goyal5cc7bbd2017-06-12 09:50:39 -07001359 unregisterReceiver(mReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001360 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001361
Winson Chungcd2b0142011-06-08 16:02:26 -07001362 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001363 // It's possible to receive onDestroy after a new Launcher activity has
1364 // been created. In this case, don't interfere with the new Launcher.
1365 if (mModel.isCurrentCallbacks(this)) {
1366 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001367 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001368 }
Sunny Goyal623eddd2018-03-02 12:24:41 -08001369 mRotationHelper.destroy();
Sunny Goyal745bad92016-05-02 10:54:12 -07001370
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001371 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001372 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001373 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001374 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001375 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001376
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001377 TextKeyListener.getInstance().release();
Mario Bertschler27288382017-05-24 15:35:09 -07001378 WallpaperColorInfo.getInstance(this).setOnThemeChangeListener(null);
1379
Michael Jurka2ecf9952012-06-18 12:52:28 -07001380 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001381
Tony2917a8b2017-07-31 23:29:42 -07001382 clearPendingBinds();
1383
Adam Cohen9211d422014-10-07 18:14:53 -07001384 if (mLauncherCallbacks != null) {
1385 mLauncherCallbacks.onDestroy();
1386 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001387 }
1388
Sunny Goyalae502842016-06-17 08:43:56 -07001389 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1390 return mAccessibilityDelegate;
1391 }
1392
Adam Cohena9cf38f2011-05-02 15:36:58 -07001393 public DragController getDragController() {
1394 return mDragController;
1395 }
1396
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001397 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001398 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001399 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001400 }
1401
1402 @Override
1403 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1404 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001405 try {
1406 super.startIntentSenderForResult(intent, requestCode,
1407 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1408 } catch (IntentSender.SendIntentException e) {
1409 throw new ActivityNotFoundException();
1410 }
1411 }
1412
Winson Chung70d72102011-08-12 11:24:35 -07001413 /**
1414 * Indicates that we want global search for this activity by setting the globalSearch
1415 * argument for {@link #startSearch} to true.
1416 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001417 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001418 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001419 Bundle appSearchData, boolean globalSearch) {
1420 if (appSearchData == null) {
1421 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001422 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001423 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001424
Sunny Goyal6f28e712016-09-13 14:19:29 -07001425 if (mLauncherCallbacks == null ||
1426 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1427 // Starting search from the callbacks failed. Start the default global search.
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001428 super.startSearch(initialQuery, selectInitialQuery, appSearchData, true);
Ian Parkinson047036e2014-09-01 15:40:53 +01001429 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001430
1431 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001432 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001433 }
1434
Joe Onorato9c1289c2009-08-17 11:03:03 -04001435 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001436 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001437 }
1438
Adam Cohen517a7f52014-03-01 12:12:59 -08001439 public boolean isWorkspaceLoading() {
1440 return mWorkspaceLoading;
1441 }
1442
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001443 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001444 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001445 }
1446
Sunny Goyal04a324a2017-01-20 21:08:59 -08001447 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001448 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001449 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001450
Sunny Goyal2100c782016-08-22 16:00:03 -07001451 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001452 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001453 if (LOGD) {
1454 Log.d(TAG, "Adding widget from drop");
1455 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001456 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001457 }
1458
Sunny Goyal2100c782016-08-22 16:00:03 -07001459 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001460 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1461 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1462 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001463
Adam Cohenad4e15c2013-10-17 16:21:35 -07001464 Runnable onComplete = new Runnable() {
1465 @Override
1466 public void run() {
1467 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001468 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001469 }
1470 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001471 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001472 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001473 }
1474 }
Romain Guycbb89e42009-06-08 15:52:54 -07001475
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001476 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1477 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001478 info.container = container;
1479 info.screenId = screenId;
1480 if (cell != null) {
1481 info.cellX = cell[0];
1482 info.cellY = cell[1];
1483 }
1484 info.spanX = spanX;
1485 info.spanY = spanY;
1486
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001487 switch (info.itemType) {
1488 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1489 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001490 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001491 break;
1492 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001493 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001494 break;
1495 default:
1496 throw new IllegalStateException("Unknown item type: " + info.itemType);
1497 }
1498 }
1499
Adam Cohenfbba09b2011-07-18 21:43:05 -07001500 /**
1501 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001502 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001503 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001504 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1505 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001506 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1507 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1508 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001509 }
1510
Adam Cohenfbba09b2011-07-18 21:43:05 -07001511 /**
1512 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001513 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001514 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001515 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001516 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001517 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001518 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001519 // In the case where we've prebound the widget, we remove it from the DragLayer
1520 if (LOGD) {
1521 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1522 }
1523 getDragLayer().removeView(hostView);
1524
Adam Cohened66b2b2012-01-23 17:28:51 -08001525 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001526 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001527
1528 // Clear the boundWidget so that it doesn't get destroyed.
1529 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001530 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001531 // In this case, we either need to start an activity to get permission to bind
1532 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001533 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1534 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1535 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1536 this, info.componentName);
1537 } else {
1538 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1539 }
Adam Cohendd70d662012-10-04 16:53:44 -07001540 Bundle options = info.bindOptions;
1541
Sunny Goyalffe83f12014-08-14 17:39:34 -07001542 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1543 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001544 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001545 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001546 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001547 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001548 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001549 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001550 }
1551
Adam Cohendcd297f2013-06-18 13:13:40 -07001552 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001553 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001554 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001555 folderInfo.title = getText(R.string.folder_name);
1556
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001557 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001558 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001559
1560 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001561 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301562 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001563 // Force measure the new folder icon
1564 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1565 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001566 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001567 }
Romain Guycbb89e42009-06-08 15:52:54 -07001568
Winsonc0b52fe2015-09-09 16:38:15 -07001569 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001570 * Unbinds the view for the specified item, and removes the item and all its children.
1571 *
1572 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001573 * @param itemInfo the {@link ItemInfo} for this view.
1574 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001575 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001576 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001577 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001578 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001579 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1580 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001581 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001582 } else {
1583 mWorkspace.removeWorkspaceItem(v);
1584 }
Winsonc0b52fe2015-09-09 16:38:15 -07001585 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001586 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001587 }
1588 } else if (itemInfo instanceof FolderInfo) {
1589 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001590 if (v instanceof FolderIcon) {
1591 ((FolderIcon) v).removeListeners();
1592 }
Winsonc0b52fe2015-09-09 16:38:15 -07001593 mWorkspace.removeWorkspaceItem(v);
1594 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001595 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001596 }
1597 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1598 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001599 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001600 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07001601 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001602 }
1603 } else {
1604 return false;
1605 }
1606 return true;
1607 }
1608
1609 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07001610 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07001611 */
Sunny Goyal56c73602015-09-25 12:55:01 -07001612 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07001613 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07001614 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07001615 // Deleting an app widget ID is a void call but writes to disk before returning
1616 // to the caller...
1617 new AsyncTask<Void, Void, Void>() {
1618 public Void doInBackground(Void ... args) {
1619 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
1620 return null;
1621 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07001622 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07001623 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001624 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001625 }
1626
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001627 @Override
1628 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001629 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001630 }
1631
Joe Onorato88ec0992009-11-19 13:16:06 -08001632 @Override
1633 public void onBackPressed() {
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08001634 if (finishAutoCancelActionMode()) {
1635 return;
1636 }
Adam Cohen9211d422014-10-07 18:14:53 -07001637 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1638 return;
1639 }
1640
Sunny Goyal45478022015-06-08 16:52:41 -07001641 if (mDragController.isDragging()) {
1642 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001643 return;
1644 }
1645
Jon Mirandafeba90f2016-10-06 10:53:29 -07001646 // Note: There should be at most one log per method call. This is enforced implicitly
1647 // by using if-else statements.
1648 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001649 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
1650 if (topView != null) {
Sunny Goyal37920962017-09-28 13:43:24 -07001651 topView.onBackPressed();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001652 } else if (!isInState(NORMAL)) {
Sunny Goyal49bcf342018-01-11 13:59:53 -08001653 LauncherState lastState = mStateManager.getLastState();
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001654 ued.logActionCommand(Action.Command.BACK, mStateManager.getState().containerType,
1655 lastState.containerType);
Sunny Goyal49bcf342018-01-11 13:59:53 -08001656 mStateManager.goToState(lastState);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001657 } else {
1658 // Back button is a no-op here, but give at least some feedback for the button press
1659 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001660 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001661 }
1662
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001663 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001664 * Launches the intent referred by the clicked shortcut.
1665 *
1666 * @param v The view representing the clicked shortcut.
1667 */
1668 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001669 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1670 // view has detached (it's possible for this to happen if the view is removed mid touch).
1671 if (v.getWindowToken() == null) {
1672 return;
1673 }
1674
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001675 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001676 return;
1677 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001678
Adam Cohen1697b792013-09-17 19:08:21 -07001679 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001680 if (isInState(OVERVIEW)) {
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001681 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Touch.TAP,
Jon Miranda2e61fba2017-02-24 09:12:59 -08001682 LauncherLogProto.Action.Direction.NONE,
1683 LauncherLogProto.ContainerType.OVERVIEW, mWorkspace.getCurrentPage());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001684 mStateManager.goToState(NORMAL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001685 }
1686 return;
1687 }
1688
1689 if (v instanceof CellLayout) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001690 if (isInState(OVERVIEW)) {
Jon Mirandac6cf4932017-02-07 17:12:36 -08001691 int page = mWorkspace.indexOfChild(v);
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001692 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Touch.TAP,
Jon Mirandac6cf4932017-02-07 17:12:36 -08001693 LauncherLogProto.Action.Direction.NONE,
1694 LauncherLogProto.ContainerType.OVERVIEW, page);
1695 mWorkspace.snapToPageFromOverView(page);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001696 mStateManager.goToState(NORMAL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001697 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07001698 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001699 }
1700
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001701 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001702 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001703 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001704 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001705 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001706 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001707 }
Sunny Goyal508da152014-08-14 10:53:27 -07001708 } else if (tag instanceof AppInfo) {
1709 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07001710 } else if (tag instanceof LauncherAppWidgetInfo) {
1711 if (v instanceof PendingAppWidgetHostView) {
1712 onClickPendingWidget((PendingAppWidgetHostView) v);
1713 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001714 }
1715 }
1716
Sunny Goyal70660032015-05-14 00:07:08 -07001717 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07001718 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07001719 return false;
1720 }
1721
Michael Jurkaaf442092010-06-10 17:01:57 -07001722 /**
Sunny Goyalff572272014-07-23 13:58:07 -07001723 * Event handler for the app widget view which has not fully restored.
1724 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001725 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07001726 if (mIsSafeModeEnabled) {
1727 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
1728 return;
1729 }
1730
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001731 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07001732 if (v.isReadyForClickSetup()) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001733 LauncherAppWidgetProviderInfo appWidgetInfo =
1734 mAppWidgetManager.findProvider(info.providerName, info.user);
1735 if (appWidgetInfo == null) {
1736 return;
1737 }
1738 WidgetAddFlowHandler addFlowHandler = new WidgetAddFlowHandler(appWidgetInfo);
1739
Sunny Goyald478c832016-04-01 12:04:16 -07001740 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
1741 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
1742 // This should not happen, as we make sure that an Id is allocated during bind.
1743 return;
1744 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001745 addFlowHandler.startBindFlow(this, info.appWidgetId, info,
1746 REQUEST_BIND_PENDING_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -07001747 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001748 addFlowHandler.startConfigActivity(this, info, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07001749 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001750 } else {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001751 final String packageName = info.providerName.getPackageName();
Sunny Goyale6e72002017-01-12 16:55:36 -08001752 onClickPendingAppItem(v, packageName, info.installProgress >= 0);
Sunny Goyalff572272014-07-23 13:58:07 -07001753 }
1754 }
1755
Sunny Goyale6e72002017-01-12 16:55:36 -08001756 private void onClickPendingAppItem(final View v, final String packageName,
1757 boolean downloadStarted) {
1758 if (downloadStarted) {
1759 // If the download has started, simply direct to the market app.
1760 startMarketIntentForPackage(v, packageName);
1761 return;
1762 }
Sunny Goyale03b8122014-10-08 09:55:24 -07001763 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001764 .setTitle(R.string.abandoned_promises_title)
1765 .setMessage(R.string.abandoned_promise_explanation)
Sunny Goyale6e72002017-01-12 16:55:36 -08001766 .setPositiveButton(R.string.abandoned_search, new DialogInterface.OnClickListener() {
1767 @Override
1768 public void onClick(DialogInterface dialogInterface, int i) {
1769 startMarketIntentForPackage(v, packageName);
1770 }
1771 })
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001772 .setNeutralButton(R.string.abandoned_clean_this,
1773 new DialogInterface.OnClickListener() {
1774 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001775 final UserHandle user = Process.myUserHandle();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001776 mWorkspace.removeAbandonedPromise(packageName, user);
1777 }
1778 })
1779 .create().show();
Sunny Goyale6e72002017-01-12 16:55:36 -08001780 }
1781
1782 private void startMarketIntentForPackage(View v, String packageName) {
1783 ItemInfo item = (ItemInfo) v.getTag();
Sunny Goyal67e75e22018-01-22 15:14:00 -08001784 Intent intent = new PackageManagerHelper(v.getContext()).getMarketIntent(packageName);
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001785 startActivitySafely(v, intent, item);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001786 }
1787
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001788 /**
1789 * Event handler for an app shortcut click.
1790 *
1791 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
1792 */
Chris Wren40c5ed32014-06-24 18:24:23 -04001793 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001794 if (LOGD) Log.d(TAG, "onClickAppShortcut");
1795 Object tag = v.getTag();
1796 if (!(tag instanceof ShortcutInfo)) {
1797 throw new IllegalArgumentException("Input must be a Shortcut");
1798 }
1799
1800 // Open shortcut
1801 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001802
Sunny Goyal076839c2017-10-30 13:52:20 -07001803 if (shortcut.isDisabled()) {
Tony Makf6308652017-12-02 16:56:22 +00001804 final int disabledFlags = shortcut.runtimeStatusFlags & ShortcutInfo.FLAG_DISABLED_MASK;
1805 if ((disabledFlags &
Sunny Goyal076839c2017-10-30 13:52:20 -07001806 ~FLAG_DISABLED_SUSPENDED &
1807 ~FLAG_DISABLED_QUIET_USER) == 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001808 // If the app is only disabled because of the above flags, launch activity anyway.
1809 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00001810 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07001811 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
1812 // Use a message specific to this shortcut, if it has one.
1813 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
1814 return;
1815 }
1816 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00001817 int error = R.string.activity_not_available;
Sunny Goyal076839c2017-10-30 13:52:20 -07001818 if ((shortcut.runtimeStatusFlags & FLAG_DISABLED_SAFEMODE) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00001819 error = R.string.safemode_shortcut_error;
Sunny Goyal076839c2017-10-30 13:52:20 -07001820 } else if ((shortcut.runtimeStatusFlags & FLAG_DISABLED_BY_PUBLISHER) != 0 ||
1821 (shortcut.runtimeStatusFlags & FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07001822 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00001823 }
1824 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
1825 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001826 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001827 }
1828
Chris Wren40c5ed32014-06-24 18:24:23 -04001829 // Check for abandoned promise
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -07001830 if ((v instanceof BubbleTextView) && shortcut.hasPromiseIconUi()) {
Sunny Goyale6e72002017-01-12 16:55:36 -08001831 String packageName = shortcut.intent.getComponent() != null ?
1832 shortcut.intent.getComponent().getPackageName() : shortcut.intent.getPackage();
1833 if (!TextUtils.isEmpty(packageName)) {
1834 onClickPendingAppItem(v, packageName,
1835 shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE));
1836 return;
1837 }
Chris Wren40c5ed32014-06-24 18:24:23 -04001838 }
1839
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001840 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07001841 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04001842 }
1843
Sunny Goyala7ce1662016-05-31 15:01:35 -07001844 private void startAppShortcutOrInfoActivity(View v) {
1845 ItemInfo item = (ItemInfo) v.getTag();
Mario Bertschler08ffaae2017-03-20 11:30:27 -07001846 Intent intent;
1847 if (item instanceof PromiseAppInfo) {
1848 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) item;
Sunny Goyal67e75e22018-01-22 15:14:00 -08001849 intent = promiseAppInfo.getMarketIntent(this);
Mario Bertschler08ffaae2017-03-20 11:30:27 -07001850 } else {
1851 intent = item.getIntent();
1852 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07001853 if (intent == null) {
1854 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07001855 }
Mario Bertschler8d01cb42018-02-13 14:47:02 -08001856 if (item instanceof ShortcutInfo) {
1857 ShortcutInfo si = (ShortcutInfo) item;
1858 if (si.hasStatusFlag(ShortcutInfo.FLAG_SUPPORTS_WEB_UI)
1859 && intent.getAction() == Intent.ACTION_VIEW) {
1860 // make a copy of the intent that has the package set to null
1861 // we do this because the platform sometimes disables instant
1862 // apps temporarily (triggered by the user) and fallbacks to the
1863 // web ui. This only works though if the package isn't set
1864 intent = new Intent(intent);
1865 intent.setPackage(null);
1866 }
1867 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001868 startActivitySafely(v, intent, item);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001869 }
1870
1871 /**
1872 * Event handler for a folder icon click.
1873 *
1874 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
1875 */
1876 protected void onClickFolderIcon(View v) {
1877 if (LOGD) Log.d(TAG, "onClickFolder");
1878 if (!(v instanceof FolderIcon)){
1879 throw new IllegalArgumentException("Input must be a FolderIcon");
1880 }
1881
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001882 Folder folder = ((FolderIcon) v).getFolder();
1883 if (!folder.isOpen() && !folder.isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001884 // Open the requested folder
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001885 folder.animateOpen();
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001886 }
Michael Jurka5130e402011-10-13 04:55:35 -07001887 }
1888
Sandeep Siddharthad8058372014-01-28 10:41:15 -08001889 /**
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001890 * Event handler for the wallpaper picker button that appears after a long press
1891 * on the home screen.
1892 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001893 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07001894 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07001895 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
1896 return;
1897 }
1898
Tony Wickham785f7a52015-08-31 17:28:32 -07001899 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
1900 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal2100c782016-08-22 16:00:03 -07001901 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001902 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001903 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001904
1905 String pickerPackage = getString(R.string.wallpaper_picker_package);
Tony85efb4b2017-06-02 13:06:10 -07001906 boolean hasTargetPackage = !TextUtils.isEmpty(pickerPackage);
1907 if (hasTargetPackage) {
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001908 intent.setPackage(pickerPackage);
1909 }
1910
Sunny Goyala5ace712017-11-15 17:12:51 -08001911 final Bundle launchOptions;
1912 if (v != null) {
1913 intent.setSourceBounds(getViewBounds(v));
1914 // If there is no target package, use the default intent chooser animation
Jon Miranda17940f02018-01-18 14:46:34 -08001915 launchOptions = hasTargetPackage
Tony Wickham005df0b2018-02-13 11:28:12 -08001916 ? getActivityLaunchOptionsAsBundle(v, isInMultiWindowModeCompat())
Jon Miranda17940f02018-01-18 14:46:34 -08001917 : null;
Sunny Goyala5ace712017-11-15 17:12:51 -08001918 } else {
1919 launchOptions = null;
1920 }
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001921 try {
Sunny Goyala5ace712017-11-15 17:12:51 -08001922 startActivityForResult(intent, REQUEST_PICK_WALLPAPER, launchOptions);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001923 } catch (ActivityNotFoundException e) {
1924 setWaitingForResult(null);
1925 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1926 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001927 }
1928
Sunny Goyala7ce1662016-05-31 15:01:35 -07001929 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001930 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001931 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
1932 try {
1933 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
1934 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
1935 // is enabled by default on NYC.
1936 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
1937 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001938
1939 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
1940 String id = ((ShortcutInfo) info).getDeepShortcutId();
1941 String packageName = intent.getPackage();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301942 DeepShortcutManager.getInstance(this).startShortcut(
Sunny Goyal9994b2b2016-06-23 14:17:24 -07001943 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001944 } else {
1945 // Could be launching some bookkeeping activity
1946 startActivity(intent, optsBundle);
1947 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07001948 } finally {
1949 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01001950 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001951 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001952 // Due to legacy reasons, direct call shortcuts require Launchers to have the
1953 // corresponding permission. Show the appropriate permission prompt if that
1954 // is the case.
1955 if (intent.getComponent() == null
1956 && Intent.ACTION_CALL.equals(intent.getAction())
1957 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
1958 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001959
1960 setWaitingForResult(PendingRequestArgs
1961 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07001962 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
1963 REQUEST_PERMISSION_CALL_PHONE);
1964 } else {
1965 // No idea why this was thrown.
1966 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07001967 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001968 }
1969 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001970
Tony Wickham005df0b2018-02-13 11:28:12 -08001971 public Bundle getActivityLaunchOptionsAsBundle(View v, boolean useDefaultLaunchOptions) {
1972 ActivityOptions activityOptions = getActivityLaunchOptions(v, useDefaultLaunchOptions);
1973 return activityOptions == null ? null : activityOptions.toBundle();
1974 }
1975
Sunny Goyalfe770c92016-09-27 14:38:58 -07001976 @TargetApi(Build.VERSION_CODES.M)
Tony Wickham005df0b2018-02-13 11:28:12 -08001977 public ActivityOptions getActivityLaunchOptions(View v, boolean useDefaultLaunchOptions) {
Jon Miranda17940f02018-01-18 14:46:34 -08001978 return useDefaultLaunchOptions
Jon Miranda54441f52018-01-24 15:38:25 -08001979 ? mAppTransitionManager.getDefaultActivityLaunchOptions(this, v)
1980 : mAppTransitionManager.getActivityLaunchOptions(this, v);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001981 }
1982
Tonye3c59252017-05-02 21:32:27 -07001983 public Rect getViewBounds(View v) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001984 int[] pos = new int[2];
1985 v.getLocationOnScreen(pos);
1986 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
1987 }
1988
Sunny Goyala7ce1662016-05-31 15:01:35 -07001989 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07001990 mAppLaunchSuccess = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001991 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
1992 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07001993 return mAppLaunchSuccess;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001994 }
Jon Miranda17940f02018-01-18 14:46:34 -08001995
Sunny Goyala7ce1662016-05-31 15:01:35 -07001996 // Only launch using the new animation if the shortcut has not opted out (this is a
1997 // private contract between launcher and may be ignored in the future).
1998 boolean useLaunchAnimation = (v != null) &&
1999 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Jon Miranda17940f02018-01-18 14:46:34 -08002000 Bundle optsBundle = useLaunchAnimation
Tony Wickham005df0b2018-02-13 11:28:12 -08002001 ? getActivityLaunchOptionsAsBundle(v, isInMultiWindowModeCompat())
Jon Miranda17940f02018-01-18 14:46:34 -08002002 : null;
Sunny Goyala7ce1662016-05-31 15:01:35 -07002003
Sunny Goyal24bb66a2017-03-21 15:12:01 -07002004 UserHandle user = item == null ? null : item.user;
Sunny Goyala7ce1662016-05-31 15:01:35 -07002005
2006 // Prepare intent
2007 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2008 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002009 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002010 }
2011 try {
Jon Miranda84e71bf2018-02-13 15:36:20 -08002012 boolean isShortcut = Utilities.ATLEAST_MARSHMALLOW
2013 && (item instanceof ShortcutInfo)
2014 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
2015 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
2016 && !((ShortcutInfo) item).isPromise();
Jon Miranda17940f02018-01-18 14:46:34 -08002017 if (isShortcut) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002018 // Shortcuts need some special checks due to legacy reasons.
2019 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002020 } else if (user == null || user.equals(Process.myUserHandle())) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002021 // Could be launching some bookkeeping activity
2022 startActivity(intent, optsBundle);
2023 } else {
2024 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2025 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2026 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002027
2028 if (v instanceof BubbleTextView) {
2029 // This is set to the view that launched the activity that navigated the user away
2030 // from launcher. Since there is no callback for when the activity has finished
2031 // launching, enable the press state and keep this reference to reset the press
2032 // state when we return to launcher.
2033 BubbleTextView btv = (BubbleTextView) v;
2034 btv.setStayPressed(true);
2035 setOnResumeCallback(btv);
2036 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002037 mAppLaunchSuccess = true;
2038 getUserEventDispatcher().logAppLaunch(v, intent); // TODO for discovered apps b/35802115
Sunny Goyala7ce1662016-05-31 15:01:35 -07002039 } catch (ActivityNotFoundException|SecurityException e) {
2040 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2041 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2042 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002043 return mAppLaunchSuccess;
Michael Jurka86a720e2012-05-09 11:23:23 -07002044 }
2045
Tony Wickhamdadb3042016-02-24 11:07:00 -08002046 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07002047 public boolean dispatchTouchEvent(MotionEvent ev) {
Sunny Goyal02424b22018-01-19 11:24:32 -08002048 mLastDispatchTouchEvent.set(ev.getX(), ev.getY());
Jon Miranda379198e2016-09-23 08:54:40 -07002049 return super.dispatchTouchEvent(ev);
2050 }
2051
2052 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002053 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002054 if (!isDraggingEnabled()) return false;
2055 if (isWorkspaceLocked()) return false;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002056 if (!isInState(NORMAL) && !isInState(OVERVIEW)) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002057
Jon Miranda51f037d2016-11-07 08:37:20 -08002058 boolean ignoreLongPressToOverview =
Sunny Goyal02424b22018-01-19 11:24:32 -08002059 mDeviceProfile.shouldIgnoreLongPressToOverview(mLastDispatchTouchEvent.x);
Jon Miranda379198e2016-09-23 08:54:40 -07002060
Adam Cohen1697b792013-09-17 19:08:21 -07002061 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002062 if (!isInState(OVERVIEW)) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002063 if (!mWorkspace.isTouchActive() && !ignoreLongPressToOverview) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302064 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2065 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002066 mWorkspace.getCurrentPage());
Sunny Goyal02424b22018-01-19 11:24:32 -08002067 UiFactory.onWorkspaceLongPress(this, mLastDispatchTouchEvent);
Adam Cohen93c97562013-09-26 13:48:01 -07002068 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2069 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2070 return true;
2071 } else {
2072 return false;
2073 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002074 } else {
2075 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002076 }
Adam Cohen1697b792013-09-17 19:08:21 -07002077 }
2078
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002079 CellLayout.CellInfo longClickCellInfo = null;
2080 View itemUnderLongClick = null;
2081 if (v.getTag() instanceof ItemInfo) {
2082 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002083 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002084 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07002085 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002086 }
2087
Winson Chung3d503fb2011-07-13 17:25:49 -07002088 // The hotseat touch handling does not go through Workspace, and we always allow long press
2089 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002090 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002091 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002092 // User long pressed on empty space
Sunny Goyalc99cb172017-10-19 16:15:09 -07002093 if (mWorkspace.isPageRearrangeEnabled()) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002094 mWorkspace.startReordering(v);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302095 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2096 Action.Direction.NONE, ContainerType.OVERVIEW);
Adam Cohend3ecce92013-09-16 14:58:00 -07002097 } else {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002098 if (ignoreLongPressToOverview) {
Jon Miranda379198e2016-09-23 08:54:40 -07002099 return false;
2100 }
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302101 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2102 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002103 mWorkspace.getCurrentPage());
Sunny Goyal02424b22018-01-19 11:24:32 -08002104 UiFactory.onWorkspaceLongPress(this, mLastDispatchTouchEvent);
Adam Cohendedbd962013-07-11 14:21:49 -07002105 }
Jon Miranda379198e2016-09-23 08:54:40 -07002106 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2107 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002108 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07002109 final boolean isAllAppsButton =
2110 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
2111 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
2112 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05002113 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002114 // User long pressed on an item
Tony Wickham10236d62016-09-28 12:49:25 -07002115 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002116 }
2117 }
2118 }
2119 return true;
2120 }
2121
Winson Chung3d503fb2011-07-13 17:25:49 -07002122 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08002123 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07002124 return mHotseat != null && layout != null &&
2125 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2126 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002127
Winson Chung3d503fb2011-07-13 17:25:49 -07002128 /**
2129 * Returns the CellLayout of the specified container at the specified screen.
2130 */
Sunny Goyal92820592015-03-02 11:31:35 -08002131 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002132 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2133 if (mHotseat != null) {
2134 return mHotseat.getLayout();
2135 } else {
2136 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002137 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002138 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002139 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002140 }
2141 }
2142
Michael Jurkae326f182011-11-21 14:05:46 -08002143 @Override
2144 public void onTrimMemory(int level) {
2145 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07002146 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2147 // The widget preview db can result in holding onto over
2148 // 3MB of memory for caching which isn't necessary.
2149 SQLiteDatabase.releaseMemory();
2150
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08002151 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07002152 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08002153 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01002154 if (mLauncherCallbacks != null) {
2155 mLauncherCallbacks.onTrimMemory(level);
2156 }
Winson Chung62a70be2018-02-23 15:10:05 -08002157 UiFactory.onTrimMemory(this, level);
Michael Jurkae326f182011-11-21 14:05:46 -08002158 }
2159
Michael Jurkad7c28052012-04-27 15:43:36 -07002160 @Override
2161 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07002162 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07002163 final List<CharSequence> text = event.getText();
2164 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07002165 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002166 // TODO: When can workspace be null?
2167 text.add(mWorkspace == null
2168 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07002169 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07002170 return result;
2171 }
2172
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002173 public void setOnResumeCallback(OnResumeCallback callback) {
2174 if (mOnResumeCallback != null) {
2175 mOnResumeCallback.onLauncherResume();
2176 }
2177 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07002178 }
2179
Sunny Goyal60876ac2018-02-20 14:21:20 -08002180 public void setOnStartCallback(OnStartCallback callback) {
2181 mOnStartCallback = callback;
2182 }
2183
Michael Jurka7607c2f2013-04-03 14:33:19 -07002184 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002185 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002186 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002187 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002188 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002189 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002190 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002191 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07002192 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07002193 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002194 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002195
Joe Onorato9c1289c2009-08-17 11:03:03 -04002196 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07002197 * Clear any pending bind callbacks. This is called when is loader is planning to
2198 * perform a full rebind from scratch.
2199 */
2200 @Override
2201 public void clearPendingBinds() {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002202 if (mPendingExecutor != null) {
2203 mPendingExecutor.markCompleted();
2204 mPendingExecutor = null;
2205 }
2206 }
2207
2208 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002209 * Refreshes the shortcuts shown on the workspace.
2210 *
2211 * Implementation of the method from LauncherModel.Callbacks.
2212 */
2213 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002214 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002215 // Floating panels (except the full widget sheet) are associated with individual icons. If
2216 // we are starting a fresh bind, close all such panels as all the icons are about
2217 // to go away.
2218 AbstractFloatingView.closeOpenViews(this, true,
Sunny Goyal7ede6112017-12-05 15:11:21 -08002219 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_REBIND_SAFE);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08002220
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002221 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08002222
Winson Chungd64d1762013-08-20 14:37:16 -07002223 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07002224 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07002225 mWorkspace.removeAllWorkspaceScreens();
Sunny Goyalc11fac32018-02-27 19:20:15 -08002226 mAppWidgetHost.clearViews();
Winson Chungd64d1762013-08-20 14:37:16 -07002227
Winson Chung3d503fb2011-07-13 17:25:49 -07002228 if (mHotseat != null) {
2229 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002230 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002231 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002232 }
2233
Adam Cohendcd297f2013-06-18 13:13:40 -07002234 @Override
2235 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002236 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002237 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
2238 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002239 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
2240 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002241 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002242 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
2243 // If there are no screens, we need to have an empty screen
2244 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07002245 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002246 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07002247
Winsonc7d2e832016-07-28 12:24:55 -07002248 // After we have added all the screens, if the wallpaper was locked to the default state,
2249 // then notify to indicate that it can be released and a proper wallpaper offset can be
2250 // computed before the next layout
2251 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07002252 }
2253
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002254 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002255 int count = orderedScreenIds.size();
2256 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002257 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002258 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002259 // No need to bind the first screen, as its always bound.
2260 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
2261 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002262 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002263 }
2264
Sunny Goyalb23980c2017-08-17 07:45:25 -07002265 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002266 public void bindAppsAdded(ArrayList<Long> newScreens, ArrayList<ItemInfo> addNotAnimated,
2267 ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07002268 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08002269 if (newScreens != null) {
2270 bindAddScreens(newScreens);
2271 }
Winson Chungd64d1762013-08-20 14:37:16 -07002272
2273 // We add the items without animation on non-visible pages, and with
2274 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08002275 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002276 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002277 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002278 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002279 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07002280 }
Winson Chungc58497e2013-09-03 17:48:37 -07002281
Winson Chung87412982013-10-03 18:34:14 -07002282 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07002283 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002284 }
2285
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002286 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002287 * Bind the items start-end from the list.
2288 *
2289 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002290 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07002291 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07002292 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002293 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07002294 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002295 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07002296 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07002297 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002298 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07002299 int end = items.size();
2300 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002301 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002302
2303 // Short circuit if we are loading dock items for a configuration which has no dock
2304 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2305 mHotseat == null) {
2306 continue;
2307 }
2308
Sunny Goyal639e9062015-08-19 19:17:06 -07002309 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002310 switch (item.itemType) {
2311 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2312 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08002313 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07002314 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07002315 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002316 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002317 }
2318 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07002319 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002320 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002321 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002322 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002323 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07002324 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2325 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07002326 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002327 if (view == null) {
2328 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002329 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08002330 break;
2331 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002332 default:
2333 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002334 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002335
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002336 /*
2337 * Remove colliding items.
2338 */
2339 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
2340 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
2341 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
2342 View v = cl.getChildAt(item.cellX, item.cellY);
2343 Object tag = v.getTag();
2344 String desc = "Collision while binding workspace item: " + item
2345 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08002346 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002347 throw (new RuntimeException(desc));
2348 } else {
2349 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002350 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002351 continue;
2352 }
2353 }
2354 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302355 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07002356 if (animateIcons) {
2357 // Animate all the applications up now
2358 view.setAlpha(0f);
2359 view.setScaleX(0f);
2360 view.setScaleY(0f);
2361 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08002362 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07002363 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002364 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07002365
Winson Chung997a9232013-07-24 15:33:46 -07002366 if (animateIcons) {
2367 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08002368 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07002369 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08002370 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07002371 final Runnable startBounceAnimRunnable = new Runnable() {
2372 public void run() {
2373 anim.playTogether(bounceAnims);
2374 anim.start();
2375 }
2376 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08002377 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07002378 // We post the animation slightly delayed to prevent slowdowns
2379 // when we are loading right after we return to launcher.
2380 mWorkspace.postDelayed(new Runnable() {
2381 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00002382 if (mWorkspace != null) {
2383 mWorkspace.snapToPage(newScreenIndex);
2384 mWorkspace.postDelayed(startBounceAnimRunnable,
2385 NEW_APPS_ANIMATION_DELAY);
2386 }
Winson Chung94d67682013-09-25 16:29:40 -07002387 }
2388 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07002389 } else {
2390 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07002391 }
2392 }
Winson Chung64359a52013-07-08 17:17:08 -07002393 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002394 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002395 }
2396
Joe Onorato9c1289c2009-08-17 11:03:03 -04002397 /**
2398 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002399 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07002400 public void bindAppWidget(LauncherAppWidgetInfo item) {
2401 View view = inflateAppWidget(item);
2402 if (view != null) {
2403 mWorkspace.addInScreen(view, item);
2404 mWorkspace.requestLayout();
2405 }
2406 }
2407
2408 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002409 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302410 PendingAppWidgetHostView view =
2411 new PendingAppWidgetHostView(this, item, mIconCache, true);
2412 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002413 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002414 }
2415
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002416 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002417
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002418 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08002419
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002420 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2421 // If the provider is not ready, bind as a pending widget.
2422 appWidgetInfo = null;
2423 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2424 // The widget id is not valid. Try to find the widget based on the provider info.
2425 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
2426 } else {
2427 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
2428 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002429
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002430 // If the provider is ready, but the width is not yet restored, try to restore it.
2431 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
2432 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07002433 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002434 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
2435 + " belongs to component " + item.providerName
2436 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002437 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002438 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002439 }
Sunny Goyalff572272014-07-23 13:58:07 -07002440
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002441 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002442 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07002443 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2444 // Id has not been allocated yet. Allocate a new id.
2445 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
2446 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07002447
Sunny Goyald478c832016-04-01 12:04:16 -07002448 // Also try to bind the widget. If the bind fails, the user will be shown
2449 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08002450 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07002451 pendingInfo.spanX = item.spanX;
2452 pendingInfo.spanY = item.spanY;
2453 pendingInfo.minSpanX = item.minSpanX;
2454 pendingInfo.minSpanY = item.minSpanY;
2455 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07002456
2457 boolean isDirectConfig =
2458 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2459 if (isDirectConfig && item.bindOptions != null) {
2460 Bundle newOptions = item.bindOptions.getExtras();
2461 if (options != null) {
2462 newOptions.putAll(options);
2463 }
2464 options = newOptions;
2465 }
Sunny Goyald478c832016-04-01 12:04:16 -07002466 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2467 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002468
Sunny Goyal86df1382016-08-10 15:03:22 -07002469 // We tried to bind once. If we were not able to bind, we would need to
2470 // go through the permission dialog, which means we cannot skip the config
2471 // activity.
2472 item.bindOptions = null;
2473 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2474
Sunny Goyald478c832016-04-01 12:04:16 -07002475 // Bind succeeded
2476 if (success) {
2477 // If the widget has a configure activity, it is still needs to set it up,
2478 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002479 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002480 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2481 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002482 }
Sunny Goyald478c832016-04-01 12:04:16 -07002483
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002484 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002485 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002486 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002487 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002488 // The widget was marked as UI not ready, but there is no configure activity to
2489 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002490 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002491 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002492 }
Sunny Goyalff572272014-07-23 13:58:07 -07002493 }
2494
Sunny Goyald5462aa2016-11-22 16:52:39 +05302495 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002496 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002497 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002498 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002499 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07002500 deleteWidgetInfo(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002501 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002502 }
2503
Sunny Goyal233ee962015-08-03 13:05:01 -07002504 item.minSpanX = appWidgetInfo.minSpanX;
2505 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302506 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002507 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302508 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002509 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302510 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002511
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002512 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002513 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002514 }
2515
Sunny Goyalff572272014-07-23 13:58:07 -07002516 /**
2517 * Restores a pending widget.
2518 *
2519 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002520 */
Sunny Goyald478c832016-04-01 12:04:16 -07002521 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002522 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2523 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2524 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002525 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002526 }
2527
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002528 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002529 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002530 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2531 info.pendingItemInfo = null;
2532 }
Sunny Goyalff572272014-07-23 13:58:07 -07002533
Sunny Goyalba47faa2017-10-04 16:50:57 -07002534 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002535 view.reInflate();
Sunny Goyalba47faa2017-10-04 16:50:57 -07002536 }
2537
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002538 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002539 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002540 }
2541
Adam Cohen1462de32012-07-24 22:34:36 -07002542 public void onPageBoundSynchronously(int page) {
Sunny Goyalf4aceab2017-12-06 16:16:31 -08002543 mSynchronouslyBoundPage = page;
Adam Cohen1462de32012-07-24 22:34:36 -07002544 }
2545
Sunny Goyal527c7d32015-08-28 15:19:36 -07002546 @Override
2547 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2548 if (mPendingExecutor != null) {
2549 mPendingExecutor.markCompleted();
2550 }
2551 mPendingExecutor = executor;
Sunny Goyal60180b02018-02-07 12:56:30 -08002552 if (!isInState(ALL_APPS)) {
2553 mAppsView.getAppsStore().setDeferUpdates(true);
2554 mPendingExecutor.execute(() -> mAppsView.getAppsStore().setDeferUpdates(false));
2555 }
2556
Sunny Goyal527c7d32015-08-28 15:19:36 -07002557 executor.attachTo(this);
2558 }
2559
2560 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2561 if (mPendingExecutor == executor) {
2562 mPendingExecutor = null;
2563 }
2564 }
2565
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002566 @Override
2567 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002568 if (mDragLayer.getAlpha() < 1) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002569 mDragLayer.animate().alpha(1).withEndAction(
2570 executor == null ? null : executor::onLoadAnimationCompleted).start();
2571 } else if (executor != null) {
2572 executor.onLoadAnimationCompleted();
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002573 }
2574 }
2575
Joe Onorato9c1289c2009-08-17 11:03:03 -04002576 /**
2577 * Callback saying that there aren't any more items to bind.
2578 *
2579 * Implementation of the method from LauncherModel.Callbacks.
2580 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002581 public void finishBindingItems() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002582 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002583 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002584
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002585 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002586
Sunny Goyal2100c782016-08-22 16:00:03 -07002587 if (mPendingActivityResult != null) {
2588 handleActivityResult(mPendingActivityResult.requestCode,
2589 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2590 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002591 }
2592
Sunny Goyala474a9b2017-05-04 16:47:11 -07002593 InstallShortcutReceiver.disableAndFlushInstallQueue(
2594 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002595
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002596 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002597 }
2598
Winson Chunga2413752012-04-03 14:22:34 -07002599 private boolean canRunNewAppsAnimation() {
2600 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002601 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002602 }
2603
Winson Chungc9168342013-06-26 14:54:55 -07002604 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002605 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07002606 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
2607 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002608 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002609 return bounceAnim;
2610 }
2611
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002612 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002613 * Add the icons for all apps.
2614 *
2615 * Implementation of the method from LauncherModel.Callbacks.
2616 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002617 public void bindAllApplications(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002618 mAppsView.getAppsStore().setApps(apps);
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002619
Adam Cohen9211d422014-10-07 18:14:53 -07002620 if (mLauncherCallbacks != null) {
2621 mLauncherCallbacks.bindAllApplications(apps);
2622 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002623 }
2624
2625 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002626 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
2627 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2628 */
2629 @Override
2630 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002631 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002632 }
2633
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002634 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002635 * A package was updated.
2636 *
2637 * Implementation of the method from LauncherModel.Callbacks.
2638 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002639 @Override
2640 public void bindAppsAddedOrUpdated(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002641 mAppsView.getAppsStore().addOrUpdateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002642 }
2643
Sunny Goyal4390ace2014-10-13 11:33:11 -07002644 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002645 public void bindPromiseAppProgressUpdated(PromiseAppInfo app) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002646 mAppsView.getAppsStore().updatePromiseAppProgress(app);
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002647 }
2648
2649 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002650 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002651 mWorkspace.widgetsRestored(widgets);
2652 }
2653
Joe Onorato9c1289c2009-08-17 11:03:03 -04002654 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002655 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002656 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002657 *
2658 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002659 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002660 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002661 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002662 if (!updated.isEmpty()) {
2663 mWorkspace.updateShortcuts(updated);
2664 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002665 }
2666
2667 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002668 * Update the state of a package, typically related to install state.
2669 *
2670 * Implementation of the method from LauncherModel.Callbacks.
2671 */
Sunny Goyale755d462014-07-22 13:48:29 -07002672 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002673 public void bindRestoreItemsChange(HashSet<ItemInfo> updates) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07002674 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07002675 }
2676
2677 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002678 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07002679 * in addition to specific applications to remove, the reason being that
2680 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002681 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07002682 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002683 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07002684 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002685 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002686 mWorkspace.removeItemsByMatcher(matcher);
2687 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002688 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002689
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002690 @Override
2691 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002692 mAppsView.getAppsStore().removeApps(appInfos);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002693 }
Joe Onoratobe386092009-11-17 17:32:16 -08002694
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002695 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002696 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
2697 mPopupDataProvider.setAllWidgets(allWidgets);
Tony Wickham26b17462017-03-20 17:12:24 -07002698 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
2699 if (topView != null) {
2700 topView.onWidgetsBound();
2701 }
2702 }
2703
Tony Wickham86222d22017-03-29 15:30:43 -07002704 /**
2705 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
2706 * refreshes the widgets and shortcuts associated with the given package/user
2707 */
2708 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07002709 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002710 }
2711
Winson Chung80baf5a2010-08-09 16:03:15 -07002712 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002713 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08002714 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002715 @Override
2716 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
2717 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002718
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002719 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
2720 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07002721 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002722 writer.println(prefix + " Homescreen " + i);
2723
2724 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
2725 for (int j = 0; j < layout.getChildCount(); j++) {
2726 Object tag = layout.getChildAt(j).getTag();
2727 if (tag != null) {
2728 writer.println(prefix + " " + tag.toString());
2729 }
2730 }
2731 }
2732
2733 writer.println(prefix + " Hotseat");
2734 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07002735 for (int j = 0; j < layout.getChildCount(); j++) {
2736 Object tag = layout.getChildAt(j).getTag();
2737 if (tag != null) {
2738 writer.println(prefix + " " + tag.toString());
2739 }
Adam Cohen4caf2982013-08-20 18:54:31 -07002740 }
Sunny Goyala1365452015-10-01 15:46:24 -07002741 }
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);
Sunny Goyal2bba1902018-02-27 12:20:50 -08002747 writer.println(" deviceProfile isTransposed=" + getDeviceProfile().isVerticalBarLayout());
2748 writer.println(" orientation=" + getResources().getConfiguration().orientation);
2749
2750 try {
2751 FileLog.flushAll(writer);
2752 } catch (Exception e) {
2753 // Ignore
2754 }
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002755
2756 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002757
Adam Cohen9211d422014-10-07 18:14:53 -07002758 if (mLauncherCallbacks != null) {
2759 mLauncherCallbacks.dump(prefix, fd, writer, args);
2760 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002761 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002762
Sunny Goyal66b24572016-09-21 15:57:55 -07002763 @Override
2764 @TargetApi(Build.VERSION_CODES.N)
2765 public void onProvideKeyboardShortcuts(
2766 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
2767
2768 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002769 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002770 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
2771 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
2772 }
2773 View currentFocus = getCurrentFocus();
2774 if (new CustomActionsPopup(this, currentFocus).canShow()) {
2775 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
2776 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
2777 }
Tony18c4aa42017-05-10 21:23:57 -05002778 if (currentFocus.getTag() instanceof ItemInfo
2779 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002780 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
2781 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
2782 }
2783 if (!shortcutInfos.isEmpty()) {
2784 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
2785 }
2786
2787 super.onProvideKeyboardShortcuts(data, menu, deviceId);
2788 }
2789
2790 @Override
2791 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
2792 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
2793 switch (keyCode) {
2794 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002795 if (isInState(NORMAL)) {
2796 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07002797 return true;
2798 }
2799 break;
2800 case KeyEvent.KEYCODE_S: {
2801 View focusedView = getCurrentFocus();
2802 if (focusedView instanceof BubbleTextView
2803 && focusedView.getTag() instanceof ItemInfo
2804 && mAccessibilityDelegate.performAction(focusedView,
2805 (ItemInfo) focusedView.getTag(),
2806 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal00ac9202017-11-09 15:24:06 -08002807 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07002808 return true;
2809 }
2810 break;
2811 }
2812 case KeyEvent.KEYCODE_O:
2813 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
2814 return true;
2815 }
2816 break;
2817 }
2818 }
2819 return super.onKeyShortcut(keyCode, event);
2820 }
2821
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002822 @Override
2823 public boolean onKeyUp(int keyCode, KeyEvent event) {
2824 if (keyCode == KeyEvent.KEYCODE_MENU) {
2825 // KEYCODE_MENU is sent by some tests, for example
2826 // LauncherJankTests#testWidgetsContainerFling. Don't just remove its handling.
2827 if (!mDragController.isDragging() && !mWorkspace.isSwitchingState() &&
2828 isInState(NORMAL)) {
2829 // Close any open floating views.
2830 AbstractFloatingView.closeAllOpenViews(this);
2831
2832 // Setting the touch point to (-1, -1) will show the options popup in the center of
2833 // the screen.
2834 mLastDispatchTouchEvent.set(-1, -1);
2835 UiFactory.onWorkspaceLongPress(this, mLastDispatchTouchEvent);
2836 }
2837 return true;
2838 }
2839 return super.onKeyUp(keyCode, event);
2840 }
2841
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002842 public static Launcher getLauncher(Context context) {
2843 if (context instanceof Launcher) {
2844 return (Launcher) context;
2845 }
2846 return ((Launcher) ((ContextWrapper) context).getBaseContext());
2847 }
2848
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08002849 @Override
2850 public void onActionModeStarted(ActionMode mode) {
2851 super.onActionModeStarted(mode);
2852 mCurrentActionMode = mode;
2853 }
2854
2855 @Override
2856 public void onActionModeFinished(ActionMode mode) {
2857 super.onActionModeFinished(mode);
2858 mCurrentActionMode = null;
2859 }
2860
2861 public boolean finishAutoCancelActionMode() {
2862 if (mCurrentActionMode != null && AUTO_CANCEL_ACTION_MODE == mCurrentActionMode.getTag()) {
2863 mCurrentActionMode.finish();
2864 return true;
2865 }
2866 return false;
2867 }
2868
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002869 /**
2870 * Callback for listening for onResume
2871 */
2872 public interface OnResumeCallback {
2873
2874 void onLauncherResume();
2875 }
Sunny Goyal60876ac2018-02-20 14:21:20 -08002876
2877 /**
2878 * Callback for listening for onStart
2879 */
2880 public interface OnStartCallback {
2881
2882 void onLauncherStart(Launcher launcher);
2883 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002884}