blob: 38b0140781a8d8e0cb63d0725e6cc5afab60678e [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080019import static android.content.pm.ActivityInfo.CONFIG_ORIENTATION;
20import static android.content.pm.ActivityInfo.CONFIG_SCREEN_SIZE;
Sunny Goyalc99cb172017-10-19 16:15:09 -070021import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_NOSENSOR;
22import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Sunny Goyal076839c2017-10-30 13:52:20 -070023import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_BY_PUBLISHER;
24import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_LOCKED_USER;
25import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_QUIET_USER;
26import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_SAFEMODE;
27import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_SUSPENDED;
Sunny Goyal4c7f2152017-10-17 17:17:16 -070028import static com.android.launcher3.LauncherAnimUtils.SPRING_LOADED_EXIT_DELAY;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -070029import static com.android.launcher3.LauncherState.ALL_APPS;
30import static com.android.launcher3.LauncherState.NORMAL;
31import static com.android.launcher3.LauncherState.OVERVIEW;
Sunny Goyalaeb16432017-10-16 11:46:41 -070032import static com.android.launcher3.logging.LoggerUtils.newContainerTarget;
Sunny Goyal326403e2017-10-02 12:45:10 -070033
Sunny Goyal28c6b962015-10-12 11:42:05 -070034import android.Manifest;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070035import android.animation.Animator;
Jon Miranda2d89ea82017-05-04 11:47:53 -070036import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070037import android.animation.AnimatorSet;
Jon Miranda2d89ea82017-05-04 11:47:53 -070038import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070039import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070040import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000041import android.annotation.TargetApi;
Tony Wickham005df0b2018-02-13 11:28:12 -080042import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040043import android.app.AlertDialog;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080044import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070045import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080047import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080048import android.content.ComponentCallbacks2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.content.Context;
Andrew Sappersteinabef55a2016-06-19 12:49:00 -070050import android.content.ContextWrapper;
Chris Wren40c5ed32014-06-24 18:24:23 -040051import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070053import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070054import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070055import android.content.SharedPreferences;
Sunny Goyal745bad92016-05-02 10:54:12 -070056import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057import android.content.pm.PackageManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080058import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070059import android.database.sqlite.SQLiteDatabase;
Jon Mirandacc42c5b2016-10-27 17:15:27 -070060import android.graphics.Point;
Sunny Goyal02424b22018-01-19 11:24:32 -080061import android.graphics.PointF;
Michael Jurkaaf442092010-06-10 17:01:57 -070062import android.graphics.Rect;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070063import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020064import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065import android.os.Bundle;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070066import android.os.Parcelable;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080067import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070068import android.os.StrictMode;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070069import android.os.UserHandle;
Tony Wickham86222d22017-03-29 15:30:43 -070070import android.support.annotation.Nullable;
Michael Jurkaa33411c2012-06-14 16:18:21 -070071import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070073import android.util.Log;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070074import android.util.SparseArray;
Sunny Goyal7c8a65e2017-12-22 11:11:33 -080075import android.view.ActionMode;
Adam Cohen96d30a12013-07-16 18:13:21 -070076import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070077import android.view.HapticFeedbackConstants;
78import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070079import android.view.KeyboardShortcutGroup;
80import android.view.KeyboardShortcutInfo;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080081import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -070082import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070083import android.view.MotionEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070084import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080085import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070086import android.view.ViewGroup;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080087import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070088import android.view.animation.OvershootInterpolator;
Adam Cohen96d30a12013-07-16 18:13:21 -070089import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070090
Sunny Goyal651077b2014-06-30 14:15:31 -070091import com.android.launcher3.DropTarget.DragObject;
Sunny Goyala7ce1662016-05-31 15:01:35 -070092import com.android.launcher3.LauncherSettings.Favorites;
Jon Mirandac476d6e2017-05-04 16:30:01 -070093import com.android.launcher3.Workspace.ItemOperator;
Sunny Goyalae502842016-06-17 08:43:56 -070094import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070095import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070096import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal7ede6112017-12-05 15:11:21 -080097import com.android.launcher3.allapps.DiscoveryBounce;
Sunny Goyalffe83f12014-08-14 17:39:34 -070098import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010099import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -0700100import com.android.launcher3.compat.LauncherAppsCompatVO;
Tony Wickhame0c33232016-02-08 11:37:04 -0800101import com.android.launcher3.config.FeatureFlags;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700102import com.android.launcher3.dragndrop.DragController;
103import com.android.launcher3.dragndrop.DragLayer;
Sunny Goyal94b510c2016-08-16 15:36:48 -0700104import com.android.launcher3.dragndrop.DragOptions;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700105import com.android.launcher3.dragndrop.DragView;
Mario Bertschler27288382017-05-24 15:35:09 -0700106import com.android.launcher3.dynamicui.WallpaperColorInfo;
Sunny Goyal26119432016-02-18 22:09:23 +0000107import com.android.launcher3.folder.Folder;
108import com.android.launcher3.folder.FolderIcon;
Sunny Goyal66b24572016-09-21 15:57:55 -0700109import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700110import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700111import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700112import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyala535ae42017-02-27 10:07:13 -0800113import com.android.launcher3.model.ModelWriter;
Sunny Goyala535ae42017-02-27 10:07:13 -0800114import com.android.launcher3.notification.NotificationListener;
Tony Wickham540913e2017-01-23 11:47:51 -0800115import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -0800116import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700117import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyalf8088ee2017-11-10 14:52:00 -0800118import com.android.launcher3.states.InternalStateHandler;
Sunny Goyala5ace712017-11-15 17:12:51 -0800119import com.android.launcher3.uioverrides.UiFactory;
Jon Mirandac6cf4932017-02-07 17:12:36 -0800120import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530121import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
122import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
Sunny Goyalaeb16432017-10-16 11:46:41 -0700123import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyal2100c782016-08-22 16:00:03 -0700124import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700125import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700126import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700127import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700128import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800129import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700130import com.android.launcher3.util.PendingRequestArgs;
Sunny Goyal8392c822017-06-20 10:03:56 -0700131import com.android.launcher3.util.SystemUiController;
Mario Bertschlera6936942017-05-31 14:48:19 -0700132import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700133import com.android.launcher3.util.Thunk;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700134import com.android.launcher3.util.TraceHelper;
Sunny Goyal326403e2017-10-02 12:45:10 -0700135import com.android.launcher3.util.UiThreadHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700136import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyal29947f02017-12-18 13:49:44 -0800137import com.android.launcher3.widget.LauncherAppWidgetHostView;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800138import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700139import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal29947f02017-12-18 13:49:44 -0800140import com.android.launcher3.widget.PendingAppWidgetHostView;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800141import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700142import com.android.launcher3.widget.WidgetHostViewLoader;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700143import com.android.launcher3.widget.WidgetListRowEntry;
144import com.android.launcher3.widget.WidgetsFullSheet;
Sunny Goyal952e63d2017-08-16 04:59:08 -0700145import com.android.launcher3.widget.custom.CustomWidgetParser;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700146
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700147import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700148import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700149import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700150import java.util.Collection;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700151import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700152import java.util.List;
Tony Wickham010d2552017-01-20 08:15:28 -0800153import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700154
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155/**
156 * Default launcher application.
157 */
Sunny Goyal27835952017-01-13 12:15:53 -0800158public class Launcher extends BaseActivity
Adam Cohen79d90c52016-04-22 13:29:20 -0700159 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
160 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Mario Bertschler27288382017-05-24 15:35:09 -0700161 WallpaperColorInfo.OnThemeChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700162 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700163 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164
Winson Chunga2413752012-04-03 14:22:34 -0700165 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700166
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700168 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700169
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700170 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700171 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172
Michael Jurka8b805b12012-04-18 14:23:14 -0700173 private static final int REQUEST_BIND_APPWIDGET = 11;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700174 private static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
175 private static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700176
Jon Mirandac476d6e2017-05-04 16:30:01 -0700177 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700178
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700179 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
180
Mathew Inwood876a8462013-06-14 14:12:41 +0100181 /**
182 * IntentStarter uses request codes starting with this. This must be greater than all activity
183 * request codes used internally.
184 */
185 protected static final int REQUEST_LAST = 100;
186
Winson Chung2672ff92012-05-04 16:22:30 -0700187 // The Intent extra that defines whether to ignore the launch animation
188 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400189 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700190
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191 // Type: int
192 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700193 // Type: int
194 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700195 // Type: PendingRequestArgs
196 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
197 // Type: ActivityResultInfo
198 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700199 // Type: SparseArray<Parcelable>
200 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800201
Sunny Goyal7c8a65e2017-12-22 11:11:33 -0800202 // When starting an action mode, setting this tag will cause the action mode to be cancelled
203 // automatically when user interacts with the launcher.
204 public static final Object AUTO_CANCEL_ACTION_MODE = new Object();
205
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700206 private LauncherStateManager mStateManager;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700207
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700208 private boolean mIsSafeModeEnabled;
209
Adam Cohenad4e15c2013-10-17 16:21:35 -0700210 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211
Winson Chunga2413752012-04-03 14:22:34 -0700212 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700213 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
214 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
215 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700216
Jon Miranda54441f52018-01-24 15:38:25 -0800217 private LauncherAppTransitionManager mAppTransitionManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800218 private Configuration mOldConfig;
Jon Miranda54441f52018-01-24 15:38:25 -0800219
Adam Cohen091440a2015-03-18 14:16:05 -0700220 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700221 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700222 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800223 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700224
Sunny Goyalffe83f12014-08-14 17:39:34 -0700225 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700226 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700227
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700228 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700229
Sunny Goyal316490e2015-06-02 09:38:28 -0700230 @Thunk Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700231
Sunny Goyal47328fd2016-05-25 18:56:41 -0700232 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700233
234 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700235 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700236 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700237
Winson Chung8ae41982017-11-10 11:42:13 -0800238 // UI and state for the overview panel
239 private ViewGroup mOverviewPanel;
240
Adam Cohen091440a2015-03-18 14:16:05 -0700241 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400242
Sunny Goyal60876ac2018-02-20 14:21:20 -0800243 private OnStartCallback mOnStartCallback;
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700244 private OnResumeCallback mOnResumeCallback;
245
Sunny Goyal527c7d32015-08-28 15:19:36 -0700246 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700247
Joe Onorato9c1289c2009-08-17 11:03:03 -0400248 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800249 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800250 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700251 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400252
Jon Miranda2d89ea82017-05-04 11:47:53 -0700253 private ObjectAnimator mScrimAnimator;
Jon Miranda7fda2852017-07-19 16:07:01 -0700254 private boolean mShouldFadeInScrim;
Jon Miranda2d89ea82017-05-04 11:47:53 -0700255
Tony Wickham010d2552017-01-20 08:15:28 -0800256 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700257
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800258 private int mSynchronouslyBoundPage = PagedView.INVALID_PAGE;
Adam Cohen1462de32012-07-24 22:34:36 -0700259
Winson Chung46353de2012-02-16 14:05:10 -0800260 // We only want to get the SharedPreferences once since it does an FS stat each time we get
261 // it from the context.
262 private SharedPreferences mSharedPrefs;
263
Sunny Goyal2100c782016-08-22 16:00:03 -0700264 // Activity result which needs to be processed after workspace has loaded.
265 private ActivityResultInfo mPendingActivityResult;
266 /**
267 * Holds extra information required to handle a result from an external call, like
268 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
269 */
270 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800271
Sunny Goyal02424b22018-01-19 11:24:32 -0800272 private final PointF mLastDispatchTouchEvent = new PointF();
Jon Miranda379198e2016-09-23 08:54:40 -0700273
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700274 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700275 private boolean mRotationEnabled = false;
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700276 private boolean mAppLaunchSuccess;
277
Sunny Goyal745bad92016-05-02 10:54:12 -0700278 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7c8a65e2017-12-22 11:11:33 -0800279 private ActionMode mCurrentActionMode;
Sunny Goyal7779d622015-06-11 16:18:39 -0700280
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800281 @Override
282 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700283 if (DEBUG_STRICT_MODE) {
284 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
285 .detectDiskReads()
286 .detectDiskWrites()
287 .detectNetwork() // or .detectAll() for all detectable problems
288 .penaltyLog()
289 .build());
290 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
291 .detectLeakedSqlLiteObjects()
292 .detectLeakedClosableObjects()
293 .penaltyLog()
294 .penaltyDeath()
295 .build());
296 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700297 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700298
Mario Bertschler27288382017-05-24 15:35:09 -0700299 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
300 wallpaperColorInfo.setOnThemeChangeListener(this);
Mario Bertschlera6936942017-05-31 14:48:19 -0700301 overrideTheme(wallpaperColorInfo.isDark(), wallpaperColorInfo.supportsDarkText());
Mario Bertschler27288382017-05-24 15:35:09 -0700302
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800303 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700304 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400305
Sunny Goyal87f784c2017-01-11 10:48:34 -0800306 LauncherAppState app = LauncherAppState.getInstance(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800307 mOldConfig = new Configuration(getResources().getConfiguration());
Sunny Goyal0e7724c2018-02-22 13:22:21 -0800308 mModel = app.setLauncher(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800309 initDeviceProfile(app.getInvariantDeviceProfile());
Winson Chung6e1c0d32013-10-25 15:24:24 -0700310
Sunny Goyalf7258242015-10-19 16:59:07 -0700311 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700312 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800313 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700314 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700315
Joe Onorato41a12d22009-10-31 18:30:00 -0400316 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700317 mAllAppsController = new AllAppsTransitionController(this);
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800318 mStateManager = new LauncherStateManager(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700319
Sunny Goyalffe83f12014-08-14 17:39:34 -0700320 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700321
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700322 mAppWidgetHost = new LauncherAppWidgetHost(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700323 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700324
Sunny Goyal60820d72017-05-09 12:40:11 -0700325 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700326
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800327 setupViews();
Tony Wickham010d2552017-01-20 08:15:28 -0800328 mPopupDataProvider = new PopupDataProvider(this);
329
Sunny Goyald3864fa2017-11-14 15:06:36 -0800330 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
331 // In case we are on a device with locked rotation, we should look at preferences to check
332 // if the user has specifically allowed rotation.
333 if (!mRotationEnabled) {
334 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
335 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
336 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
337 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800338
Sunny Goyald3864fa2017-11-14 15:06:36 -0800339 boolean internalStateHandled = InternalStateHandler.handleCreate(this, getIntent());
340 if (internalStateHandled) {
341 // Temporarily enable the rotation
342 mRotationEnabled = true;
343
344 if (savedInstanceState != null) {
345 // InternalStateHandler has already set the appropriate state.
346 // We dont need to do anything.
347 savedInstanceState.remove(RUNTIME_STATE);
348 }
349 }
350 restoreState(savedInstanceState);
Winson Chungb63b44c2017-11-10 17:54:44 -0800351
Sunny Goyal2100c782016-08-22 16:00:03 -0700352 // We only load the page synchronously if the user rotates (or triggers a
353 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700354 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
355 if (savedInstanceState != null) {
356 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
357 }
Sunny Goyald3864fa2017-11-14 15:06:36 -0800358
Sunny Goyalfe770c92016-09-27 14:38:58 -0700359 if (!mModel.startLoader(currentScreen)) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800360 if (!internalStateHandled) {
361 // If we are not binding synchronously, show a fade in animation when
362 // the first page bind completes.
363 mDragLayer.setAlpha(0);
364 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700365 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700366 // Pages bound synchronously.
367 mWorkspace.setCurrentPage(currentScreen);
368
Sunny Goyal2100c782016-08-22 16:00:03 -0700369 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800370 }
371
372 // For handling default keys
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800373 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800374
Winson Chung90049122018-02-15 11:33:53 -0800375 updateRequestedOrientation();
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800376 setContentView(mLauncherView);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800377 getRootView().dispatchInsets();
Mario Bertschlera6936942017-05-31 14:48:19 -0700378
Jon Miranda7fda2852017-07-19 16:07:01 -0700379 // Listen for broadcasts
380 IntentFilter filter = new IntentFilter();
381 filter.addAction(Intent.ACTION_SCREEN_OFF);
382 filter.addAction(Intent.ACTION_USER_PRESENT); // When the device wakes up + keyguard is gone
383 registerReceiver(mReceiver, filter);
384 mShouldFadeInScrim = true;
Sunny Goyala616d2b2017-06-12 13:54:01 -0700385
Sunny Goyal8392c822017-06-20 10:03:56 -0700386 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
387 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700388
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800389 mAppTransitionManager = LauncherAppTransitionManager.newInstance(this);
Jon Miranda7853bdb2018-01-22 17:32:30 -0800390
Sunny Goyal14b32402017-07-31 10:03:28 -0700391 if (mLauncherCallbacks != null) {
392 mLauncherCallbacks.onCreate(savedInstanceState);
393 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700394
395 TraceHelper.endSection("Launcher-onCreate");
Adam Cohen9211d422014-10-07 18:14:53 -0700396 }
397
Winson Chung90049122018-02-15 11:33:53 -0800398 public void updateRequestedOrientation() {
399 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
400 // we want the screen to auto-rotate based on the current orientation
401 setRequestedOrientation(mRotationEnabled
402 ? SCREEN_ORIENTATION_UNSPECIFIED : SCREEN_ORIENTATION_NOSENSOR);
403 }
404
Sunny Goyal7779d622015-06-11 16:18:39 -0700405 @Override
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800406 public void onConfigurationChanged(Configuration newConfig) {
407 int diff = newConfig.diff(mOldConfig);
408 if ((diff & (CONFIG_ORIENTATION | CONFIG_SCREEN_SIZE)) != 0) {
409 mUserEventDispatcher = null;
410 initDeviceProfile(mDeviceProfile.inv);
Sunny Goyal2bba1902018-02-27 12:20:50 -0800411 FileLog.d(TAG, "Config changed, my orientation=" +
412 getResources().getConfiguration().orientation +
413 ", new orientation=" + newConfig.orientation +
414 ", old orientation=" + mOldConfig.orientation +
415 ", isTransposed=" + mDeviceProfile.isVerticalBarLayout());
Sunny Goyalfde55052018-02-01 14:46:13 -0800416 dispatchDeviceProfileChanged();
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800417
418 getRootView().dispatchInsets();
419 getStateManager().reapplyState();
420
421 // TODO: We can probably avoid rebind when only screen size changed.
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800422 int currentPage = mWorkspace.getNextPage();
423 if (mModel.startLoader(currentPage)) {
424 mWorkspace.setCurrentPage(currentPage);
425 setWorkspaceLoading(true);
426 }
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800427 }
428
429 mOldConfig.setTo(newConfig);
430 super.onConfigurationChanged(newConfig);
431 }
432
433 private void initDeviceProfile(InvariantDeviceProfile idp) {
434 // Load configuration-specific DeviceProfile
435 mDeviceProfile = idp.getDeviceProfile(this);
436 if (isInMultiWindowModeCompat()) {
437 Display display = getWindowManager().getDefaultDisplay();
438 Point mwSize = new Point();
439 display.getSize(mwSize);
440 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
441 }
Sunny Goyal0e7724c2018-02-22 13:22:21 -0800442 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout());
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800443 }
444
445 @Override
Mario Bertschler27288382017-05-24 15:35:09 -0700446 public void onThemeChanged() {
447 recreate();
448 }
449
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700450 public LauncherStateManager getStateManager() {
451 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700452 }
453
Tonydc76f8e2018-02-02 21:45:39 -0600454 public LauncherAppTransitionManager getAppTransitionManager() {
455 return mAppTransitionManager;
456 }
457
Mario Bertschlera6936942017-05-31 14:48:19 -0700458 protected void overrideTheme(boolean isDark, boolean supportsDarkText) {
Mario Bertschler27288382017-05-24 15:35:09 -0700459 if (isDark) {
460 setTheme(R.style.LauncherThemeDark);
Mario Bertschlera6936942017-05-31 14:48:19 -0700461 } else if (supportsDarkText) {
462 setTheme(R.style.LauncherThemeDarkText);
Mario Bertschler27288382017-05-24 15:35:09 -0700463 }
464 }
465
466 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700467 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800468 return mLauncherView.findViewById(id);
469 }
470
471 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700472 public void onAppWidgetHostReset() {
473 if (mAppWidgetHost != null) {
474 mAppWidgetHost.startListening();
475 }
476 }
477
Adam Cohen9211d422014-10-07 18:14:53 -0700478 private LauncherCallbacks mLauncherCallbacks;
479
Sunny Goyal32554d12015-12-03 15:31:25 -0800480 /**
481 * Call this after onCreate to set or clear overlay.
482 */
483 public void setLauncherOverlay(LauncherOverlay overlay) {
484 if (overlay != null) {
485 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
486 }
487 mWorkspace.setLauncherOverlay(overlay);
488 }
489
Adam Cohen9211d422014-10-07 18:14:53 -0700490 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
491 mLauncherCallbacks = callbacks;
492 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700493 }
494
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700495 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700496 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700497 if (mLauncherCallbacks != null) {
498 mLauncherCallbacks.onLauncherProviderChange();
499 }
500 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700501
Hyunyoung Song3f471442015-04-08 19:01:34 -0700502 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700503 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
504 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700505 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700506 }
507
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000508 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700509 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
510 // This cast is safe as long as the id < 0x00FFFFFF
511 // Since we jail all the dynamically generated views, there should be no clashes
512 // with any other views.
513 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000514 }
515
Tony Wickham010d2552017-01-20 08:15:28 -0800516 public PopupDataProvider getPopupDataProvider() {
517 return mPopupDataProvider;
518 }
519
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000520 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700521 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
522 * a configuration step, this allows the proper animations to run after other transitions.
523 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700524 private long completeAdd(
525 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
526 long screenId = info.screenId;
527 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700528 // When the screen id represents an actual screen (as opposed to a rank) we make sure
529 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700530 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700531 }
Adam Cohendb364c32014-05-20 14:23:40 -0700532
Sunny Goyal2100c782016-08-22 16:00:03 -0700533 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800534 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700535 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700536 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800537 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700538 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700539 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700540 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700541 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700542 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700543 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700544 int widgetId = appWidgetId;
545 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700546 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700547 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700548 // Since the view was just bound, also launch the configure activity if needed
549 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
550 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800551 if (provider != null) {
552 new WidgetAddFlowHandler(provider)
553 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700554 }
555 }
556 break;
557 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800558 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700559
Adam Cohendb364c32014-05-20 14:23:40 -0700560 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800561 }
562
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800563 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700564 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700565 if (isWorkspaceLoading()) {
566 // process the result once the workspace has loaded.
567 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
568 return;
569 }
570 mPendingActivityResult = null;
571
Winson Chunge341d302013-08-16 14:31:00 -0700572 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700573 final PendingRequestArgs requestArgs = mPendingRequestArgs;
574 setWaitingForResult(null);
575 if (requestArgs == null) {
576 return;
577 }
578
579 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700580
Adam Cohenad4e15c2013-10-17 16:21:35 -0700581 Runnable exitSpringLoaded = new Runnable() {
582 @Override
583 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700584 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700585 }
586 };
587
Michael Jurka8b805b12012-04-18 14:23:14 -0700588 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700589 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700590 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700591 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
592 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700593 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700594 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700595 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700596 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700597 addAppWidgetImpl(
598 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800599 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700600 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700601 }
602 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200603 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700604 if (resultCode == RESULT_OK && isInState(OVERVIEW)) {
Tony Wickhame3054412015-09-09 09:05:25 -0700605 // User could have free-scrolled between pages before picking a wallpaper; make sure
606 // we move to the closest one now.
607 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700608 mStateManager.goToState(NORMAL, false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200609 }
610 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700611 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200612
Adam Cohened66b2b2012-01-23 17:28:51 -0800613 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700614 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700615
Adam Cohened66b2b2012-01-23 17:28:51 -0800616 // We have special handling for widgets
617 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800618 final int appWidgetId;
619 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
620 : -1;
621 if (widgetId < 0) {
622 appWidgetId = pendingAddWidgetId;
623 } else {
624 appWidgetId = widgetId;
625 }
626
Adam Cohenad4e15c2013-10-17 16:21:35 -0700627 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800628 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700629 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
630 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700631 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700632 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700633 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700634 @Override
635 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700636 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700637 }
638 };
Adam Cohendb364c32014-05-20 14:23:40 -0700639
Sunny Goyal2100c782016-08-22 16:00:03 -0700640 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
641 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
642 } else {
643 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
644 // When the screen id represents an actual screen (as opposed to a rank)
645 // we make sure that the drop page actually exists.
646 requestArgs.screenId =
647 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700648 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700649 final CellLayout dropLayout =
650 mWorkspace.getScreenWithId(requestArgs.screenId);
651
652 dropLayout.setDropPending(true);
653 final Runnable onComplete = new Runnable() {
654 @Override
655 public void run() {
656 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
657 dropLayout.setDropPending(false);
658 }
659 };
660 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
661 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700662 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800663 return;
664 }
665
Sunny Goyald478c832016-04-01 12:04:16 -0700666 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
667 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700668 if (resultCode == RESULT_OK) {
669 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700670 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700671 }
672 // Leave the widget in the pending state if the user canceled the configure.
673 return;
674 }
675
Sunny Goyalaad90582015-07-09 14:22:50 -0700676 if (requestCode == REQUEST_CREATE_SHORTCUT) {
677 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700678 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
679 completeAdd(requestCode, data, -1, requestArgs);
680 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
681 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
682
Sunny Goyalaad90582015-07-09 14:22:50 -0700683 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700684 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
685 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800686 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800687 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800688 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800689 }
690
691 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700692 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800693 final int requestCode, final int resultCode, final Intent data) {
694 handleActivityResult(requestCode, resultCode, data);
695 if (mLauncherCallbacks != null) {
696 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
697 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800698 }
699
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700700 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700701 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600702 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700703 PendingRequestArgs pendingArgs = mPendingRequestArgs;
704 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
705 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
706 setWaitingForResult(null);
707
Sunny Goyal28c6b962015-10-12 11:42:05 -0700708 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700709 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700710 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700711 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700712 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700713 Intent intent = pendingArgs.getPendingIntent();
714
Sunny Goyal28c6b962015-10-12 11:42:05 -0700715 if (grantResults.length > 0
716 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700717 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700718 } else {
719 // TODO: Show a snack bar with link to settings
720 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700721 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700722 }
723 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600724 if (mLauncherCallbacks != null) {
725 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
726 grantResults);
727 }
728 }
729
Adam Cohendb364c32014-05-20 14:23:40 -0700730 /**
731 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
732 *
733 * @param screenId the screen id to check
734 * @return the new screen, or screenId if it exists
735 */
736 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800737 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700738 if (dropLayout == null) {
739 // it's possible that the add screen was removed because it was
740 // empty and a re-bind occurred
741 mWorkspace.addExtraEmptyScreen();
742 return mWorkspace.commitExtraEmptyScreen();
743 } else {
744 return screenId;
745 }
746 }
747
Sunny Goyal2100c782016-08-22 16:00:03 -0700748 @Thunk void completeTwoStageWidgetDrop(
749 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
750 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800751 Runnable onCompleteRunnable = null;
752 int animationType = 0;
753
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700754 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800755 if (resultCode == RESULT_OK) {
756 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
757 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800758 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700759 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800760 onCompleteRunnable = new Runnable() {
761 @Override
762 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700763 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700764 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800765 }
766 };
767 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800768 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800769 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800770 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700771 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700772 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700773 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
774 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700775 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700776 // The animated view may be null in the case of a rotation during widget configuration
777 onCompleteRunnable.run();
778 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800779 }
780
781 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700782 protected void onStop() {
783 super.onStop();
784 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700785
786 if (mLauncherCallbacks != null) {
787 mLauncherCallbacks.onStop();
788 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800789 mAppWidgetHost.setListenIfResumed(false);
Tony Wickham010d2552017-01-20 08:15:28 -0800790
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700791 if (!mAppLaunchSuccess) {
792 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -0800793 mStateManager.getState().containerType, -1);
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700794 }
Tony Wickham010d2552017-01-20 08:15:28 -0800795 NotificationListener.removeNotificationsChangedListener();
Sunny Goyalaf161fd2018-02-05 16:34:57 -0800796 getStateManager().moveToRestState();
Michael Jurkacd496d72013-04-11 11:32:45 -0700797 }
798
799 @Override
800 protected void onStart() {
801 super.onStart();
802 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700803
Sunny Goyal60876ac2018-02-20 14:21:20 -0800804 if (mOnStartCallback != null) {
805 mOnStartCallback.onLauncherStart(this);
806 mOnStartCallback = null;
807 }
Adam Cohen9211d422014-10-07 18:14:53 -0700808 if (mLauncherCallbacks != null) {
809 mLauncherCallbacks.onStart();
810 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800811 mAppWidgetHost.setListenIfResumed(true);
Sunny Goyalcac002d2018-01-04 09:51:45 -0800812 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
Jon Miranda2d89ea82017-05-04 11:47:53 -0700813
Jon Mirandade43a712018-01-18 11:35:10 -0800814 if (mShouldFadeInScrim && mLauncherView.getBackground() != null) {
Jon Miranda2d89ea82017-05-04 11:47:53 -0700815 if (mScrimAnimator != null) {
816 mScrimAnimator.cancel();
817 }
Jon Mirandade43a712018-01-18 11:35:10 -0800818 mLauncherView.getBackground().setAlpha(0);
819 mScrimAnimator = ObjectAnimator.ofInt(mLauncherView.getBackground(),
Jon Miranda2d89ea82017-05-04 11:47:53 -0700820 LauncherAnimUtils.DRAWABLE_ALPHA, 0, 255);
821 mScrimAnimator.addListener(new AnimatorListenerAdapter() {
822 @Override
823 public void onAnimationEnd(Animator animation) {
824 mScrimAnimator = null;
825 }
826 });
827 mScrimAnimator.setDuration(600);
828 mScrimAnimator.setStartDelay(getWindow().getTransitionBackgroundFadeDuration());
829 mScrimAnimator.start();
830 }
Jon Miranda7fda2852017-07-19 16:07:01 -0700831 mShouldFadeInScrim = false;
Winson Chung62a70be2018-02-23 15:10:05 -0800832 UiFactory.onStart(this);
Michael Jurkacd496d72013-04-11 11:32:45 -0700833 }
834
835 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800836 protected void onResume() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700837 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800838 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700839 TraceHelper.partitionSection("ON_RESUME", "superCall");
840
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700841 mAppLaunchSuccess = false;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700842 getUserEventDispatcher().resetElapsedSessionMillis();
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700843 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700844 // Process any items that were added while Launcher was away.
845 InstallShortcutReceiver.disableAndFlushInstallQueue(
846 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700847
Sunny Goyala474a9b2017-05-04 16:47:11 -0700848 // Refresh shortcuts if the permission changed.
849 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700850
Sunny Goyal7ede6112017-12-05 15:11:21 -0800851 DiscoveryBounce.showIfNeeded(this);
Adam Cohen9211d422014-10-07 18:14:53 -0700852 if (mLauncherCallbacks != null) {
853 mLauncherCallbacks.onResume();
854 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800855
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700856 TraceHelper.endSection("ON_RESUME");
Adam Cohen06dff352012-06-01 17:17:08 -0700857 }
858
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800859 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700860 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700861 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700862 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700863
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700864 super.onPause();
Joe Onorato24b6fd82009-11-12 13:47:09 -0800865 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700866 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700867
Adam Cohen9211d422014-10-07 18:14:53 -0700868 if (mLauncherCallbacks != null) {
869 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700870 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700871 }
872
Winson Chungd7823942018-02-16 03:23:47 +0000873 @Override
874 public void onWindowFocusChanged(boolean hasFocus) {
875 super.onWindowFocusChanged(hasFocus);
876 mStateManager.onWindowFocusChanged();
877 }
878
Adam Cohenc2d6e892014-10-16 09:49:24 -0700879 public interface LauncherOverlay {
880
881 /**
882 * Touch interaction leading to overscroll has begun
883 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700884 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700885
886 /**
887 * Touch interaction related to overscroll has ended
888 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700889 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700890
891 /**
892 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
893 * screen (or in the case of RTL, the rightmost screen).
894 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700895 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700896
897 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800898 * Called when the launcher is ready to use the overlay
899 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700900 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700901 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700902 }
903
904 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700905 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700906 }
907
908 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
909
Sunny Goyalc86df472016-02-25 09:19:38 -0800910 public void onScrollChanged(float progress) {
911 if (mWorkspace != null) {
912 mWorkspace.onOverlayScrollChanged(progress);
913 }
914 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700915 }
916
Sunny Goyal16764582017-11-06 16:00:34 -0800917 public boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -0700918 if (mLauncherCallbacks != null) {
919 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700920 } else {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800921 // On O and above we there is always some setting present settings (add icon to
922 // home screen or icon badging). On earlier APIs we will have the allow rotation
923 // setting, on devices with a locked orientation,
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700924 return Utilities.ATLEAST_OREO || !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -0700925 }
Jorim Jaggid017f882014-01-14 17:08:48 -0800926 }
927
Sunny Goyalf9403d92017-10-18 10:55:56 -0700928 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -0700929 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700930 }
931
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800932 /**
933 * Restores the previous state, if it exists.
934 *
935 * @param savedState The previous state.
936 */
937 private void restoreState(Bundle savedState) {
938 if (savedState == null) {
939 return;
940 }
941
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700942 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -0700943 LauncherState[] stateValues = LauncherState.values();
944 LauncherState state = stateValues[stateOrdinal];
Sunny Goyalbc683e92017-12-06 10:25:07 -0800945 if (!state.disableRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700946 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800947 }
948
Sunny Goyal2100c782016-08-22 16:00:03 -0700949 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
950 if (requestArgs != null) {
951 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800952 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700953
954 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700955
956 SparseArray<Parcelable> widgetsState =
957 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
958 if (widgetsState != null) {
959 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
960 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800961 }
962
963 /**
964 * Finds all the views we need and configure them properly.
965 */
966 private void setupViews() {
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800967 mDragLayer = findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700968 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -0700969 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700970 mWorkspace.initParentViews(mDragLayer);
Sunny Goyal16764582017-11-06 16:00:34 -0800971 mOverviewPanel = findViewById(R.id.overview_panel);
Craig Mautner360310b2012-10-26 15:13:08 -0700972
Sunny Goyal784f9c32016-03-23 16:56:54 -0700973 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
974 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
975 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800976
Winson Chung4c98d922011-05-31 16:50:48 -0700977 // Setup the drag layer
Sunny Goyal7ede6112017-12-05 15:11:21 -0800978 mDragLayer.setup(this, mDragController);
Winson Chung4c98d922011-05-31 16:50:48 -0700979
Winson Chung3d503fb2011-07-13 17:25:49 -0700980 // Setup the hotseat
981 mHotseat = (Hotseat) findViewById(R.id.hotseat);
982 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -0700983 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -0700984 }
985
Winson Chung4c98d922011-05-31 16:50:48 -0700986 // Setup the workspace
987 mWorkspace.setHapticFeedbackEnabled(false);
988 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700989 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -0700990 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
991 // default state, otherwise we will update to the wrong offsets in RTL
992 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700993 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700994 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700995
Tony Wickham34d2c912015-09-11 09:27:58 -0700996 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700997 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700998
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700999 // Setup Apps
1000 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001001
Winson Chung3d503fb2011-07-13 17:25:49 -07001002 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001003 mDragController.setMoveTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001004 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001005
Hyunyoung Songd725f642017-08-17 22:26:35 -07001006 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001007 }
1008
1009 /**
1010 * Creates a view representing a shortcut.
1011 *
1012 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001013 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001014 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001015 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001016 }
1017
1018 /**
1019 * Creates a view representing a shortcut inflated from the specified resource.
1020 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001021 * @param parent The group the shortcut belongs to.
1022 * @param info The data structure describing the shortcut.
1023 *
1024 * @return A View inflated from layoutResId.
1025 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001026 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -08001027 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
1028 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001029 favorite.applyFromShortcutInfo(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001030 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001031 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001032 return favorite;
1033 }
1034
1035 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -07001036 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001037 *
1038 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001039 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001040 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001041 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001042 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
1043 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001044 return;
1045 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001046
Jon Mirandac476d6e2017-05-04 16:30:01 -07001047 int[] cellXY = mTmpAddItemCellCoordinates;
1048 CellLayout layout = getCellLayout(container, screenId);
1049
Sunny Goyal782f0c92017-01-19 10:27:54 -08001050 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001051 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001052 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
1053 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001054 }
1055
1056 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001057 // Legacy shortcuts are only supported for primary profile.
1058 info = Process.myUserHandle().equals(args.user)
1059 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001060
Sunny Goyalb57645f2017-03-20 13:57:28 -07001061 if (info == null) {
1062 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1063 return;
1064 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001065 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001066 // The app is trying to add a shortcut without sufficient permissions
1067 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1068 return;
1069 }
1070 }
1071
Jon Mirandac476d6e2017-05-04 16:30:01 -07001072 if (container < 0) {
1073 // Adding a shortcut to the Workspace.
1074 final View view = createShortcut(info);
1075 boolean foundCellSpan = false;
1076 // First we check if we already know the exact location where we want to add this item.
1077 if (cellX >= 0 && cellY >= 0) {
1078 cellXY[0] = cellX;
1079 cellXY[1] = cellY;
1080 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001081
Jon Mirandac476d6e2017-05-04 16:30:01 -07001082 // If appropriate, either create a folder or add to an existing folder
1083 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001084 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001085 return;
1086 }
1087 DragObject dragObject = new DragObject();
1088 dragObject.dragInfo = info;
1089 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1090 true)) {
1091 return;
1092 }
1093 } else {
1094 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1095 }
1096
1097 if (!foundCellSpan) {
1098 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001099 return;
1100 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001101
1102 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1103 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001104 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001105 // Adding a shortcut to a Folder.
Sunny Goyale29897f52017-07-20 10:09:42 -07001106 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001107 if (folderIcon != null) {
1108 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1109 folderInfo.add(info, args.rank, false);
1110 } else {
Sunny Goyale29897f52017-07-20 10:09:42 -07001111 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001112 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001113 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001114 }
1115
Sunny Goyale29897f52017-07-20 10:09:42 -07001116 public FolderIcon findFolderIcon(final long folderIconId) {
1117 return (FolderIcon) mWorkspace.getFirstMatch(new ItemOperator() {
1118 @Override
1119 public boolean evaluate(ItemInfo info, View view) {
1120 return info != null && info.id == folderIconId;
1121 }
1122 });
1123 }
1124
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001125 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001126 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001127 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001128 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001129 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001130 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001131 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1132
Adam Cohened66b2b2012-01-23 17:28:51 -08001133 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001134 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001135 }
Romain Guycbb89e42009-06-08 15:52:54 -07001136
Adam Cohen59400422014-03-05 18:07:04 -08001137 LauncherAppWidgetInfo launcherInfo;
1138 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001139 launcherInfo.spanX = itemInfo.spanX;
1140 launcherInfo.spanY = itemInfo.spanY;
1141 launcherInfo.minSpanX = itemInfo.minSpanX;
1142 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001143 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001144
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001145 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001146 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001147
Sunny Goyal2100c782016-08-22 16:00:03 -07001148 if (hostView == null) {
1149 // Perform actual inflation because we're live
1150 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001151 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001152 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301153 prepareAppWidget(hostView, launcherInfo);
1154 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001155 }
Romain Guycbb89e42009-06-08 15:52:54 -07001156
Sunny Goyald5462aa2016-11-22 16:52:39 +05301157 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001158 hostView.setTag(item);
1159 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001160 hostView.setFocusable(true);
1161 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001162 }
1163
Adam Cohended9f8d2010-11-03 13:25:16 -07001164 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1165 @Override
1166 public void onReceive(Context context, Intent intent) {
1167 final String action = intent.getAction();
1168 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001169 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001170 // processing a multi-step drop
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001171 if (mAppsView != null && mPendingRequestArgs == null) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001172 mStateManager.goToState(NORMAL);
Winson Chung785d2eb2011-04-14 16:08:02 -07001173 }
Jon Miranda7fda2852017-07-19 16:07:01 -07001174 mShouldFadeInScrim = true;
1175 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1176 // ACTION_USER_PRESENT is sent after onStart/onResume. This covers the case where
1177 // the user unlocked and the Launcher is not in the foreground.
1178 mShouldFadeInScrim = false;
Adam Cohended9f8d2010-11-03 13:25:16 -07001179 }
1180 }
1181 };
1182
Tony Wickham010d2552017-01-20 08:15:28 -08001183 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
Sunny Goyal29947f02017-12-18 13:49:44 -08001184 mWorkspace.updateIconBadges(updatedBadges);
Sunny Goyal60180b02018-02-07 12:56:30 -08001185 mAppsView.getAppsStore().updateIconBadges(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001186
Sunny Goyal29947f02017-12-18 13:49:44 -08001187 PopupContainerWithArrow popup = PopupContainerWithArrow.getOpen(Launcher.this);
1188 if (popup != null) {
1189 popup.updateNotificationHeader(updatedBadges);
Tony Wickham010d2552017-01-20 08:15:28 -08001190 }
1191 }
1192
Adam Cohended9f8d2010-11-03 13:25:16 -07001193 @Override
1194 public void onAttachedToWindow() {
1195 super.onAttachedToWindow();
1196
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001197 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyalc86df472016-02-25 09:19:38 -08001198 if (mLauncherCallbacks != null) {
1199 mLauncherCallbacks.onAttachedToWindow();
1200 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001201 }
1202
1203 @Override
1204 public void onDetachedFromWindow() {
1205 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001206
1207 if (mLauncherCallbacks != null) {
1208 mLauncherCallbacks.onDetachedFromWindow();
1209 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001210 }
1211
Sunny Goyal3bac2c22018-02-07 14:44:05 -08001212 public void onQuickstepGestureStarted(boolean isVisible) {
1213 if (mLauncherCallbacks != null) {
1214 mLauncherCallbacks.onQuickstepGestureStarted(isVisible);
1215 }
1216 }
1217
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001218 public AllAppsTransitionController getAllAppsController() {
1219 return mAllAppsController;
1220 }
1221
Winson Chung1a341002018-01-17 10:00:23 -08001222 public LauncherRootView getRootView() {
1223 return (LauncherRootView) mLauncherView;
1224 }
1225
Winson Chunga6945242014-01-08 14:04:34 -08001226 public DragLayer getDragLayer() {
1227 return mDragLayer;
1228 }
1229
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001230 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001231 return mAppsView;
1232 }
1233
Winson Chunga6945242014-01-08 14:04:34 -08001234 public Workspace getWorkspace() {
1235 return mWorkspace;
1236 }
1237
1238 public Hotseat getHotseat() {
1239 return mHotseat;
1240 }
1241
Winson Chung8ae41982017-11-10 11:42:13 -08001242 public <T extends ViewGroup> T getOverviewPanel() {
1243 return (T) mOverviewPanel;
Winson Chunga6945242014-01-08 14:04:34 -08001244 }
1245
Sunny Goyal47328fd2016-05-25 18:56:41 -07001246 public DropTargetBar getDropTargetBar() {
1247 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001248 }
1249
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001250 public LauncherAppWidgetHost getAppWidgetHost() {
1251 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001252 }
Romain Guycbb89e42009-06-08 15:52:54 -07001253
Winson Chunga9abd0e2010-10-27 17:18:37 -07001254 public LauncherModel getModel() {
1255 return mModel;
1256 }
1257
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001258 public ModelWriter getModelWriter() {
1259 return mModelWriter;
1260 }
1261
Adam Cohen79d90c52016-04-22 13:29:20 -07001262 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001263 return mSharedPrefs;
1264 }
1265
Sunny Goyalf8d56fc2018-01-31 15:18:11 -08001266 public int getOrientation() { return mOldConfig.orientation; }
Jon Miranda6bed3502017-09-19 14:43:17 -07001267
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001268 @Override
1269 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001270 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001271 super.onNewIntent(intent);
1272
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001273 boolean alreadyOnHome = hasWindowFocus() && ((intent.getFlags() &
Winson15f8b172015-08-19 11:02:39 -07001274 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1275 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001276
1277 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001278 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001279 && AbstractFloatingView.getTopOpenView(this) == null;
Winson15f8b172015-08-19 11:02:39 -07001280 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001281 boolean internalStateHandled = InternalStateHandler
Sunny Goyal8b264562018-02-03 23:00:13 -08001282 .handleNewIntent(this, intent, isStarted());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001283
Winson15f8b172015-08-19 11:02:39 -07001284 if (isActionMain) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001285 if (!internalStateHandled) {
1286 // Note: There should be at most one log per method call. This is enforced
1287 // implicitly by using if-else statements.
1288 UserEventDispatcher ued = getUserEventDispatcher();
1289 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
1290 if (topOpenView != null) {
1291 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
1292 } else if (alreadyOnHome) {
1293 Target target = newContainerTarget(mStateManager.getState().containerType);
1294 target.pageIndex = mWorkspace.getCurrentPage();
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001295 ued.logActionCommand(Action.Command.HOME_INTENT, target,
1296 newContainerTarget(ContainerType.WORKSPACE));
Sunny Goyald3864fa2017-11-14 15:06:36 -08001297 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001298
Sunny Goyald3864fa2017-11-14 15:06:36 -08001299 // In all these cases, only animate if we're already on home
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001300 AbstractFloatingView.closeAllOpenViews(this, isStarted());
Jon Mirandafeba90f2016-10-06 10:53:29 -07001301
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001302 mStateManager.goToState(NORMAL);
Sunny Goyald3864fa2017-11-14 15:06:36 -08001303
1304 // Reset the apps view
1305 if (!alreadyOnHome && mAppsView != null) {
1306 mAppsView.reset();
1307 }
1308
1309 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()) {
1310 mWorkspace.post(mWorkspace::moveToDefaultScreen);
1311 }
1312 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001313
1314 final View v = getWindow().peekDecorView();
1315 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001316 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001317 }
1318
Adam Cohen9211d422014-10-07 18:14:53 -07001319 if (mLauncherCallbacks != null) {
1320 mLauncherCallbacks.onHomeIntent();
1321 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001322 }
Winson15f8b172015-08-19 11:02:39 -07001323
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001324 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001325 }
1326
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001327 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001328 public void onRestoreInstanceState(Bundle state) {
1329 super.onRestoreInstanceState(state);
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001330 mWorkspace.restoreInstanceStateForChild(mSynchronouslyBoundPage);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001331 }
1332
1333 @Override
1334 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001335 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001336 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001337
Adam Cohen21cd0022013-10-09 18:57:02 -07001338 }
Sunny Goyalea609262017-10-25 15:47:38 -07001339 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001340
1341
1342 AbstractFloatingView widgets = AbstractFloatingView
1343 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1344 if (widgets != null) {
1345 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1346 widgets.saveHierarchyState(widgetsState);
1347 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1348 } else {
1349 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1350 }
1351
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001352 // We close any open folders and shortcut containers since they will not be re-opened,
1353 // and we need to make sure this state is reflected.
1354 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001355
Sunny Goyal2100c782016-08-22 16:00:03 -07001356 if (mPendingRequestArgs != null) {
1357 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1358 }
1359 if (mPendingActivityResult != null) {
1360 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001361 }
1362
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001363 super.onSaveInstanceState(outState);
1364
Adam Cohen9211d422014-10-07 18:14:53 -07001365 if (mLauncherCallbacks != null) {
1366 mLauncherCallbacks.onSaveInstanceState(outState);
1367 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001368 }
1369
1370 @Override
1371 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001372 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001373
Sunny Goyal5cc7bbd2017-06-12 09:50:39 -07001374 unregisterReceiver(mReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001375 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001376
Winson Chungcd2b0142011-06-08 16:02:26 -07001377 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001378 // It's possible to receive onDestroy after a new Launcher activity has
1379 // been created. In this case, don't interfere with the new Launcher.
1380 if (mModel.isCurrentCallbacks(this)) {
1381 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001382 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001383 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001384
Sunny Goyal745bad92016-05-02 10:54:12 -07001385 if (mRotationPrefChangeHandler != null) {
1386 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1387 }
1388
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001389 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001390 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001391 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001392 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001393 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001394
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001395 TextKeyListener.getInstance().release();
Mario Bertschler27288382017-05-24 15:35:09 -07001396 WallpaperColorInfo.getInstance(this).setOnThemeChangeListener(null);
1397
Michael Jurka2ecf9952012-06-18 12:52:28 -07001398 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001399
Tony2917a8b2017-07-31 23:29:42 -07001400 clearPendingBinds();
1401
Adam Cohen9211d422014-10-07 18:14:53 -07001402 if (mLauncherCallbacks != null) {
1403 mLauncherCallbacks.onDestroy();
1404 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001405 }
1406
Sunny Goyalae502842016-06-17 08:43:56 -07001407 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1408 return mAccessibilityDelegate;
1409 }
1410
Adam Cohena9cf38f2011-05-02 15:36:58 -07001411 public DragController getDragController() {
1412 return mDragController;
1413 }
1414
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001415 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001416 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001417 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001418 }
1419
1420 @Override
1421 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1422 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001423 try {
1424 super.startIntentSenderForResult(intent, requestCode,
1425 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1426 } catch (IntentSender.SendIntentException e) {
1427 throw new ActivityNotFoundException();
1428 }
1429 }
1430
Winson Chung70d72102011-08-12 11:24:35 -07001431 /**
1432 * Indicates that we want global search for this activity by setting the globalSearch
1433 * argument for {@link #startSearch} to true.
1434 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001435 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001436 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001437 Bundle appSearchData, boolean globalSearch) {
1438 if (appSearchData == null) {
1439 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001440 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001441 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001442
Sunny Goyal6f28e712016-09-13 14:19:29 -07001443 if (mLauncherCallbacks == null ||
1444 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1445 // Starting search from the callbacks failed. Start the default global search.
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001446 super.startSearch(initialQuery, selectInitialQuery, appSearchData, true);
Ian Parkinson047036e2014-09-01 15:40:53 +01001447 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001448
1449 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001450 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001451 }
1452
Joe Onorato9c1289c2009-08-17 11:03:03 -04001453 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001454 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001455 }
1456
Adam Cohen517a7f52014-03-01 12:12:59 -08001457 public boolean isWorkspaceLoading() {
1458 return mWorkspaceLoading;
1459 }
1460
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001461 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001462 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001463 }
1464
Sunny Goyal04a324a2017-01-20 21:08:59 -08001465 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001466 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001467 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001468
Sunny Goyal2100c782016-08-22 16:00:03 -07001469 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001470 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001471 if (LOGD) {
1472 Log.d(TAG, "Adding widget from drop");
1473 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001474 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001475 }
1476
Sunny Goyal2100c782016-08-22 16:00:03 -07001477 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001478 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1479 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1480 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001481
Adam Cohenad4e15c2013-10-17 16:21:35 -07001482 Runnable onComplete = new Runnable() {
1483 @Override
1484 public void run() {
1485 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001486 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001487 }
1488 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001489 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001490 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001491 }
1492 }
Romain Guycbb89e42009-06-08 15:52:54 -07001493
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001494 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1495 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001496 info.container = container;
1497 info.screenId = screenId;
1498 if (cell != null) {
1499 info.cellX = cell[0];
1500 info.cellY = cell[1];
1501 }
1502 info.spanX = spanX;
1503 info.spanY = spanY;
1504
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001505 switch (info.itemType) {
1506 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1507 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001508 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001509 break;
1510 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001511 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001512 break;
1513 default:
1514 throw new IllegalStateException("Unknown item type: " + info.itemType);
1515 }
1516 }
1517
Adam Cohenfbba09b2011-07-18 21:43:05 -07001518 /**
1519 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001520 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001521 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001522 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1523 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001524 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1525 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1526 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001527 }
1528
Adam Cohenfbba09b2011-07-18 21:43:05 -07001529 /**
1530 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001531 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001532 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001533 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001534 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001535 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001536 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001537 // In the case where we've prebound the widget, we remove it from the DragLayer
1538 if (LOGD) {
1539 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1540 }
1541 getDragLayer().removeView(hostView);
1542
Adam Cohened66b2b2012-01-23 17:28:51 -08001543 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001544 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001545
1546 // Clear the boundWidget so that it doesn't get destroyed.
1547 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001548 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001549 // In this case, we either need to start an activity to get permission to bind
1550 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001551 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1552 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1553 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1554 this, info.componentName);
1555 } else {
1556 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1557 }
Adam Cohendd70d662012-10-04 16:53:44 -07001558 Bundle options = info.bindOptions;
1559
Sunny Goyalffe83f12014-08-14 17:39:34 -07001560 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1561 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001562 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001563 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001564 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001565 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001566 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001567 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001568 }
1569
Adam Cohendcd297f2013-06-18 13:13:40 -07001570 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001571 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001572 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001573 folderInfo.title = getText(R.string.folder_name);
1574
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001575 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001576 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001577
1578 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001579 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301580 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001581 // Force measure the new folder icon
1582 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1583 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001584 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001585 }
Romain Guycbb89e42009-06-08 15:52:54 -07001586
Winsonc0b52fe2015-09-09 16:38:15 -07001587 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001588 * Unbinds the view for the specified item, and removes the item and all its children.
1589 *
1590 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001591 * @param itemInfo the {@link ItemInfo} for this view.
1592 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001593 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001594 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001595 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001596 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001597 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1598 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001599 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001600 } else {
1601 mWorkspace.removeWorkspaceItem(v);
1602 }
Winsonc0b52fe2015-09-09 16:38:15 -07001603 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001604 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001605 }
1606 } else if (itemInfo instanceof FolderInfo) {
1607 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001608 if (v instanceof FolderIcon) {
1609 ((FolderIcon) v).removeListeners();
1610 }
Winsonc0b52fe2015-09-09 16:38:15 -07001611 mWorkspace.removeWorkspaceItem(v);
1612 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001613 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001614 }
1615 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1616 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001617 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001618 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07001619 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001620 }
1621 } else {
1622 return false;
1623 }
1624 return true;
1625 }
1626
1627 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07001628 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07001629 */
Sunny Goyal56c73602015-09-25 12:55:01 -07001630 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07001631 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07001632 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07001633 // Deleting an app widget ID is a void call but writes to disk before returning
1634 // to the caller...
1635 new AsyncTask<Void, Void, Void>() {
1636 public Void doInBackground(Void ... args) {
1637 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
1638 return null;
1639 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07001640 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07001641 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001642 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001643 }
1644
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001645 @Override
1646 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001647 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001648 }
1649
Joe Onorato88ec0992009-11-19 13:16:06 -08001650 @Override
1651 public void onBackPressed() {
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08001652 if (finishAutoCancelActionMode()) {
1653 return;
1654 }
Adam Cohen9211d422014-10-07 18:14:53 -07001655 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1656 return;
1657 }
1658
Sunny Goyal45478022015-06-08 16:52:41 -07001659 if (mDragController.isDragging()) {
1660 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001661 return;
1662 }
1663
Jon Mirandafeba90f2016-10-06 10:53:29 -07001664 // Note: There should be at most one log per method call. This is enforced implicitly
1665 // by using if-else statements.
1666 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001667 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
1668 if (topView != null) {
Sunny Goyal37920962017-09-28 13:43:24 -07001669 topView.onBackPressed();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001670 } else if (!isInState(NORMAL)) {
Sunny Goyal49bcf342018-01-11 13:59:53 -08001671 LauncherState lastState = mStateManager.getLastState();
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001672 ued.logActionCommand(Action.Command.BACK, mStateManager.getState().containerType,
1673 lastState.containerType);
Sunny Goyal49bcf342018-01-11 13:59:53 -08001674 mStateManager.goToState(lastState);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001675 } else {
1676 // Back button is a no-op here, but give at least some feedback for the button press
1677 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001678 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001679 }
1680
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001681 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001682 * Launches the intent referred by the clicked shortcut.
1683 *
1684 * @param v The view representing the clicked shortcut.
1685 */
1686 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001687 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1688 // view has detached (it's possible for this to happen if the view is removed mid touch).
1689 if (v.getWindowToken() == null) {
1690 return;
1691 }
1692
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001693 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001694 return;
1695 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001696
Adam Cohen1697b792013-09-17 19:08:21 -07001697 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001698 if (isInState(OVERVIEW)) {
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001699 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Touch.TAP,
Jon Miranda2e61fba2017-02-24 09:12:59 -08001700 LauncherLogProto.Action.Direction.NONE,
1701 LauncherLogProto.ContainerType.OVERVIEW, mWorkspace.getCurrentPage());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001702 mStateManager.goToState(NORMAL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001703 }
1704 return;
1705 }
1706
1707 if (v instanceof CellLayout) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001708 if (isInState(OVERVIEW)) {
Jon Mirandac6cf4932017-02-07 17:12:36 -08001709 int page = mWorkspace.indexOfChild(v);
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001710 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Touch.TAP,
Jon Mirandac6cf4932017-02-07 17:12:36 -08001711 LauncherLogProto.Action.Direction.NONE,
1712 LauncherLogProto.ContainerType.OVERVIEW, page);
1713 mWorkspace.snapToPageFromOverView(page);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001714 mStateManager.goToState(NORMAL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001715 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07001716 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001717 }
1718
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001719 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001720 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001721 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001722 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001723 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001724 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001725 }
Sunny Goyal508da152014-08-14 10:53:27 -07001726 } else if (tag instanceof AppInfo) {
1727 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07001728 } else if (tag instanceof LauncherAppWidgetInfo) {
1729 if (v instanceof PendingAppWidgetHostView) {
1730 onClickPendingWidget((PendingAppWidgetHostView) v);
1731 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001732 }
1733 }
1734
Sunny Goyal70660032015-05-14 00:07:08 -07001735 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07001736 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07001737 return false;
1738 }
1739
Michael Jurkaaf442092010-06-10 17:01:57 -07001740 /**
Sunny Goyalff572272014-07-23 13:58:07 -07001741 * Event handler for the app widget view which has not fully restored.
1742 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001743 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07001744 if (mIsSafeModeEnabled) {
1745 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
1746 return;
1747 }
1748
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001749 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07001750 if (v.isReadyForClickSetup()) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001751 LauncherAppWidgetProviderInfo appWidgetInfo =
1752 mAppWidgetManager.findProvider(info.providerName, info.user);
1753 if (appWidgetInfo == null) {
1754 return;
1755 }
1756 WidgetAddFlowHandler addFlowHandler = new WidgetAddFlowHandler(appWidgetInfo);
1757
Sunny Goyald478c832016-04-01 12:04:16 -07001758 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
1759 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
1760 // This should not happen, as we make sure that an Id is allocated during bind.
1761 return;
1762 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001763 addFlowHandler.startBindFlow(this, info.appWidgetId, info,
1764 REQUEST_BIND_PENDING_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -07001765 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001766 addFlowHandler.startConfigActivity(this, info, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07001767 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001768 } else {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001769 final String packageName = info.providerName.getPackageName();
Sunny Goyale6e72002017-01-12 16:55:36 -08001770 onClickPendingAppItem(v, packageName, info.installProgress >= 0);
Sunny Goyalff572272014-07-23 13:58:07 -07001771 }
1772 }
1773
Sunny Goyale6e72002017-01-12 16:55:36 -08001774 private void onClickPendingAppItem(final View v, final String packageName,
1775 boolean downloadStarted) {
1776 if (downloadStarted) {
1777 // If the download has started, simply direct to the market app.
1778 startMarketIntentForPackage(v, packageName);
1779 return;
1780 }
Sunny Goyale03b8122014-10-08 09:55:24 -07001781 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001782 .setTitle(R.string.abandoned_promises_title)
1783 .setMessage(R.string.abandoned_promise_explanation)
Sunny Goyale6e72002017-01-12 16:55:36 -08001784 .setPositiveButton(R.string.abandoned_search, new DialogInterface.OnClickListener() {
1785 @Override
1786 public void onClick(DialogInterface dialogInterface, int i) {
1787 startMarketIntentForPackage(v, packageName);
1788 }
1789 })
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001790 .setNeutralButton(R.string.abandoned_clean_this,
1791 new DialogInterface.OnClickListener() {
1792 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001793 final UserHandle user = Process.myUserHandle();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001794 mWorkspace.removeAbandonedPromise(packageName, user);
1795 }
1796 })
1797 .create().show();
Sunny Goyale6e72002017-01-12 16:55:36 -08001798 }
1799
1800 private void startMarketIntentForPackage(View v, String packageName) {
1801 ItemInfo item = (ItemInfo) v.getTag();
Sunny Goyal67e75e22018-01-22 15:14:00 -08001802 Intent intent = new PackageManagerHelper(v.getContext()).getMarketIntent(packageName);
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001803 startActivitySafely(v, intent, item);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001804 }
1805
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001806 /**
1807 * Event handler for an app shortcut click.
1808 *
1809 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
1810 */
Chris Wren40c5ed32014-06-24 18:24:23 -04001811 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001812 if (LOGD) Log.d(TAG, "onClickAppShortcut");
1813 Object tag = v.getTag();
1814 if (!(tag instanceof ShortcutInfo)) {
1815 throw new IllegalArgumentException("Input must be a Shortcut");
1816 }
1817
1818 // Open shortcut
1819 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001820
Sunny Goyal076839c2017-10-30 13:52:20 -07001821 if (shortcut.isDisabled()) {
Tony Makf6308652017-12-02 16:56:22 +00001822 final int disabledFlags = shortcut.runtimeStatusFlags & ShortcutInfo.FLAG_DISABLED_MASK;
1823 if ((disabledFlags &
Sunny Goyal076839c2017-10-30 13:52:20 -07001824 ~FLAG_DISABLED_SUSPENDED &
1825 ~FLAG_DISABLED_QUIET_USER) == 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001826 // If the app is only disabled because of the above flags, launch activity anyway.
1827 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00001828 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07001829 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
1830 // Use a message specific to this shortcut, if it has one.
1831 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
1832 return;
1833 }
1834 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00001835 int error = R.string.activity_not_available;
Sunny Goyal076839c2017-10-30 13:52:20 -07001836 if ((shortcut.runtimeStatusFlags & FLAG_DISABLED_SAFEMODE) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00001837 error = R.string.safemode_shortcut_error;
Sunny Goyal076839c2017-10-30 13:52:20 -07001838 } else if ((shortcut.runtimeStatusFlags & FLAG_DISABLED_BY_PUBLISHER) != 0 ||
1839 (shortcut.runtimeStatusFlags & FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07001840 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00001841 }
1842 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
1843 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001844 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001845 }
1846
Chris Wren40c5ed32014-06-24 18:24:23 -04001847 // Check for abandoned promise
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -07001848 if ((v instanceof BubbleTextView) && shortcut.hasPromiseIconUi()) {
Sunny Goyale6e72002017-01-12 16:55:36 -08001849 String packageName = shortcut.intent.getComponent() != null ?
1850 shortcut.intent.getComponent().getPackageName() : shortcut.intent.getPackage();
1851 if (!TextUtils.isEmpty(packageName)) {
1852 onClickPendingAppItem(v, packageName,
1853 shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE));
1854 return;
1855 }
Chris Wren40c5ed32014-06-24 18:24:23 -04001856 }
1857
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001858 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07001859 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04001860 }
1861
Sunny Goyala7ce1662016-05-31 15:01:35 -07001862 private void startAppShortcutOrInfoActivity(View v) {
1863 ItemInfo item = (ItemInfo) v.getTag();
Mario Bertschler08ffaae2017-03-20 11:30:27 -07001864 Intent intent;
1865 if (item instanceof PromiseAppInfo) {
1866 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) item;
Sunny Goyal67e75e22018-01-22 15:14:00 -08001867 intent = promiseAppInfo.getMarketIntent(this);
Mario Bertschler08ffaae2017-03-20 11:30:27 -07001868 } else {
1869 intent = item.getIntent();
1870 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07001871 if (intent == null) {
1872 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07001873 }
Mario Bertschler8d01cb42018-02-13 14:47:02 -08001874 if (item instanceof ShortcutInfo) {
1875 ShortcutInfo si = (ShortcutInfo) item;
1876 if (si.hasStatusFlag(ShortcutInfo.FLAG_SUPPORTS_WEB_UI)
1877 && intent.getAction() == Intent.ACTION_VIEW) {
1878 // make a copy of the intent that has the package set to null
1879 // we do this because the platform sometimes disables instant
1880 // apps temporarily (triggered by the user) and fallbacks to the
1881 // web ui. This only works though if the package isn't set
1882 intent = new Intent(intent);
1883 intent.setPackage(null);
1884 }
1885 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001886 startActivitySafely(v, intent, item);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001887 }
1888
1889 /**
1890 * Event handler for a folder icon click.
1891 *
1892 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
1893 */
1894 protected void onClickFolderIcon(View v) {
1895 if (LOGD) Log.d(TAG, "onClickFolder");
1896 if (!(v instanceof FolderIcon)){
1897 throw new IllegalArgumentException("Input must be a FolderIcon");
1898 }
1899
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001900 Folder folder = ((FolderIcon) v).getFolder();
1901 if (!folder.isOpen() && !folder.isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001902 // Open the requested folder
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001903 folder.animateOpen();
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001904 }
Michael Jurka5130e402011-10-13 04:55:35 -07001905 }
1906
Sandeep Siddharthad8058372014-01-28 10:41:15 -08001907 /**
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001908 * Event handler for the wallpaper picker button that appears after a long press
1909 * on the home screen.
1910 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001911 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07001912 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07001913 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
1914 return;
1915 }
1916
Tony Wickham785f7a52015-08-31 17:28:32 -07001917 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
1918 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal2100c782016-08-22 16:00:03 -07001919 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001920 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001921 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001922
1923 String pickerPackage = getString(R.string.wallpaper_picker_package);
Tony85efb4b2017-06-02 13:06:10 -07001924 boolean hasTargetPackage = !TextUtils.isEmpty(pickerPackage);
1925 if (hasTargetPackage) {
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001926 intent.setPackage(pickerPackage);
1927 }
1928
Sunny Goyala5ace712017-11-15 17:12:51 -08001929 final Bundle launchOptions;
1930 if (v != null) {
1931 intent.setSourceBounds(getViewBounds(v));
1932 // If there is no target package, use the default intent chooser animation
Jon Miranda17940f02018-01-18 14:46:34 -08001933 launchOptions = hasTargetPackage
Tony Wickham005df0b2018-02-13 11:28:12 -08001934 ? getActivityLaunchOptionsAsBundle(v, isInMultiWindowModeCompat())
Jon Miranda17940f02018-01-18 14:46:34 -08001935 : null;
Sunny Goyala5ace712017-11-15 17:12:51 -08001936 } else {
1937 launchOptions = null;
1938 }
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001939 try {
Sunny Goyala5ace712017-11-15 17:12:51 -08001940 startActivityForResult(intent, REQUEST_PICK_WALLPAPER, launchOptions);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001941 } catch (ActivityNotFoundException e) {
1942 setWaitingForResult(null);
1943 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1944 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001945 }
1946
Sunny Goyala7ce1662016-05-31 15:01:35 -07001947 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001948 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001949 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
1950 try {
1951 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
1952 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
1953 // is enabled by default on NYC.
1954 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
1955 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001956
1957 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
1958 String id = ((ShortcutInfo) info).getDeepShortcutId();
1959 String packageName = intent.getPackage();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301960 DeepShortcutManager.getInstance(this).startShortcut(
Sunny Goyal9994b2b2016-06-23 14:17:24 -07001961 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001962 } else {
1963 // Could be launching some bookkeeping activity
1964 startActivity(intent, optsBundle);
1965 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07001966 } finally {
1967 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01001968 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001969 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001970 // Due to legacy reasons, direct call shortcuts require Launchers to have the
1971 // corresponding permission. Show the appropriate permission prompt if that
1972 // is the case.
1973 if (intent.getComponent() == null
1974 && Intent.ACTION_CALL.equals(intent.getAction())
1975 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
1976 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001977
1978 setWaitingForResult(PendingRequestArgs
1979 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07001980 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
1981 REQUEST_PERMISSION_CALL_PHONE);
1982 } else {
1983 // No idea why this was thrown.
1984 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07001985 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001986 }
1987 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001988
Tony Wickham005df0b2018-02-13 11:28:12 -08001989 public Bundle getActivityLaunchOptionsAsBundle(View v, boolean useDefaultLaunchOptions) {
1990 ActivityOptions activityOptions = getActivityLaunchOptions(v, useDefaultLaunchOptions);
1991 return activityOptions == null ? null : activityOptions.toBundle();
1992 }
1993
Sunny Goyalfe770c92016-09-27 14:38:58 -07001994 @TargetApi(Build.VERSION_CODES.M)
Tony Wickham005df0b2018-02-13 11:28:12 -08001995 public ActivityOptions getActivityLaunchOptions(View v, boolean useDefaultLaunchOptions) {
Jon Miranda17940f02018-01-18 14:46:34 -08001996 return useDefaultLaunchOptions
Jon Miranda54441f52018-01-24 15:38:25 -08001997 ? mAppTransitionManager.getDefaultActivityLaunchOptions(this, v)
1998 : mAppTransitionManager.getActivityLaunchOptions(this, v);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001999 }
2000
Tonye3c59252017-05-02 21:32:27 -07002001 public Rect getViewBounds(View v) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002002 int[] pos = new int[2];
2003 v.getLocationOnScreen(pos);
2004 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2005 }
2006
Sunny Goyala7ce1662016-05-31 15:01:35 -07002007 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002008 mAppLaunchSuccess = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002009 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2010 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002011 return mAppLaunchSuccess;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002012 }
Jon Miranda17940f02018-01-18 14:46:34 -08002013
Sunny Goyala7ce1662016-05-31 15:01:35 -07002014 // Only launch using the new animation if the shortcut has not opted out (this is a
2015 // private contract between launcher and may be ignored in the future).
2016 boolean useLaunchAnimation = (v != null) &&
2017 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Jon Miranda17940f02018-01-18 14:46:34 -08002018 Bundle optsBundle = useLaunchAnimation
Tony Wickham005df0b2018-02-13 11:28:12 -08002019 ? getActivityLaunchOptionsAsBundle(v, isInMultiWindowModeCompat())
Jon Miranda17940f02018-01-18 14:46:34 -08002020 : null;
Sunny Goyala7ce1662016-05-31 15:01:35 -07002021
Sunny Goyal24bb66a2017-03-21 15:12:01 -07002022 UserHandle user = item == null ? null : item.user;
Sunny Goyala7ce1662016-05-31 15:01:35 -07002023
2024 // Prepare intent
2025 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2026 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002027 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002028 }
2029 try {
Jon Miranda84e71bf2018-02-13 15:36:20 -08002030 boolean isShortcut = Utilities.ATLEAST_MARSHMALLOW
2031 && (item instanceof ShortcutInfo)
2032 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
2033 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
2034 && !((ShortcutInfo) item).isPromise();
Jon Miranda17940f02018-01-18 14:46:34 -08002035 if (isShortcut) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002036 // Shortcuts need some special checks due to legacy reasons.
2037 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002038 } else if (user == null || user.equals(Process.myUserHandle())) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002039 // Could be launching some bookkeeping activity
2040 startActivity(intent, optsBundle);
2041 } else {
2042 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2043 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2044 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002045
2046 if (v instanceof BubbleTextView) {
2047 // This is set to the view that launched the activity that navigated the user away
2048 // from launcher. Since there is no callback for when the activity has finished
2049 // launching, enable the press state and keep this reference to reset the press
2050 // state when we return to launcher.
2051 BubbleTextView btv = (BubbleTextView) v;
2052 btv.setStayPressed(true);
2053 setOnResumeCallback(btv);
2054 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002055 mAppLaunchSuccess = true;
2056 getUserEventDispatcher().logAppLaunch(v, intent); // TODO for discovered apps b/35802115
Sunny Goyala7ce1662016-05-31 15:01:35 -07002057 } catch (ActivityNotFoundException|SecurityException e) {
2058 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2059 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2060 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002061 return mAppLaunchSuccess;
Michael Jurka86a720e2012-05-09 11:23:23 -07002062 }
2063
Tony Wickhamdadb3042016-02-24 11:07:00 -08002064 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07002065 public boolean dispatchTouchEvent(MotionEvent ev) {
Sunny Goyal02424b22018-01-19 11:24:32 -08002066 mLastDispatchTouchEvent.set(ev.getX(), ev.getY());
Jon Miranda379198e2016-09-23 08:54:40 -07002067 return super.dispatchTouchEvent(ev);
2068 }
2069
2070 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002071 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002072 if (!isDraggingEnabled()) return false;
2073 if (isWorkspaceLocked()) return false;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002074 if (!isInState(NORMAL) && !isInState(OVERVIEW)) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002075
Jon Miranda51f037d2016-11-07 08:37:20 -08002076 boolean ignoreLongPressToOverview =
Sunny Goyal02424b22018-01-19 11:24:32 -08002077 mDeviceProfile.shouldIgnoreLongPressToOverview(mLastDispatchTouchEvent.x);
Jon Miranda379198e2016-09-23 08:54:40 -07002078
Adam Cohen1697b792013-09-17 19:08:21 -07002079 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002080 if (!isInState(OVERVIEW)) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002081 if (!mWorkspace.isTouchActive() && !ignoreLongPressToOverview) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302082 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2083 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002084 mWorkspace.getCurrentPage());
Sunny Goyal02424b22018-01-19 11:24:32 -08002085 UiFactory.onWorkspaceLongPress(this, mLastDispatchTouchEvent);
Adam Cohen93c97562013-09-26 13:48:01 -07002086 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2087 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2088 return true;
2089 } else {
2090 return false;
2091 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002092 } else {
2093 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002094 }
Adam Cohen1697b792013-09-17 19:08:21 -07002095 }
2096
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002097 CellLayout.CellInfo longClickCellInfo = null;
2098 View itemUnderLongClick = null;
2099 if (v.getTag() instanceof ItemInfo) {
2100 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002101 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002102 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07002103 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002104 }
2105
Winson Chung3d503fb2011-07-13 17:25:49 -07002106 // The hotseat touch handling does not go through Workspace, and we always allow long press
2107 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002108 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002109 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002110 // User long pressed on empty space
Sunny Goyalc99cb172017-10-19 16:15:09 -07002111 if (mWorkspace.isPageRearrangeEnabled()) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002112 mWorkspace.startReordering(v);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302113 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2114 Action.Direction.NONE, ContainerType.OVERVIEW);
Adam Cohend3ecce92013-09-16 14:58:00 -07002115 } else {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002116 if (ignoreLongPressToOverview) {
Jon Miranda379198e2016-09-23 08:54:40 -07002117 return false;
2118 }
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302119 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2120 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002121 mWorkspace.getCurrentPage());
Sunny Goyal02424b22018-01-19 11:24:32 -08002122 UiFactory.onWorkspaceLongPress(this, mLastDispatchTouchEvent);
Adam Cohendedbd962013-07-11 14:21:49 -07002123 }
Jon Miranda379198e2016-09-23 08:54:40 -07002124 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2125 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002126 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07002127 final boolean isAllAppsButton =
2128 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
2129 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
2130 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05002131 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002132 // User long pressed on an item
Tony Wickham10236d62016-09-28 12:49:25 -07002133 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002134 }
2135 }
2136 }
2137 return true;
2138 }
2139
Winson Chung3d503fb2011-07-13 17:25:49 -07002140 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08002141 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07002142 return mHotseat != null && layout != null &&
2143 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2144 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002145
Winson Chung3d503fb2011-07-13 17:25:49 -07002146 /**
2147 * Returns the CellLayout of the specified container at the specified screen.
2148 */
Sunny Goyal92820592015-03-02 11:31:35 -08002149 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002150 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2151 if (mHotseat != null) {
2152 return mHotseat.getLayout();
2153 } else {
2154 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002155 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002156 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002157 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002158 }
2159 }
2160
Michael Jurkae326f182011-11-21 14:05:46 -08002161 @Override
2162 public void onTrimMemory(int level) {
2163 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07002164 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2165 // The widget preview db can result in holding onto over
2166 // 3MB of memory for caching which isn't necessary.
2167 SQLiteDatabase.releaseMemory();
2168
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08002169 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07002170 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08002171 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01002172 if (mLauncherCallbacks != null) {
2173 mLauncherCallbacks.onTrimMemory(level);
2174 }
Winson Chung62a70be2018-02-23 15:10:05 -08002175 UiFactory.onTrimMemory(this, level);
Michael Jurkae326f182011-11-21 14:05:46 -08002176 }
2177
Michael Jurkad7c28052012-04-27 15:43:36 -07002178 @Override
2179 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07002180 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07002181 final List<CharSequence> text = event.getText();
2182 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07002183 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002184 // TODO: When can workspace be null?
2185 text.add(mWorkspace == null
2186 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07002187 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07002188 return result;
2189 }
2190
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002191 public void setOnResumeCallback(OnResumeCallback callback) {
2192 if (mOnResumeCallback != null) {
2193 mOnResumeCallback.onLauncherResume();
2194 }
2195 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07002196 }
2197
Sunny Goyal60876ac2018-02-20 14:21:20 -08002198 public void setOnStartCallback(OnStartCallback callback) {
2199 mOnStartCallback = callback;
2200 }
2201
Michael Jurka7607c2f2013-04-03 14:33:19 -07002202 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002203 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002204 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002205 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002206 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002207 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002208 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002209 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07002210 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07002211 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002212 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002213
Joe Onorato9c1289c2009-08-17 11:03:03 -04002214 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07002215 * Clear any pending bind callbacks. This is called when is loader is planning to
2216 * perform a full rebind from scratch.
2217 */
2218 @Override
2219 public void clearPendingBinds() {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002220 if (mPendingExecutor != null) {
2221 mPendingExecutor.markCompleted();
2222 mPendingExecutor = null;
2223 }
2224 }
2225
2226 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002227 * Refreshes the shortcuts shown on the workspace.
2228 *
2229 * Implementation of the method from LauncherModel.Callbacks.
2230 */
2231 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002232 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002233 // Floating panels (except the full widget sheet) are associated with individual icons. If
2234 // we are starting a fresh bind, close all such panels as all the icons are about
2235 // to go away.
2236 AbstractFloatingView.closeOpenViews(this, true,
Sunny Goyal7ede6112017-12-05 15:11:21 -08002237 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_REBIND_SAFE);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08002238
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002239 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08002240
Winson Chungd64d1762013-08-20 14:37:16 -07002241 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07002242 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07002243 mWorkspace.removeAllWorkspaceScreens();
Sunny Goyalc11fac32018-02-27 19:20:15 -08002244 mAppWidgetHost.clearViews();
Winson Chungd64d1762013-08-20 14:37:16 -07002245
Winson Chung3d503fb2011-07-13 17:25:49 -07002246 if (mHotseat != null) {
2247 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002248 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002249 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002250 }
2251
Adam Cohendcd297f2013-06-18 13:13:40 -07002252 @Override
2253 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002254 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002255 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
2256 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002257 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
2258 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002259 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002260 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
2261 // If there are no screens, we need to have an empty screen
2262 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07002263 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002264 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07002265
Winsonc7d2e832016-07-28 12:24:55 -07002266 // After we have added all the screens, if the wallpaper was locked to the default state,
2267 // then notify to indicate that it can be released and a proper wallpaper offset can be
2268 // computed before the next layout
2269 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07002270 }
2271
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002272 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002273 int count = orderedScreenIds.size();
2274 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002275 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002276 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002277 // No need to bind the first screen, as its always bound.
2278 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
2279 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002280 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002281 }
2282
Sunny Goyalb23980c2017-08-17 07:45:25 -07002283 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002284 public void bindAppsAdded(ArrayList<Long> newScreens, ArrayList<ItemInfo> addNotAnimated,
2285 ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07002286 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08002287 if (newScreens != null) {
2288 bindAddScreens(newScreens);
2289 }
Winson Chungd64d1762013-08-20 14:37:16 -07002290
2291 // We add the items without animation on non-visible pages, and with
2292 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08002293 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002294 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002295 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002296 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002297 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07002298 }
Winson Chungc58497e2013-09-03 17:48:37 -07002299
Winson Chung87412982013-10-03 18:34:14 -07002300 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07002301 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002302 }
2303
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002304 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002305 * Bind the items start-end from the list.
2306 *
2307 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002308 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07002309 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07002310 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002311 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07002312 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002313 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07002314 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07002315 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002316 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07002317 int end = items.size();
2318 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002319 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002320
2321 // Short circuit if we are loading dock items for a configuration which has no dock
2322 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2323 mHotseat == null) {
2324 continue;
2325 }
2326
Sunny Goyal639e9062015-08-19 19:17:06 -07002327 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002328 switch (item.itemType) {
2329 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2330 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08002331 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07002332 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07002333 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002334 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002335 }
2336 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07002337 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002338 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002339 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002340 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002341 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07002342 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2343 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07002344 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002345 if (view == null) {
2346 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002347 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08002348 break;
2349 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002350 default:
2351 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002352 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002353
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002354 /*
2355 * Remove colliding items.
2356 */
2357 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
2358 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
2359 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
2360 View v = cl.getChildAt(item.cellX, item.cellY);
2361 Object tag = v.getTag();
2362 String desc = "Collision while binding workspace item: " + item
2363 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08002364 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002365 throw (new RuntimeException(desc));
2366 } else {
2367 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002368 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002369 continue;
2370 }
2371 }
2372 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302373 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07002374 if (animateIcons) {
2375 // Animate all the applications up now
2376 view.setAlpha(0f);
2377 view.setScaleX(0f);
2378 view.setScaleY(0f);
2379 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08002380 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07002381 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002382 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07002383
Winson Chung997a9232013-07-24 15:33:46 -07002384 if (animateIcons) {
2385 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08002386 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07002387 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08002388 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07002389 final Runnable startBounceAnimRunnable = new Runnable() {
2390 public void run() {
2391 anim.playTogether(bounceAnims);
2392 anim.start();
2393 }
2394 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08002395 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07002396 // We post the animation slightly delayed to prevent slowdowns
2397 // when we are loading right after we return to launcher.
2398 mWorkspace.postDelayed(new Runnable() {
2399 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00002400 if (mWorkspace != null) {
2401 mWorkspace.snapToPage(newScreenIndex);
2402 mWorkspace.postDelayed(startBounceAnimRunnable,
2403 NEW_APPS_ANIMATION_DELAY);
2404 }
Winson Chung94d67682013-09-25 16:29:40 -07002405 }
2406 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07002407 } else {
2408 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07002409 }
2410 }
Winson Chung64359a52013-07-08 17:17:08 -07002411 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002412 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002413 }
2414
Joe Onorato9c1289c2009-08-17 11:03:03 -04002415 /**
2416 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002417 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07002418 public void bindAppWidget(LauncherAppWidgetInfo item) {
2419 View view = inflateAppWidget(item);
2420 if (view != null) {
2421 mWorkspace.addInScreen(view, item);
2422 mWorkspace.requestLayout();
2423 }
2424 }
2425
2426 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002427 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302428 PendingAppWidgetHostView view =
2429 new PendingAppWidgetHostView(this, item, mIconCache, true);
2430 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002431 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002432 }
2433
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002434 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002435
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002436 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08002437
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002438 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2439 // If the provider is not ready, bind as a pending widget.
2440 appWidgetInfo = null;
2441 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2442 // The widget id is not valid. Try to find the widget based on the provider info.
2443 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
2444 } else {
2445 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
2446 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002447
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002448 // If the provider is ready, but the width is not yet restored, try to restore it.
2449 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
2450 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07002451 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002452 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
2453 + " belongs to component " + item.providerName
2454 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002455 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002456 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002457 }
Sunny Goyalff572272014-07-23 13:58:07 -07002458
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002459 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002460 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07002461 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2462 // Id has not been allocated yet. Allocate a new id.
2463 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
2464 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07002465
Sunny Goyald478c832016-04-01 12:04:16 -07002466 // Also try to bind the widget. If the bind fails, the user will be shown
2467 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08002468 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07002469 pendingInfo.spanX = item.spanX;
2470 pendingInfo.spanY = item.spanY;
2471 pendingInfo.minSpanX = item.minSpanX;
2472 pendingInfo.minSpanY = item.minSpanY;
2473 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07002474
2475 boolean isDirectConfig =
2476 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2477 if (isDirectConfig && item.bindOptions != null) {
2478 Bundle newOptions = item.bindOptions.getExtras();
2479 if (options != null) {
2480 newOptions.putAll(options);
2481 }
2482 options = newOptions;
2483 }
Sunny Goyald478c832016-04-01 12:04:16 -07002484 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2485 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002486
Sunny Goyal86df1382016-08-10 15:03:22 -07002487 // We tried to bind once. If we were not able to bind, we would need to
2488 // go through the permission dialog, which means we cannot skip the config
2489 // activity.
2490 item.bindOptions = null;
2491 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2492
Sunny Goyald478c832016-04-01 12:04:16 -07002493 // Bind succeeded
2494 if (success) {
2495 // If the widget has a configure activity, it is still needs to set it up,
2496 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002497 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002498 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2499 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002500 }
Sunny Goyald478c832016-04-01 12:04:16 -07002501
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002502 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002503 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002504 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002505 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002506 // The widget was marked as UI not ready, but there is no configure activity to
2507 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002508 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002509 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002510 }
Sunny Goyalff572272014-07-23 13:58:07 -07002511 }
2512
Sunny Goyald5462aa2016-11-22 16:52:39 +05302513 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002514 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002515 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002516 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002517 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07002518 deleteWidgetInfo(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002519 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002520 }
2521
Sunny Goyal233ee962015-08-03 13:05:01 -07002522 item.minSpanX = appWidgetInfo.minSpanX;
2523 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302524 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002525 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302526 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002527 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302528 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002529
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002530 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002531 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002532 }
2533
Sunny Goyalff572272014-07-23 13:58:07 -07002534 /**
2535 * Restores a pending widget.
2536 *
2537 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002538 */
Sunny Goyald478c832016-04-01 12:04:16 -07002539 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002540 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2541 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2542 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002543 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002544 }
2545
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002546 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002547 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002548 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2549 info.pendingItemInfo = null;
2550 }
Sunny Goyalff572272014-07-23 13:58:07 -07002551
Sunny Goyalba47faa2017-10-04 16:50:57 -07002552 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002553 view.reInflate();
Sunny Goyalba47faa2017-10-04 16:50:57 -07002554 }
2555
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002556 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002557 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002558 }
2559
Adam Cohen1462de32012-07-24 22:34:36 -07002560 public void onPageBoundSynchronously(int page) {
Sunny Goyalf4aceab2017-12-06 16:16:31 -08002561 mSynchronouslyBoundPage = page;
Adam Cohen1462de32012-07-24 22:34:36 -07002562 }
2563
Sunny Goyal527c7d32015-08-28 15:19:36 -07002564 @Override
2565 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2566 if (mPendingExecutor != null) {
2567 mPendingExecutor.markCompleted();
2568 }
2569 mPendingExecutor = executor;
Sunny Goyal60180b02018-02-07 12:56:30 -08002570 if (!isInState(ALL_APPS)) {
2571 mAppsView.getAppsStore().setDeferUpdates(true);
2572 mPendingExecutor.execute(() -> mAppsView.getAppsStore().setDeferUpdates(false));
2573 }
2574
Sunny Goyal527c7d32015-08-28 15:19:36 -07002575 executor.attachTo(this);
2576 }
2577
2578 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2579 if (mPendingExecutor == executor) {
2580 mPendingExecutor = null;
2581 }
2582 }
2583
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002584 @Override
2585 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002586 if (mDragLayer.getAlpha() < 1) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002587 mDragLayer.animate().alpha(1).withEndAction(
2588 executor == null ? null : executor::onLoadAnimationCompleted).start();
2589 } else if (executor != null) {
2590 executor.onLoadAnimationCompleted();
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002591 }
2592 }
2593
Joe Onorato9c1289c2009-08-17 11:03:03 -04002594 /**
2595 * Callback saying that there aren't any more items to bind.
2596 *
2597 * Implementation of the method from LauncherModel.Callbacks.
2598 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002599 public void finishBindingItems() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002600 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002601 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002602
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002603 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002604
Sunny Goyal2100c782016-08-22 16:00:03 -07002605 if (mPendingActivityResult != null) {
2606 handleActivityResult(mPendingActivityResult.requestCode,
2607 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2608 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002609 }
2610
Sunny Goyala474a9b2017-05-04 16:47:11 -07002611 InstallShortcutReceiver.disableAndFlushInstallQueue(
2612 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002613
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002614 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002615 }
2616
Winson Chunga2413752012-04-03 14:22:34 -07002617 private boolean canRunNewAppsAnimation() {
2618 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002619 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002620 }
2621
Winson Chungc9168342013-06-26 14:54:55 -07002622 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002623 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07002624 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
2625 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002626 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002627 return bounceAnim;
2628 }
2629
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002630 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002631 * Add the icons for all apps.
2632 *
2633 * Implementation of the method from LauncherModel.Callbacks.
2634 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002635 public void bindAllApplications(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002636 mAppsView.getAppsStore().setApps(apps);
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002637
Adam Cohen9211d422014-10-07 18:14:53 -07002638 if (mLauncherCallbacks != null) {
2639 mLauncherCallbacks.bindAllApplications(apps);
2640 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002641 }
2642
2643 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002644 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
2645 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2646 */
2647 @Override
2648 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002649 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002650 }
2651
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002652 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002653 * A package was updated.
2654 *
2655 * Implementation of the method from LauncherModel.Callbacks.
2656 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002657 @Override
2658 public void bindAppsAddedOrUpdated(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002659 mAppsView.getAppsStore().addOrUpdateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002660 }
2661
Sunny Goyal4390ace2014-10-13 11:33:11 -07002662 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002663 public void bindPromiseAppProgressUpdated(PromiseAppInfo app) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002664 mAppsView.getAppsStore().updatePromiseAppProgress(app);
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002665 }
2666
2667 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002668 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002669 mWorkspace.widgetsRestored(widgets);
2670 }
2671
Joe Onorato9c1289c2009-08-17 11:03:03 -04002672 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002673 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002674 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002675 *
2676 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002677 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002678 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002679 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002680 if (!updated.isEmpty()) {
2681 mWorkspace.updateShortcuts(updated);
2682 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002683 }
2684
2685 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002686 * Update the state of a package, typically related to install state.
2687 *
2688 * Implementation of the method from LauncherModel.Callbacks.
2689 */
Sunny Goyale755d462014-07-22 13:48:29 -07002690 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002691 public void bindRestoreItemsChange(HashSet<ItemInfo> updates) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07002692 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07002693 }
2694
2695 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002696 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07002697 * in addition to specific applications to remove, the reason being that
2698 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002699 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07002700 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002701 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07002702 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002703 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002704 mWorkspace.removeItemsByMatcher(matcher);
2705 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002706 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002707
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002708 @Override
2709 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002710 mAppsView.getAppsStore().removeApps(appInfos);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002711 }
Joe Onoratobe386092009-11-17 17:32:16 -08002712
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002713 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002714 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
2715 mPopupDataProvider.setAllWidgets(allWidgets);
Tony Wickham26b17462017-03-20 17:12:24 -07002716 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
2717 if (topView != null) {
2718 topView.onWidgetsBound();
2719 }
2720 }
2721
Tony Wickham86222d22017-03-29 15:30:43 -07002722 /**
2723 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
2724 * refreshes the widgets and shortcuts associated with the given package/user
2725 */
2726 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07002727 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002728 }
2729
Sunny Goyalc99cb172017-10-19 16:15:09 -07002730 public boolean isRotationEnabled () {
2731 return mRotationEnabled;
Winson Chung400438b2011-01-16 17:53:48 -08002732 }
2733
Winson Chung80baf5a2010-08-09 16:03:15 -07002734 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002735 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08002736 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002737 @Override
2738 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
2739 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002740
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002741 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
2742 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07002743 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002744 writer.println(prefix + " Homescreen " + i);
2745
2746 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
2747 for (int j = 0; j < layout.getChildCount(); j++) {
2748 Object tag = layout.getChildAt(j).getTag();
2749 if (tag != null) {
2750 writer.println(prefix + " " + tag.toString());
2751 }
2752 }
2753 }
2754
2755 writer.println(prefix + " Hotseat");
2756 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07002757 for (int j = 0; j < layout.getChildCount(); j++) {
2758 Object tag = layout.getChildAt(j).getTag();
2759 if (tag != null) {
2760 writer.println(prefix + " " + tag.toString());
2761 }
Adam Cohen4caf2982013-08-20 18:54:31 -07002762 }
Sunny Goyala1365452015-10-01 15:46:24 -07002763 }
2764
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002765 writer.println(prefix + "Misc:");
2766 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
2767 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
2768 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
Sunny Goyal2bba1902018-02-27 12:20:50 -08002769 writer.println(" deviceProfile isTransposed=" + getDeviceProfile().isVerticalBarLayout());
2770 writer.println(" orientation=" + getResources().getConfiguration().orientation);
2771
2772 try {
2773 FileLog.flushAll(writer);
2774 } catch (Exception e) {
2775 // Ignore
2776 }
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002777
2778 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002779
Adam Cohen9211d422014-10-07 18:14:53 -07002780 if (mLauncherCallbacks != null) {
2781 mLauncherCallbacks.dump(prefix, fd, writer, args);
2782 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002783 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002784
Sunny Goyal66b24572016-09-21 15:57:55 -07002785 @Override
2786 @TargetApi(Build.VERSION_CODES.N)
2787 public void onProvideKeyboardShortcuts(
2788 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
2789
2790 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002791 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002792 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
2793 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
2794 }
2795 View currentFocus = getCurrentFocus();
2796 if (new CustomActionsPopup(this, currentFocus).canShow()) {
2797 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
2798 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
2799 }
Tony18c4aa42017-05-10 21:23:57 -05002800 if (currentFocus.getTag() instanceof ItemInfo
2801 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002802 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
2803 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
2804 }
2805 if (!shortcutInfos.isEmpty()) {
2806 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
2807 }
2808
2809 super.onProvideKeyboardShortcuts(data, menu, deviceId);
2810 }
2811
2812 @Override
2813 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
2814 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
2815 switch (keyCode) {
2816 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002817 if (isInState(NORMAL)) {
2818 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07002819 return true;
2820 }
2821 break;
2822 case KeyEvent.KEYCODE_S: {
2823 View focusedView = getCurrentFocus();
2824 if (focusedView instanceof BubbleTextView
2825 && focusedView.getTag() instanceof ItemInfo
2826 && mAccessibilityDelegate.performAction(focusedView,
2827 (ItemInfo) focusedView.getTag(),
2828 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal00ac9202017-11-09 15:24:06 -08002829 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07002830 return true;
2831 }
2832 break;
2833 }
2834 case KeyEvent.KEYCODE_O:
2835 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
2836 return true;
2837 }
2838 break;
2839 }
2840 }
2841 return super.onKeyShortcut(keyCode, event);
2842 }
2843
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002844 @Override
2845 public boolean onKeyUp(int keyCode, KeyEvent event) {
2846 if (keyCode == KeyEvent.KEYCODE_MENU) {
2847 // KEYCODE_MENU is sent by some tests, for example
2848 // LauncherJankTests#testWidgetsContainerFling. Don't just remove its handling.
2849 if (!mDragController.isDragging() && !mWorkspace.isSwitchingState() &&
2850 isInState(NORMAL)) {
2851 // Close any open floating views.
2852 AbstractFloatingView.closeAllOpenViews(this);
2853
2854 // Setting the touch point to (-1, -1) will show the options popup in the center of
2855 // the screen.
2856 mLastDispatchTouchEvent.set(-1, -1);
2857 UiFactory.onWorkspaceLongPress(this, mLastDispatchTouchEvent);
2858 }
2859 return true;
2860 }
2861 return super.onKeyUp(keyCode, event);
2862 }
2863
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002864 public static Launcher getLauncher(Context context) {
2865 if (context instanceof Launcher) {
2866 return (Launcher) context;
2867 }
2868 return ((Launcher) ((ContextWrapper) context).getBaseContext());
2869 }
2870
Sunny Goyal5a81c382017-03-20 15:08:06 -07002871 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener {
Sunny Goyal745bad92016-05-02 10:54:12 -07002872
2873 @Override
2874 public void onSharedPreferenceChanged(
2875 SharedPreferences sharedPreferences, String key) {
2876 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
Sunny Goyale13d4642017-05-22 15:58:42 -07002877 // Recreate the activity so that it initializes the rotation preference again.
2878 recreate();
Sunny Goyal745bad92016-05-02 10:54:12 -07002879 }
2880 }
Sunny Goyal745bad92016-05-02 10:54:12 -07002881 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002882
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08002883 @Override
2884 public void onActionModeStarted(ActionMode mode) {
2885 super.onActionModeStarted(mode);
2886 mCurrentActionMode = mode;
2887 }
2888
2889 @Override
2890 public void onActionModeFinished(ActionMode mode) {
2891 super.onActionModeFinished(mode);
2892 mCurrentActionMode = null;
2893 }
2894
2895 public boolean finishAutoCancelActionMode() {
2896 if (mCurrentActionMode != null && AUTO_CANCEL_ACTION_MODE == mCurrentActionMode.getTag()) {
2897 mCurrentActionMode.finish();
2898 return true;
2899 }
2900 return false;
2901 }
2902
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002903 /**
2904 * Callback for listening for onResume
2905 */
2906 public interface OnResumeCallback {
2907
2908 void onLauncherResume();
2909 }
Sunny Goyal60876ac2018-02-20 14:21:20 -08002910
2911 /**
2912 * Callback for listening for onStart
2913 */
2914 public interface OnStartCallback {
2915
2916 void onLauncherStart(Launcher launcher);
2917 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002918}