blob: 375deb745ccec29afa07b62394c26c03999586c0 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080019import static android.content.pm.ActivityInfo.CONFIG_ORIENTATION;
20import static android.content.pm.ActivityInfo.CONFIG_SCREEN_SIZE;
Sunny Goyal623eddd2018-03-02 12:24:41 -080021
Sunny Goyal4c7f2152017-10-17 17:17:16 -070022import static com.android.launcher3.LauncherAnimUtils.SPRING_LOADED_EXIT_DELAY;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -070023import static com.android.launcher3.LauncherState.ALL_APPS;
24import static com.android.launcher3.LauncherState.NORMAL;
25import static com.android.launcher3.LauncherState.OVERVIEW;
Sunny Goyalaeb16432017-10-16 11:46:41 -070026import static com.android.launcher3.logging.LoggerUtils.newContainerTarget;
Sunny Goyal326403e2017-10-02 12:45:10 -070027
Sunny Goyal28c6b962015-10-12 11:42:05 -070028import android.Manifest;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070029import android.animation.Animator;
Jon Miranda2d89ea82017-05-04 11:47:53 -070030import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070031import android.animation.AnimatorSet;
Jon Miranda2d89ea82017-05-04 11:47:53 -070032import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070033import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070034import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000035import android.annotation.TargetApi;
Tony Wickham005df0b2018-02-13 11:28:12 -080036import android.app.ActivityOptions;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080037import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070038import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080040import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080041import android.content.ComponentCallbacks2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Context;
Andrew Sappersteinabef55a2016-06-19 12:49:00 -070043import android.content.ContextWrapper;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070045import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070046import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070047import android.content.SharedPreferences;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.pm.PackageManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080049import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070050import android.database.sqlite.SQLiteDatabase;
Jon Mirandacc42c5b2016-10-27 17:15:27 -070051import android.graphics.Point;
Sunny Goyal02424b22018-01-19 11:24:32 -080052import android.graphics.PointF;
Michael Jurkaaf442092010-06-10 17:01:57 -070053import android.graphics.Rect;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070054import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020055import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056import android.os.Bundle;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070057import android.os.Parcelable;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080058import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070059import android.os.StrictMode;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070060import android.os.UserHandle;
Tony Wickham86222d22017-03-29 15:30:43 -070061import android.support.annotation.Nullable;
Michael Jurkaa33411c2012-06-14 16:18:21 -070062import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070064import android.util.Log;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070065import android.util.SparseArray;
Sunny Goyal7c8a65e2017-12-22 11:11:33 -080066import android.view.ActionMode;
Adam Cohen96d30a12013-07-16 18:13:21 -070067import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070068import android.view.HapticFeedbackConstants;
69import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070070import android.view.KeyboardShortcutGroup;
71import android.view.KeyboardShortcutInfo;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080072import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -070073import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070074import android.view.MotionEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070075import android.view.View;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -080076import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080077import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070078import android.view.ViewGroup;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080079import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070080import android.view.animation.OvershootInterpolator;
Adam Cohen96d30a12013-07-16 18:13:21 -070081import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070082
Sunny Goyal651077b2014-06-30 14:15:31 -070083import com.android.launcher3.DropTarget.DragObject;
Sunny Goyala7ce1662016-05-31 15:01:35 -070084import com.android.launcher3.LauncherSettings.Favorites;
Jon Mirandac476d6e2017-05-04 16:30:01 -070085import com.android.launcher3.Workspace.ItemOperator;
Sunny Goyalae502842016-06-17 08:43:56 -070086import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070087import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070088import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal7ede6112017-12-05 15:11:21 -080089import com.android.launcher3.allapps.DiscoveryBounce;
Sunny Goyalffe83f12014-08-14 17:39:34 -070090import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010091import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -070092import com.android.launcher3.compat.LauncherAppsCompatVO;
Tony Wickhame0c33232016-02-08 11:37:04 -080093import com.android.launcher3.config.FeatureFlags;
Vadim Tryshevfedca432015-08-19 17:55:02 -070094import com.android.launcher3.dragndrop.DragController;
95import com.android.launcher3.dragndrop.DragLayer;
96import com.android.launcher3.dragndrop.DragView;
Mario Bertschler27288382017-05-24 15:35:09 -070097import com.android.launcher3.dynamicui.WallpaperColorInfo;
Sunny Goyal26119432016-02-18 22:09:23 +000098import com.android.launcher3.folder.FolderIcon;
Sunny Goyal66b24572016-09-21 15:57:55 -070099import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700100import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700101import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700102import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyala535ae42017-02-27 10:07:13 -0800103import com.android.launcher3.model.ModelWriter;
Sunny Goyala535ae42017-02-27 10:07:13 -0800104import com.android.launcher3.notification.NotificationListener;
Tony Wickham540913e2017-01-23 11:47:51 -0800105import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -0800106import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700107import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyalf8088ee2017-11-10 14:52:00 -0800108import com.android.launcher3.states.InternalStateHandler;
Sunny Goyal623eddd2018-03-02 12:24:41 -0800109import com.android.launcher3.states.RotationHelper;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800110import com.android.launcher3.touch.ItemClickHandler;
Sunny Goyala5ace712017-11-15 17:12:51 -0800111import com.android.launcher3.uioverrides.UiFactory;
Jon Mirandac6cf4932017-02-07 17:12:36 -0800112import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530113import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
114import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
Sunny Goyalaeb16432017-10-16 11:46:41 -0700115import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyal2100c782016-08-22 16:00:03 -0700116import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700117import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700118import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700119import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700120import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800121import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700122import com.android.launcher3.util.PendingRequestArgs;
Sunny Goyal8392c822017-06-20 10:03:56 -0700123import com.android.launcher3.util.SystemUiController;
Mario Bertschlera6936942017-05-31 14:48:19 -0700124import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700125import com.android.launcher3.util.Thunk;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700126import com.android.launcher3.util.TraceHelper;
Sunny Goyal326403e2017-10-02 12:45:10 -0700127import com.android.launcher3.util.UiThreadHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700128import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyal29947f02017-12-18 13:49:44 -0800129import com.android.launcher3.widget.LauncherAppWidgetHostView;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800130import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700131import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal29947f02017-12-18 13:49:44 -0800132import com.android.launcher3.widget.PendingAppWidgetHostView;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800133import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700134import com.android.launcher3.widget.WidgetHostViewLoader;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700135import com.android.launcher3.widget.WidgetListRowEntry;
136import com.android.launcher3.widget.WidgetsFullSheet;
Sunny Goyal952e63d2017-08-16 04:59:08 -0700137import com.android.launcher3.widget.custom.CustomWidgetParser;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700138
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700139import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700140import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700141import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700142import java.util.Collection;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700143import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700144import java.util.List;
Tony Wickham010d2552017-01-20 08:15:28 -0800145import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700146
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147/**
148 * Default launcher application.
149 */
Sunny Goyal27835952017-01-13 12:15:53 -0800150public class Launcher extends BaseActivity
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800151 implements LauncherExterns, OnClickListener, OnLongClickListener,
Adam Cohen79d90c52016-04-22 13:29:20 -0700152 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Mario Bertschler27288382017-05-24 15:35:09 -0700153 WallpaperColorInfo.OnThemeChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700154 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700155 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156
Winson Chunga2413752012-04-03 14:22:34 -0700157 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700158
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700160 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700161
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700162 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700163 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164
Michael Jurka8b805b12012-04-18 14:23:14 -0700165 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800166 public static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
167 public static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700168
Jon Mirandac476d6e2017-05-04 16:30:01 -0700169 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700170
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700171 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
172
Mathew Inwood876a8462013-06-14 14:12:41 +0100173 /**
174 * IntentStarter uses request codes starting with this. This must be greater than all activity
175 * request codes used internally.
176 */
177 protected static final int REQUEST_LAST = 100;
178
Winson Chung2672ff92012-05-04 16:22:30 -0700179 // The Intent extra that defines whether to ignore the launch animation
180 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400181 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700182
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 // Type: int
184 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700185 // Type: int
186 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700187 // Type: PendingRequestArgs
188 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
189 // Type: ActivityResultInfo
190 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700191 // Type: SparseArray<Parcelable>
192 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193
Sunny Goyal7c8a65e2017-12-22 11:11:33 -0800194 // When starting an action mode, setting this tag will cause the action mode to be cancelled
195 // automatically when user interacts with the launcher.
196 public static final Object AUTO_CANCEL_ACTION_MODE = new Object();
197
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700198 private LauncherStateManager mStateManager;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700199
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700200 private boolean mIsSafeModeEnabled;
201
Adam Cohenad4e15c2013-10-17 16:21:35 -0700202 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800203
Winson Chunga2413752012-04-03 14:22:34 -0700204 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700205 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
206 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
207 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700208
Jon Miranda54441f52018-01-24 15:38:25 -0800209 private LauncherAppTransitionManager mAppTransitionManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800210 private Configuration mOldConfig;
Jon Miranda54441f52018-01-24 15:38:25 -0800211
Adam Cohen091440a2015-03-18 14:16:05 -0700212 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700213 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700214 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800215 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700216
Sunny Goyalffe83f12014-08-14 17:39:34 -0700217 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700218 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700219
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700220 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700221
Sunny Goyal316490e2015-06-02 09:38:28 -0700222 @Thunk Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700223
Sunny Goyal47328fd2016-05-25 18:56:41 -0700224 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700225
226 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700227 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700228 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700229
Winson Chung8ae41982017-11-10 11:42:13 -0800230 // UI and state for the overview panel
231 private ViewGroup mOverviewPanel;
232
Adam Cohen091440a2015-03-18 14:16:05 -0700233 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400234
Sunny Goyal60876ac2018-02-20 14:21:20 -0800235 private OnStartCallback mOnStartCallback;
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700236 private OnResumeCallback mOnResumeCallback;
237
Sunny Goyal527c7d32015-08-28 15:19:36 -0700238 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700239
Joe Onorato9c1289c2009-08-17 11:03:03 -0400240 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800241 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800242 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700243 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400244
Jon Miranda2d89ea82017-05-04 11:47:53 -0700245 private ObjectAnimator mScrimAnimator;
Jon Miranda7fda2852017-07-19 16:07:01 -0700246 private boolean mShouldFadeInScrim;
Jon Miranda2d89ea82017-05-04 11:47:53 -0700247
Tony Wickham010d2552017-01-20 08:15:28 -0800248 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700249
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800250 private int mSynchronouslyBoundPage = PagedView.INVALID_PAGE;
Adam Cohen1462de32012-07-24 22:34:36 -0700251
Winson Chung46353de2012-02-16 14:05:10 -0800252 // We only want to get the SharedPreferences once since it does an FS stat each time we get
253 // it from the context.
254 private SharedPreferences mSharedPrefs;
255
Sunny Goyal2100c782016-08-22 16:00:03 -0700256 // Activity result which needs to be processed after workspace has loaded.
257 private ActivityResultInfo mPendingActivityResult;
258 /**
259 * Holds extra information required to handle a result from an external call, like
260 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
261 */
262 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800263
Sunny Goyal02424b22018-01-19 11:24:32 -0800264 private final PointF mLastDispatchTouchEvent = new PointF();
Jon Miranda379198e2016-09-23 08:54:40 -0700265
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700266 public ViewGroupFocusHelper mFocusHandler;
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700267 private boolean mAppLaunchSuccess;
268
Sunny Goyal623eddd2018-03-02 12:24:41 -0800269 private RotationHelper mRotationHelper;
Sunny Goyal7c8a65e2017-12-22 11:11:33 -0800270 private ActionMode mCurrentActionMode;
Sunny Goyal7779d622015-06-11 16:18:39 -0700271
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800272 @Override
273 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700274 if (DEBUG_STRICT_MODE) {
275 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
276 .detectDiskReads()
277 .detectDiskWrites()
278 .detectNetwork() // or .detectAll() for all detectable problems
279 .penaltyLog()
280 .build());
281 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
282 .detectLeakedSqlLiteObjects()
283 .detectLeakedClosableObjects()
284 .penaltyLog()
285 .penaltyDeath()
286 .build());
287 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700288 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700289
Mario Bertschler27288382017-05-24 15:35:09 -0700290 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
291 wallpaperColorInfo.setOnThemeChangeListener(this);
Mario Bertschlera6936942017-05-31 14:48:19 -0700292 overrideTheme(wallpaperColorInfo.isDark(), wallpaperColorInfo.supportsDarkText());
Mario Bertschler27288382017-05-24 15:35:09 -0700293
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800294 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700295 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400296
Sunny Goyal87f784c2017-01-11 10:48:34 -0800297 LauncherAppState app = LauncherAppState.getInstance(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800298 mOldConfig = new Configuration(getResources().getConfiguration());
Sunny Goyal0e7724c2018-02-22 13:22:21 -0800299 mModel = app.setLauncher(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800300 initDeviceProfile(app.getInvariantDeviceProfile());
Winson Chung6e1c0d32013-10-25 15:24:24 -0700301
Sunny Goyalf7258242015-10-19 16:59:07 -0700302 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700303 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800304 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700305 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700306
Joe Onorato41a12d22009-10-31 18:30:00 -0400307 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700308 mAllAppsController = new AllAppsTransitionController(this);
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800309 mStateManager = new LauncherStateManager(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700310
Sunny Goyalffe83f12014-08-14 17:39:34 -0700311 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700312
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700313 mAppWidgetHost = new LauncherAppWidgetHost(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700314 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700315
Sunny Goyal60820d72017-05-09 12:40:11 -0700316 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700317
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800318 setupViews();
Tony Wickham010d2552017-01-20 08:15:28 -0800319 mPopupDataProvider = new PopupDataProvider(this);
320
Sunny Goyal623eddd2018-03-02 12:24:41 -0800321 mRotationHelper = new RotationHelper(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800322
Sunny Goyald3864fa2017-11-14 15:06:36 -0800323 boolean internalStateHandled = InternalStateHandler.handleCreate(this, getIntent());
324 if (internalStateHandled) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800325 if (savedInstanceState != null) {
326 // InternalStateHandler has already set the appropriate state.
327 // We dont need to do anything.
328 savedInstanceState.remove(RUNTIME_STATE);
329 }
330 }
331 restoreState(savedInstanceState);
Winson Chungb63b44c2017-11-10 17:54:44 -0800332
Sunny Goyal2100c782016-08-22 16:00:03 -0700333 // We only load the page synchronously if the user rotates (or triggers a
334 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700335 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
336 if (savedInstanceState != null) {
337 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
338 }
Sunny Goyald3864fa2017-11-14 15:06:36 -0800339
Sunny Goyalfe770c92016-09-27 14:38:58 -0700340 if (!mModel.startLoader(currentScreen)) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800341 if (!internalStateHandled) {
342 // If we are not binding synchronously, show a fade in animation when
343 // the first page bind completes.
344 mDragLayer.setAlpha(0);
345 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700346 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700347 // Pages bound synchronously.
348 mWorkspace.setCurrentPage(currentScreen);
349
Sunny Goyal2100c782016-08-22 16:00:03 -0700350 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800351 }
352
353 // For handling default keys
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800354 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800355
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800356 setContentView(mLauncherView);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800357 getRootView().dispatchInsets();
Mario Bertschlera6936942017-05-31 14:48:19 -0700358
Jon Miranda7fda2852017-07-19 16:07:01 -0700359 // Listen for broadcasts
360 IntentFilter filter = new IntentFilter();
361 filter.addAction(Intent.ACTION_SCREEN_OFF);
362 filter.addAction(Intent.ACTION_USER_PRESENT); // When the device wakes up + keyguard is gone
363 registerReceiver(mReceiver, filter);
364 mShouldFadeInScrim = true;
Sunny Goyala616d2b2017-06-12 13:54:01 -0700365
Sunny Goyal8392c822017-06-20 10:03:56 -0700366 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
367 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700368
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800369 mAppTransitionManager = LauncherAppTransitionManager.newInstance(this);
Jon Miranda7853bdb2018-01-22 17:32:30 -0800370
Sunny Goyal14b32402017-07-31 10:03:28 -0700371 if (mLauncherCallbacks != null) {
372 mLauncherCallbacks.onCreate(savedInstanceState);
373 }
Sunny Goyal623eddd2018-03-02 12:24:41 -0800374 mRotationHelper.initialize();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700375
376 TraceHelper.endSection("Launcher-onCreate");
Adam Cohen9211d422014-10-07 18:14:53 -0700377 }
378
Sunny Goyal7779d622015-06-11 16:18:39 -0700379 @Override
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800380 public void onConfigurationChanged(Configuration newConfig) {
381 int diff = newConfig.diff(mOldConfig);
382 if ((diff & (CONFIG_ORIENTATION | CONFIG_SCREEN_SIZE)) != 0) {
383 mUserEventDispatcher = null;
384 initDeviceProfile(mDeviceProfile.inv);
Sunny Goyal2bba1902018-02-27 12:20:50 -0800385 FileLog.d(TAG, "Config changed, my orientation=" +
386 getResources().getConfiguration().orientation +
387 ", new orientation=" + newConfig.orientation +
388 ", old orientation=" + mOldConfig.orientation +
389 ", isTransposed=" + mDeviceProfile.isVerticalBarLayout());
Sunny Goyalfde55052018-02-01 14:46:13 -0800390 dispatchDeviceProfileChanged();
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800391
392 getRootView().dispatchInsets();
393 getStateManager().reapplyState();
394
395 // TODO: We can probably avoid rebind when only screen size changed.
Sunny Goyal605bcf32018-03-02 15:16:12 -0800396 rebindModel();
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800397 }
398
399 mOldConfig.setTo(newConfig);
400 super.onConfigurationChanged(newConfig);
401 }
402
Sunny Goyal605bcf32018-03-02 15:16:12 -0800403 @Override
404 public void rebindModel() {
405 int currentPage = mWorkspace.getNextPage();
406 if (mModel.startLoader(currentPage)) {
407 mWorkspace.setCurrentPage(currentPage);
408 setWorkspaceLoading(true);
409 }
410 }
411
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800412 private void initDeviceProfile(InvariantDeviceProfile idp) {
413 // Load configuration-specific DeviceProfile
414 mDeviceProfile = idp.getDeviceProfile(this);
415 if (isInMultiWindowModeCompat()) {
416 Display display = getWindowManager().getDefaultDisplay();
417 Point mwSize = new Point();
418 display.getSize(mwSize);
419 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
420 }
Sunny Goyal605bcf32018-03-02 15:16:12 -0800421 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout(), true);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800422 }
423
Sunny Goyal623eddd2018-03-02 12:24:41 -0800424 public RotationHelper getRotationHelper() {
425 return mRotationHelper;
426 }
427
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800428 @Override
Mario Bertschler27288382017-05-24 15:35:09 -0700429 public void onThemeChanged() {
430 recreate();
431 }
432
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700433 public LauncherStateManager getStateManager() {
434 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700435 }
436
Tonydc76f8e2018-02-02 21:45:39 -0600437 public LauncherAppTransitionManager getAppTransitionManager() {
438 return mAppTransitionManager;
439 }
440
Mario Bertschlera6936942017-05-31 14:48:19 -0700441 protected void overrideTheme(boolean isDark, boolean supportsDarkText) {
Mario Bertschler27288382017-05-24 15:35:09 -0700442 if (isDark) {
443 setTheme(R.style.LauncherThemeDark);
Mario Bertschlera6936942017-05-31 14:48:19 -0700444 } else if (supportsDarkText) {
445 setTheme(R.style.LauncherThemeDarkText);
Mario Bertschler27288382017-05-24 15:35:09 -0700446 }
447 }
448
449 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700450 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800451 return mLauncherView.findViewById(id);
452 }
453
454 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700455 public void onAppWidgetHostReset() {
456 if (mAppWidgetHost != null) {
457 mAppWidgetHost.startListening();
458 }
459 }
460
Adam Cohen9211d422014-10-07 18:14:53 -0700461 private LauncherCallbacks mLauncherCallbacks;
462
Sunny Goyal32554d12015-12-03 15:31:25 -0800463 /**
464 * Call this after onCreate to set or clear overlay.
465 */
466 public void setLauncherOverlay(LauncherOverlay overlay) {
467 if (overlay != null) {
468 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
469 }
470 mWorkspace.setLauncherOverlay(overlay);
471 }
472
Adam Cohen9211d422014-10-07 18:14:53 -0700473 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
474 mLauncherCallbacks = callbacks;
475 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700476 }
477
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700478 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700479 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700480 if (mLauncherCallbacks != null) {
481 mLauncherCallbacks.onLauncherProviderChange();
482 }
483 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700484
Hyunyoung Song3f471442015-04-08 19:01:34 -0700485 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700486 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
487 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700488 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700489 }
490
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000491 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700492 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
493 // This cast is safe as long as the id < 0x00FFFFFF
494 // Since we jail all the dynamically generated views, there should be no clashes
495 // with any other views.
496 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000497 }
498
Tony Wickham010d2552017-01-20 08:15:28 -0800499 public PopupDataProvider getPopupDataProvider() {
500 return mPopupDataProvider;
501 }
502
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000503 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700504 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
505 * a configuration step, this allows the proper animations to run after other transitions.
506 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700507 private long completeAdd(
508 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
509 long screenId = info.screenId;
510 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700511 // When the screen id represents an actual screen (as opposed to a rank) we make sure
512 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700513 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700514 }
Adam Cohendb364c32014-05-20 14:23:40 -0700515
Sunny Goyal2100c782016-08-22 16:00:03 -0700516 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800517 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700518 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700519 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800520 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700521 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700522 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700523 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700524 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700525 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700526 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700527 int widgetId = appWidgetId;
528 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700529 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700530 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700531 // Since the view was just bound, also launch the configure activity if needed
532 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
533 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800534 if (provider != null) {
535 new WidgetAddFlowHandler(provider)
536 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700537 }
538 }
539 break;
540 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800541 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700542
Adam Cohendb364c32014-05-20 14:23:40 -0700543 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800544 }
545
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800546 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700547 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700548 if (isWorkspaceLoading()) {
549 // process the result once the workspace has loaded.
550 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
551 return;
552 }
553 mPendingActivityResult = null;
554
Winson Chunge341d302013-08-16 14:31:00 -0700555 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700556 final PendingRequestArgs requestArgs = mPendingRequestArgs;
557 setWaitingForResult(null);
558 if (requestArgs == null) {
559 return;
560 }
561
562 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700563
Adam Cohenad4e15c2013-10-17 16:21:35 -0700564 Runnable exitSpringLoaded = new Runnable() {
565 @Override
566 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700567 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700568 }
569 };
570
Michael Jurka8b805b12012-04-18 14:23:14 -0700571 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700572 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700573 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700574 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
575 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700576 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700577 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700578 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700579 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700580 addAppWidgetImpl(
581 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800582 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700583 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700584 }
585 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200586 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700587 if (resultCode == RESULT_OK && isInState(OVERVIEW)) {
Tony Wickhame3054412015-09-09 09:05:25 -0700588 // User could have free-scrolled between pages before picking a wallpaper; make sure
589 // we move to the closest one now.
590 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700591 mStateManager.goToState(NORMAL, false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200592 }
593 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700594 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200595
Adam Cohened66b2b2012-01-23 17:28:51 -0800596 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700597 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700598
Adam Cohened66b2b2012-01-23 17:28:51 -0800599 // We have special handling for widgets
600 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800601 final int appWidgetId;
602 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
603 : -1;
604 if (widgetId < 0) {
605 appWidgetId = pendingAddWidgetId;
606 } else {
607 appWidgetId = widgetId;
608 }
609
Adam Cohenad4e15c2013-10-17 16:21:35 -0700610 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800611 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700612 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
613 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700614 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700615 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700616 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700617 @Override
618 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700619 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700620 }
621 };
Adam Cohendb364c32014-05-20 14:23:40 -0700622
Sunny Goyal2100c782016-08-22 16:00:03 -0700623 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
624 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
625 } else {
626 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
627 // When the screen id represents an actual screen (as opposed to a rank)
628 // we make sure that the drop page actually exists.
629 requestArgs.screenId =
630 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700631 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700632 final CellLayout dropLayout =
633 mWorkspace.getScreenWithId(requestArgs.screenId);
634
635 dropLayout.setDropPending(true);
636 final Runnable onComplete = new Runnable() {
637 @Override
638 public void run() {
639 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
640 dropLayout.setDropPending(false);
641 }
642 };
643 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
644 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700645 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800646 return;
647 }
648
Sunny Goyald478c832016-04-01 12:04:16 -0700649 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
650 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700651 if (resultCode == RESULT_OK) {
652 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700653 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700654 }
655 // Leave the widget in the pending state if the user canceled the configure.
656 return;
657 }
658
Sunny Goyalaad90582015-07-09 14:22:50 -0700659 if (requestCode == REQUEST_CREATE_SHORTCUT) {
660 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700661 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
662 completeAdd(requestCode, data, -1, requestArgs);
663 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
664 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
665
Sunny Goyalaad90582015-07-09 14:22:50 -0700666 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700667 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
668 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800669 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800670 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800671 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800672 }
673
674 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700675 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800676 final int requestCode, final int resultCode, final Intent data) {
677 handleActivityResult(requestCode, resultCode, data);
678 if (mLauncherCallbacks != null) {
679 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
680 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800681 }
682
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700683 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700684 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600685 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700686 PendingRequestArgs pendingArgs = mPendingRequestArgs;
687 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
688 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
689 setWaitingForResult(null);
690
Sunny Goyal28c6b962015-10-12 11:42:05 -0700691 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700692 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700693 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700694 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700695 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700696 Intent intent = pendingArgs.getPendingIntent();
697
Sunny Goyal28c6b962015-10-12 11:42:05 -0700698 if (grantResults.length > 0
699 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700700 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700701 } else {
702 // TODO: Show a snack bar with link to settings
703 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700704 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700705 }
706 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600707 if (mLauncherCallbacks != null) {
708 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
709 grantResults);
710 }
711 }
712
Adam Cohendb364c32014-05-20 14:23:40 -0700713 /**
714 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
715 *
716 * @param screenId the screen id to check
717 * @return the new screen, or screenId if it exists
718 */
719 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800720 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700721 if (dropLayout == null) {
722 // it's possible that the add screen was removed because it was
723 // empty and a re-bind occurred
724 mWorkspace.addExtraEmptyScreen();
725 return mWorkspace.commitExtraEmptyScreen();
726 } else {
727 return screenId;
728 }
729 }
730
Sunny Goyal2100c782016-08-22 16:00:03 -0700731 @Thunk void completeTwoStageWidgetDrop(
732 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
733 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800734 Runnable onCompleteRunnable = null;
735 int animationType = 0;
736
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700737 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800738 if (resultCode == RESULT_OK) {
739 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
740 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800741 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700742 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800743 onCompleteRunnable = new Runnable() {
744 @Override
745 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700746 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700747 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800748 }
749 };
750 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800751 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800752 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800753 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700754 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700755 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700756 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
757 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700758 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700759 // The animated view may be null in the case of a rotation during widget configuration
760 onCompleteRunnable.run();
761 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800762 }
763
764 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700765 protected void onStop() {
766 super.onStop();
767 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700768
769 if (mLauncherCallbacks != null) {
770 mLauncherCallbacks.onStop();
771 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800772 mAppWidgetHost.setListenIfResumed(false);
Tony Wickham010d2552017-01-20 08:15:28 -0800773
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700774 if (!mAppLaunchSuccess) {
775 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -0800776 mStateManager.getState().containerType, -1);
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700777 }
Tony Wickham010d2552017-01-20 08:15:28 -0800778 NotificationListener.removeNotificationsChangedListener();
Sunny Goyalaf161fd2018-02-05 16:34:57 -0800779 getStateManager().moveToRestState();
Michael Jurkacd496d72013-04-11 11:32:45 -0700780 }
781
782 @Override
783 protected void onStart() {
784 super.onStart();
785 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700786
Sunny Goyal60876ac2018-02-20 14:21:20 -0800787 if (mOnStartCallback != null) {
788 mOnStartCallback.onLauncherStart(this);
789 mOnStartCallback = null;
790 }
Adam Cohen9211d422014-10-07 18:14:53 -0700791 if (mLauncherCallbacks != null) {
792 mLauncherCallbacks.onStart();
793 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800794 mAppWidgetHost.setListenIfResumed(true);
Sunny Goyalcac002d2018-01-04 09:51:45 -0800795 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
Jon Miranda2d89ea82017-05-04 11:47:53 -0700796
Jon Mirandade43a712018-01-18 11:35:10 -0800797 if (mShouldFadeInScrim && mLauncherView.getBackground() != null) {
Jon Miranda2d89ea82017-05-04 11:47:53 -0700798 if (mScrimAnimator != null) {
799 mScrimAnimator.cancel();
800 }
Jon Mirandade43a712018-01-18 11:35:10 -0800801 mLauncherView.getBackground().setAlpha(0);
802 mScrimAnimator = ObjectAnimator.ofInt(mLauncherView.getBackground(),
Jon Miranda2d89ea82017-05-04 11:47:53 -0700803 LauncherAnimUtils.DRAWABLE_ALPHA, 0, 255);
804 mScrimAnimator.addListener(new AnimatorListenerAdapter() {
805 @Override
806 public void onAnimationEnd(Animator animation) {
807 mScrimAnimator = null;
808 }
809 });
810 mScrimAnimator.setDuration(600);
811 mScrimAnimator.setStartDelay(getWindow().getTransitionBackgroundFadeDuration());
812 mScrimAnimator.start();
813 }
Jon Miranda7fda2852017-07-19 16:07:01 -0700814 mShouldFadeInScrim = false;
Winson Chung62a70be2018-02-23 15:10:05 -0800815 UiFactory.onStart(this);
Michael Jurkacd496d72013-04-11 11:32:45 -0700816 }
817
818 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800819 protected void onResume() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700820 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800821 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700822 TraceHelper.partitionSection("ON_RESUME", "superCall");
823
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700824 mAppLaunchSuccess = false;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700825 getUserEventDispatcher().resetElapsedSessionMillis();
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700826 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700827 // Process any items that were added while Launcher was away.
828 InstallShortcutReceiver.disableAndFlushInstallQueue(
829 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700830
Sunny Goyala474a9b2017-05-04 16:47:11 -0700831 // Refresh shortcuts if the permission changed.
832 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700833
Sunny Goyal7ede6112017-12-05 15:11:21 -0800834 DiscoveryBounce.showIfNeeded(this);
Adam Cohen9211d422014-10-07 18:14:53 -0700835 if (mLauncherCallbacks != null) {
836 mLauncherCallbacks.onResume();
837 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800838
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700839 TraceHelper.endSection("ON_RESUME");
Adam Cohen06dff352012-06-01 17:17:08 -0700840 }
841
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800842 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700843 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700844 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700845 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700846
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700847 super.onPause();
Joe Onorato24b6fd82009-11-12 13:47:09 -0800848 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700849 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700850
Adam Cohen9211d422014-10-07 18:14:53 -0700851 if (mLauncherCallbacks != null) {
852 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700853 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700854 }
855
Winson Chungd7823942018-02-16 03:23:47 +0000856 @Override
857 public void onWindowFocusChanged(boolean hasFocus) {
858 super.onWindowFocusChanged(hasFocus);
859 mStateManager.onWindowFocusChanged();
860 }
861
Adam Cohenc2d6e892014-10-16 09:49:24 -0700862 public interface LauncherOverlay {
863
864 /**
865 * Touch interaction leading to overscroll has begun
866 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700867 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700868
869 /**
870 * Touch interaction related to overscroll has ended
871 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700872 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700873
874 /**
875 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
876 * screen (or in the case of RTL, the rightmost screen).
877 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700878 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700879
880 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800881 * Called when the launcher is ready to use the overlay
882 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700883 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700884 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700885 }
886
887 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700888 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700889 }
890
891 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
892
Sunny Goyalc86df472016-02-25 09:19:38 -0800893 public void onScrollChanged(float progress) {
894 if (mWorkspace != null) {
895 mWorkspace.onOverlayScrollChanged(progress);
896 }
897 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700898 }
899
Sunny Goyal16764582017-11-06 16:00:34 -0800900 public boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -0700901 if (mLauncherCallbacks != null) {
902 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700903 } else {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800904 // On O and above we there is always some setting present settings (add icon to
905 // home screen or icon badging). On earlier APIs we will have the allow rotation
906 // setting, on devices with a locked orientation,
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700907 return Utilities.ATLEAST_OREO || !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -0700908 }
Jorim Jaggid017f882014-01-14 17:08:48 -0800909 }
910
Sunny Goyalf9403d92017-10-18 10:55:56 -0700911 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -0700912 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700913 }
914
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800915 /**
916 * Restores the previous state, if it exists.
917 *
918 * @param savedState The previous state.
919 */
920 private void restoreState(Bundle savedState) {
921 if (savedState == null) {
922 return;
923 }
924
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700925 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -0700926 LauncherState[] stateValues = LauncherState.values();
927 LauncherState state = stateValues[stateOrdinal];
Sunny Goyalbc683e92017-12-06 10:25:07 -0800928 if (!state.disableRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700929 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800930 }
931
Sunny Goyal2100c782016-08-22 16:00:03 -0700932 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
933 if (requestArgs != null) {
934 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800935 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700936
937 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700938
939 SparseArray<Parcelable> widgetsState =
940 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
941 if (widgetsState != null) {
942 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
943 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800944 }
945
946 /**
947 * Finds all the views we need and configure them properly.
948 */
949 private void setupViews() {
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800950 mDragLayer = findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700951 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -0700952 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700953 mWorkspace.initParentViews(mDragLayer);
Sunny Goyal16764582017-11-06 16:00:34 -0800954 mOverviewPanel = findViewById(R.id.overview_panel);
Craig Mautner360310b2012-10-26 15:13:08 -0700955
Sunny Goyal784f9c32016-03-23 16:56:54 -0700956 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
957 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
958 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800959
Winson Chung4c98d922011-05-31 16:50:48 -0700960 // Setup the drag layer
Sunny Goyal7ede6112017-12-05 15:11:21 -0800961 mDragLayer.setup(this, mDragController);
Winson Chung4c98d922011-05-31 16:50:48 -0700962
Winson Chung3d503fb2011-07-13 17:25:49 -0700963 // Setup the hotseat
964 mHotseat = (Hotseat) findViewById(R.id.hotseat);
965 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -0700966 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -0700967 }
968
Winson Chung4c98d922011-05-31 16:50:48 -0700969 // Setup the workspace
970 mWorkspace.setHapticFeedbackEnabled(false);
971 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700972 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -0700973 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
974 // default state, otherwise we will update to the wrong offsets in RTL
975 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700976 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700977 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700978
Tony Wickham34d2c912015-09-11 09:27:58 -0700979 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700980 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700981
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700982 // Setup Apps
983 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -0700984
Winson Chung3d503fb2011-07-13 17:25:49 -0700985 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -0700986 mDragController.setMoveTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700987 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -0400988
Sunny Goyal6639a5d2018-02-28 15:09:36 -0800989 mAllAppsController.setupViews(mAppsView, mHotseat);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800990 }
991
992 /**
993 * Creates a view representing a shortcut.
994 *
995 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800996 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800997 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700998 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800999 }
1000
1001 /**
1002 * Creates a view representing a shortcut inflated from the specified resource.
1003 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001004 * @param parent The group the shortcut belongs to.
1005 * @param info The data structure describing the shortcut.
1006 *
1007 * @return A View inflated from layoutResId.
1008 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001009 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -08001010 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
1011 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001012 favorite.applyFromShortcutInfo(info);
Sunny Goyal8a2a63b2018-03-06 22:15:18 -08001013 favorite.setOnClickListener(ItemClickHandler.INSTANCE);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001014 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001015 return favorite;
1016 }
1017
1018 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -07001019 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001020 *
1021 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001022 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001023 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001024 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001025 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
1026 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001027 return;
1028 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001029
Jon Mirandac476d6e2017-05-04 16:30:01 -07001030 int[] cellXY = mTmpAddItemCellCoordinates;
1031 CellLayout layout = getCellLayout(container, screenId);
1032
Sunny Goyal782f0c92017-01-19 10:27:54 -08001033 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001034 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001035 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
1036 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001037 }
1038
1039 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001040 // Legacy shortcuts are only supported for primary profile.
1041 info = Process.myUserHandle().equals(args.user)
1042 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001043
Sunny Goyalb57645f2017-03-20 13:57:28 -07001044 if (info == null) {
1045 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1046 return;
1047 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001048 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001049 // The app is trying to add a shortcut without sufficient permissions
1050 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1051 return;
1052 }
1053 }
1054
Jon Mirandac476d6e2017-05-04 16:30:01 -07001055 if (container < 0) {
1056 // Adding a shortcut to the Workspace.
1057 final View view = createShortcut(info);
1058 boolean foundCellSpan = false;
1059 // First we check if we already know the exact location where we want to add this item.
1060 if (cellX >= 0 && cellY >= 0) {
1061 cellXY[0] = cellX;
1062 cellXY[1] = cellY;
1063 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001064
Jon Mirandac476d6e2017-05-04 16:30:01 -07001065 // If appropriate, either create a folder or add to an existing folder
1066 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001067 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001068 return;
1069 }
1070 DragObject dragObject = new DragObject();
1071 dragObject.dragInfo = info;
1072 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1073 true)) {
1074 return;
1075 }
1076 } else {
1077 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1078 }
1079
1080 if (!foundCellSpan) {
1081 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001082 return;
1083 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001084
1085 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1086 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001087 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001088 // Adding a shortcut to a Folder.
Sunny Goyale29897f52017-07-20 10:09:42 -07001089 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001090 if (folderIcon != null) {
1091 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1092 folderInfo.add(info, args.rank, false);
1093 } else {
Sunny Goyale29897f52017-07-20 10:09:42 -07001094 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001095 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001096 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001097 }
1098
Sunny Goyale29897f52017-07-20 10:09:42 -07001099 public FolderIcon findFolderIcon(final long folderIconId) {
1100 return (FolderIcon) mWorkspace.getFirstMatch(new ItemOperator() {
1101 @Override
1102 public boolean evaluate(ItemInfo info, View view) {
1103 return info != null && info.id == folderIconId;
1104 }
1105 });
1106 }
1107
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001108 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001109 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001110 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001111 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001112 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001113 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001114 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1115
Adam Cohened66b2b2012-01-23 17:28:51 -08001116 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001117 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001118 }
Romain Guycbb89e42009-06-08 15:52:54 -07001119
Adam Cohen59400422014-03-05 18:07:04 -08001120 LauncherAppWidgetInfo launcherInfo;
1121 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001122 launcherInfo.spanX = itemInfo.spanX;
1123 launcherInfo.spanY = itemInfo.spanY;
1124 launcherInfo.minSpanX = itemInfo.minSpanX;
1125 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001126 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001127
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001128 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001129 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001130
Sunny Goyal2100c782016-08-22 16:00:03 -07001131 if (hostView == null) {
1132 // Perform actual inflation because we're live
1133 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001134 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001135 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301136 prepareAppWidget(hostView, launcherInfo);
1137 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001138 }
Romain Guycbb89e42009-06-08 15:52:54 -07001139
Sunny Goyald5462aa2016-11-22 16:52:39 +05301140 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001141 hostView.setTag(item);
1142 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001143 hostView.setFocusable(true);
1144 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001145 }
1146
Adam Cohended9f8d2010-11-03 13:25:16 -07001147 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1148 @Override
1149 public void onReceive(Context context, Intent intent) {
1150 final String action = intent.getAction();
1151 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001152 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001153 // processing a multi-step drop
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001154 if (mAppsView != null && mPendingRequestArgs == null) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001155 mStateManager.goToState(NORMAL);
Winson Chung785d2eb2011-04-14 16:08:02 -07001156 }
Jon Miranda7fda2852017-07-19 16:07:01 -07001157 mShouldFadeInScrim = true;
1158 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1159 // ACTION_USER_PRESENT is sent after onStart/onResume. This covers the case where
1160 // the user unlocked and the Launcher is not in the foreground.
1161 mShouldFadeInScrim = false;
Adam Cohended9f8d2010-11-03 13:25:16 -07001162 }
1163 }
1164 };
1165
Tony Wickham010d2552017-01-20 08:15:28 -08001166 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
Sunny Goyal29947f02017-12-18 13:49:44 -08001167 mWorkspace.updateIconBadges(updatedBadges);
Sunny Goyal60180b02018-02-07 12:56:30 -08001168 mAppsView.getAppsStore().updateIconBadges(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001169
Sunny Goyal29947f02017-12-18 13:49:44 -08001170 PopupContainerWithArrow popup = PopupContainerWithArrow.getOpen(Launcher.this);
1171 if (popup != null) {
1172 popup.updateNotificationHeader(updatedBadges);
Tony Wickham010d2552017-01-20 08:15:28 -08001173 }
1174 }
1175
Adam Cohended9f8d2010-11-03 13:25:16 -07001176 @Override
1177 public void onAttachedToWindow() {
1178 super.onAttachedToWindow();
1179
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001180 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyalc86df472016-02-25 09:19:38 -08001181 if (mLauncherCallbacks != null) {
1182 mLauncherCallbacks.onAttachedToWindow();
1183 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001184 }
1185
1186 @Override
1187 public void onDetachedFromWindow() {
1188 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001189
1190 if (mLauncherCallbacks != null) {
1191 mLauncherCallbacks.onDetachedFromWindow();
1192 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001193 }
1194
Sunny Goyal3bac2c22018-02-07 14:44:05 -08001195 public void onQuickstepGestureStarted(boolean isVisible) {
1196 if (mLauncherCallbacks != null) {
1197 mLauncherCallbacks.onQuickstepGestureStarted(isVisible);
1198 }
1199 }
1200
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001201 public AllAppsTransitionController getAllAppsController() {
1202 return mAllAppsController;
1203 }
1204
Winson Chung1a341002018-01-17 10:00:23 -08001205 public LauncherRootView getRootView() {
1206 return (LauncherRootView) mLauncherView;
1207 }
1208
Winson Chunga6945242014-01-08 14:04:34 -08001209 public DragLayer getDragLayer() {
1210 return mDragLayer;
1211 }
1212
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001213 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001214 return mAppsView;
1215 }
1216
Winson Chunga6945242014-01-08 14:04:34 -08001217 public Workspace getWorkspace() {
1218 return mWorkspace;
1219 }
1220
1221 public Hotseat getHotseat() {
1222 return mHotseat;
1223 }
1224
Winson Chung8ae41982017-11-10 11:42:13 -08001225 public <T extends ViewGroup> T getOverviewPanel() {
1226 return (T) mOverviewPanel;
Winson Chunga6945242014-01-08 14:04:34 -08001227 }
1228
Sunny Goyal47328fd2016-05-25 18:56:41 -07001229 public DropTargetBar getDropTargetBar() {
1230 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001231 }
1232
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001233 public LauncherAppWidgetHost getAppWidgetHost() {
1234 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001235 }
Romain Guycbb89e42009-06-08 15:52:54 -07001236
Winson Chunga9abd0e2010-10-27 17:18:37 -07001237 public LauncherModel getModel() {
1238 return mModel;
1239 }
1240
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001241 public ModelWriter getModelWriter() {
1242 return mModelWriter;
1243 }
1244
Adam Cohen79d90c52016-04-22 13:29:20 -07001245 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001246 return mSharedPrefs;
1247 }
1248
Sunny Goyalf8d56fc2018-01-31 15:18:11 -08001249 public int getOrientation() { return mOldConfig.orientation; }
Jon Miranda6bed3502017-09-19 14:43:17 -07001250
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001251 @Override
1252 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001253 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001254 super.onNewIntent(intent);
1255
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001256 boolean alreadyOnHome = hasWindowFocus() && ((intent.getFlags() &
Winson15f8b172015-08-19 11:02:39 -07001257 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1258 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001259
1260 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001261 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001262 && AbstractFloatingView.getTopOpenView(this) == null;
Winson15f8b172015-08-19 11:02:39 -07001263 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001264 boolean internalStateHandled = InternalStateHandler
Sunny Goyal8b264562018-02-03 23:00:13 -08001265 .handleNewIntent(this, intent, isStarted());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001266
Winson15f8b172015-08-19 11:02:39 -07001267 if (isActionMain) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001268 if (!internalStateHandled) {
1269 // Note: There should be at most one log per method call. This is enforced
1270 // implicitly by using if-else statements.
1271 UserEventDispatcher ued = getUserEventDispatcher();
1272 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
1273 if (topOpenView != null) {
1274 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
1275 } else if (alreadyOnHome) {
1276 Target target = newContainerTarget(mStateManager.getState().containerType);
1277 target.pageIndex = mWorkspace.getCurrentPage();
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001278 ued.logActionCommand(Action.Command.HOME_INTENT, target,
1279 newContainerTarget(ContainerType.WORKSPACE));
Sunny Goyald3864fa2017-11-14 15:06:36 -08001280 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001281
Sunny Goyald3864fa2017-11-14 15:06:36 -08001282 // In all these cases, only animate if we're already on home
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001283 AbstractFloatingView.closeAllOpenViews(this, isStarted());
Jon Mirandafeba90f2016-10-06 10:53:29 -07001284
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001285 mStateManager.goToState(NORMAL);
Sunny Goyald3864fa2017-11-14 15:06:36 -08001286
1287 // Reset the apps view
1288 if (!alreadyOnHome && mAppsView != null) {
1289 mAppsView.reset();
1290 }
1291
1292 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()) {
1293 mWorkspace.post(mWorkspace::moveToDefaultScreen);
1294 }
1295 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001296
1297 final View v = getWindow().peekDecorView();
1298 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001299 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001300 }
1301
Adam Cohen9211d422014-10-07 18:14:53 -07001302 if (mLauncherCallbacks != null) {
1303 mLauncherCallbacks.onHomeIntent();
1304 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001305 }
Winson15f8b172015-08-19 11:02:39 -07001306
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001307 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001308 }
1309
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001310 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001311 public void onRestoreInstanceState(Bundle state) {
1312 super.onRestoreInstanceState(state);
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001313 mWorkspace.restoreInstanceStateForChild(mSynchronouslyBoundPage);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001314 }
1315
1316 @Override
1317 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001318 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001319 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001320
Adam Cohen21cd0022013-10-09 18:57:02 -07001321 }
Sunny Goyalea609262017-10-25 15:47:38 -07001322 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001323
1324
1325 AbstractFloatingView widgets = AbstractFloatingView
1326 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1327 if (widgets != null) {
1328 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1329 widgets.saveHierarchyState(widgetsState);
1330 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1331 } else {
1332 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1333 }
1334
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001335 // We close any open folders and shortcut containers since they will not be re-opened,
1336 // and we need to make sure this state is reflected.
1337 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001338
Sunny Goyal2100c782016-08-22 16:00:03 -07001339 if (mPendingRequestArgs != null) {
1340 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1341 }
1342 if (mPendingActivityResult != null) {
1343 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001344 }
1345
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001346 super.onSaveInstanceState(outState);
1347
Adam Cohen9211d422014-10-07 18:14:53 -07001348 if (mLauncherCallbacks != null) {
1349 mLauncherCallbacks.onSaveInstanceState(outState);
1350 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001351 }
1352
1353 @Override
1354 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001355 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001356
Sunny Goyal5cc7bbd2017-06-12 09:50:39 -07001357 unregisterReceiver(mReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001358 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001359
Winson Chungcd2b0142011-06-08 16:02:26 -07001360 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001361 // It's possible to receive onDestroy after a new Launcher activity has
1362 // been created. In this case, don't interfere with the new Launcher.
1363 if (mModel.isCurrentCallbacks(this)) {
1364 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001365 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001366 }
Sunny Goyal623eddd2018-03-02 12:24:41 -08001367 mRotationHelper.destroy();
Sunny Goyal745bad92016-05-02 10:54:12 -07001368
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001369 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001370 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001371 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001372 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001373 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001374
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001375 TextKeyListener.getInstance().release();
Mario Bertschler27288382017-05-24 15:35:09 -07001376 WallpaperColorInfo.getInstance(this).setOnThemeChangeListener(null);
1377
Michael Jurka2ecf9952012-06-18 12:52:28 -07001378 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001379
Tony2917a8b2017-07-31 23:29:42 -07001380 clearPendingBinds();
1381
Adam Cohen9211d422014-10-07 18:14:53 -07001382 if (mLauncherCallbacks != null) {
1383 mLauncherCallbacks.onDestroy();
1384 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001385 }
1386
Sunny Goyalae502842016-06-17 08:43:56 -07001387 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1388 return mAccessibilityDelegate;
1389 }
1390
Adam Cohena9cf38f2011-05-02 15:36:58 -07001391 public DragController getDragController() {
1392 return mDragController;
1393 }
1394
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001395 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001396 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001397 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001398 }
1399
1400 @Override
1401 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1402 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001403 try {
1404 super.startIntentSenderForResult(intent, requestCode,
1405 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1406 } catch (IntentSender.SendIntentException e) {
1407 throw new ActivityNotFoundException();
1408 }
1409 }
1410
Winson Chung70d72102011-08-12 11:24:35 -07001411 /**
1412 * Indicates that we want global search for this activity by setting the globalSearch
1413 * argument for {@link #startSearch} to true.
1414 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001415 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001416 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001417 Bundle appSearchData, boolean globalSearch) {
1418 if (appSearchData == null) {
1419 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001420 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001421 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001422
Sunny Goyal6f28e712016-09-13 14:19:29 -07001423 if (mLauncherCallbacks == null ||
1424 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1425 // Starting search from the callbacks failed. Start the default global search.
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001426 super.startSearch(initialQuery, selectInitialQuery, appSearchData, true);
Ian Parkinson047036e2014-09-01 15:40:53 +01001427 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001428
1429 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001430 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001431 }
1432
Joe Onorato9c1289c2009-08-17 11:03:03 -04001433 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001434 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001435 }
1436
Adam Cohen517a7f52014-03-01 12:12:59 -08001437 public boolean isWorkspaceLoading() {
1438 return mWorkspaceLoading;
1439 }
1440
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001441 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001442 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001443 }
1444
Sunny Goyal04a324a2017-01-20 21:08:59 -08001445 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001446 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001447 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001448
Sunny Goyal2100c782016-08-22 16:00:03 -07001449 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001450 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001451 if (LOGD) {
1452 Log.d(TAG, "Adding widget from drop");
1453 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001454 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001455 }
1456
Sunny Goyal2100c782016-08-22 16:00:03 -07001457 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001458 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1459 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1460 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001461
Adam Cohenad4e15c2013-10-17 16:21:35 -07001462 Runnable onComplete = new Runnable() {
1463 @Override
1464 public void run() {
1465 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001466 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001467 }
1468 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001469 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001470 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001471 }
1472 }
Romain Guycbb89e42009-06-08 15:52:54 -07001473
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001474 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1475 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001476 info.container = container;
1477 info.screenId = screenId;
1478 if (cell != null) {
1479 info.cellX = cell[0];
1480 info.cellY = cell[1];
1481 }
1482 info.spanX = spanX;
1483 info.spanY = spanY;
1484
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001485 switch (info.itemType) {
1486 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1487 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001488 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001489 break;
1490 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001491 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001492 break;
1493 default:
1494 throw new IllegalStateException("Unknown item type: " + info.itemType);
1495 }
1496 }
1497
Adam Cohenfbba09b2011-07-18 21:43:05 -07001498 /**
1499 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001500 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001501 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001502 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1503 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001504 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1505 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1506 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001507 }
1508
Adam Cohenfbba09b2011-07-18 21:43:05 -07001509 /**
1510 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001511 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001512 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001513 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001514 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001515 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001516 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001517 // In the case where we've prebound the widget, we remove it from the DragLayer
1518 if (LOGD) {
1519 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1520 }
1521 getDragLayer().removeView(hostView);
1522
Adam Cohened66b2b2012-01-23 17:28:51 -08001523 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001524 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001525
1526 // Clear the boundWidget so that it doesn't get destroyed.
1527 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001528 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001529 // In this case, we either need to start an activity to get permission to bind
1530 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001531 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1532 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1533 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1534 this, info.componentName);
1535 } else {
1536 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1537 }
Adam Cohendd70d662012-10-04 16:53:44 -07001538 Bundle options = info.bindOptions;
1539
Sunny Goyalffe83f12014-08-14 17:39:34 -07001540 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1541 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001542 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001543 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001544 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001545 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001546 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001547 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001548 }
1549
Adam Cohendcd297f2013-06-18 13:13:40 -07001550 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001551 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001552 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001553 folderInfo.title = getText(R.string.folder_name);
1554
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001555 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001556 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001557
1558 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001559 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301560 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001561 // Force measure the new folder icon
1562 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1563 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001564 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001565 }
Romain Guycbb89e42009-06-08 15:52:54 -07001566
Winsonc0b52fe2015-09-09 16:38:15 -07001567 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001568 * Unbinds the view for the specified item, and removes the item and all its children.
1569 *
1570 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001571 * @param itemInfo the {@link ItemInfo} for this view.
1572 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001573 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001574 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001575 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001576 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001577 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1578 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001579 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001580 } else {
1581 mWorkspace.removeWorkspaceItem(v);
1582 }
Winsonc0b52fe2015-09-09 16:38:15 -07001583 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001584 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001585 }
1586 } else if (itemInfo instanceof FolderInfo) {
1587 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001588 if (v instanceof FolderIcon) {
1589 ((FolderIcon) v).removeListeners();
1590 }
Winsonc0b52fe2015-09-09 16:38:15 -07001591 mWorkspace.removeWorkspaceItem(v);
1592 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001593 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001594 }
1595 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1596 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001597 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001598 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07001599 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001600 }
1601 } else {
1602 return false;
1603 }
1604 return true;
1605 }
1606
1607 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07001608 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07001609 */
Sunny Goyal56c73602015-09-25 12:55:01 -07001610 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07001611 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07001612 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07001613 // Deleting an app widget ID is a void call but writes to disk before returning
1614 // to the caller...
1615 new AsyncTask<Void, Void, Void>() {
1616 public Void doInBackground(Void ... args) {
1617 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
1618 return null;
1619 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07001620 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07001621 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001622 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001623 }
1624
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001625 @Override
1626 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001627 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001628 }
1629
Joe Onorato88ec0992009-11-19 13:16:06 -08001630 @Override
1631 public void onBackPressed() {
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08001632 if (finishAutoCancelActionMode()) {
1633 return;
1634 }
Adam Cohen9211d422014-10-07 18:14:53 -07001635 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1636 return;
1637 }
1638
Sunny Goyal45478022015-06-08 16:52:41 -07001639 if (mDragController.isDragging()) {
1640 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001641 return;
1642 }
1643
Jon Mirandafeba90f2016-10-06 10:53:29 -07001644 // Note: There should be at most one log per method call. This is enforced implicitly
1645 // by using if-else statements.
1646 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001647 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
1648 if (topView != null) {
Sunny Goyal37920962017-09-28 13:43:24 -07001649 topView.onBackPressed();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001650 } else if (!isInState(NORMAL)) {
Sunny Goyal49bcf342018-01-11 13:59:53 -08001651 LauncherState lastState = mStateManager.getLastState();
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001652 ued.logActionCommand(Action.Command.BACK, mStateManager.getState().containerType,
1653 lastState.containerType);
Sunny Goyal49bcf342018-01-11 13:59:53 -08001654 mStateManager.goToState(lastState);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001655 } else {
1656 // Back button is a no-op here, but give at least some feedback for the button press
1657 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001658 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001659 }
1660
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001661 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001662 * Launches the intent referred by the clicked shortcut.
1663 *
1664 * @param v The view representing the clicked shortcut.
1665 */
Sunny Goyal8a2a63b2018-03-06 22:15:18 -08001666 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001667 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001668 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1669 // view has detached (it's possible for this to happen if the view is removed mid touch).
1670 if (v.getWindowToken() == null) {
1671 return;
1672 }
1673
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001674 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001675 return;
1676 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001677
Adam Cohen1697b792013-09-17 19:08:21 -07001678 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001679 if (isInState(OVERVIEW)) {
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001680 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Touch.TAP,
Jon Miranda2e61fba2017-02-24 09:12:59 -08001681 LauncherLogProto.Action.Direction.NONE,
1682 LauncherLogProto.ContainerType.OVERVIEW, mWorkspace.getCurrentPage());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001683 mStateManager.goToState(NORMAL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001684 }
1685 return;
1686 }
1687
1688 if (v instanceof CellLayout) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001689 if (isInState(OVERVIEW)) {
Jon Mirandac6cf4932017-02-07 17:12:36 -08001690 int page = mWorkspace.indexOfChild(v);
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001691 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Touch.TAP,
Jon Mirandac6cf4932017-02-07 17:12:36 -08001692 LauncherLogProto.Action.Direction.NONE,
1693 LauncherLogProto.ContainerType.OVERVIEW, page);
1694 mWorkspace.snapToPageFromOverView(page);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001695 mStateManager.goToState(NORMAL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001696 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07001697 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001698 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001699 }
1700
Sunny Goyal70660032015-05-14 00:07:08 -07001701 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07001702 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07001703 return false;
1704 }
1705
Michael Jurkaaf442092010-06-10 17:01:57 -07001706 /**
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001707 * Event handler for the wallpaper picker button that appears after a long press
1708 * on the home screen.
1709 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001710 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07001711 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07001712 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
1713 return;
1714 }
1715
Tony Wickham785f7a52015-08-31 17:28:32 -07001716 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
1717 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal2100c782016-08-22 16:00:03 -07001718 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001719 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001720 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001721
1722 String pickerPackage = getString(R.string.wallpaper_picker_package);
Tony85efb4b2017-06-02 13:06:10 -07001723 boolean hasTargetPackage = !TextUtils.isEmpty(pickerPackage);
1724 if (hasTargetPackage) {
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001725 intent.setPackage(pickerPackage);
1726 }
1727
Sunny Goyala5ace712017-11-15 17:12:51 -08001728 final Bundle launchOptions;
1729 if (v != null) {
1730 intent.setSourceBounds(getViewBounds(v));
1731 // If there is no target package, use the default intent chooser animation
Jon Miranda17940f02018-01-18 14:46:34 -08001732 launchOptions = hasTargetPackage
Tony Wickham005df0b2018-02-13 11:28:12 -08001733 ? getActivityLaunchOptionsAsBundle(v, isInMultiWindowModeCompat())
Jon Miranda17940f02018-01-18 14:46:34 -08001734 : null;
Sunny Goyala5ace712017-11-15 17:12:51 -08001735 } else {
1736 launchOptions = null;
1737 }
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001738 try {
Sunny Goyala5ace712017-11-15 17:12:51 -08001739 startActivityForResult(intent, REQUEST_PICK_WALLPAPER, launchOptions);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001740 } catch (ActivityNotFoundException e) {
1741 setWaitingForResult(null);
1742 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1743 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001744 }
1745
Sunny Goyala7ce1662016-05-31 15:01:35 -07001746 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001747 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001748 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
1749 try {
1750 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
1751 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
1752 // is enabled by default on NYC.
1753 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
1754 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001755
1756 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
1757 String id = ((ShortcutInfo) info).getDeepShortcutId();
1758 String packageName = intent.getPackage();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301759 DeepShortcutManager.getInstance(this).startShortcut(
Sunny Goyal9994b2b2016-06-23 14:17:24 -07001760 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001761 } else {
1762 // Could be launching some bookkeeping activity
1763 startActivity(intent, optsBundle);
1764 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07001765 } finally {
1766 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01001767 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001768 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001769 // Due to legacy reasons, direct call shortcuts require Launchers to have the
1770 // corresponding permission. Show the appropriate permission prompt if that
1771 // is the case.
1772 if (intent.getComponent() == null
1773 && Intent.ACTION_CALL.equals(intent.getAction())
1774 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
1775 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001776
1777 setWaitingForResult(PendingRequestArgs
1778 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07001779 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
1780 REQUEST_PERMISSION_CALL_PHONE);
1781 } else {
1782 // No idea why this was thrown.
1783 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07001784 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001785 }
1786 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001787
Tony Wickham005df0b2018-02-13 11:28:12 -08001788 public Bundle getActivityLaunchOptionsAsBundle(View v, boolean useDefaultLaunchOptions) {
1789 ActivityOptions activityOptions = getActivityLaunchOptions(v, useDefaultLaunchOptions);
1790 return activityOptions == null ? null : activityOptions.toBundle();
1791 }
1792
Sunny Goyalfe770c92016-09-27 14:38:58 -07001793 @TargetApi(Build.VERSION_CODES.M)
Tony Wickham005df0b2018-02-13 11:28:12 -08001794 public ActivityOptions getActivityLaunchOptions(View v, boolean useDefaultLaunchOptions) {
Jon Miranda17940f02018-01-18 14:46:34 -08001795 return useDefaultLaunchOptions
Jon Miranda54441f52018-01-24 15:38:25 -08001796 ? mAppTransitionManager.getDefaultActivityLaunchOptions(this, v)
1797 : mAppTransitionManager.getActivityLaunchOptions(this, v);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001798 }
1799
Tonye3c59252017-05-02 21:32:27 -07001800 public Rect getViewBounds(View v) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001801 int[] pos = new int[2];
1802 v.getLocationOnScreen(pos);
1803 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
1804 }
1805
Sunny Goyala7ce1662016-05-31 15:01:35 -07001806 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07001807 mAppLaunchSuccess = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001808 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
1809 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07001810 return mAppLaunchSuccess;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001811 }
Jon Miranda17940f02018-01-18 14:46:34 -08001812
Sunny Goyala7ce1662016-05-31 15:01:35 -07001813 // Only launch using the new animation if the shortcut has not opted out (this is a
1814 // private contract between launcher and may be ignored in the future).
1815 boolean useLaunchAnimation = (v != null) &&
1816 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Jon Miranda17940f02018-01-18 14:46:34 -08001817 Bundle optsBundle = useLaunchAnimation
Tony Wickham005df0b2018-02-13 11:28:12 -08001818 ? getActivityLaunchOptionsAsBundle(v, isInMultiWindowModeCompat())
Jon Miranda17940f02018-01-18 14:46:34 -08001819 : null;
Sunny Goyala7ce1662016-05-31 15:01:35 -07001820
Sunny Goyal24bb66a2017-03-21 15:12:01 -07001821 UserHandle user = item == null ? null : item.user;
Sunny Goyala7ce1662016-05-31 15:01:35 -07001822
1823 // Prepare intent
1824 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1825 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001826 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07001827 }
1828 try {
Jon Miranda84e71bf2018-02-13 15:36:20 -08001829 boolean isShortcut = Utilities.ATLEAST_MARSHMALLOW
1830 && (item instanceof ShortcutInfo)
1831 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
1832 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
1833 && !((ShortcutInfo) item).isPromise();
Jon Miranda17940f02018-01-18 14:46:34 -08001834 if (isShortcut) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001835 // Shortcuts need some special checks due to legacy reasons.
1836 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001837 } else if (user == null || user.equals(Process.myUserHandle())) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001838 // Could be launching some bookkeeping activity
1839 startActivity(intent, optsBundle);
1840 } else {
1841 LauncherAppsCompat.getInstance(this).startActivityForProfile(
1842 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
1843 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001844
1845 if (v instanceof BubbleTextView) {
1846 // This is set to the view that launched the activity that navigated the user away
1847 // from launcher. Since there is no callback for when the activity has finished
1848 // launching, enable the press state and keep this reference to reset the press
1849 // state when we return to launcher.
1850 BubbleTextView btv = (BubbleTextView) v;
1851 btv.setStayPressed(true);
1852 setOnResumeCallback(btv);
1853 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07001854 mAppLaunchSuccess = true;
1855 getUserEventDispatcher().logAppLaunch(v, intent); // TODO for discovered apps b/35802115
Sunny Goyala7ce1662016-05-31 15:01:35 -07001856 } catch (ActivityNotFoundException|SecurityException e) {
1857 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1858 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
1859 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07001860 return mAppLaunchSuccess;
Michael Jurka86a720e2012-05-09 11:23:23 -07001861 }
1862
Tony Wickhamdadb3042016-02-24 11:07:00 -08001863 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07001864 public boolean dispatchTouchEvent(MotionEvent ev) {
Sunny Goyal02424b22018-01-19 11:24:32 -08001865 mLastDispatchTouchEvent.set(ev.getX(), ev.getY());
Jon Miranda379198e2016-09-23 08:54:40 -07001866 return super.dispatchTouchEvent(ev);
1867 }
1868
1869 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001870 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07001871 if (!isDraggingEnabled()) return false;
1872 if (isWorkspaceLocked()) return false;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001873 if (!isInState(NORMAL) && !isInState(OVERVIEW)) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001874
Jon Miranda51f037d2016-11-07 08:37:20 -08001875 boolean ignoreLongPressToOverview =
Sunny Goyal02424b22018-01-19 11:24:32 -08001876 mDeviceProfile.shouldIgnoreLongPressToOverview(mLastDispatchTouchEvent.x);
Jon Miranda379198e2016-09-23 08:54:40 -07001877
Adam Cohen1697b792013-09-17 19:08:21 -07001878 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001879 if (!isInState(OVERVIEW)) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07001880 if (!mWorkspace.isTouchActive() && !ignoreLongPressToOverview) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301881 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
1882 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001883 mWorkspace.getCurrentPage());
Sunny Goyal02424b22018-01-19 11:24:32 -08001884 UiFactory.onWorkspaceLongPress(this, mLastDispatchTouchEvent);
Adam Cohen93c97562013-09-26 13:48:01 -07001885 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1886 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1887 return true;
1888 } else {
1889 return false;
1890 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07001891 } else {
1892 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07001893 }
Adam Cohen1697b792013-09-17 19:08:21 -07001894 }
1895
Winson Chung3d503fb2011-07-13 17:25:49 -07001896 // The hotseat touch handling does not go through Workspace, and we always allow long press
1897 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001898 if (!mDragController.isDragging()) {
Sunny Goyal8a2a63b2018-03-06 22:15:18 -08001899 // User long pressed on empty space
1900 if (mWorkspace.isPageRearrangeEnabled()) {
1901 mWorkspace.startReordering(v);
1902 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
1903 Action.Direction.NONE, ContainerType.OVERVIEW);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001904 } else {
Sunny Goyal8a2a63b2018-03-06 22:15:18 -08001905 if (ignoreLongPressToOverview) {
1906 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001907 }
Sunny Goyal8a2a63b2018-03-06 22:15:18 -08001908 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
1909 Action.Direction.NONE, ContainerType.WORKSPACE,
1910 mWorkspace.getCurrentPage());
1911 UiFactory.onWorkspaceLongPress(this, mLastDispatchTouchEvent);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001912 }
Sunny Goyal8a2a63b2018-03-06 22:15:18 -08001913 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1914 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001915 }
1916 return true;
1917 }
1918
Winson Chung3d503fb2011-07-13 17:25:49 -07001919 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001920 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07001921 return mHotseat != null && layout != null &&
1922 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
1923 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001924
Winson Chung3d503fb2011-07-13 17:25:49 -07001925 /**
1926 * Returns the CellLayout of the specified container at the specified screen.
1927 */
Sunny Goyal92820592015-03-02 11:31:35 -08001928 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001929 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1930 if (mHotseat != null) {
1931 return mHotseat.getLayout();
1932 } else {
1933 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08001934 }
Winson Chung3d503fb2011-07-13 17:25:49 -07001935 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08001936 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08001937 }
1938 }
1939
Michael Jurkae326f182011-11-21 14:05:46 -08001940 @Override
1941 public void onTrimMemory(int level) {
1942 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07001943 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
1944 // The widget preview db can result in holding onto over
1945 // 3MB of memory for caching which isn't necessary.
1946 SQLiteDatabase.releaseMemory();
1947
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08001948 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07001949 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08001950 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01001951 if (mLauncherCallbacks != null) {
1952 mLauncherCallbacks.onTrimMemory(level);
1953 }
Winson Chung62a70be2018-02-23 15:10:05 -08001954 UiFactory.onTrimMemory(this, level);
Michael Jurkae326f182011-11-21 14:05:46 -08001955 }
1956
Michael Jurkad7c28052012-04-27 15:43:36 -07001957 @Override
1958 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07001959 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07001960 final List<CharSequence> text = event.getText();
1961 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07001962 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001963 // TODO: When can workspace be null?
1964 text.add(mWorkspace == null
1965 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07001966 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07001967 return result;
1968 }
1969
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001970 public void setOnResumeCallback(OnResumeCallback callback) {
1971 if (mOnResumeCallback != null) {
1972 mOnResumeCallback.onLauncherResume();
1973 }
1974 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07001975 }
1976
Sunny Goyal60876ac2018-02-20 14:21:20 -08001977 public void setOnStartCallback(OnStartCallback callback) {
1978 mOnStartCallback = callback;
1979 }
1980
Michael Jurka7607c2f2013-04-03 14:33:19 -07001981 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001982 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001983 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07001984 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001985 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07001986 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001987 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07001988 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07001989 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07001990 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001991 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001992
Joe Onorato9c1289c2009-08-17 11:03:03 -04001993 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07001994 * Clear any pending bind callbacks. This is called when is loader is planning to
1995 * perform a full rebind from scratch.
1996 */
1997 @Override
1998 public void clearPendingBinds() {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001999 if (mPendingExecutor != null) {
2000 mPendingExecutor.markCompleted();
2001 mPendingExecutor = null;
2002 }
2003 }
2004
2005 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002006 * Refreshes the shortcuts shown on the workspace.
2007 *
2008 * Implementation of the method from LauncherModel.Callbacks.
2009 */
2010 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002011 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002012 // Floating panels (except the full widget sheet) are associated with individual icons. If
2013 // we are starting a fresh bind, close all such panels as all the icons are about
2014 // to go away.
2015 AbstractFloatingView.closeOpenViews(this, true,
Sunny Goyal7ede6112017-12-05 15:11:21 -08002016 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_REBIND_SAFE);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08002017
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002018 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08002019
Winson Chungd64d1762013-08-20 14:37:16 -07002020 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07002021 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07002022 mWorkspace.removeAllWorkspaceScreens();
Sunny Goyalc11fac32018-02-27 19:20:15 -08002023 mAppWidgetHost.clearViews();
Winson Chungd64d1762013-08-20 14:37:16 -07002024
Winson Chung3d503fb2011-07-13 17:25:49 -07002025 if (mHotseat != null) {
2026 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002027 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002028 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002029 }
2030
Adam Cohendcd297f2013-06-18 13:13:40 -07002031 @Override
2032 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002033 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002034 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
2035 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002036 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
2037 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002038 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002039 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
2040 // If there are no screens, we need to have an empty screen
2041 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07002042 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002043 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07002044
Winsonc7d2e832016-07-28 12:24:55 -07002045 // After we have added all the screens, if the wallpaper was locked to the default state,
2046 // then notify to indicate that it can be released and a proper wallpaper offset can be
2047 // computed before the next layout
2048 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07002049 }
2050
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002051 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002052 int count = orderedScreenIds.size();
2053 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002054 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002055 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002056 // No need to bind the first screen, as its always bound.
2057 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
2058 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002059 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002060 }
2061
Sunny Goyalb23980c2017-08-17 07:45:25 -07002062 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002063 public void bindAppsAdded(ArrayList<Long> newScreens, ArrayList<ItemInfo> addNotAnimated,
2064 ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07002065 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08002066 if (newScreens != null) {
2067 bindAddScreens(newScreens);
2068 }
Winson Chungd64d1762013-08-20 14:37:16 -07002069
2070 // We add the items without animation on non-visible pages, and with
2071 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08002072 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002073 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002074 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002075 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002076 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07002077 }
Winson Chungc58497e2013-09-03 17:48:37 -07002078
Winson Chung87412982013-10-03 18:34:14 -07002079 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07002080 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002081 }
2082
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002083 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002084 * Bind the items start-end from the list.
2085 *
2086 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002087 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07002088 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07002089 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002090 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07002091 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002092 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07002093 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07002094 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002095 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07002096 int end = items.size();
2097 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002098 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002099
2100 // Short circuit if we are loading dock items for a configuration which has no dock
2101 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2102 mHotseat == null) {
2103 continue;
2104 }
2105
Sunny Goyal639e9062015-08-19 19:17:06 -07002106 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002107 switch (item.itemType) {
2108 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2109 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08002110 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07002111 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07002112 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002113 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002114 }
2115 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07002116 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002117 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002118 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002119 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002120 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07002121 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2122 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07002123 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002124 if (view == null) {
2125 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002126 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08002127 break;
2128 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002129 default:
2130 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002131 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002132
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002133 /*
2134 * Remove colliding items.
2135 */
2136 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
2137 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
2138 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
2139 View v = cl.getChildAt(item.cellX, item.cellY);
2140 Object tag = v.getTag();
2141 String desc = "Collision while binding workspace item: " + item
2142 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08002143 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002144 throw (new RuntimeException(desc));
2145 } else {
2146 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002147 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002148 continue;
2149 }
2150 }
2151 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302152 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07002153 if (animateIcons) {
2154 // Animate all the applications up now
2155 view.setAlpha(0f);
2156 view.setScaleX(0f);
2157 view.setScaleY(0f);
2158 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08002159 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07002160 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002161 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07002162
Winson Chung997a9232013-07-24 15:33:46 -07002163 if (animateIcons) {
2164 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08002165 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07002166 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08002167 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07002168 final Runnable startBounceAnimRunnable = new Runnable() {
2169 public void run() {
2170 anim.playTogether(bounceAnims);
2171 anim.start();
2172 }
2173 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08002174 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07002175 // We post the animation slightly delayed to prevent slowdowns
2176 // when we are loading right after we return to launcher.
2177 mWorkspace.postDelayed(new Runnable() {
2178 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00002179 if (mWorkspace != null) {
2180 mWorkspace.snapToPage(newScreenIndex);
2181 mWorkspace.postDelayed(startBounceAnimRunnable,
2182 NEW_APPS_ANIMATION_DELAY);
2183 }
Winson Chung94d67682013-09-25 16:29:40 -07002184 }
2185 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07002186 } else {
2187 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07002188 }
2189 }
Winson Chung64359a52013-07-08 17:17:08 -07002190 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002191 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002192 }
2193
Joe Onorato9c1289c2009-08-17 11:03:03 -04002194 /**
2195 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002196 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07002197 public void bindAppWidget(LauncherAppWidgetInfo item) {
2198 View view = inflateAppWidget(item);
2199 if (view != null) {
2200 mWorkspace.addInScreen(view, item);
2201 mWorkspace.requestLayout();
2202 }
2203 }
2204
2205 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002206 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302207 PendingAppWidgetHostView view =
2208 new PendingAppWidgetHostView(this, item, mIconCache, true);
2209 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002210 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002211 }
2212
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002213 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002214
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002215 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08002216
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002217 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2218 // If the provider is not ready, bind as a pending widget.
2219 appWidgetInfo = null;
2220 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2221 // The widget id is not valid. Try to find the widget based on the provider info.
2222 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
2223 } else {
2224 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
2225 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002226
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002227 // If the provider is ready, but the width is not yet restored, try to restore it.
2228 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
2229 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07002230 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002231 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
2232 + " belongs to component " + item.providerName
2233 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002234 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002235 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002236 }
Sunny Goyalff572272014-07-23 13:58:07 -07002237
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002238 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002239 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07002240 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2241 // Id has not been allocated yet. Allocate a new id.
2242 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
2243 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07002244
Sunny Goyald478c832016-04-01 12:04:16 -07002245 // Also try to bind the widget. If the bind fails, the user will be shown
2246 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08002247 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07002248 pendingInfo.spanX = item.spanX;
2249 pendingInfo.spanY = item.spanY;
2250 pendingInfo.minSpanX = item.minSpanX;
2251 pendingInfo.minSpanY = item.minSpanY;
2252 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07002253
2254 boolean isDirectConfig =
2255 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2256 if (isDirectConfig && item.bindOptions != null) {
2257 Bundle newOptions = item.bindOptions.getExtras();
2258 if (options != null) {
2259 newOptions.putAll(options);
2260 }
2261 options = newOptions;
2262 }
Sunny Goyald478c832016-04-01 12:04:16 -07002263 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2264 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002265
Sunny Goyal86df1382016-08-10 15:03:22 -07002266 // We tried to bind once. If we were not able to bind, we would need to
2267 // go through the permission dialog, which means we cannot skip the config
2268 // activity.
2269 item.bindOptions = null;
2270 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2271
Sunny Goyald478c832016-04-01 12:04:16 -07002272 // Bind succeeded
2273 if (success) {
2274 // If the widget has a configure activity, it is still needs to set it up,
2275 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002276 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002277 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2278 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002279 }
Sunny Goyald478c832016-04-01 12:04:16 -07002280
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002281 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002282 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002283 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002284 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002285 // The widget was marked as UI not ready, but there is no configure activity to
2286 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002287 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002288 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002289 }
Sunny Goyalff572272014-07-23 13:58:07 -07002290 }
2291
Sunny Goyald5462aa2016-11-22 16:52:39 +05302292 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002293 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002294 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002295 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002296 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07002297 deleteWidgetInfo(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002298 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002299 }
2300
Sunny Goyal233ee962015-08-03 13:05:01 -07002301 item.minSpanX = appWidgetInfo.minSpanX;
2302 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302303 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002304 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302305 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002306 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302307 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002308
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002309 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002310 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002311 }
2312
Sunny Goyalff572272014-07-23 13:58:07 -07002313 /**
2314 * Restores a pending widget.
2315 *
2316 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002317 */
Sunny Goyald478c832016-04-01 12:04:16 -07002318 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002319 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2320 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2321 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002322 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002323 }
2324
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002325 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002326 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002327 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2328 info.pendingItemInfo = null;
2329 }
Sunny Goyalff572272014-07-23 13:58:07 -07002330
Sunny Goyalba47faa2017-10-04 16:50:57 -07002331 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002332 view.reInflate();
Sunny Goyalba47faa2017-10-04 16:50:57 -07002333 }
2334
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002335 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002336 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002337 }
2338
Adam Cohen1462de32012-07-24 22:34:36 -07002339 public void onPageBoundSynchronously(int page) {
Sunny Goyalf4aceab2017-12-06 16:16:31 -08002340 mSynchronouslyBoundPage = page;
Adam Cohen1462de32012-07-24 22:34:36 -07002341 }
2342
Sunny Goyal527c7d32015-08-28 15:19:36 -07002343 @Override
2344 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2345 if (mPendingExecutor != null) {
2346 mPendingExecutor.markCompleted();
2347 }
2348 mPendingExecutor = executor;
Sunny Goyal60180b02018-02-07 12:56:30 -08002349 if (!isInState(ALL_APPS)) {
2350 mAppsView.getAppsStore().setDeferUpdates(true);
2351 mPendingExecutor.execute(() -> mAppsView.getAppsStore().setDeferUpdates(false));
2352 }
2353
Sunny Goyal527c7d32015-08-28 15:19:36 -07002354 executor.attachTo(this);
2355 }
2356
2357 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2358 if (mPendingExecutor == executor) {
2359 mPendingExecutor = null;
2360 }
2361 }
2362
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002363 @Override
2364 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002365 if (mDragLayer.getAlpha() < 1) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002366 mDragLayer.animate().alpha(1).withEndAction(
2367 executor == null ? null : executor::onLoadAnimationCompleted).start();
2368 } else if (executor != null) {
2369 executor.onLoadAnimationCompleted();
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002370 }
2371 }
2372
Joe Onorato9c1289c2009-08-17 11:03:03 -04002373 /**
2374 * Callback saying that there aren't any more items to bind.
2375 *
2376 * Implementation of the method from LauncherModel.Callbacks.
2377 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002378 public void finishBindingItems() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002379 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002380 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002381
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002382 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002383
Sunny Goyal2100c782016-08-22 16:00:03 -07002384 if (mPendingActivityResult != null) {
2385 handleActivityResult(mPendingActivityResult.requestCode,
2386 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2387 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002388 }
2389
Sunny Goyala474a9b2017-05-04 16:47:11 -07002390 InstallShortcutReceiver.disableAndFlushInstallQueue(
2391 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002392
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002393 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002394 }
2395
Winson Chunga2413752012-04-03 14:22:34 -07002396 private boolean canRunNewAppsAnimation() {
2397 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002398 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002399 }
2400
Winson Chungc9168342013-06-26 14:54:55 -07002401 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002402 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07002403 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
2404 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002405 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002406 return bounceAnim;
2407 }
2408
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002409 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002410 * Add the icons for all apps.
2411 *
2412 * Implementation of the method from LauncherModel.Callbacks.
2413 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002414 public void bindAllApplications(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002415 mAppsView.getAppsStore().setApps(apps);
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002416
Adam Cohen9211d422014-10-07 18:14:53 -07002417 if (mLauncherCallbacks != null) {
2418 mLauncherCallbacks.bindAllApplications(apps);
2419 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002420 }
2421
2422 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002423 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
2424 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2425 */
2426 @Override
2427 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002428 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002429 }
2430
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002431 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002432 * A package was updated.
2433 *
2434 * Implementation of the method from LauncherModel.Callbacks.
2435 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002436 @Override
2437 public void bindAppsAddedOrUpdated(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002438 mAppsView.getAppsStore().addOrUpdateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002439 }
2440
Sunny Goyal4390ace2014-10-13 11:33:11 -07002441 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002442 public void bindPromiseAppProgressUpdated(PromiseAppInfo app) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002443 mAppsView.getAppsStore().updatePromiseAppProgress(app);
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002444 }
2445
2446 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002447 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002448 mWorkspace.widgetsRestored(widgets);
2449 }
2450
Joe Onorato9c1289c2009-08-17 11:03:03 -04002451 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002452 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002453 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002454 *
2455 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002456 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002457 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002458 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002459 if (!updated.isEmpty()) {
2460 mWorkspace.updateShortcuts(updated);
2461 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002462 }
2463
2464 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002465 * Update the state of a package, typically related to install state.
2466 *
2467 * Implementation of the method from LauncherModel.Callbacks.
2468 */
Sunny Goyale755d462014-07-22 13:48:29 -07002469 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002470 public void bindRestoreItemsChange(HashSet<ItemInfo> updates) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07002471 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07002472 }
2473
2474 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002475 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07002476 * in addition to specific applications to remove, the reason being that
2477 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002478 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07002479 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002480 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07002481 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002482 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002483 mWorkspace.removeItemsByMatcher(matcher);
2484 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002485 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002486
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002487 @Override
2488 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002489 mAppsView.getAppsStore().removeApps(appInfos);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002490 }
Joe Onoratobe386092009-11-17 17:32:16 -08002491
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002492 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002493 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
2494 mPopupDataProvider.setAllWidgets(allWidgets);
Tony Wickham26b17462017-03-20 17:12:24 -07002495 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
2496 if (topView != null) {
2497 topView.onWidgetsBound();
2498 }
2499 }
2500
Tony Wickham86222d22017-03-29 15:30:43 -07002501 /**
2502 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
2503 * refreshes the widgets and shortcuts associated with the given package/user
2504 */
2505 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07002506 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002507 }
2508
Winson Chung80baf5a2010-08-09 16:03:15 -07002509 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002510 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08002511 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002512 @Override
2513 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
2514 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002515
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002516 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
2517 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07002518 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002519 writer.println(prefix + " Homescreen " + i);
2520
2521 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
2522 for (int j = 0; j < layout.getChildCount(); j++) {
2523 Object tag = layout.getChildAt(j).getTag();
2524 if (tag != null) {
2525 writer.println(prefix + " " + tag.toString());
2526 }
2527 }
2528 }
2529
2530 writer.println(prefix + " Hotseat");
2531 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07002532 for (int j = 0; j < layout.getChildCount(); j++) {
2533 Object tag = layout.getChildAt(j).getTag();
2534 if (tag != null) {
2535 writer.println(prefix + " " + tag.toString());
2536 }
Adam Cohen4caf2982013-08-20 18:54:31 -07002537 }
Sunny Goyala1365452015-10-01 15:46:24 -07002538 }
2539
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002540 writer.println(prefix + "Misc:");
2541 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
2542 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
2543 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
Sunny Goyal2bba1902018-02-27 12:20:50 -08002544 writer.println(" deviceProfile isTransposed=" + getDeviceProfile().isVerticalBarLayout());
2545 writer.println(" orientation=" + getResources().getConfiguration().orientation);
2546
2547 try {
2548 FileLog.flushAll(writer);
2549 } catch (Exception e) {
2550 // Ignore
2551 }
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002552
2553 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002554
Adam Cohen9211d422014-10-07 18:14:53 -07002555 if (mLauncherCallbacks != null) {
2556 mLauncherCallbacks.dump(prefix, fd, writer, args);
2557 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002558 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002559
Sunny Goyal66b24572016-09-21 15:57:55 -07002560 @Override
2561 @TargetApi(Build.VERSION_CODES.N)
2562 public void onProvideKeyboardShortcuts(
2563 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
2564
2565 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002566 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002567 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
2568 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
2569 }
2570 View currentFocus = getCurrentFocus();
2571 if (new CustomActionsPopup(this, currentFocus).canShow()) {
2572 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
2573 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
2574 }
Tony18c4aa42017-05-10 21:23:57 -05002575 if (currentFocus.getTag() instanceof ItemInfo
2576 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002577 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
2578 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
2579 }
2580 if (!shortcutInfos.isEmpty()) {
2581 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
2582 }
2583
2584 super.onProvideKeyboardShortcuts(data, menu, deviceId);
2585 }
2586
2587 @Override
2588 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
2589 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
2590 switch (keyCode) {
2591 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002592 if (isInState(NORMAL)) {
2593 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07002594 return true;
2595 }
2596 break;
2597 case KeyEvent.KEYCODE_S: {
2598 View focusedView = getCurrentFocus();
2599 if (focusedView instanceof BubbleTextView
2600 && focusedView.getTag() instanceof ItemInfo
2601 && mAccessibilityDelegate.performAction(focusedView,
2602 (ItemInfo) focusedView.getTag(),
2603 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal00ac9202017-11-09 15:24:06 -08002604 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07002605 return true;
2606 }
2607 break;
2608 }
2609 case KeyEvent.KEYCODE_O:
2610 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
2611 return true;
2612 }
2613 break;
2614 }
2615 }
2616 return super.onKeyShortcut(keyCode, event);
2617 }
2618
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002619 @Override
2620 public boolean onKeyUp(int keyCode, KeyEvent event) {
2621 if (keyCode == KeyEvent.KEYCODE_MENU) {
2622 // KEYCODE_MENU is sent by some tests, for example
2623 // LauncherJankTests#testWidgetsContainerFling. Don't just remove its handling.
2624 if (!mDragController.isDragging() && !mWorkspace.isSwitchingState() &&
2625 isInState(NORMAL)) {
2626 // Close any open floating views.
2627 AbstractFloatingView.closeAllOpenViews(this);
2628
2629 // Setting the touch point to (-1, -1) will show the options popup in the center of
2630 // the screen.
2631 mLastDispatchTouchEvent.set(-1, -1);
2632 UiFactory.onWorkspaceLongPress(this, mLastDispatchTouchEvent);
2633 }
2634 return true;
2635 }
2636 return super.onKeyUp(keyCode, event);
2637 }
2638
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002639 public static Launcher getLauncher(Context context) {
2640 if (context instanceof Launcher) {
2641 return (Launcher) context;
2642 }
2643 return ((Launcher) ((ContextWrapper) context).getBaseContext());
2644 }
2645
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08002646 @Override
2647 public void onActionModeStarted(ActionMode mode) {
2648 super.onActionModeStarted(mode);
2649 mCurrentActionMode = mode;
2650 }
2651
2652 @Override
2653 public void onActionModeFinished(ActionMode mode) {
2654 super.onActionModeFinished(mode);
2655 mCurrentActionMode = null;
2656 }
2657
2658 public boolean finishAutoCancelActionMode() {
2659 if (mCurrentActionMode != null && AUTO_CANCEL_ACTION_MODE == mCurrentActionMode.getTag()) {
2660 mCurrentActionMode.finish();
2661 return true;
2662 }
2663 return false;
2664 }
2665
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002666 /**
2667 * Callback for listening for onResume
2668 */
2669 public interface OnResumeCallback {
2670
2671 void onLauncherResume();
2672 }
Sunny Goyal60876ac2018-02-20 14:21:20 -08002673
2674 /**
2675 * Callback for listening for onStart
2676 */
2677 public interface OnStartCallback {
2678
2679 void onLauncherStart(Launcher launcher);
2680 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002681}