blob: fdf468c98d97ee6f6135c9770290f7118beefbe1 [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 Goyal605bcf32018-03-02 15:16:12 -0800403 rebindModel();
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800404 }
405
406 mOldConfig.setTo(newConfig);
407 super.onConfigurationChanged(newConfig);
408 }
409
Sunny Goyal605bcf32018-03-02 15:16:12 -0800410 @Override
411 public void rebindModel() {
412 int currentPage = mWorkspace.getNextPage();
413 if (mModel.startLoader(currentPage)) {
414 mWorkspace.setCurrentPage(currentPage);
415 setWorkspaceLoading(true);
416 }
417 }
418
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800419 private void initDeviceProfile(InvariantDeviceProfile idp) {
420 // Load configuration-specific DeviceProfile
421 mDeviceProfile = idp.getDeviceProfile(this);
422 if (isInMultiWindowModeCompat()) {
423 Display display = getWindowManager().getDefaultDisplay();
424 Point mwSize = new Point();
425 display.getSize(mwSize);
426 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
427 }
Sunny Goyal605bcf32018-03-02 15:16:12 -0800428 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout(), true);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800429 }
430
Sunny Goyal623eddd2018-03-02 12:24:41 -0800431 public RotationHelper getRotationHelper() {
432 return mRotationHelper;
433 }
434
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800435 @Override
Mario Bertschler27288382017-05-24 15:35:09 -0700436 public void onThemeChanged() {
437 recreate();
438 }
439
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700440 public LauncherStateManager getStateManager() {
441 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700442 }
443
Tonydc76f8e2018-02-02 21:45:39 -0600444 public LauncherAppTransitionManager getAppTransitionManager() {
445 return mAppTransitionManager;
446 }
447
Mario Bertschlera6936942017-05-31 14:48:19 -0700448 protected void overrideTheme(boolean isDark, boolean supportsDarkText) {
Mario Bertschler27288382017-05-24 15:35:09 -0700449 if (isDark) {
450 setTheme(R.style.LauncherThemeDark);
Mario Bertschlera6936942017-05-31 14:48:19 -0700451 } else if (supportsDarkText) {
452 setTheme(R.style.LauncherThemeDarkText);
Mario Bertschler27288382017-05-24 15:35:09 -0700453 }
454 }
455
456 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700457 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800458 return mLauncherView.findViewById(id);
459 }
460
461 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700462 public void onAppWidgetHostReset() {
463 if (mAppWidgetHost != null) {
464 mAppWidgetHost.startListening();
465 }
466 }
467
Adam Cohen9211d422014-10-07 18:14:53 -0700468 private LauncherCallbacks mLauncherCallbacks;
469
Sunny Goyal32554d12015-12-03 15:31:25 -0800470 /**
471 * Call this after onCreate to set or clear overlay.
472 */
473 public void setLauncherOverlay(LauncherOverlay overlay) {
474 if (overlay != null) {
475 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
476 }
477 mWorkspace.setLauncherOverlay(overlay);
478 }
479
Adam Cohen9211d422014-10-07 18:14:53 -0700480 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
481 mLauncherCallbacks = callbacks;
482 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700483 }
484
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700485 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700486 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700487 if (mLauncherCallbacks != null) {
488 mLauncherCallbacks.onLauncherProviderChange();
489 }
490 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700491
Hyunyoung Song3f471442015-04-08 19:01:34 -0700492 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700493 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
494 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700495 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700496 }
497
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000498 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700499 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
500 // This cast is safe as long as the id < 0x00FFFFFF
501 // Since we jail all the dynamically generated views, there should be no clashes
502 // with any other views.
503 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000504 }
505
Tony Wickham010d2552017-01-20 08:15:28 -0800506 public PopupDataProvider getPopupDataProvider() {
507 return mPopupDataProvider;
508 }
509
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000510 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700511 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
512 * a configuration step, this allows the proper animations to run after other transitions.
513 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700514 private long completeAdd(
515 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
516 long screenId = info.screenId;
517 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700518 // When the screen id represents an actual screen (as opposed to a rank) we make sure
519 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700520 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700521 }
Adam Cohendb364c32014-05-20 14:23:40 -0700522
Sunny Goyal2100c782016-08-22 16:00:03 -0700523 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800524 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700525 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700526 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800527 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700528 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700529 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700530 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700531 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700532 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700533 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700534 int widgetId = appWidgetId;
535 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700536 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700537 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700538 // Since the view was just bound, also launch the configure activity if needed
539 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
540 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800541 if (provider != null) {
542 new WidgetAddFlowHandler(provider)
543 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700544 }
545 }
546 break;
547 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800548 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700549
Adam Cohendb364c32014-05-20 14:23:40 -0700550 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800551 }
552
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800553 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700554 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700555 if (isWorkspaceLoading()) {
556 // process the result once the workspace has loaded.
557 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
558 return;
559 }
560 mPendingActivityResult = null;
561
Winson Chunge341d302013-08-16 14:31:00 -0700562 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700563 final PendingRequestArgs requestArgs = mPendingRequestArgs;
564 setWaitingForResult(null);
565 if (requestArgs == null) {
566 return;
567 }
568
569 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700570
Adam Cohenad4e15c2013-10-17 16:21:35 -0700571 Runnable exitSpringLoaded = new Runnable() {
572 @Override
573 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700574 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700575 }
576 };
577
Michael Jurka8b805b12012-04-18 14:23:14 -0700578 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700579 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700580 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700581 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
582 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700583 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700584 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700585 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700586 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700587 addAppWidgetImpl(
588 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800589 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700590 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700591 }
592 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200593 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700594 if (resultCode == RESULT_OK && isInState(OVERVIEW)) {
Tony Wickhame3054412015-09-09 09:05:25 -0700595 // User could have free-scrolled between pages before picking a wallpaper; make sure
596 // we move to the closest one now.
597 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700598 mStateManager.goToState(NORMAL, false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200599 }
600 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700601 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200602
Adam Cohened66b2b2012-01-23 17:28:51 -0800603 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700604 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700605
Adam Cohened66b2b2012-01-23 17:28:51 -0800606 // We have special handling for widgets
607 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800608 final int appWidgetId;
609 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
610 : -1;
611 if (widgetId < 0) {
612 appWidgetId = pendingAddWidgetId;
613 } else {
614 appWidgetId = widgetId;
615 }
616
Adam Cohenad4e15c2013-10-17 16:21:35 -0700617 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800618 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700619 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
620 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700621 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700622 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700623 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700624 @Override
625 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700626 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700627 }
628 };
Adam Cohendb364c32014-05-20 14:23:40 -0700629
Sunny Goyal2100c782016-08-22 16:00:03 -0700630 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
631 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
632 } else {
633 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
634 // When the screen id represents an actual screen (as opposed to a rank)
635 // we make sure that the drop page actually exists.
636 requestArgs.screenId =
637 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700638 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700639 final CellLayout dropLayout =
640 mWorkspace.getScreenWithId(requestArgs.screenId);
641
642 dropLayout.setDropPending(true);
643 final Runnable onComplete = new Runnable() {
644 @Override
645 public void run() {
646 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
647 dropLayout.setDropPending(false);
648 }
649 };
650 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
651 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700652 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800653 return;
654 }
655
Sunny Goyald478c832016-04-01 12:04:16 -0700656 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
657 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700658 if (resultCode == RESULT_OK) {
659 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700660 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700661 }
662 // Leave the widget in the pending state if the user canceled the configure.
663 return;
664 }
665
Sunny Goyalaad90582015-07-09 14:22:50 -0700666 if (requestCode == REQUEST_CREATE_SHORTCUT) {
667 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700668 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
669 completeAdd(requestCode, data, -1, requestArgs);
670 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
671 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
672
Sunny Goyalaad90582015-07-09 14:22:50 -0700673 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700674 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
675 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800676 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800677 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800678 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800679 }
680
681 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700682 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800683 final int requestCode, final int resultCode, final Intent data) {
684 handleActivityResult(requestCode, resultCode, data);
685 if (mLauncherCallbacks != null) {
686 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
687 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800688 }
689
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700690 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700691 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600692 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700693 PendingRequestArgs pendingArgs = mPendingRequestArgs;
694 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
695 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
696 setWaitingForResult(null);
697
Sunny Goyal28c6b962015-10-12 11:42:05 -0700698 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700699 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700700 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700701 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700702 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700703 Intent intent = pendingArgs.getPendingIntent();
704
Sunny Goyal28c6b962015-10-12 11:42:05 -0700705 if (grantResults.length > 0
706 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700707 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700708 } else {
709 // TODO: Show a snack bar with link to settings
710 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700711 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700712 }
713 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600714 if (mLauncherCallbacks != null) {
715 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
716 grantResults);
717 }
718 }
719
Adam Cohendb364c32014-05-20 14:23:40 -0700720 /**
721 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
722 *
723 * @param screenId the screen id to check
724 * @return the new screen, or screenId if it exists
725 */
726 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800727 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700728 if (dropLayout == null) {
729 // it's possible that the add screen was removed because it was
730 // empty and a re-bind occurred
731 mWorkspace.addExtraEmptyScreen();
732 return mWorkspace.commitExtraEmptyScreen();
733 } else {
734 return screenId;
735 }
736 }
737
Sunny Goyal2100c782016-08-22 16:00:03 -0700738 @Thunk void completeTwoStageWidgetDrop(
739 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
740 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800741 Runnable onCompleteRunnable = null;
742 int animationType = 0;
743
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700744 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800745 if (resultCode == RESULT_OK) {
746 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
747 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800748 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700749 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800750 onCompleteRunnable = new Runnable() {
751 @Override
752 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700753 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700754 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800755 }
756 };
757 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800758 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800759 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800760 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700761 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700762 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700763 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
764 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700765 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700766 // The animated view may be null in the case of a rotation during widget configuration
767 onCompleteRunnable.run();
768 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800769 }
770
771 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700772 protected void onStop() {
773 super.onStop();
774 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700775
776 if (mLauncherCallbacks != null) {
777 mLauncherCallbacks.onStop();
778 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800779 mAppWidgetHost.setListenIfResumed(false);
Tony Wickham010d2552017-01-20 08:15:28 -0800780
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700781 if (!mAppLaunchSuccess) {
782 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -0800783 mStateManager.getState().containerType, -1);
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700784 }
Tony Wickham010d2552017-01-20 08:15:28 -0800785 NotificationListener.removeNotificationsChangedListener();
Sunny Goyalaf161fd2018-02-05 16:34:57 -0800786 getStateManager().moveToRestState();
Michael Jurkacd496d72013-04-11 11:32:45 -0700787 }
788
789 @Override
790 protected void onStart() {
791 super.onStart();
792 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700793
Sunny Goyal60876ac2018-02-20 14:21:20 -0800794 if (mOnStartCallback != null) {
795 mOnStartCallback.onLauncherStart(this);
796 mOnStartCallback = null;
797 }
Adam Cohen9211d422014-10-07 18:14:53 -0700798 if (mLauncherCallbacks != null) {
799 mLauncherCallbacks.onStart();
800 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800801 mAppWidgetHost.setListenIfResumed(true);
Sunny Goyalcac002d2018-01-04 09:51:45 -0800802 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
Jon Miranda2d89ea82017-05-04 11:47:53 -0700803
Jon Mirandade43a712018-01-18 11:35:10 -0800804 if (mShouldFadeInScrim && mLauncherView.getBackground() != null) {
Jon Miranda2d89ea82017-05-04 11:47:53 -0700805 if (mScrimAnimator != null) {
806 mScrimAnimator.cancel();
807 }
Jon Mirandade43a712018-01-18 11:35:10 -0800808 mLauncherView.getBackground().setAlpha(0);
809 mScrimAnimator = ObjectAnimator.ofInt(mLauncherView.getBackground(),
Jon Miranda2d89ea82017-05-04 11:47:53 -0700810 LauncherAnimUtils.DRAWABLE_ALPHA, 0, 255);
811 mScrimAnimator.addListener(new AnimatorListenerAdapter() {
812 @Override
813 public void onAnimationEnd(Animator animation) {
814 mScrimAnimator = null;
815 }
816 });
817 mScrimAnimator.setDuration(600);
818 mScrimAnimator.setStartDelay(getWindow().getTransitionBackgroundFadeDuration());
819 mScrimAnimator.start();
820 }
Jon Miranda7fda2852017-07-19 16:07:01 -0700821 mShouldFadeInScrim = false;
Winson Chung62a70be2018-02-23 15:10:05 -0800822 UiFactory.onStart(this);
Michael Jurkacd496d72013-04-11 11:32:45 -0700823 }
824
825 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800826 protected void onResume() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700827 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800828 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700829 TraceHelper.partitionSection("ON_RESUME", "superCall");
830
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700831 mAppLaunchSuccess = false;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700832 getUserEventDispatcher().resetElapsedSessionMillis();
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700833 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700834 // Process any items that were added while Launcher was away.
835 InstallShortcutReceiver.disableAndFlushInstallQueue(
836 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700837
Sunny Goyala474a9b2017-05-04 16:47:11 -0700838 // Refresh shortcuts if the permission changed.
839 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700840
Sunny Goyal7ede6112017-12-05 15:11:21 -0800841 DiscoveryBounce.showIfNeeded(this);
Adam Cohen9211d422014-10-07 18:14:53 -0700842 if (mLauncherCallbacks != null) {
843 mLauncherCallbacks.onResume();
844 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800845
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700846 TraceHelper.endSection("ON_RESUME");
Adam Cohen06dff352012-06-01 17:17:08 -0700847 }
848
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800849 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700850 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700851 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700852 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700853
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700854 super.onPause();
Joe Onorato24b6fd82009-11-12 13:47:09 -0800855 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700856 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700857
Adam Cohen9211d422014-10-07 18:14:53 -0700858 if (mLauncherCallbacks != null) {
859 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700860 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700861 }
862
Winson Chungd7823942018-02-16 03:23:47 +0000863 @Override
864 public void onWindowFocusChanged(boolean hasFocus) {
865 super.onWindowFocusChanged(hasFocus);
866 mStateManager.onWindowFocusChanged();
867 }
868
Adam Cohenc2d6e892014-10-16 09:49:24 -0700869 public interface LauncherOverlay {
870
871 /**
872 * Touch interaction leading to overscroll has begun
873 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700874 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700875
876 /**
877 * Touch interaction related to overscroll has ended
878 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700879 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700880
881 /**
882 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
883 * screen (or in the case of RTL, the rightmost screen).
884 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700885 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700886
887 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800888 * Called when the launcher is ready to use the overlay
889 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700890 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700891 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700892 }
893
894 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700895 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700896 }
897
898 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
899
Sunny Goyalc86df472016-02-25 09:19:38 -0800900 public void onScrollChanged(float progress) {
901 if (mWorkspace != null) {
902 mWorkspace.onOverlayScrollChanged(progress);
903 }
904 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700905 }
906
Sunny Goyal16764582017-11-06 16:00:34 -0800907 public boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -0700908 if (mLauncherCallbacks != null) {
909 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700910 } else {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800911 // On O and above we there is always some setting present settings (add icon to
912 // home screen or icon badging). On earlier APIs we will have the allow rotation
913 // setting, on devices with a locked orientation,
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700914 return Utilities.ATLEAST_OREO || !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -0700915 }
Jorim Jaggid017f882014-01-14 17:08:48 -0800916 }
917
Sunny Goyalf9403d92017-10-18 10:55:56 -0700918 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -0700919 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700920 }
921
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800922 /**
923 * Restores the previous state, if it exists.
924 *
925 * @param savedState The previous state.
926 */
927 private void restoreState(Bundle savedState) {
928 if (savedState == null) {
929 return;
930 }
931
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700932 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -0700933 LauncherState[] stateValues = LauncherState.values();
934 LauncherState state = stateValues[stateOrdinal];
Sunny Goyalbc683e92017-12-06 10:25:07 -0800935 if (!state.disableRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700936 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800937 }
938
Sunny Goyal2100c782016-08-22 16:00:03 -0700939 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
940 if (requestArgs != null) {
941 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800942 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700943
944 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700945
946 SparseArray<Parcelable> widgetsState =
947 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
948 if (widgetsState != null) {
949 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
950 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800951 }
952
953 /**
954 * Finds all the views we need and configure them properly.
955 */
956 private void setupViews() {
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800957 mDragLayer = findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700958 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -0700959 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700960 mWorkspace.initParentViews(mDragLayer);
Sunny Goyal16764582017-11-06 16:00:34 -0800961 mOverviewPanel = findViewById(R.id.overview_panel);
Craig Mautner360310b2012-10-26 15:13:08 -0700962
Sunny Goyal784f9c32016-03-23 16:56:54 -0700963 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
964 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
965 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800966
Winson Chung4c98d922011-05-31 16:50:48 -0700967 // Setup the drag layer
Sunny Goyal7ede6112017-12-05 15:11:21 -0800968 mDragLayer.setup(this, mDragController);
Winson Chung4c98d922011-05-31 16:50:48 -0700969
Winson Chung3d503fb2011-07-13 17:25:49 -0700970 // Setup the hotseat
971 mHotseat = (Hotseat) findViewById(R.id.hotseat);
972 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -0700973 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -0700974 }
975
Winson Chung4c98d922011-05-31 16:50:48 -0700976 // Setup the workspace
977 mWorkspace.setHapticFeedbackEnabled(false);
978 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700979 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -0700980 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
981 // default state, otherwise we will update to the wrong offsets in RTL
982 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700983 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700984 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700985
Tony Wickham34d2c912015-09-11 09:27:58 -0700986 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700987 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700988
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700989 // Setup Apps
990 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -0700991
Winson Chung3d503fb2011-07-13 17:25:49 -0700992 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -0700993 mDragController.setMoveTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700994 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -0400995
Sunny Goyal6639a5d2018-02-28 15:09:36 -0800996 mAllAppsController.setupViews(mAppsView, mHotseat);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800997 }
998
999 /**
1000 * Creates a view representing a shortcut.
1001 *
1002 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001003 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001004 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001005 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001006 }
1007
1008 /**
1009 * Creates a view representing a shortcut inflated from the specified resource.
1010 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001011 * @param parent The group the shortcut belongs to.
1012 * @param info The data structure describing the shortcut.
1013 *
1014 * @return A View inflated from layoutResId.
1015 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001016 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -08001017 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
1018 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001019 favorite.applyFromShortcutInfo(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001020 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001021 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001022 return favorite;
1023 }
1024
1025 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -07001026 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001027 *
1028 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001029 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001030 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001031 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001032 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
1033 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001034 return;
1035 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001036
Jon Mirandac476d6e2017-05-04 16:30:01 -07001037 int[] cellXY = mTmpAddItemCellCoordinates;
1038 CellLayout layout = getCellLayout(container, screenId);
1039
Sunny Goyal782f0c92017-01-19 10:27:54 -08001040 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001041 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001042 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
1043 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001044 }
1045
1046 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001047 // Legacy shortcuts are only supported for primary profile.
1048 info = Process.myUserHandle().equals(args.user)
1049 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001050
Sunny Goyalb57645f2017-03-20 13:57:28 -07001051 if (info == null) {
1052 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1053 return;
1054 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001055 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001056 // The app is trying to add a shortcut without sufficient permissions
1057 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1058 return;
1059 }
1060 }
1061
Jon Mirandac476d6e2017-05-04 16:30:01 -07001062 if (container < 0) {
1063 // Adding a shortcut to the Workspace.
1064 final View view = createShortcut(info);
1065 boolean foundCellSpan = false;
1066 // First we check if we already know the exact location where we want to add this item.
1067 if (cellX >= 0 && cellY >= 0) {
1068 cellXY[0] = cellX;
1069 cellXY[1] = cellY;
1070 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001071
Jon Mirandac476d6e2017-05-04 16:30:01 -07001072 // If appropriate, either create a folder or add to an existing folder
1073 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001074 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001075 return;
1076 }
1077 DragObject dragObject = new DragObject();
1078 dragObject.dragInfo = info;
1079 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1080 true)) {
1081 return;
1082 }
1083 } else {
1084 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1085 }
1086
1087 if (!foundCellSpan) {
1088 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001089 return;
1090 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001091
1092 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1093 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001094 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001095 // Adding a shortcut to a Folder.
Sunny Goyale29897f52017-07-20 10:09:42 -07001096 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001097 if (folderIcon != null) {
1098 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1099 folderInfo.add(info, args.rank, false);
1100 } else {
Sunny Goyale29897f52017-07-20 10:09:42 -07001101 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001102 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001103 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001104 }
1105
Sunny Goyale29897f52017-07-20 10:09:42 -07001106 public FolderIcon findFolderIcon(final long folderIconId) {
1107 return (FolderIcon) mWorkspace.getFirstMatch(new ItemOperator() {
1108 @Override
1109 public boolean evaluate(ItemInfo info, View view) {
1110 return info != null && info.id == folderIconId;
1111 }
1112 });
1113 }
1114
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001115 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001116 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001117 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001118 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001119 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001120 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001121 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1122
Adam Cohened66b2b2012-01-23 17:28:51 -08001123 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001124 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001125 }
Romain Guycbb89e42009-06-08 15:52:54 -07001126
Adam Cohen59400422014-03-05 18:07:04 -08001127 LauncherAppWidgetInfo launcherInfo;
1128 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001129 launcherInfo.spanX = itemInfo.spanX;
1130 launcherInfo.spanY = itemInfo.spanY;
1131 launcherInfo.minSpanX = itemInfo.minSpanX;
1132 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001133 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001134
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001135 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001136 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001137
Sunny Goyal2100c782016-08-22 16:00:03 -07001138 if (hostView == null) {
1139 // Perform actual inflation because we're live
1140 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001141 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001142 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301143 prepareAppWidget(hostView, launcherInfo);
1144 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001145 }
Romain Guycbb89e42009-06-08 15:52:54 -07001146
Sunny Goyald5462aa2016-11-22 16:52:39 +05301147 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001148 hostView.setTag(item);
1149 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001150 hostView.setFocusable(true);
1151 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001152 }
1153
Adam Cohended9f8d2010-11-03 13:25:16 -07001154 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1155 @Override
1156 public void onReceive(Context context, Intent intent) {
1157 final String action = intent.getAction();
1158 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001159 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001160 // processing a multi-step drop
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001161 if (mAppsView != null && mPendingRequestArgs == null) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001162 mStateManager.goToState(NORMAL);
Winson Chung785d2eb2011-04-14 16:08:02 -07001163 }
Jon Miranda7fda2852017-07-19 16:07:01 -07001164 mShouldFadeInScrim = true;
1165 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1166 // ACTION_USER_PRESENT is sent after onStart/onResume. This covers the case where
1167 // the user unlocked and the Launcher is not in the foreground.
1168 mShouldFadeInScrim = false;
Adam Cohended9f8d2010-11-03 13:25:16 -07001169 }
1170 }
1171 };
1172
Tony Wickham010d2552017-01-20 08:15:28 -08001173 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
Sunny Goyal29947f02017-12-18 13:49:44 -08001174 mWorkspace.updateIconBadges(updatedBadges);
Sunny Goyal60180b02018-02-07 12:56:30 -08001175 mAppsView.getAppsStore().updateIconBadges(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001176
Sunny Goyal29947f02017-12-18 13:49:44 -08001177 PopupContainerWithArrow popup = PopupContainerWithArrow.getOpen(Launcher.this);
1178 if (popup != null) {
1179 popup.updateNotificationHeader(updatedBadges);
Tony Wickham010d2552017-01-20 08:15:28 -08001180 }
1181 }
1182
Adam Cohended9f8d2010-11-03 13:25:16 -07001183 @Override
1184 public void onAttachedToWindow() {
1185 super.onAttachedToWindow();
1186
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001187 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyalc86df472016-02-25 09:19:38 -08001188 if (mLauncherCallbacks != null) {
1189 mLauncherCallbacks.onAttachedToWindow();
1190 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001191 }
1192
1193 @Override
1194 public void onDetachedFromWindow() {
1195 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001196
1197 if (mLauncherCallbacks != null) {
1198 mLauncherCallbacks.onDetachedFromWindow();
1199 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001200 }
1201
Sunny Goyal3bac2c22018-02-07 14:44:05 -08001202 public void onQuickstepGestureStarted(boolean isVisible) {
1203 if (mLauncherCallbacks != null) {
1204 mLauncherCallbacks.onQuickstepGestureStarted(isVisible);
1205 }
1206 }
1207
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001208 public AllAppsTransitionController getAllAppsController() {
1209 return mAllAppsController;
1210 }
1211
Winson Chung1a341002018-01-17 10:00:23 -08001212 public LauncherRootView getRootView() {
1213 return (LauncherRootView) mLauncherView;
1214 }
1215
Winson Chunga6945242014-01-08 14:04:34 -08001216 public DragLayer getDragLayer() {
1217 return mDragLayer;
1218 }
1219
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001220 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001221 return mAppsView;
1222 }
1223
Winson Chunga6945242014-01-08 14:04:34 -08001224 public Workspace getWorkspace() {
1225 return mWorkspace;
1226 }
1227
1228 public Hotseat getHotseat() {
1229 return mHotseat;
1230 }
1231
Winson Chung8ae41982017-11-10 11:42:13 -08001232 public <T extends ViewGroup> T getOverviewPanel() {
1233 return (T) mOverviewPanel;
Winson Chunga6945242014-01-08 14:04:34 -08001234 }
1235
Sunny Goyal47328fd2016-05-25 18:56:41 -07001236 public DropTargetBar getDropTargetBar() {
1237 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001238 }
1239
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001240 public LauncherAppWidgetHost getAppWidgetHost() {
1241 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001242 }
Romain Guycbb89e42009-06-08 15:52:54 -07001243
Winson Chunga9abd0e2010-10-27 17:18:37 -07001244 public LauncherModel getModel() {
1245 return mModel;
1246 }
1247
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001248 public ModelWriter getModelWriter() {
1249 return mModelWriter;
1250 }
1251
Adam Cohen79d90c52016-04-22 13:29:20 -07001252 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001253 return mSharedPrefs;
1254 }
1255
Sunny Goyalf8d56fc2018-01-31 15:18:11 -08001256 public int getOrientation() { return mOldConfig.orientation; }
Jon Miranda6bed3502017-09-19 14:43:17 -07001257
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001258 @Override
1259 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001260 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001261 super.onNewIntent(intent);
1262
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001263 boolean alreadyOnHome = hasWindowFocus() && ((intent.getFlags() &
Winson15f8b172015-08-19 11:02:39 -07001264 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1265 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001266
1267 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001268 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001269 && AbstractFloatingView.getTopOpenView(this) == null;
Winson15f8b172015-08-19 11:02:39 -07001270 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001271 boolean internalStateHandled = InternalStateHandler
Sunny Goyal8b264562018-02-03 23:00:13 -08001272 .handleNewIntent(this, intent, isStarted());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001273
Winson15f8b172015-08-19 11:02:39 -07001274 if (isActionMain) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001275 if (!internalStateHandled) {
1276 // Note: There should be at most one log per method call. This is enforced
1277 // implicitly by using if-else statements.
1278 UserEventDispatcher ued = getUserEventDispatcher();
1279 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
1280 if (topOpenView != null) {
1281 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
1282 } else if (alreadyOnHome) {
1283 Target target = newContainerTarget(mStateManager.getState().containerType);
1284 target.pageIndex = mWorkspace.getCurrentPage();
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001285 ued.logActionCommand(Action.Command.HOME_INTENT, target,
1286 newContainerTarget(ContainerType.WORKSPACE));
Sunny Goyald3864fa2017-11-14 15:06:36 -08001287 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001288
Sunny Goyald3864fa2017-11-14 15:06:36 -08001289 // In all these cases, only animate if we're already on home
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001290 AbstractFloatingView.closeAllOpenViews(this, isStarted());
Jon Mirandafeba90f2016-10-06 10:53:29 -07001291
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001292 mStateManager.goToState(NORMAL);
Sunny Goyald3864fa2017-11-14 15:06:36 -08001293
1294 // Reset the apps view
1295 if (!alreadyOnHome && mAppsView != null) {
1296 mAppsView.reset();
1297 }
1298
1299 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()) {
1300 mWorkspace.post(mWorkspace::moveToDefaultScreen);
1301 }
1302 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001303
1304 final View v = getWindow().peekDecorView();
1305 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001306 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001307 }
1308
Adam Cohen9211d422014-10-07 18:14:53 -07001309 if (mLauncherCallbacks != null) {
1310 mLauncherCallbacks.onHomeIntent();
1311 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001312 }
Winson15f8b172015-08-19 11:02:39 -07001313
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001314 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001315 }
1316
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001317 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001318 public void onRestoreInstanceState(Bundle state) {
1319 super.onRestoreInstanceState(state);
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001320 mWorkspace.restoreInstanceStateForChild(mSynchronouslyBoundPage);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001321 }
1322
1323 @Override
1324 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001325 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001326 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001327
Adam Cohen21cd0022013-10-09 18:57:02 -07001328 }
Sunny Goyalea609262017-10-25 15:47:38 -07001329 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001330
1331
1332 AbstractFloatingView widgets = AbstractFloatingView
1333 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1334 if (widgets != null) {
1335 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1336 widgets.saveHierarchyState(widgetsState);
1337 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1338 } else {
1339 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1340 }
1341
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001342 // We close any open folders and shortcut containers since they will not be re-opened,
1343 // and we need to make sure this state is reflected.
1344 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001345
Sunny Goyal2100c782016-08-22 16:00:03 -07001346 if (mPendingRequestArgs != null) {
1347 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1348 }
1349 if (mPendingActivityResult != null) {
1350 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001351 }
1352
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001353 super.onSaveInstanceState(outState);
1354
Adam Cohen9211d422014-10-07 18:14:53 -07001355 if (mLauncherCallbacks != null) {
1356 mLauncherCallbacks.onSaveInstanceState(outState);
1357 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001358 }
1359
1360 @Override
1361 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001362 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001363
Sunny Goyal5cc7bbd2017-06-12 09:50:39 -07001364 unregisterReceiver(mReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001365 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001366
Winson Chungcd2b0142011-06-08 16:02:26 -07001367 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001368 // It's possible to receive onDestroy after a new Launcher activity has
1369 // been created. In this case, don't interfere with the new Launcher.
1370 if (mModel.isCurrentCallbacks(this)) {
1371 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001372 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001373 }
Sunny Goyal623eddd2018-03-02 12:24:41 -08001374 mRotationHelper.destroy();
Sunny Goyal745bad92016-05-02 10:54:12 -07001375
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001376 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001377 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001378 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001379 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001380 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001381
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001382 TextKeyListener.getInstance().release();
Mario Bertschler27288382017-05-24 15:35:09 -07001383 WallpaperColorInfo.getInstance(this).setOnThemeChangeListener(null);
1384
Michael Jurka2ecf9952012-06-18 12:52:28 -07001385 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001386
Tony2917a8b2017-07-31 23:29:42 -07001387 clearPendingBinds();
1388
Adam Cohen9211d422014-10-07 18:14:53 -07001389 if (mLauncherCallbacks != null) {
1390 mLauncherCallbacks.onDestroy();
1391 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001392 }
1393
Sunny Goyalae502842016-06-17 08:43:56 -07001394 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1395 return mAccessibilityDelegate;
1396 }
1397
Adam Cohena9cf38f2011-05-02 15:36:58 -07001398 public DragController getDragController() {
1399 return mDragController;
1400 }
1401
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001402 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001403 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001404 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001405 }
1406
1407 @Override
1408 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1409 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001410 try {
1411 super.startIntentSenderForResult(intent, requestCode,
1412 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1413 } catch (IntentSender.SendIntentException e) {
1414 throw new ActivityNotFoundException();
1415 }
1416 }
1417
Winson Chung70d72102011-08-12 11:24:35 -07001418 /**
1419 * Indicates that we want global search for this activity by setting the globalSearch
1420 * argument for {@link #startSearch} to true.
1421 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001422 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001423 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001424 Bundle appSearchData, boolean globalSearch) {
1425 if (appSearchData == null) {
1426 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001427 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001428 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001429
Sunny Goyal6f28e712016-09-13 14:19:29 -07001430 if (mLauncherCallbacks == null ||
1431 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1432 // Starting search from the callbacks failed. Start the default global search.
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001433 super.startSearch(initialQuery, selectInitialQuery, appSearchData, true);
Ian Parkinson047036e2014-09-01 15:40:53 +01001434 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001435
1436 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001437 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001438 }
1439
Joe Onorato9c1289c2009-08-17 11:03:03 -04001440 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001441 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001442 }
1443
Adam Cohen517a7f52014-03-01 12:12:59 -08001444 public boolean isWorkspaceLoading() {
1445 return mWorkspaceLoading;
1446 }
1447
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001448 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001449 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001450 }
1451
Sunny Goyal04a324a2017-01-20 21:08:59 -08001452 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001453 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001454 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001455
Sunny Goyal2100c782016-08-22 16:00:03 -07001456 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001457 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001458 if (LOGD) {
1459 Log.d(TAG, "Adding widget from drop");
1460 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001461 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001462 }
1463
Sunny Goyal2100c782016-08-22 16:00:03 -07001464 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001465 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1466 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1467 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001468
Adam Cohenad4e15c2013-10-17 16:21:35 -07001469 Runnable onComplete = new Runnable() {
1470 @Override
1471 public void run() {
1472 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001473 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001474 }
1475 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001476 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001477 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001478 }
1479 }
Romain Guycbb89e42009-06-08 15:52:54 -07001480
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001481 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1482 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001483 info.container = container;
1484 info.screenId = screenId;
1485 if (cell != null) {
1486 info.cellX = cell[0];
1487 info.cellY = cell[1];
1488 }
1489 info.spanX = spanX;
1490 info.spanY = spanY;
1491
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001492 switch (info.itemType) {
1493 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1494 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001495 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001496 break;
1497 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001498 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001499 break;
1500 default:
1501 throw new IllegalStateException("Unknown item type: " + info.itemType);
1502 }
1503 }
1504
Adam Cohenfbba09b2011-07-18 21:43:05 -07001505 /**
1506 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001507 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001508 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001509 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1510 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001511 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1512 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1513 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001514 }
1515
Adam Cohenfbba09b2011-07-18 21:43:05 -07001516 /**
1517 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001518 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001519 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001520 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001521 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001522 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001523 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001524 // In the case where we've prebound the widget, we remove it from the DragLayer
1525 if (LOGD) {
1526 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1527 }
1528 getDragLayer().removeView(hostView);
1529
Adam Cohened66b2b2012-01-23 17:28:51 -08001530 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001531 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001532
1533 // Clear the boundWidget so that it doesn't get destroyed.
1534 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001535 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001536 // In this case, we either need to start an activity to get permission to bind
1537 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001538 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1539 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1540 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1541 this, info.componentName);
1542 } else {
1543 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1544 }
Adam Cohendd70d662012-10-04 16:53:44 -07001545 Bundle options = info.bindOptions;
1546
Sunny Goyalffe83f12014-08-14 17:39:34 -07001547 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1548 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001549 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001550 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001551 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001552 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001553 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001554 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001555 }
1556
Adam Cohendcd297f2013-06-18 13:13:40 -07001557 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001558 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001559 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001560 folderInfo.title = getText(R.string.folder_name);
1561
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001562 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001563 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001564
1565 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001566 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301567 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001568 // Force measure the new folder icon
1569 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1570 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001571 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001572 }
Romain Guycbb89e42009-06-08 15:52:54 -07001573
Winsonc0b52fe2015-09-09 16:38:15 -07001574 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001575 * Unbinds the view for the specified item, and removes the item and all its children.
1576 *
1577 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001578 * @param itemInfo the {@link ItemInfo} for this view.
1579 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001580 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001581 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001582 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001583 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001584 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1585 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001586 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001587 } else {
1588 mWorkspace.removeWorkspaceItem(v);
1589 }
Winsonc0b52fe2015-09-09 16:38:15 -07001590 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001591 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001592 }
1593 } else if (itemInfo instanceof FolderInfo) {
1594 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001595 if (v instanceof FolderIcon) {
1596 ((FolderIcon) v).removeListeners();
1597 }
Winsonc0b52fe2015-09-09 16:38:15 -07001598 mWorkspace.removeWorkspaceItem(v);
1599 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001600 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001601 }
1602 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1603 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001604 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001605 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07001606 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001607 }
1608 } else {
1609 return false;
1610 }
1611 return true;
1612 }
1613
1614 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07001615 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07001616 */
Sunny Goyal56c73602015-09-25 12:55:01 -07001617 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07001618 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07001619 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07001620 // Deleting an app widget ID is a void call but writes to disk before returning
1621 // to the caller...
1622 new AsyncTask<Void, Void, Void>() {
1623 public Void doInBackground(Void ... args) {
1624 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
1625 return null;
1626 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07001627 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07001628 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001629 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001630 }
1631
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001632 @Override
1633 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001634 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001635 }
1636
Joe Onorato88ec0992009-11-19 13:16:06 -08001637 @Override
1638 public void onBackPressed() {
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08001639 if (finishAutoCancelActionMode()) {
1640 return;
1641 }
Adam Cohen9211d422014-10-07 18:14:53 -07001642 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1643 return;
1644 }
1645
Sunny Goyal45478022015-06-08 16:52:41 -07001646 if (mDragController.isDragging()) {
1647 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001648 return;
1649 }
1650
Jon Mirandafeba90f2016-10-06 10:53:29 -07001651 // Note: There should be at most one log per method call. This is enforced implicitly
1652 // by using if-else statements.
1653 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001654 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
1655 if (topView != null) {
Sunny Goyal37920962017-09-28 13:43:24 -07001656 topView.onBackPressed();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001657 } else if (!isInState(NORMAL)) {
Sunny Goyal49bcf342018-01-11 13:59:53 -08001658 LauncherState lastState = mStateManager.getLastState();
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001659 ued.logActionCommand(Action.Command.BACK, mStateManager.getState().containerType,
1660 lastState.containerType);
Sunny Goyal49bcf342018-01-11 13:59:53 -08001661 mStateManager.goToState(lastState);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001662 } else {
1663 // Back button is a no-op here, but give at least some feedback for the button press
1664 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001665 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001666 }
1667
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001668 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001669 * Launches the intent referred by the clicked shortcut.
1670 *
1671 * @param v The view representing the clicked shortcut.
1672 */
1673 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001674 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1675 // view has detached (it's possible for this to happen if the view is removed mid touch).
1676 if (v.getWindowToken() == null) {
1677 return;
1678 }
1679
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001680 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001681 return;
1682 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001683
Adam Cohen1697b792013-09-17 19:08:21 -07001684 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001685 if (isInState(OVERVIEW)) {
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001686 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Touch.TAP,
Jon Miranda2e61fba2017-02-24 09:12:59 -08001687 LauncherLogProto.Action.Direction.NONE,
1688 LauncherLogProto.ContainerType.OVERVIEW, mWorkspace.getCurrentPage());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001689 mStateManager.goToState(NORMAL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001690 }
1691 return;
1692 }
1693
1694 if (v instanceof CellLayout) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001695 if (isInState(OVERVIEW)) {
Jon Mirandac6cf4932017-02-07 17:12:36 -08001696 int page = mWorkspace.indexOfChild(v);
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001697 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Touch.TAP,
Jon Mirandac6cf4932017-02-07 17:12:36 -08001698 LauncherLogProto.Action.Direction.NONE,
1699 LauncherLogProto.ContainerType.OVERVIEW, page);
1700 mWorkspace.snapToPageFromOverView(page);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001701 mStateManager.goToState(NORMAL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001702 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07001703 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001704 }
1705
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001706 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001707 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001708 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001709 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001710 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001711 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001712 }
Sunny Goyal508da152014-08-14 10:53:27 -07001713 } else if (tag instanceof AppInfo) {
1714 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07001715 } else if (tag instanceof LauncherAppWidgetInfo) {
1716 if (v instanceof PendingAppWidgetHostView) {
1717 onClickPendingWidget((PendingAppWidgetHostView) v);
1718 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001719 }
1720 }
1721
Sunny Goyal70660032015-05-14 00:07:08 -07001722 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07001723 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07001724 return false;
1725 }
1726
Michael Jurkaaf442092010-06-10 17:01:57 -07001727 /**
Sunny Goyalff572272014-07-23 13:58:07 -07001728 * Event handler for the app widget view which has not fully restored.
1729 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001730 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07001731 if (mIsSafeModeEnabled) {
1732 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
1733 return;
1734 }
1735
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001736 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07001737 if (v.isReadyForClickSetup()) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001738 LauncherAppWidgetProviderInfo appWidgetInfo =
1739 mAppWidgetManager.findProvider(info.providerName, info.user);
1740 if (appWidgetInfo == null) {
1741 return;
1742 }
1743 WidgetAddFlowHandler addFlowHandler = new WidgetAddFlowHandler(appWidgetInfo);
1744
Sunny Goyald478c832016-04-01 12:04:16 -07001745 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
1746 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
1747 // This should not happen, as we make sure that an Id is allocated during bind.
1748 return;
1749 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001750 addFlowHandler.startBindFlow(this, info.appWidgetId, info,
1751 REQUEST_BIND_PENDING_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -07001752 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001753 addFlowHandler.startConfigActivity(this, info, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07001754 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001755 } else {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001756 final String packageName = info.providerName.getPackageName();
Sunny Goyale6e72002017-01-12 16:55:36 -08001757 onClickPendingAppItem(v, packageName, info.installProgress >= 0);
Sunny Goyalff572272014-07-23 13:58:07 -07001758 }
1759 }
1760
Sunny Goyale6e72002017-01-12 16:55:36 -08001761 private void onClickPendingAppItem(final View v, final String packageName,
1762 boolean downloadStarted) {
1763 if (downloadStarted) {
1764 // If the download has started, simply direct to the market app.
1765 startMarketIntentForPackage(v, packageName);
1766 return;
1767 }
Sunny Goyale03b8122014-10-08 09:55:24 -07001768 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001769 .setTitle(R.string.abandoned_promises_title)
1770 .setMessage(R.string.abandoned_promise_explanation)
Sunny Goyale6e72002017-01-12 16:55:36 -08001771 .setPositiveButton(R.string.abandoned_search, new DialogInterface.OnClickListener() {
1772 @Override
1773 public void onClick(DialogInterface dialogInterface, int i) {
1774 startMarketIntentForPackage(v, packageName);
1775 }
1776 })
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001777 .setNeutralButton(R.string.abandoned_clean_this,
1778 new DialogInterface.OnClickListener() {
1779 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001780 final UserHandle user = Process.myUserHandle();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001781 mWorkspace.removeAbandonedPromise(packageName, user);
1782 }
1783 })
1784 .create().show();
Sunny Goyale6e72002017-01-12 16:55:36 -08001785 }
1786
1787 private void startMarketIntentForPackage(View v, String packageName) {
1788 ItemInfo item = (ItemInfo) v.getTag();
Sunny Goyal67e75e22018-01-22 15:14:00 -08001789 Intent intent = new PackageManagerHelper(v.getContext()).getMarketIntent(packageName);
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001790 startActivitySafely(v, intent, item);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001791 }
1792
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001793 /**
1794 * Event handler for an app shortcut click.
1795 *
1796 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
1797 */
Chris Wren40c5ed32014-06-24 18:24:23 -04001798 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001799 if (LOGD) Log.d(TAG, "onClickAppShortcut");
1800 Object tag = v.getTag();
1801 if (!(tag instanceof ShortcutInfo)) {
1802 throw new IllegalArgumentException("Input must be a Shortcut");
1803 }
1804
1805 // Open shortcut
1806 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001807
Sunny Goyal076839c2017-10-30 13:52:20 -07001808 if (shortcut.isDisabled()) {
Tony Makf6308652017-12-02 16:56:22 +00001809 final int disabledFlags = shortcut.runtimeStatusFlags & ShortcutInfo.FLAG_DISABLED_MASK;
1810 if ((disabledFlags &
Sunny Goyal076839c2017-10-30 13:52:20 -07001811 ~FLAG_DISABLED_SUSPENDED &
1812 ~FLAG_DISABLED_QUIET_USER) == 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001813 // If the app is only disabled because of the above flags, launch activity anyway.
1814 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00001815 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07001816 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
1817 // Use a message specific to this shortcut, if it has one.
1818 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
1819 return;
1820 }
1821 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00001822 int error = R.string.activity_not_available;
Sunny Goyal076839c2017-10-30 13:52:20 -07001823 if ((shortcut.runtimeStatusFlags & FLAG_DISABLED_SAFEMODE) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00001824 error = R.string.safemode_shortcut_error;
Sunny Goyal076839c2017-10-30 13:52:20 -07001825 } else if ((shortcut.runtimeStatusFlags & FLAG_DISABLED_BY_PUBLISHER) != 0 ||
1826 (shortcut.runtimeStatusFlags & FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07001827 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00001828 }
1829 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
1830 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001831 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001832 }
1833
Chris Wren40c5ed32014-06-24 18:24:23 -04001834 // Check for abandoned promise
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -07001835 if ((v instanceof BubbleTextView) && shortcut.hasPromiseIconUi()) {
Sunny Goyale6e72002017-01-12 16:55:36 -08001836 String packageName = shortcut.intent.getComponent() != null ?
1837 shortcut.intent.getComponent().getPackageName() : shortcut.intent.getPackage();
1838 if (!TextUtils.isEmpty(packageName)) {
1839 onClickPendingAppItem(v, packageName,
1840 shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE));
1841 return;
1842 }
Chris Wren40c5ed32014-06-24 18:24:23 -04001843 }
1844
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001845 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07001846 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04001847 }
1848
Sunny Goyala7ce1662016-05-31 15:01:35 -07001849 private void startAppShortcutOrInfoActivity(View v) {
1850 ItemInfo item = (ItemInfo) v.getTag();
Mario Bertschler08ffaae2017-03-20 11:30:27 -07001851 Intent intent;
1852 if (item instanceof PromiseAppInfo) {
1853 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) item;
Sunny Goyal67e75e22018-01-22 15:14:00 -08001854 intent = promiseAppInfo.getMarketIntent(this);
Mario Bertschler08ffaae2017-03-20 11:30:27 -07001855 } else {
1856 intent = item.getIntent();
1857 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07001858 if (intent == null) {
1859 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07001860 }
Mario Bertschler8d01cb42018-02-13 14:47:02 -08001861 if (item instanceof ShortcutInfo) {
1862 ShortcutInfo si = (ShortcutInfo) item;
1863 if (si.hasStatusFlag(ShortcutInfo.FLAG_SUPPORTS_WEB_UI)
1864 && intent.getAction() == Intent.ACTION_VIEW) {
1865 // make a copy of the intent that has the package set to null
1866 // we do this because the platform sometimes disables instant
1867 // apps temporarily (triggered by the user) and fallbacks to the
1868 // web ui. This only works though if the package isn't set
1869 intent = new Intent(intent);
1870 intent.setPackage(null);
1871 }
1872 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001873 startActivitySafely(v, intent, item);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001874 }
1875
1876 /**
1877 * Event handler for a folder icon click.
1878 *
1879 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
1880 */
1881 protected void onClickFolderIcon(View v) {
1882 if (LOGD) Log.d(TAG, "onClickFolder");
1883 if (!(v instanceof FolderIcon)){
1884 throw new IllegalArgumentException("Input must be a FolderIcon");
1885 }
1886
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001887 Folder folder = ((FolderIcon) v).getFolder();
1888 if (!folder.isOpen() && !folder.isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001889 // Open the requested folder
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001890 folder.animateOpen();
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001891 }
Michael Jurka5130e402011-10-13 04:55:35 -07001892 }
1893
Sandeep Siddharthad8058372014-01-28 10:41:15 -08001894 /**
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001895 * Event handler for the wallpaper picker button that appears after a long press
1896 * on the home screen.
1897 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001898 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07001899 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07001900 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
1901 return;
1902 }
1903
Tony Wickham785f7a52015-08-31 17:28:32 -07001904 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
1905 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal2100c782016-08-22 16:00:03 -07001906 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001907 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001908 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001909
1910 String pickerPackage = getString(R.string.wallpaper_picker_package);
Tony85efb4b2017-06-02 13:06:10 -07001911 boolean hasTargetPackage = !TextUtils.isEmpty(pickerPackage);
1912 if (hasTargetPackage) {
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001913 intent.setPackage(pickerPackage);
1914 }
1915
Sunny Goyala5ace712017-11-15 17:12:51 -08001916 final Bundle launchOptions;
1917 if (v != null) {
1918 intent.setSourceBounds(getViewBounds(v));
1919 // If there is no target package, use the default intent chooser animation
Jon Miranda17940f02018-01-18 14:46:34 -08001920 launchOptions = hasTargetPackage
Tony Wickham005df0b2018-02-13 11:28:12 -08001921 ? getActivityLaunchOptionsAsBundle(v, isInMultiWindowModeCompat())
Jon Miranda17940f02018-01-18 14:46:34 -08001922 : null;
Sunny Goyala5ace712017-11-15 17:12:51 -08001923 } else {
1924 launchOptions = null;
1925 }
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001926 try {
Sunny Goyala5ace712017-11-15 17:12:51 -08001927 startActivityForResult(intent, REQUEST_PICK_WALLPAPER, launchOptions);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001928 } catch (ActivityNotFoundException e) {
1929 setWaitingForResult(null);
1930 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1931 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001932 }
1933
Sunny Goyala7ce1662016-05-31 15:01:35 -07001934 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001935 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001936 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
1937 try {
1938 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
1939 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
1940 // is enabled by default on NYC.
1941 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
1942 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001943
1944 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
1945 String id = ((ShortcutInfo) info).getDeepShortcutId();
1946 String packageName = intent.getPackage();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301947 DeepShortcutManager.getInstance(this).startShortcut(
Sunny Goyal9994b2b2016-06-23 14:17:24 -07001948 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001949 } else {
1950 // Could be launching some bookkeeping activity
1951 startActivity(intent, optsBundle);
1952 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07001953 } finally {
1954 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01001955 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001956 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001957 // Due to legacy reasons, direct call shortcuts require Launchers to have the
1958 // corresponding permission. Show the appropriate permission prompt if that
1959 // is the case.
1960 if (intent.getComponent() == null
1961 && Intent.ACTION_CALL.equals(intent.getAction())
1962 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
1963 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001964
1965 setWaitingForResult(PendingRequestArgs
1966 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07001967 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
1968 REQUEST_PERMISSION_CALL_PHONE);
1969 } else {
1970 // No idea why this was thrown.
1971 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07001972 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001973 }
1974 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001975
Tony Wickham005df0b2018-02-13 11:28:12 -08001976 public Bundle getActivityLaunchOptionsAsBundle(View v, boolean useDefaultLaunchOptions) {
1977 ActivityOptions activityOptions = getActivityLaunchOptions(v, useDefaultLaunchOptions);
1978 return activityOptions == null ? null : activityOptions.toBundle();
1979 }
1980
Sunny Goyalfe770c92016-09-27 14:38:58 -07001981 @TargetApi(Build.VERSION_CODES.M)
Tony Wickham005df0b2018-02-13 11:28:12 -08001982 public ActivityOptions getActivityLaunchOptions(View v, boolean useDefaultLaunchOptions) {
Jon Miranda17940f02018-01-18 14:46:34 -08001983 return useDefaultLaunchOptions
Jon Miranda54441f52018-01-24 15:38:25 -08001984 ? mAppTransitionManager.getDefaultActivityLaunchOptions(this, v)
1985 : mAppTransitionManager.getActivityLaunchOptions(this, v);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001986 }
1987
Tonye3c59252017-05-02 21:32:27 -07001988 public Rect getViewBounds(View v) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001989 int[] pos = new int[2];
1990 v.getLocationOnScreen(pos);
1991 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
1992 }
1993
Sunny Goyala7ce1662016-05-31 15:01:35 -07001994 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07001995 mAppLaunchSuccess = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001996 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
1997 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07001998 return mAppLaunchSuccess;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001999 }
Jon Miranda17940f02018-01-18 14:46:34 -08002000
Sunny Goyala7ce1662016-05-31 15:01:35 -07002001 // Only launch using the new animation if the shortcut has not opted out (this is a
2002 // private contract between launcher and may be ignored in the future).
2003 boolean useLaunchAnimation = (v != null) &&
2004 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Jon Miranda17940f02018-01-18 14:46:34 -08002005 Bundle optsBundle = useLaunchAnimation
Tony Wickham005df0b2018-02-13 11:28:12 -08002006 ? getActivityLaunchOptionsAsBundle(v, isInMultiWindowModeCompat())
Jon Miranda17940f02018-01-18 14:46:34 -08002007 : null;
Sunny Goyala7ce1662016-05-31 15:01:35 -07002008
Sunny Goyal24bb66a2017-03-21 15:12:01 -07002009 UserHandle user = item == null ? null : item.user;
Sunny Goyala7ce1662016-05-31 15:01:35 -07002010
2011 // Prepare intent
2012 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2013 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002014 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002015 }
2016 try {
Jon Miranda84e71bf2018-02-13 15:36:20 -08002017 boolean isShortcut = Utilities.ATLEAST_MARSHMALLOW
2018 && (item instanceof ShortcutInfo)
2019 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
2020 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
2021 && !((ShortcutInfo) item).isPromise();
Jon Miranda17940f02018-01-18 14:46:34 -08002022 if (isShortcut) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002023 // Shortcuts need some special checks due to legacy reasons.
2024 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002025 } else if (user == null || user.equals(Process.myUserHandle())) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002026 // Could be launching some bookkeeping activity
2027 startActivity(intent, optsBundle);
2028 } else {
2029 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2030 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2031 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002032
2033 if (v instanceof BubbleTextView) {
2034 // This is set to the view that launched the activity that navigated the user away
2035 // from launcher. Since there is no callback for when the activity has finished
2036 // launching, enable the press state and keep this reference to reset the press
2037 // state when we return to launcher.
2038 BubbleTextView btv = (BubbleTextView) v;
2039 btv.setStayPressed(true);
2040 setOnResumeCallback(btv);
2041 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002042 mAppLaunchSuccess = true;
2043 getUserEventDispatcher().logAppLaunch(v, intent); // TODO for discovered apps b/35802115
Sunny Goyala7ce1662016-05-31 15:01:35 -07002044 } catch (ActivityNotFoundException|SecurityException e) {
2045 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2046 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2047 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002048 return mAppLaunchSuccess;
Michael Jurka86a720e2012-05-09 11:23:23 -07002049 }
2050
Tony Wickhamdadb3042016-02-24 11:07:00 -08002051 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07002052 public boolean dispatchTouchEvent(MotionEvent ev) {
Sunny Goyal02424b22018-01-19 11:24:32 -08002053 mLastDispatchTouchEvent.set(ev.getX(), ev.getY());
Jon Miranda379198e2016-09-23 08:54:40 -07002054 return super.dispatchTouchEvent(ev);
2055 }
2056
2057 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002058 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002059 if (!isDraggingEnabled()) return false;
2060 if (isWorkspaceLocked()) return false;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002061 if (!isInState(NORMAL) && !isInState(OVERVIEW)) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002062
Jon Miranda51f037d2016-11-07 08:37:20 -08002063 boolean ignoreLongPressToOverview =
Sunny Goyal02424b22018-01-19 11:24:32 -08002064 mDeviceProfile.shouldIgnoreLongPressToOverview(mLastDispatchTouchEvent.x);
Jon Miranda379198e2016-09-23 08:54:40 -07002065
Adam Cohen1697b792013-09-17 19:08:21 -07002066 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002067 if (!isInState(OVERVIEW)) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002068 if (!mWorkspace.isTouchActive() && !ignoreLongPressToOverview) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302069 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2070 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002071 mWorkspace.getCurrentPage());
Sunny Goyal02424b22018-01-19 11:24:32 -08002072 UiFactory.onWorkspaceLongPress(this, mLastDispatchTouchEvent);
Adam Cohen93c97562013-09-26 13:48:01 -07002073 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2074 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2075 return true;
2076 } else {
2077 return false;
2078 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002079 } else {
2080 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002081 }
Adam Cohen1697b792013-09-17 19:08:21 -07002082 }
2083
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002084 CellLayout.CellInfo longClickCellInfo = null;
2085 View itemUnderLongClick = null;
2086 if (v.getTag() instanceof ItemInfo) {
2087 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002088 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002089 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07002090 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002091 }
2092
Winson Chung3d503fb2011-07-13 17:25:49 -07002093 // The hotseat touch handling does not go through Workspace, and we always allow long press
2094 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002095 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002096 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002097 // User long pressed on empty space
Sunny Goyalc99cb172017-10-19 16:15:09 -07002098 if (mWorkspace.isPageRearrangeEnabled()) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002099 mWorkspace.startReordering(v);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302100 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2101 Action.Direction.NONE, ContainerType.OVERVIEW);
Adam Cohend3ecce92013-09-16 14:58:00 -07002102 } else {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002103 if (ignoreLongPressToOverview) {
Jon Miranda379198e2016-09-23 08:54:40 -07002104 return false;
2105 }
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302106 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2107 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002108 mWorkspace.getCurrentPage());
Sunny Goyal02424b22018-01-19 11:24:32 -08002109 UiFactory.onWorkspaceLongPress(this, mLastDispatchTouchEvent);
Adam Cohendedbd962013-07-11 14:21:49 -07002110 }
Jon Miranda379198e2016-09-23 08:54:40 -07002111 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2112 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002113 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07002114 final boolean isAllAppsButton =
2115 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
2116 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
2117 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05002118 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002119 // User long pressed on an item
Tony Wickham10236d62016-09-28 12:49:25 -07002120 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002121 }
2122 }
2123 }
2124 return true;
2125 }
2126
Winson Chung3d503fb2011-07-13 17:25:49 -07002127 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08002128 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07002129 return mHotseat != null && layout != null &&
2130 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2131 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002132
Winson Chung3d503fb2011-07-13 17:25:49 -07002133 /**
2134 * Returns the CellLayout of the specified container at the specified screen.
2135 */
Sunny Goyal92820592015-03-02 11:31:35 -08002136 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002137 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2138 if (mHotseat != null) {
2139 return mHotseat.getLayout();
2140 } else {
2141 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002142 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002143 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002144 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002145 }
2146 }
2147
Michael Jurkae326f182011-11-21 14:05:46 -08002148 @Override
2149 public void onTrimMemory(int level) {
2150 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07002151 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2152 // The widget preview db can result in holding onto over
2153 // 3MB of memory for caching which isn't necessary.
2154 SQLiteDatabase.releaseMemory();
2155
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08002156 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07002157 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08002158 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01002159 if (mLauncherCallbacks != null) {
2160 mLauncherCallbacks.onTrimMemory(level);
2161 }
Winson Chung62a70be2018-02-23 15:10:05 -08002162 UiFactory.onTrimMemory(this, level);
Michael Jurkae326f182011-11-21 14:05:46 -08002163 }
2164
Michael Jurkad7c28052012-04-27 15:43:36 -07002165 @Override
2166 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07002167 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07002168 final List<CharSequence> text = event.getText();
2169 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07002170 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002171 // TODO: When can workspace be null?
2172 text.add(mWorkspace == null
2173 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07002174 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07002175 return result;
2176 }
2177
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002178 public void setOnResumeCallback(OnResumeCallback callback) {
2179 if (mOnResumeCallback != null) {
2180 mOnResumeCallback.onLauncherResume();
2181 }
2182 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07002183 }
2184
Sunny Goyal60876ac2018-02-20 14:21:20 -08002185 public void setOnStartCallback(OnStartCallback callback) {
2186 mOnStartCallback = callback;
2187 }
2188
Michael Jurka7607c2f2013-04-03 14:33:19 -07002189 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002190 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002191 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002192 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002193 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002194 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002195 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002196 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07002197 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07002198 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002199 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002200
Joe Onorato9c1289c2009-08-17 11:03:03 -04002201 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07002202 * Clear any pending bind callbacks. This is called when is loader is planning to
2203 * perform a full rebind from scratch.
2204 */
2205 @Override
2206 public void clearPendingBinds() {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002207 if (mPendingExecutor != null) {
2208 mPendingExecutor.markCompleted();
2209 mPendingExecutor = null;
2210 }
2211 }
2212
2213 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002214 * Refreshes the shortcuts shown on the workspace.
2215 *
2216 * Implementation of the method from LauncherModel.Callbacks.
2217 */
2218 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002219 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002220 // Floating panels (except the full widget sheet) are associated with individual icons. If
2221 // we are starting a fresh bind, close all such panels as all the icons are about
2222 // to go away.
2223 AbstractFloatingView.closeOpenViews(this, true,
Sunny Goyal7ede6112017-12-05 15:11:21 -08002224 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_REBIND_SAFE);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08002225
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002226 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08002227
Winson Chungd64d1762013-08-20 14:37:16 -07002228 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07002229 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07002230 mWorkspace.removeAllWorkspaceScreens();
Sunny Goyalc11fac32018-02-27 19:20:15 -08002231 mAppWidgetHost.clearViews();
Winson Chungd64d1762013-08-20 14:37:16 -07002232
Winson Chung3d503fb2011-07-13 17:25:49 -07002233 if (mHotseat != null) {
2234 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002235 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002236 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002237 }
2238
Adam Cohendcd297f2013-06-18 13:13:40 -07002239 @Override
2240 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002241 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002242 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
2243 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002244 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
2245 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002246 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002247 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
2248 // If there are no screens, we need to have an empty screen
2249 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07002250 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002251 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07002252
Winsonc7d2e832016-07-28 12:24:55 -07002253 // After we have added all the screens, if the wallpaper was locked to the default state,
2254 // then notify to indicate that it can be released and a proper wallpaper offset can be
2255 // computed before the next layout
2256 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07002257 }
2258
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002259 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002260 int count = orderedScreenIds.size();
2261 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002262 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002263 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002264 // No need to bind the first screen, as its always bound.
2265 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
2266 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002267 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002268 }
2269
Sunny Goyalb23980c2017-08-17 07:45:25 -07002270 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002271 public void bindAppsAdded(ArrayList<Long> newScreens, ArrayList<ItemInfo> addNotAnimated,
2272 ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07002273 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08002274 if (newScreens != null) {
2275 bindAddScreens(newScreens);
2276 }
Winson Chungd64d1762013-08-20 14:37:16 -07002277
2278 // We add the items without animation on non-visible pages, and with
2279 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08002280 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002281 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002282 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002283 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002284 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07002285 }
Winson Chungc58497e2013-09-03 17:48:37 -07002286
Winson Chung87412982013-10-03 18:34:14 -07002287 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07002288 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002289 }
2290
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002291 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002292 * Bind the items start-end from the list.
2293 *
2294 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002295 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07002296 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07002297 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002298 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07002299 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002300 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07002301 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07002302 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002303 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07002304 int end = items.size();
2305 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002306 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002307
2308 // Short circuit if we are loading dock items for a configuration which has no dock
2309 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2310 mHotseat == null) {
2311 continue;
2312 }
2313
Sunny Goyal639e9062015-08-19 19:17:06 -07002314 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002315 switch (item.itemType) {
2316 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2317 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08002318 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07002319 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07002320 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002321 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002322 }
2323 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07002324 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002325 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002326 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002327 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002328 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07002329 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2330 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07002331 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002332 if (view == null) {
2333 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002334 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08002335 break;
2336 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002337 default:
2338 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002339 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002340
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002341 /*
2342 * Remove colliding items.
2343 */
2344 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
2345 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
2346 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
2347 View v = cl.getChildAt(item.cellX, item.cellY);
2348 Object tag = v.getTag();
2349 String desc = "Collision while binding workspace item: " + item
2350 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08002351 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002352 throw (new RuntimeException(desc));
2353 } else {
2354 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002355 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002356 continue;
2357 }
2358 }
2359 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302360 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07002361 if (animateIcons) {
2362 // Animate all the applications up now
2363 view.setAlpha(0f);
2364 view.setScaleX(0f);
2365 view.setScaleY(0f);
2366 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08002367 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07002368 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002369 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07002370
Winson Chung997a9232013-07-24 15:33:46 -07002371 if (animateIcons) {
2372 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08002373 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07002374 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08002375 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07002376 final Runnable startBounceAnimRunnable = new Runnable() {
2377 public void run() {
2378 anim.playTogether(bounceAnims);
2379 anim.start();
2380 }
2381 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08002382 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07002383 // We post the animation slightly delayed to prevent slowdowns
2384 // when we are loading right after we return to launcher.
2385 mWorkspace.postDelayed(new Runnable() {
2386 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00002387 if (mWorkspace != null) {
2388 mWorkspace.snapToPage(newScreenIndex);
2389 mWorkspace.postDelayed(startBounceAnimRunnable,
2390 NEW_APPS_ANIMATION_DELAY);
2391 }
Winson Chung94d67682013-09-25 16:29:40 -07002392 }
2393 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07002394 } else {
2395 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07002396 }
2397 }
Winson Chung64359a52013-07-08 17:17:08 -07002398 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002399 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002400 }
2401
Joe Onorato9c1289c2009-08-17 11:03:03 -04002402 /**
2403 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002404 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07002405 public void bindAppWidget(LauncherAppWidgetInfo item) {
2406 View view = inflateAppWidget(item);
2407 if (view != null) {
2408 mWorkspace.addInScreen(view, item);
2409 mWorkspace.requestLayout();
2410 }
2411 }
2412
2413 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002414 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302415 PendingAppWidgetHostView view =
2416 new PendingAppWidgetHostView(this, item, mIconCache, true);
2417 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002418 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002419 }
2420
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002421 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002422
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002423 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08002424
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002425 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2426 // If the provider is not ready, bind as a pending widget.
2427 appWidgetInfo = null;
2428 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2429 // The widget id is not valid. Try to find the widget based on the provider info.
2430 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
2431 } else {
2432 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
2433 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002434
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002435 // If the provider is ready, but the width is not yet restored, try to restore it.
2436 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
2437 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07002438 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002439 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
2440 + " belongs to component " + item.providerName
2441 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002442 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002443 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002444 }
Sunny Goyalff572272014-07-23 13:58:07 -07002445
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002446 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002447 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07002448 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2449 // Id has not been allocated yet. Allocate a new id.
2450 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
2451 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07002452
Sunny Goyald478c832016-04-01 12:04:16 -07002453 // Also try to bind the widget. If the bind fails, the user will be shown
2454 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08002455 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07002456 pendingInfo.spanX = item.spanX;
2457 pendingInfo.spanY = item.spanY;
2458 pendingInfo.minSpanX = item.minSpanX;
2459 pendingInfo.minSpanY = item.minSpanY;
2460 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07002461
2462 boolean isDirectConfig =
2463 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2464 if (isDirectConfig && item.bindOptions != null) {
2465 Bundle newOptions = item.bindOptions.getExtras();
2466 if (options != null) {
2467 newOptions.putAll(options);
2468 }
2469 options = newOptions;
2470 }
Sunny Goyald478c832016-04-01 12:04:16 -07002471 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2472 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002473
Sunny Goyal86df1382016-08-10 15:03:22 -07002474 // We tried to bind once. If we were not able to bind, we would need to
2475 // go through the permission dialog, which means we cannot skip the config
2476 // activity.
2477 item.bindOptions = null;
2478 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2479
Sunny Goyald478c832016-04-01 12:04:16 -07002480 // Bind succeeded
2481 if (success) {
2482 // If the widget has a configure activity, it is still needs to set it up,
2483 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002484 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002485 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2486 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002487 }
Sunny Goyald478c832016-04-01 12:04:16 -07002488
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002489 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002490 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002491 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002492 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002493 // The widget was marked as UI not ready, but there is no configure activity to
2494 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002495 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002496 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002497 }
Sunny Goyalff572272014-07-23 13:58:07 -07002498 }
2499
Sunny Goyald5462aa2016-11-22 16:52:39 +05302500 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002501 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002502 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002503 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002504 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07002505 deleteWidgetInfo(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002506 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002507 }
2508
Sunny Goyal233ee962015-08-03 13:05:01 -07002509 item.minSpanX = appWidgetInfo.minSpanX;
2510 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302511 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002512 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302513 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002514 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302515 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002516
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002517 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002518 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002519 }
2520
Sunny Goyalff572272014-07-23 13:58:07 -07002521 /**
2522 * Restores a pending widget.
2523 *
2524 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002525 */
Sunny Goyald478c832016-04-01 12:04:16 -07002526 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002527 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2528 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2529 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002530 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002531 }
2532
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002533 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002534 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002535 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2536 info.pendingItemInfo = null;
2537 }
Sunny Goyalff572272014-07-23 13:58:07 -07002538
Sunny Goyalba47faa2017-10-04 16:50:57 -07002539 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002540 view.reInflate();
Sunny Goyalba47faa2017-10-04 16:50:57 -07002541 }
2542
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002543 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002544 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002545 }
2546
Adam Cohen1462de32012-07-24 22:34:36 -07002547 public void onPageBoundSynchronously(int page) {
Sunny Goyalf4aceab2017-12-06 16:16:31 -08002548 mSynchronouslyBoundPage = page;
Adam Cohen1462de32012-07-24 22:34:36 -07002549 }
2550
Sunny Goyal527c7d32015-08-28 15:19:36 -07002551 @Override
2552 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2553 if (mPendingExecutor != null) {
2554 mPendingExecutor.markCompleted();
2555 }
2556 mPendingExecutor = executor;
Sunny Goyal60180b02018-02-07 12:56:30 -08002557 if (!isInState(ALL_APPS)) {
2558 mAppsView.getAppsStore().setDeferUpdates(true);
2559 mPendingExecutor.execute(() -> mAppsView.getAppsStore().setDeferUpdates(false));
2560 }
2561
Sunny Goyal527c7d32015-08-28 15:19:36 -07002562 executor.attachTo(this);
2563 }
2564
2565 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2566 if (mPendingExecutor == executor) {
2567 mPendingExecutor = null;
2568 }
2569 }
2570
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002571 @Override
2572 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002573 if (mDragLayer.getAlpha() < 1) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002574 mDragLayer.animate().alpha(1).withEndAction(
2575 executor == null ? null : executor::onLoadAnimationCompleted).start();
2576 } else if (executor != null) {
2577 executor.onLoadAnimationCompleted();
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002578 }
2579 }
2580
Joe Onorato9c1289c2009-08-17 11:03:03 -04002581 /**
2582 * Callback saying that there aren't any more items to bind.
2583 *
2584 * Implementation of the method from LauncherModel.Callbacks.
2585 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002586 public void finishBindingItems() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002587 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002588 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002589
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002590 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002591
Sunny Goyal2100c782016-08-22 16:00:03 -07002592 if (mPendingActivityResult != null) {
2593 handleActivityResult(mPendingActivityResult.requestCode,
2594 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2595 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002596 }
2597
Sunny Goyala474a9b2017-05-04 16:47:11 -07002598 InstallShortcutReceiver.disableAndFlushInstallQueue(
2599 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002600
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002601 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002602 }
2603
Winson Chunga2413752012-04-03 14:22:34 -07002604 private boolean canRunNewAppsAnimation() {
2605 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002606 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002607 }
2608
Winson Chungc9168342013-06-26 14:54:55 -07002609 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002610 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07002611 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
2612 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002613 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002614 return bounceAnim;
2615 }
2616
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002617 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002618 * Add the icons for all apps.
2619 *
2620 * Implementation of the method from LauncherModel.Callbacks.
2621 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002622 public void bindAllApplications(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002623 mAppsView.getAppsStore().setApps(apps);
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002624
Adam Cohen9211d422014-10-07 18:14:53 -07002625 if (mLauncherCallbacks != null) {
2626 mLauncherCallbacks.bindAllApplications(apps);
2627 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002628 }
2629
2630 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002631 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
2632 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2633 */
2634 @Override
2635 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002636 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002637 }
2638
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002639 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002640 * A package was updated.
2641 *
2642 * Implementation of the method from LauncherModel.Callbacks.
2643 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002644 @Override
2645 public void bindAppsAddedOrUpdated(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002646 mAppsView.getAppsStore().addOrUpdateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002647 }
2648
Sunny Goyal4390ace2014-10-13 11:33:11 -07002649 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002650 public void bindPromiseAppProgressUpdated(PromiseAppInfo app) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002651 mAppsView.getAppsStore().updatePromiseAppProgress(app);
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002652 }
2653
2654 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002655 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002656 mWorkspace.widgetsRestored(widgets);
2657 }
2658
Joe Onorato9c1289c2009-08-17 11:03:03 -04002659 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002660 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002661 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002662 *
2663 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002664 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002665 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002666 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002667 if (!updated.isEmpty()) {
2668 mWorkspace.updateShortcuts(updated);
2669 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002670 }
2671
2672 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002673 * Update the state of a package, typically related to install state.
2674 *
2675 * Implementation of the method from LauncherModel.Callbacks.
2676 */
Sunny Goyale755d462014-07-22 13:48:29 -07002677 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002678 public void bindRestoreItemsChange(HashSet<ItemInfo> updates) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07002679 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07002680 }
2681
2682 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002683 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07002684 * in addition to specific applications to remove, the reason being that
2685 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002686 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07002687 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002688 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07002689 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002690 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002691 mWorkspace.removeItemsByMatcher(matcher);
2692 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002693 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002694
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002695 @Override
2696 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002697 mAppsView.getAppsStore().removeApps(appInfos);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002698 }
Joe Onoratobe386092009-11-17 17:32:16 -08002699
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002700 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002701 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
2702 mPopupDataProvider.setAllWidgets(allWidgets);
Tony Wickham26b17462017-03-20 17:12:24 -07002703 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
2704 if (topView != null) {
2705 topView.onWidgetsBound();
2706 }
2707 }
2708
Tony Wickham86222d22017-03-29 15:30:43 -07002709 /**
2710 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
2711 * refreshes the widgets and shortcuts associated with the given package/user
2712 */
2713 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07002714 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002715 }
2716
Winson Chung80baf5a2010-08-09 16:03:15 -07002717 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002718 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08002719 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002720 @Override
2721 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
2722 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002723
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002724 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
2725 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07002726 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002727 writer.println(prefix + " Homescreen " + i);
2728
2729 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
2730 for (int j = 0; j < layout.getChildCount(); j++) {
2731 Object tag = layout.getChildAt(j).getTag();
2732 if (tag != null) {
2733 writer.println(prefix + " " + tag.toString());
2734 }
2735 }
2736 }
2737
2738 writer.println(prefix + " Hotseat");
2739 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07002740 for (int j = 0; j < layout.getChildCount(); j++) {
2741 Object tag = layout.getChildAt(j).getTag();
2742 if (tag != null) {
2743 writer.println(prefix + " " + tag.toString());
2744 }
Adam Cohen4caf2982013-08-20 18:54:31 -07002745 }
Sunny Goyala1365452015-10-01 15:46:24 -07002746 }
2747
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002748 writer.println(prefix + "Misc:");
2749 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
2750 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
2751 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
Sunny Goyal2bba1902018-02-27 12:20:50 -08002752 writer.println(" deviceProfile isTransposed=" + getDeviceProfile().isVerticalBarLayout());
2753 writer.println(" orientation=" + getResources().getConfiguration().orientation);
2754
2755 try {
2756 FileLog.flushAll(writer);
2757 } catch (Exception e) {
2758 // Ignore
2759 }
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002760
2761 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002762
Adam Cohen9211d422014-10-07 18:14:53 -07002763 if (mLauncherCallbacks != null) {
2764 mLauncherCallbacks.dump(prefix, fd, writer, args);
2765 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002766 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002767
Sunny Goyal66b24572016-09-21 15:57:55 -07002768 @Override
2769 @TargetApi(Build.VERSION_CODES.N)
2770 public void onProvideKeyboardShortcuts(
2771 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
2772
2773 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002774 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002775 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
2776 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
2777 }
2778 View currentFocus = getCurrentFocus();
2779 if (new CustomActionsPopup(this, currentFocus).canShow()) {
2780 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
2781 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
2782 }
Tony18c4aa42017-05-10 21:23:57 -05002783 if (currentFocus.getTag() instanceof ItemInfo
2784 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002785 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
2786 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
2787 }
2788 if (!shortcutInfos.isEmpty()) {
2789 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
2790 }
2791
2792 super.onProvideKeyboardShortcuts(data, menu, deviceId);
2793 }
2794
2795 @Override
2796 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
2797 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
2798 switch (keyCode) {
2799 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002800 if (isInState(NORMAL)) {
2801 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07002802 return true;
2803 }
2804 break;
2805 case KeyEvent.KEYCODE_S: {
2806 View focusedView = getCurrentFocus();
2807 if (focusedView instanceof BubbleTextView
2808 && focusedView.getTag() instanceof ItemInfo
2809 && mAccessibilityDelegate.performAction(focusedView,
2810 (ItemInfo) focusedView.getTag(),
2811 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal00ac9202017-11-09 15:24:06 -08002812 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07002813 return true;
2814 }
2815 break;
2816 }
2817 case KeyEvent.KEYCODE_O:
2818 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
2819 return true;
2820 }
2821 break;
2822 }
2823 }
2824 return super.onKeyShortcut(keyCode, event);
2825 }
2826
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002827 @Override
2828 public boolean onKeyUp(int keyCode, KeyEvent event) {
2829 if (keyCode == KeyEvent.KEYCODE_MENU) {
2830 // KEYCODE_MENU is sent by some tests, for example
2831 // LauncherJankTests#testWidgetsContainerFling. Don't just remove its handling.
2832 if (!mDragController.isDragging() && !mWorkspace.isSwitchingState() &&
2833 isInState(NORMAL)) {
2834 // Close any open floating views.
2835 AbstractFloatingView.closeAllOpenViews(this);
2836
2837 // Setting the touch point to (-1, -1) will show the options popup in the center of
2838 // the screen.
2839 mLastDispatchTouchEvent.set(-1, -1);
2840 UiFactory.onWorkspaceLongPress(this, mLastDispatchTouchEvent);
2841 }
2842 return true;
2843 }
2844 return super.onKeyUp(keyCode, event);
2845 }
2846
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002847 public static Launcher getLauncher(Context context) {
2848 if (context instanceof Launcher) {
2849 return (Launcher) context;
2850 }
2851 return ((Launcher) ((ContextWrapper) context).getBaseContext());
2852 }
2853
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08002854 @Override
2855 public void onActionModeStarted(ActionMode mode) {
2856 super.onActionModeStarted(mode);
2857 mCurrentActionMode = mode;
2858 }
2859
2860 @Override
2861 public void onActionModeFinished(ActionMode mode) {
2862 super.onActionModeFinished(mode);
2863 mCurrentActionMode = null;
2864 }
2865
2866 public boolean finishAutoCancelActionMode() {
2867 if (mCurrentActionMode != null && AUTO_CANCEL_ACTION_MODE == mCurrentActionMode.getTag()) {
2868 mCurrentActionMode.finish();
2869 return true;
2870 }
2871 return false;
2872 }
2873
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002874 /**
2875 * Callback for listening for onResume
2876 */
2877 public interface OnResumeCallback {
2878
2879 void onLauncherResume();
2880 }
Sunny Goyal60876ac2018-02-20 14:21:20 -08002881
2882 /**
2883 * Callback for listening for onStart
2884 */
2885 public interface OnStartCallback {
2886
2887 void onLauncherStart(Launcher launcher);
2888 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002889}