blob: f76d4eb4702bb0c49059ebb5a660faf3c5cb89cf [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080019import static android.content.pm.ActivityInfo.CONFIG_ORIENTATION;
20import static android.content.pm.ActivityInfo.CONFIG_SCREEN_SIZE;
Sunny Goyalc99cb172017-10-19 16:15:09 -070021import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_NOSENSOR;
22import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Sunny Goyal076839c2017-10-30 13:52:20 -070023import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_BY_PUBLISHER;
24import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_LOCKED_USER;
25import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_QUIET_USER;
26import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_SAFEMODE;
27import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_SUSPENDED;
Sunny Goyal4c7f2152017-10-17 17:17:16 -070028import static com.android.launcher3.LauncherAnimUtils.SPRING_LOADED_EXIT_DELAY;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -070029import static com.android.launcher3.LauncherState.ALL_APPS;
30import static com.android.launcher3.LauncherState.NORMAL;
31import static com.android.launcher3.LauncherState.OVERVIEW;
Sunny Goyalaeb16432017-10-16 11:46:41 -070032import static com.android.launcher3.logging.LoggerUtils.newContainerTarget;
Sunny Goyal326403e2017-10-02 12:45:10 -070033
Sunny Goyal28c6b962015-10-12 11:42:05 -070034import android.Manifest;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070035import android.animation.Animator;
Jon Miranda2d89ea82017-05-04 11:47:53 -070036import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070037import android.animation.AnimatorSet;
Jon Miranda2d89ea82017-05-04 11:47:53 -070038import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070039import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070040import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000041import android.annotation.TargetApi;
Chris Wren40c5ed32014-06-24 18:24:23 -040042import android.app.AlertDialog;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080043import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070044import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080046import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080047import android.content.ComponentCallbacks2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.Context;
Andrew Sappersteinabef55a2016-06-19 12:49:00 -070049import android.content.ContextWrapper;
Chris Wren40c5ed32014-06-24 18:24:23 -040050import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070052import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070053import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070054import android.content.SharedPreferences;
Sunny Goyal745bad92016-05-02 10:54:12 -070055import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056import android.content.pm.PackageManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080057import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070058import android.database.sqlite.SQLiteDatabase;
Jon Mirandacc42c5b2016-10-27 17:15:27 -070059import android.graphics.Point;
Sunny Goyal02424b22018-01-19 11:24:32 -080060import android.graphics.PointF;
Michael Jurkaaf442092010-06-10 17:01:57 -070061import android.graphics.Rect;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070062import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020063import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064import android.os.Bundle;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070065import android.os.Parcelable;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080066import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070067import android.os.StrictMode;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070068import android.os.UserHandle;
Tony Wickham86222d22017-03-29 15:30:43 -070069import android.support.annotation.Nullable;
Michael Jurkaa33411c2012-06-14 16:18:21 -070070import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070072import android.util.Log;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070073import android.util.SparseArray;
Sunny Goyal7c8a65e2017-12-22 11:11:33 -080074import android.view.ActionMode;
Adam Cohen96d30a12013-07-16 18:13:21 -070075import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070076import android.view.HapticFeedbackConstants;
77import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070078import android.view.KeyboardShortcutGroup;
79import android.view.KeyboardShortcutInfo;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080080import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -070081import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070082import android.view.MotionEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070083import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080084import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070085import android.view.ViewGroup;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080086import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070087import android.view.animation.OvershootInterpolator;
Adam Cohen96d30a12013-07-16 18:13:21 -070088import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070089
Sunny Goyal651077b2014-06-30 14:15:31 -070090import com.android.launcher3.DropTarget.DragObject;
Sunny Goyala7ce1662016-05-31 15:01:35 -070091import com.android.launcher3.LauncherSettings.Favorites;
Jon Mirandac476d6e2017-05-04 16:30:01 -070092import com.android.launcher3.Workspace.ItemOperator;
Sunny Goyalae502842016-06-17 08:43:56 -070093import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070094import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070095import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal7ede6112017-12-05 15:11:21 -080096import com.android.launcher3.allapps.DiscoveryBounce;
Sunny Goyalffe83f12014-08-14 17:39:34 -070097import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010098import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -070099import com.android.launcher3.compat.LauncherAppsCompatVO;
Tony Wickhame0c33232016-02-08 11:37:04 -0800100import com.android.launcher3.config.FeatureFlags;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700101import com.android.launcher3.dragndrop.DragController;
102import com.android.launcher3.dragndrop.DragLayer;
Sunny Goyal94b510c2016-08-16 15:36:48 -0700103import com.android.launcher3.dragndrop.DragOptions;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700104import com.android.launcher3.dragndrop.DragView;
Mario Bertschler27288382017-05-24 15:35:09 -0700105import com.android.launcher3.dynamicui.WallpaperColorInfo;
Sunny Goyal26119432016-02-18 22:09:23 +0000106import com.android.launcher3.folder.Folder;
107import com.android.launcher3.folder.FolderIcon;
Sunny Goyal66b24572016-09-21 15:57:55 -0700108import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700109import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700110import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700111import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyala535ae42017-02-27 10:07:13 -0800112import com.android.launcher3.model.ModelWriter;
Sunny Goyala535ae42017-02-27 10:07:13 -0800113import com.android.launcher3.notification.NotificationListener;
Tony Wickham540913e2017-01-23 11:47:51 -0800114import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -0800115import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700116import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyalf8088ee2017-11-10 14:52:00 -0800117import com.android.launcher3.states.InternalStateHandler;
Sunny Goyala5ace712017-11-15 17:12:51 -0800118import com.android.launcher3.uioverrides.UiFactory;
Jon Mirandac6cf4932017-02-07 17:12:36 -0800119import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530120import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
121import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
Sunny Goyalaeb16432017-10-16 11:46:41 -0700122import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyal2100c782016-08-22 16:00:03 -0700123import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700124import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700125import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700126import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700127import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800128import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700129import com.android.launcher3.util.PendingRequestArgs;
Sunny Goyal8392c822017-06-20 10:03:56 -0700130import com.android.launcher3.util.SystemUiController;
Mario Bertschlera6936942017-05-31 14:48:19 -0700131import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700132import com.android.launcher3.util.Thunk;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700133import com.android.launcher3.util.TraceHelper;
Sunny Goyal326403e2017-10-02 12:45:10 -0700134import com.android.launcher3.util.UiThreadHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700135import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyal29947f02017-12-18 13:49:44 -0800136import com.android.launcher3.widget.LauncherAppWidgetHostView;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800137import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700138import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal29947f02017-12-18 13:49:44 -0800139import com.android.launcher3.widget.PendingAppWidgetHostView;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800140import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700141import com.android.launcher3.widget.WidgetHostViewLoader;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700142import com.android.launcher3.widget.WidgetListRowEntry;
143import com.android.launcher3.widget.WidgetsFullSheet;
Sunny Goyal952e63d2017-08-16 04:59:08 -0700144import com.android.launcher3.widget.custom.CustomWidgetParser;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700145
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700146import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700147import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700148import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700149import java.util.Collection;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700150import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700151import java.util.List;
Tony Wickham010d2552017-01-20 08:15:28 -0800152import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700153
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154/**
155 * Default launcher application.
156 */
Sunny Goyal27835952017-01-13 12:15:53 -0800157public class Launcher extends BaseActivity
Adam Cohen79d90c52016-04-22 13:29:20 -0700158 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
159 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Mario Bertschler27288382017-05-24 15:35:09 -0700160 WallpaperColorInfo.OnThemeChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700161 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700162 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163
Winson Chunga2413752012-04-03 14:22:34 -0700164 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700165
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700167 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700168
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700169 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700170 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171
Michael Jurka8b805b12012-04-18 14:23:14 -0700172 private static final int REQUEST_BIND_APPWIDGET = 11;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700173 private static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
174 private static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700175
Jon Mirandac476d6e2017-05-04 16:30:01 -0700176 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700177
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700178 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
179
Mathew Inwood876a8462013-06-14 14:12:41 +0100180 /**
181 * IntentStarter uses request codes starting with this. This must be greater than all activity
182 * request codes used internally.
183 */
184 protected static final int REQUEST_LAST = 100;
185
Winson Chung2672ff92012-05-04 16:22:30 -0700186 // The Intent extra that defines whether to ignore the launch animation
187 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400188 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700189
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 // Type: int
191 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700192 // Type: int
193 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700194 // Type: PendingRequestArgs
195 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
196 // Type: ActivityResultInfo
197 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700198 // Type: SparseArray<Parcelable>
199 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200
Sunny Goyal7c8a65e2017-12-22 11:11:33 -0800201 // When starting an action mode, setting this tag will cause the action mode to be cancelled
202 // automatically when user interacts with the launcher.
203 public static final Object AUTO_CANCEL_ACTION_MODE = new Object();
204
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700205 private LauncherStateManager mStateManager;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700206
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700207 private boolean mIsSafeModeEnabled;
208
Adam Cohenad4e15c2013-10-17 16:21:35 -0700209 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210
Winson Chunga2413752012-04-03 14:22:34 -0700211 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700212 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
213 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
214 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700215
Jon Miranda54441f52018-01-24 15:38:25 -0800216 private LauncherAppTransitionManager mAppTransitionManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800217 private Configuration mOldConfig;
Jon Miranda54441f52018-01-24 15:38:25 -0800218
Adam Cohen091440a2015-03-18 14:16:05 -0700219 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700220 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700221 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800222 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700223
Sunny Goyalffe83f12014-08-14 17:39:34 -0700224 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700225 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700226
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700227 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700228
Sunny Goyal316490e2015-06-02 09:38:28 -0700229 @Thunk Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700230
Sunny Goyal47328fd2016-05-25 18:56:41 -0700231 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700232
233 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700234 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700235 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700236
Winson Chung8ae41982017-11-10 11:42:13 -0800237 // UI and state for the overview panel
238 private ViewGroup mOverviewPanel;
239
Adam Cohen091440a2015-03-18 14:16:05 -0700240 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400241
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700242 private OnResumeCallback mOnResumeCallback;
243
Sunny Goyal527c7d32015-08-28 15:19:36 -0700244 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700245
Joe Onorato9c1289c2009-08-17 11:03:03 -0400246 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800247 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800248 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700249 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400250
Jon Miranda2d89ea82017-05-04 11:47:53 -0700251 private ObjectAnimator mScrimAnimator;
Jon Miranda7fda2852017-07-19 16:07:01 -0700252 private boolean mShouldFadeInScrim;
Jon Miranda2d89ea82017-05-04 11:47:53 -0700253
Tony Wickham010d2552017-01-20 08:15:28 -0800254 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700255
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800256 private int mSynchronouslyBoundPage = PagedView.INVALID_PAGE;
Adam Cohen1462de32012-07-24 22:34:36 -0700257
Winson Chung46353de2012-02-16 14:05:10 -0800258 // We only want to get the SharedPreferences once since it does an FS stat each time we get
259 // it from the context.
260 private SharedPreferences mSharedPrefs;
261
Sunny Goyal2100c782016-08-22 16:00:03 -0700262 // Activity result which needs to be processed after workspace has loaded.
263 private ActivityResultInfo mPendingActivityResult;
264 /**
265 * Holds extra information required to handle a result from an external call, like
266 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
267 */
268 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800269
Sunny Goyal02424b22018-01-19 11:24:32 -0800270 private final PointF mLastDispatchTouchEvent = new PointF();
Jon Miranda379198e2016-09-23 08:54:40 -0700271
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700272 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700273 private boolean mRotationEnabled = false;
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700274 private boolean mAppLaunchSuccess;
275
Sunny Goyal745bad92016-05-02 10:54:12 -0700276 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7c8a65e2017-12-22 11:11:33 -0800277 private ActionMode mCurrentActionMode;
Sunny Goyal7779d622015-06-11 16:18:39 -0700278
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800279 @Override
280 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700281 if (DEBUG_STRICT_MODE) {
282 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
283 .detectDiskReads()
284 .detectDiskWrites()
285 .detectNetwork() // or .detectAll() for all detectable problems
286 .penaltyLog()
287 .build());
288 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
289 .detectLeakedSqlLiteObjects()
290 .detectLeakedClosableObjects()
291 .penaltyLog()
292 .penaltyDeath()
293 .build());
294 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700295 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700296
Mario Bertschler27288382017-05-24 15:35:09 -0700297 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
298 wallpaperColorInfo.setOnThemeChangeListener(this);
Mario Bertschlera6936942017-05-31 14:48:19 -0700299 overrideTheme(wallpaperColorInfo.isDark(), wallpaperColorInfo.supportsDarkText());
Mario Bertschler27288382017-05-24 15:35:09 -0700300
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800301 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700302 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400303
Sunny Goyal87f784c2017-01-11 10:48:34 -0800304 LauncherAppState app = LauncherAppState.getInstance(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800305 mOldConfig = new Configuration(getResources().getConfiguration());
306 initDeviceProfile(app.getInvariantDeviceProfile());
Winson Chung6e1c0d32013-10-25 15:24:24 -0700307
Sunny Goyalf7258242015-10-19 16:59:07 -0700308 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700309 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800310 mModel = app.setLauncher(this);
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800311 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout());
Joe Onorato0589f0f2010-02-08 13:44:00 -0800312 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700313 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700314
Joe Onorato41a12d22009-10-31 18:30:00 -0400315 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700316 mAllAppsController = new AllAppsTransitionController(this);
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800317 mStateManager = new LauncherStateManager(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700318
Sunny Goyalffe83f12014-08-14 17:39:34 -0700319 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700320
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700321 mAppWidgetHost = new LauncherAppWidgetHost(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700322 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700323
Sunny Goyal60820d72017-05-09 12:40:11 -0700324 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700325
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800326 setupViews();
Tony Wickham010d2552017-01-20 08:15:28 -0800327 mPopupDataProvider = new PopupDataProvider(this);
328
Sunny Goyald3864fa2017-11-14 15:06:36 -0800329 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
330 // In case we are on a device with locked rotation, we should look at preferences to check
331 // if the user has specifically allowed rotation.
332 if (!mRotationEnabled) {
333 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
334 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
335 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
336 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800337
Sunny Goyald3864fa2017-11-14 15:06:36 -0800338 boolean internalStateHandled = InternalStateHandler.handleCreate(this, getIntent());
339 if (internalStateHandled) {
340 // Temporarily enable the rotation
341 mRotationEnabled = true;
342
343 if (savedInstanceState != null) {
344 // InternalStateHandler has already set the appropriate state.
345 // We dont need to do anything.
346 savedInstanceState.remove(RUNTIME_STATE);
347 }
348 }
349 restoreState(savedInstanceState);
Winson Chungb63b44c2017-11-10 17:54:44 -0800350
Sunny Goyal2100c782016-08-22 16:00:03 -0700351 // We only load the page synchronously if the user rotates (or triggers a
352 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700353 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
354 if (savedInstanceState != null) {
355 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
356 }
Sunny Goyald3864fa2017-11-14 15:06:36 -0800357
Sunny Goyalfe770c92016-09-27 14:38:58 -0700358 if (!mModel.startLoader(currentScreen)) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800359 if (!internalStateHandled) {
360 // If we are not binding synchronously, show a fade in animation when
361 // the first page bind completes.
362 mDragLayer.setAlpha(0);
363 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700364 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700365 // Pages bound synchronously.
366 mWorkspace.setCurrentPage(currentScreen);
367
Sunny Goyal2100c782016-08-22 16:00:03 -0700368 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800369 }
370
371 // For handling default keys
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800372 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800373
Winson Chung90049122018-02-15 11:33:53 -0800374 updateRequestedOrientation();
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800375 setContentView(mLauncherView);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800376 getRootView().dispatchInsets();
Mario Bertschlera6936942017-05-31 14:48:19 -0700377
Jon Miranda7fda2852017-07-19 16:07:01 -0700378 // Listen for broadcasts
379 IntentFilter filter = new IntentFilter();
380 filter.addAction(Intent.ACTION_SCREEN_OFF);
381 filter.addAction(Intent.ACTION_USER_PRESENT); // When the device wakes up + keyguard is gone
382 registerReceiver(mReceiver, filter);
383 mShouldFadeInScrim = true;
Sunny Goyala616d2b2017-06-12 13:54:01 -0700384
Sunny Goyal8392c822017-06-20 10:03:56 -0700385 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
386 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700387
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800388 mAppTransitionManager = LauncherAppTransitionManager.newInstance(this);
Jon Miranda7853bdb2018-01-22 17:32:30 -0800389
Sunny Goyal14b32402017-07-31 10:03:28 -0700390 if (mLauncherCallbacks != null) {
391 mLauncherCallbacks.onCreate(savedInstanceState);
392 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700393
394 TraceHelper.endSection("Launcher-onCreate");
Adam Cohen9211d422014-10-07 18:14:53 -0700395 }
396
Winson Chung90049122018-02-15 11:33:53 -0800397 public void updateRequestedOrientation() {
398 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
399 // we want the screen to auto-rotate based on the current orientation
400 setRequestedOrientation(mRotationEnabled
401 ? SCREEN_ORIENTATION_UNSPECIFIED : SCREEN_ORIENTATION_NOSENSOR);
402 }
403
Sunny Goyal7779d622015-06-11 16:18:39 -0700404 @Override
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800405 public void onConfigurationChanged(Configuration newConfig) {
406 int diff = newConfig.diff(mOldConfig);
407 if ((diff & (CONFIG_ORIENTATION | CONFIG_SCREEN_SIZE)) != 0) {
408 mUserEventDispatcher = null;
409 initDeviceProfile(mDeviceProfile.inv);
Sunny Goyalfde55052018-02-01 14:46:13 -0800410 dispatchDeviceProfileChanged();
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800411
412 getRootView().dispatchInsets();
413 getStateManager().reapplyState();
414
415 // TODO: We can probably avoid rebind when only screen size changed.
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800416 int currentPage = mWorkspace.getNextPage();
417 if (mModel.startLoader(currentPage)) {
418 mWorkspace.setCurrentPage(currentPage);
419 setWorkspaceLoading(true);
420 }
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800421 }
422
423 mOldConfig.setTo(newConfig);
424 super.onConfigurationChanged(newConfig);
425 }
426
427 private void initDeviceProfile(InvariantDeviceProfile idp) {
428 // Load configuration-specific DeviceProfile
429 mDeviceProfile = idp.getDeviceProfile(this);
430 if (isInMultiWindowModeCompat()) {
431 Display display = getWindowManager().getDefaultDisplay();
432 Point mwSize = new Point();
433 display.getSize(mwSize);
434 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
435 }
436 }
437
438 @Override
Mario Bertschler27288382017-05-24 15:35:09 -0700439 public void onThemeChanged() {
440 recreate();
441 }
442
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700443 public LauncherStateManager getStateManager() {
444 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700445 }
446
Tonydc76f8e2018-02-02 21:45:39 -0600447 public LauncherAppTransitionManager getAppTransitionManager() {
448 return mAppTransitionManager;
449 }
450
Mario Bertschlera6936942017-05-31 14:48:19 -0700451 protected void overrideTheme(boolean isDark, boolean supportsDarkText) {
Mario Bertschler27288382017-05-24 15:35:09 -0700452 if (isDark) {
453 setTheme(R.style.LauncherThemeDark);
Mario Bertschlera6936942017-05-31 14:48:19 -0700454 } else if (supportsDarkText) {
455 setTheme(R.style.LauncherThemeDarkText);
Mario Bertschler27288382017-05-24 15:35:09 -0700456 }
457 }
458
459 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700460 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800461 return mLauncherView.findViewById(id);
462 }
463
464 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700465 public void onAppWidgetHostReset() {
466 if (mAppWidgetHost != null) {
467 mAppWidgetHost.startListening();
468 }
469 }
470
Adam Cohen9211d422014-10-07 18:14:53 -0700471 private LauncherCallbacks mLauncherCallbacks;
472
Sunny Goyal32554d12015-12-03 15:31:25 -0800473 /**
474 * Call this after onCreate to set or clear overlay.
475 */
476 public void setLauncherOverlay(LauncherOverlay overlay) {
477 if (overlay != null) {
478 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
479 }
480 mWorkspace.setLauncherOverlay(overlay);
481 }
482
Adam Cohen9211d422014-10-07 18:14:53 -0700483 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
484 mLauncherCallbacks = callbacks;
485 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700486 }
487
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700488 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700489 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700490 if (mLauncherCallbacks != null) {
491 mLauncherCallbacks.onLauncherProviderChange();
492 }
493 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700494
Hyunyoung Song3f471442015-04-08 19:01:34 -0700495 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700496 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
497 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700498 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700499 }
500
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000501 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700502 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
503 // This cast is safe as long as the id < 0x00FFFFFF
504 // Since we jail all the dynamically generated views, there should be no clashes
505 // with any other views.
506 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000507 }
508
Tony Wickham010d2552017-01-20 08:15:28 -0800509 public PopupDataProvider getPopupDataProvider() {
510 return mPopupDataProvider;
511 }
512
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000513 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700514 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
515 * a configuration step, this allows the proper animations to run after other transitions.
516 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700517 private long completeAdd(
518 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
519 long screenId = info.screenId;
520 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700521 // When the screen id represents an actual screen (as opposed to a rank) we make sure
522 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700523 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700524 }
Adam Cohendb364c32014-05-20 14:23:40 -0700525
Sunny Goyal2100c782016-08-22 16:00:03 -0700526 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800527 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700528 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700529 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800530 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700531 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700532 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700533 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700534 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700535 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700536 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700537 int widgetId = appWidgetId;
538 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700539 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700540 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700541 // Since the view was just bound, also launch the configure activity if needed
542 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
543 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800544 if (provider != null) {
545 new WidgetAddFlowHandler(provider)
546 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700547 }
548 }
549 break;
550 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800551 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700552
Adam Cohendb364c32014-05-20 14:23:40 -0700553 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800554 }
555
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800556 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700557 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700558 if (isWorkspaceLoading()) {
559 // process the result once the workspace has loaded.
560 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
561 return;
562 }
563 mPendingActivityResult = null;
564
Winson Chunge341d302013-08-16 14:31:00 -0700565 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700566 final PendingRequestArgs requestArgs = mPendingRequestArgs;
567 setWaitingForResult(null);
568 if (requestArgs == null) {
569 return;
570 }
571
572 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700573
Adam Cohenad4e15c2013-10-17 16:21:35 -0700574 Runnable exitSpringLoaded = new Runnable() {
575 @Override
576 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700577 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700578 }
579 };
580
Michael Jurka8b805b12012-04-18 14:23:14 -0700581 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700582 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700583 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700584 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
585 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700586 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700587 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700588 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700589 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700590 addAppWidgetImpl(
591 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800592 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700593 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700594 }
595 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200596 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700597 if (resultCode == RESULT_OK && isInState(OVERVIEW)) {
Tony Wickhame3054412015-09-09 09:05:25 -0700598 // User could have free-scrolled between pages before picking a wallpaper; make sure
599 // we move to the closest one now.
600 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700601 mStateManager.goToState(NORMAL, false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200602 }
603 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700604 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200605
Adam Cohened66b2b2012-01-23 17:28:51 -0800606 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700607 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700608
Adam Cohened66b2b2012-01-23 17:28:51 -0800609 // We have special handling for widgets
610 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800611 final int appWidgetId;
612 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
613 : -1;
614 if (widgetId < 0) {
615 appWidgetId = pendingAddWidgetId;
616 } else {
617 appWidgetId = widgetId;
618 }
619
Adam Cohenad4e15c2013-10-17 16:21:35 -0700620 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800621 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700622 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
623 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700624 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700625 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700626 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700627 @Override
628 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700629 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700630 }
631 };
Adam Cohendb364c32014-05-20 14:23:40 -0700632
Sunny Goyal2100c782016-08-22 16:00:03 -0700633 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
634 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
635 } else {
636 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
637 // When the screen id represents an actual screen (as opposed to a rank)
638 // we make sure that the drop page actually exists.
639 requestArgs.screenId =
640 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700641 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700642 final CellLayout dropLayout =
643 mWorkspace.getScreenWithId(requestArgs.screenId);
644
645 dropLayout.setDropPending(true);
646 final Runnable onComplete = new Runnable() {
647 @Override
648 public void run() {
649 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
650 dropLayout.setDropPending(false);
651 }
652 };
653 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
654 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700655 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800656 return;
657 }
658
Sunny Goyald478c832016-04-01 12:04:16 -0700659 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
660 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700661 if (resultCode == RESULT_OK) {
662 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700663 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700664 }
665 // Leave the widget in the pending state if the user canceled the configure.
666 return;
667 }
668
Sunny Goyalaad90582015-07-09 14:22:50 -0700669 if (requestCode == REQUEST_CREATE_SHORTCUT) {
670 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700671 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
672 completeAdd(requestCode, data, -1, requestArgs);
673 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
674 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
675
Sunny Goyalaad90582015-07-09 14:22:50 -0700676 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700677 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
678 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800679 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800680 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800681 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800682 }
683
684 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700685 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800686 final int requestCode, final int resultCode, final Intent data) {
687 handleActivityResult(requestCode, resultCode, data);
688 if (mLauncherCallbacks != null) {
689 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
690 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800691 }
692
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700693 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700694 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600695 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700696 PendingRequestArgs pendingArgs = mPendingRequestArgs;
697 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
698 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
699 setWaitingForResult(null);
700
Sunny Goyal28c6b962015-10-12 11:42:05 -0700701 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700702 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700703 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700704 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700705 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700706 Intent intent = pendingArgs.getPendingIntent();
707
Sunny Goyal28c6b962015-10-12 11:42:05 -0700708 if (grantResults.length > 0
709 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700710 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700711 } else {
712 // TODO: Show a snack bar with link to settings
713 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700714 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700715 }
716 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600717 if (mLauncherCallbacks != null) {
718 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
719 grantResults);
720 }
721 }
722
Adam Cohendb364c32014-05-20 14:23:40 -0700723 /**
724 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
725 *
726 * @param screenId the screen id to check
727 * @return the new screen, or screenId if it exists
728 */
729 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800730 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700731 if (dropLayout == null) {
732 // it's possible that the add screen was removed because it was
733 // empty and a re-bind occurred
734 mWorkspace.addExtraEmptyScreen();
735 return mWorkspace.commitExtraEmptyScreen();
736 } else {
737 return screenId;
738 }
739 }
740
Sunny Goyal2100c782016-08-22 16:00:03 -0700741 @Thunk void completeTwoStageWidgetDrop(
742 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
743 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800744 Runnable onCompleteRunnable = null;
745 int animationType = 0;
746
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700747 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800748 if (resultCode == RESULT_OK) {
749 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
750 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800751 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700752 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800753 onCompleteRunnable = new Runnable() {
754 @Override
755 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700756 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700757 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800758 }
759 };
760 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800761 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800762 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800763 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700764 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700765 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700766 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
767 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700768 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700769 // The animated view may be null in the case of a rotation during widget configuration
770 onCompleteRunnable.run();
771 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800772 }
773
774 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700775 protected void onStop() {
776 super.onStop();
777 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700778
779 if (mLauncherCallbacks != null) {
780 mLauncherCallbacks.onStop();
781 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800782 mAppWidgetHost.setListenIfResumed(false);
Tony Wickham010d2552017-01-20 08:15:28 -0800783
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700784 if (!mAppLaunchSuccess) {
785 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -0800786 mStateManager.getState().containerType, -1);
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700787 }
Tony Wickham010d2552017-01-20 08:15:28 -0800788 NotificationListener.removeNotificationsChangedListener();
Sunny Goyalaf161fd2018-02-05 16:34:57 -0800789 getStateManager().moveToRestState();
Michael Jurkacd496d72013-04-11 11:32:45 -0700790 }
791
792 @Override
793 protected void onStart() {
794 super.onStart();
795 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700796
797 if (mLauncherCallbacks != null) {
798 mLauncherCallbacks.onStart();
799 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800800 mAppWidgetHost.setListenIfResumed(true);
Sunny Goyalcac002d2018-01-04 09:51:45 -0800801 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
Jon Miranda2d89ea82017-05-04 11:47:53 -0700802
Jon Mirandade43a712018-01-18 11:35:10 -0800803 if (mShouldFadeInScrim && mLauncherView.getBackground() != null) {
Jon Miranda2d89ea82017-05-04 11:47:53 -0700804 if (mScrimAnimator != null) {
805 mScrimAnimator.cancel();
806 }
Jon Mirandade43a712018-01-18 11:35:10 -0800807 mLauncherView.getBackground().setAlpha(0);
808 mScrimAnimator = ObjectAnimator.ofInt(mLauncherView.getBackground(),
Jon Miranda2d89ea82017-05-04 11:47:53 -0700809 LauncherAnimUtils.DRAWABLE_ALPHA, 0, 255);
810 mScrimAnimator.addListener(new AnimatorListenerAdapter() {
811 @Override
812 public void onAnimationEnd(Animator animation) {
813 mScrimAnimator = null;
814 }
815 });
816 mScrimAnimator.setDuration(600);
817 mScrimAnimator.setStartDelay(getWindow().getTransitionBackgroundFadeDuration());
818 mScrimAnimator.start();
819 }
Jon Miranda7fda2852017-07-19 16:07:01 -0700820 mShouldFadeInScrim = false;
Michael Jurkacd496d72013-04-11 11:32:45 -0700821 }
822
823 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800824 protected void onResume() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700825 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800826 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700827 TraceHelper.partitionSection("ON_RESUME", "superCall");
828
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700829 mAppLaunchSuccess = false;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700830 getUserEventDispatcher().resetElapsedSessionMillis();
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700831 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700832 // Process any items that were added while Launcher was away.
833 InstallShortcutReceiver.disableAndFlushInstallQueue(
834 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700835
Sunny Goyala474a9b2017-05-04 16:47:11 -0700836 // Refresh shortcuts if the permission changed.
837 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700838
Sunny Goyal7ede6112017-12-05 15:11:21 -0800839 DiscoveryBounce.showIfNeeded(this);
Adam Cohen9211d422014-10-07 18:14:53 -0700840 if (mLauncherCallbacks != null) {
841 mLauncherCallbacks.onResume();
842 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800843
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700844 TraceHelper.endSection("ON_RESUME");
Adam Cohen06dff352012-06-01 17:17:08 -0700845 }
846
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800847 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700848 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700849 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700850 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700851
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700852 super.onPause();
Joe Onorato24b6fd82009-11-12 13:47:09 -0800853 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700854 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700855
Adam Cohen9211d422014-10-07 18:14:53 -0700856 if (mLauncherCallbacks != null) {
857 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700858 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700859 }
860
Winson Chungd7823942018-02-16 03:23:47 +0000861 @Override
862 public void onWindowFocusChanged(boolean hasFocus) {
863 super.onWindowFocusChanged(hasFocus);
864 mStateManager.onWindowFocusChanged();
865 }
866
Adam Cohenc2d6e892014-10-16 09:49:24 -0700867 public interface LauncherOverlay {
868
869 /**
870 * Touch interaction leading to overscroll has begun
871 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700872 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700873
874 /**
875 * Touch interaction related to overscroll has ended
876 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700877 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700878
879 /**
880 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
881 * screen (or in the case of RTL, the rightmost screen).
882 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700883 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700884
885 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800886 * Called when the launcher is ready to use the overlay
887 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700888 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700889 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700890 }
891
892 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700893 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700894 }
895
896 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
897
Sunny Goyalc86df472016-02-25 09:19:38 -0800898 public void onScrollChanged(float progress) {
899 if (mWorkspace != null) {
900 mWorkspace.onOverlayScrollChanged(progress);
901 }
902 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700903 }
904
Sunny Goyal16764582017-11-06 16:00:34 -0800905 public boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -0700906 if (mLauncherCallbacks != null) {
907 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700908 } else {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800909 // On O and above we there is always some setting present settings (add icon to
910 // home screen or icon badging). On earlier APIs we will have the allow rotation
911 // setting, on devices with a locked orientation,
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700912 return Utilities.ATLEAST_OREO || !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -0700913 }
Jorim Jaggid017f882014-01-14 17:08:48 -0800914 }
915
Sunny Goyalf9403d92017-10-18 10:55:56 -0700916 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -0700917 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700918 }
919
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800920 /**
921 * Restores the previous state, if it exists.
922 *
923 * @param savedState The previous state.
924 */
925 private void restoreState(Bundle savedState) {
926 if (savedState == null) {
927 return;
928 }
929
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700930 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -0700931 LauncherState[] stateValues = LauncherState.values();
932 LauncherState state = stateValues[stateOrdinal];
Sunny Goyalbc683e92017-12-06 10:25:07 -0800933 if (!state.disableRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700934 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800935 }
936
Sunny Goyal2100c782016-08-22 16:00:03 -0700937 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
938 if (requestArgs != null) {
939 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800940 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700941
942 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700943
944 SparseArray<Parcelable> widgetsState =
945 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
946 if (widgetsState != null) {
947 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
948 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800949 }
950
951 /**
952 * Finds all the views we need and configure them properly.
953 */
954 private void setupViews() {
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800955 mDragLayer = findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700956 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -0700957 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700958 mWorkspace.initParentViews(mDragLayer);
Sunny Goyal16764582017-11-06 16:00:34 -0800959 mOverviewPanel = findViewById(R.id.overview_panel);
Craig Mautner360310b2012-10-26 15:13:08 -0700960
Sunny Goyal784f9c32016-03-23 16:56:54 -0700961 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
962 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
963 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800964
Winson Chung4c98d922011-05-31 16:50:48 -0700965 // Setup the drag layer
Sunny Goyal7ede6112017-12-05 15:11:21 -0800966 mDragLayer.setup(this, mDragController);
Winson Chung4c98d922011-05-31 16:50:48 -0700967
Winson Chung3d503fb2011-07-13 17:25:49 -0700968 // Setup the hotseat
969 mHotseat = (Hotseat) findViewById(R.id.hotseat);
970 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -0700971 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -0700972 }
973
Winson Chung4c98d922011-05-31 16:50:48 -0700974 // Setup the workspace
975 mWorkspace.setHapticFeedbackEnabled(false);
976 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700977 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -0700978 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
979 // default state, otherwise we will update to the wrong offsets in RTL
980 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700981 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700982 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700983
Tony Wickham34d2c912015-09-11 09:27:58 -0700984 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700985 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700986
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700987 // Setup Apps
988 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -0700989
Winson Chung3d503fb2011-07-13 17:25:49 -0700990 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -0700991 mDragController.setMoveTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700992 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -0400993
Hyunyoung Songd725f642017-08-17 22:26:35 -0700994 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800995 }
996
997 /**
998 * Creates a view representing a shortcut.
999 *
1000 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001001 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001002 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001003 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001004 }
1005
1006 /**
1007 * Creates a view representing a shortcut inflated from the specified resource.
1008 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001009 * @param parent The group the shortcut belongs to.
1010 * @param info The data structure describing the shortcut.
1011 *
1012 * @return A View inflated from layoutResId.
1013 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001014 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -08001015 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
1016 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001017 favorite.applyFromShortcutInfo(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001018 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001019 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001020 return favorite;
1021 }
1022
1023 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -07001024 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001025 *
1026 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001027 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001028 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001029 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001030 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
1031 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001032 return;
1033 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001034
Jon Mirandac476d6e2017-05-04 16:30:01 -07001035 int[] cellXY = mTmpAddItemCellCoordinates;
1036 CellLayout layout = getCellLayout(container, screenId);
1037
Sunny Goyal782f0c92017-01-19 10:27:54 -08001038 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001039 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001040 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
1041 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001042 }
1043
1044 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001045 // Legacy shortcuts are only supported for primary profile.
1046 info = Process.myUserHandle().equals(args.user)
1047 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001048
Sunny Goyalb57645f2017-03-20 13:57:28 -07001049 if (info == null) {
1050 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1051 return;
1052 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001053 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001054 // The app is trying to add a shortcut without sufficient permissions
1055 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1056 return;
1057 }
1058 }
1059
Jon Mirandac476d6e2017-05-04 16:30:01 -07001060 if (container < 0) {
1061 // Adding a shortcut to the Workspace.
1062 final View view = createShortcut(info);
1063 boolean foundCellSpan = false;
1064 // First we check if we already know the exact location where we want to add this item.
1065 if (cellX >= 0 && cellY >= 0) {
1066 cellXY[0] = cellX;
1067 cellXY[1] = cellY;
1068 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001069
Jon Mirandac476d6e2017-05-04 16:30:01 -07001070 // If appropriate, either create a folder or add to an existing folder
1071 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001072 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001073 return;
1074 }
1075 DragObject dragObject = new DragObject();
1076 dragObject.dragInfo = info;
1077 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1078 true)) {
1079 return;
1080 }
1081 } else {
1082 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1083 }
1084
1085 if (!foundCellSpan) {
1086 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001087 return;
1088 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001089
1090 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1091 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001092 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001093 // Adding a shortcut to a Folder.
Sunny Goyale29897f2017-07-20 10:09:42 -07001094 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001095 if (folderIcon != null) {
1096 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1097 folderInfo.add(info, args.rank, false);
1098 } else {
Sunny Goyale29897f2017-07-20 10:09:42 -07001099 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001100 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001101 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001102 }
1103
Sunny Goyale29897f2017-07-20 10:09:42 -07001104 public FolderIcon findFolderIcon(final long folderIconId) {
1105 return (FolderIcon) mWorkspace.getFirstMatch(new ItemOperator() {
1106 @Override
1107 public boolean evaluate(ItemInfo info, View view) {
1108 return info != null && info.id == folderIconId;
1109 }
1110 });
1111 }
1112
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001113 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001114 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001115 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001116 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001117 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001118 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001119 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1120
Adam Cohened66b2b2012-01-23 17:28:51 -08001121 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001122 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001123 }
Romain Guycbb89e42009-06-08 15:52:54 -07001124
Adam Cohen59400422014-03-05 18:07:04 -08001125 LauncherAppWidgetInfo launcherInfo;
1126 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001127 launcherInfo.spanX = itemInfo.spanX;
1128 launcherInfo.spanY = itemInfo.spanY;
1129 launcherInfo.minSpanX = itemInfo.minSpanX;
1130 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001131 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001132
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001133 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001134 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001135
Sunny Goyal2100c782016-08-22 16:00:03 -07001136 if (hostView == null) {
1137 // Perform actual inflation because we're live
1138 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001139 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001140 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301141 prepareAppWidget(hostView, launcherInfo);
1142 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001143 }
Romain Guycbb89e42009-06-08 15:52:54 -07001144
Sunny Goyald5462aa2016-11-22 16:52:39 +05301145 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001146 hostView.setTag(item);
1147 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001148 hostView.setFocusable(true);
1149 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001150 }
1151
Adam Cohended9f8d2010-11-03 13:25:16 -07001152 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1153 @Override
1154 public void onReceive(Context context, Intent intent) {
1155 final String action = intent.getAction();
1156 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001157 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001158 // processing a multi-step drop
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001159 if (mAppsView != null && mPendingRequestArgs == null) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001160 mStateManager.goToState(NORMAL);
Winson Chung785d2eb2011-04-14 16:08:02 -07001161 }
Jon Miranda7fda2852017-07-19 16:07:01 -07001162 mShouldFadeInScrim = true;
1163 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1164 // ACTION_USER_PRESENT is sent after onStart/onResume. This covers the case where
1165 // the user unlocked and the Launcher is not in the foreground.
1166 mShouldFadeInScrim = false;
Adam Cohended9f8d2010-11-03 13:25:16 -07001167 }
1168 }
1169 };
1170
Tony Wickham010d2552017-01-20 08:15:28 -08001171 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
Sunny Goyal29947f02017-12-18 13:49:44 -08001172 mWorkspace.updateIconBadges(updatedBadges);
Sunny Goyal60180b02018-02-07 12:56:30 -08001173 mAppsView.getAppsStore().updateIconBadges(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001174
Sunny Goyal29947f02017-12-18 13:49:44 -08001175 PopupContainerWithArrow popup = PopupContainerWithArrow.getOpen(Launcher.this);
1176 if (popup != null) {
1177 popup.updateNotificationHeader(updatedBadges);
Tony Wickham010d2552017-01-20 08:15:28 -08001178 }
1179 }
1180
Adam Cohended9f8d2010-11-03 13:25:16 -07001181 @Override
1182 public void onAttachedToWindow() {
1183 super.onAttachedToWindow();
1184
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001185 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyalc86df472016-02-25 09:19:38 -08001186 if (mLauncherCallbacks != null) {
1187 mLauncherCallbacks.onAttachedToWindow();
1188 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001189 }
1190
1191 @Override
1192 public void onDetachedFromWindow() {
1193 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001194
1195 if (mLauncherCallbacks != null) {
1196 mLauncherCallbacks.onDetachedFromWindow();
1197 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001198 }
1199
Sunny Goyal3bac2c22018-02-07 14:44:05 -08001200 public void onQuickstepGestureStarted(boolean isVisible) {
1201 if (mLauncherCallbacks != null) {
1202 mLauncherCallbacks.onQuickstepGestureStarted(isVisible);
1203 }
1204 }
1205
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001206 public AllAppsTransitionController getAllAppsController() {
1207 return mAllAppsController;
1208 }
1209
Winson Chung1a341002018-01-17 10:00:23 -08001210 public LauncherRootView getRootView() {
1211 return (LauncherRootView) mLauncherView;
1212 }
1213
Winson Chunga6945242014-01-08 14:04:34 -08001214 public DragLayer getDragLayer() {
1215 return mDragLayer;
1216 }
1217
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001218 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001219 return mAppsView;
1220 }
1221
Winson Chunga6945242014-01-08 14:04:34 -08001222 public Workspace getWorkspace() {
1223 return mWorkspace;
1224 }
1225
1226 public Hotseat getHotseat() {
1227 return mHotseat;
1228 }
1229
Winson Chung8ae41982017-11-10 11:42:13 -08001230 public <T extends ViewGroup> T getOverviewPanel() {
1231 return (T) mOverviewPanel;
Winson Chunga6945242014-01-08 14:04:34 -08001232 }
1233
Sunny Goyal47328fd2016-05-25 18:56:41 -07001234 public DropTargetBar getDropTargetBar() {
1235 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001236 }
1237
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001238 public LauncherAppWidgetHost getAppWidgetHost() {
1239 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001240 }
Romain Guycbb89e42009-06-08 15:52:54 -07001241
Winson Chunga9abd0e2010-10-27 17:18:37 -07001242 public LauncherModel getModel() {
1243 return mModel;
1244 }
1245
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001246 public ModelWriter getModelWriter() {
1247 return mModelWriter;
1248 }
1249
Adam Cohen79d90c52016-04-22 13:29:20 -07001250 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001251 return mSharedPrefs;
1252 }
1253
Sunny Goyalf8d56fc2018-01-31 15:18:11 -08001254 public int getOrientation() { return mOldConfig.orientation; }
Jon Miranda6bed3502017-09-19 14:43:17 -07001255
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001256 @Override
1257 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001258 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001259 super.onNewIntent(intent);
1260
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001261 boolean alreadyOnHome = hasWindowFocus() && ((intent.getFlags() &
Winson15f8b172015-08-19 11:02:39 -07001262 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1263 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001264
1265 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001266 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001267 && AbstractFloatingView.getTopOpenView(this) == null;
Winson15f8b172015-08-19 11:02:39 -07001268 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001269 boolean internalStateHandled = InternalStateHandler
Sunny Goyal8b264562018-02-03 23:00:13 -08001270 .handleNewIntent(this, intent, isStarted());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001271
Winson15f8b172015-08-19 11:02:39 -07001272 if (isActionMain) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001273 if (!internalStateHandled) {
1274 // Note: There should be at most one log per method call. This is enforced
1275 // implicitly by using if-else statements.
1276 UserEventDispatcher ued = getUserEventDispatcher();
1277 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
1278 if (topOpenView != null) {
1279 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
1280 } else if (alreadyOnHome) {
1281 Target target = newContainerTarget(mStateManager.getState().containerType);
1282 target.pageIndex = mWorkspace.getCurrentPage();
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001283 ued.logActionCommand(Action.Command.HOME_INTENT, target,
1284 newContainerTarget(ContainerType.WORKSPACE));
Sunny Goyald3864fa2017-11-14 15:06:36 -08001285 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001286
Sunny Goyald3864fa2017-11-14 15:06:36 -08001287 // In all these cases, only animate if we're already on home
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001288 AbstractFloatingView.closeAllOpenViews(this, isStarted());
Jon Mirandafeba90f2016-10-06 10:53:29 -07001289
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001290 mStateManager.goToState(NORMAL);
Sunny Goyald3864fa2017-11-14 15:06:36 -08001291
1292 // Reset the apps view
1293 if (!alreadyOnHome && mAppsView != null) {
1294 mAppsView.reset();
1295 }
1296
1297 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()) {
1298 mWorkspace.post(mWorkspace::moveToDefaultScreen);
1299 }
1300 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001301
1302 final View v = getWindow().peekDecorView();
1303 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001304 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001305 }
1306
Adam Cohen9211d422014-10-07 18:14:53 -07001307 if (mLauncherCallbacks != null) {
1308 mLauncherCallbacks.onHomeIntent();
1309 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001310 }
Winson15f8b172015-08-19 11:02:39 -07001311
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001312 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001313 }
1314
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001315 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001316 public void onRestoreInstanceState(Bundle state) {
1317 super.onRestoreInstanceState(state);
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001318 mWorkspace.restoreInstanceStateForChild(mSynchronouslyBoundPage);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001319 }
1320
1321 @Override
1322 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001323 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001324 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001325
Adam Cohen21cd0022013-10-09 18:57:02 -07001326 }
Sunny Goyalea609262017-10-25 15:47:38 -07001327 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001328
1329
1330 AbstractFloatingView widgets = AbstractFloatingView
1331 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1332 if (widgets != null) {
1333 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1334 widgets.saveHierarchyState(widgetsState);
1335 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1336 } else {
1337 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1338 }
1339
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001340 // We close any open folders and shortcut containers since they will not be re-opened,
1341 // and we need to make sure this state is reflected.
1342 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001343
Sunny Goyal2100c782016-08-22 16:00:03 -07001344 if (mPendingRequestArgs != null) {
1345 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1346 }
1347 if (mPendingActivityResult != null) {
1348 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001349 }
1350
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001351 super.onSaveInstanceState(outState);
1352
Adam Cohen9211d422014-10-07 18:14:53 -07001353 if (mLauncherCallbacks != null) {
1354 mLauncherCallbacks.onSaveInstanceState(outState);
1355 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001356 }
1357
1358 @Override
1359 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001360 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001361
Sunny Goyal5cc7bbd2017-06-12 09:50:39 -07001362 unregisterReceiver(mReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001363 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001364
Winson Chungcd2b0142011-06-08 16:02:26 -07001365 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001366 // It's possible to receive onDestroy after a new Launcher activity has
1367 // been created. In this case, don't interfere with the new Launcher.
1368 if (mModel.isCurrentCallbacks(this)) {
1369 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001370 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001371 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001372
Sunny Goyal745bad92016-05-02 10:54:12 -07001373 if (mRotationPrefChangeHandler != null) {
1374 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1375 }
1376
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001377 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001378 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001379 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001380 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001381 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001382
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001383 TextKeyListener.getInstance().release();
Mario Bertschler27288382017-05-24 15:35:09 -07001384 WallpaperColorInfo.getInstance(this).setOnThemeChangeListener(null);
1385
Michael Jurka2ecf9952012-06-18 12:52:28 -07001386 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001387
Tony2917a8b2017-07-31 23:29:42 -07001388 clearPendingBinds();
1389
Adam Cohen9211d422014-10-07 18:14:53 -07001390 if (mLauncherCallbacks != null) {
1391 mLauncherCallbacks.onDestroy();
1392 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001393 }
1394
Sunny Goyalae502842016-06-17 08:43:56 -07001395 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1396 return mAccessibilityDelegate;
1397 }
1398
Adam Cohena9cf38f2011-05-02 15:36:58 -07001399 public DragController getDragController() {
1400 return mDragController;
1401 }
1402
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001403 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001404 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001405 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001406 }
1407
1408 @Override
1409 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1410 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001411 try {
1412 super.startIntentSenderForResult(intent, requestCode,
1413 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1414 } catch (IntentSender.SendIntentException e) {
1415 throw new ActivityNotFoundException();
1416 }
1417 }
1418
Winson Chung70d72102011-08-12 11:24:35 -07001419 /**
1420 * Indicates that we want global search for this activity by setting the globalSearch
1421 * argument for {@link #startSearch} to true.
1422 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001423 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001424 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001425 Bundle appSearchData, boolean globalSearch) {
1426 if (appSearchData == null) {
1427 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001428 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001429 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001430
Sunny Goyal6f28e712016-09-13 14:19:29 -07001431 if (mLauncherCallbacks == null ||
1432 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1433 // Starting search from the callbacks failed. Start the default global search.
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001434 super.startSearch(initialQuery, selectInitialQuery, appSearchData, true);
Ian Parkinson047036e2014-09-01 15:40:53 +01001435 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001436
1437 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001438 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001439 }
1440
Joe Onorato9c1289c2009-08-17 11:03:03 -04001441 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001442 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001443 }
1444
Adam Cohen517a7f52014-03-01 12:12:59 -08001445 public boolean isWorkspaceLoading() {
1446 return mWorkspaceLoading;
1447 }
1448
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001449 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001450 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001451 }
1452
Sunny Goyal04a324a2017-01-20 21:08:59 -08001453 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001454 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001455 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001456
Sunny Goyal2100c782016-08-22 16:00:03 -07001457 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001458 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001459 if (LOGD) {
1460 Log.d(TAG, "Adding widget from drop");
1461 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001462 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001463 }
1464
Sunny Goyal2100c782016-08-22 16:00:03 -07001465 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001466 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1467 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1468 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001469
Adam Cohenad4e15c2013-10-17 16:21:35 -07001470 Runnable onComplete = new Runnable() {
1471 @Override
1472 public void run() {
1473 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001474 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001475 }
1476 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001477 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001478 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001479 }
1480 }
Romain Guycbb89e42009-06-08 15:52:54 -07001481
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001482 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1483 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001484 info.container = container;
1485 info.screenId = screenId;
1486 if (cell != null) {
1487 info.cellX = cell[0];
1488 info.cellY = cell[1];
1489 }
1490 info.spanX = spanX;
1491 info.spanY = spanY;
1492
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001493 switch (info.itemType) {
1494 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1495 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001496 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001497 break;
1498 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001499 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001500 break;
1501 default:
1502 throw new IllegalStateException("Unknown item type: " + info.itemType);
1503 }
1504 }
1505
Adam Cohenfbba09b2011-07-18 21:43:05 -07001506 /**
1507 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001508 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001509 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001510 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1511 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001512 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1513 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1514 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001515 }
1516
Adam Cohenfbba09b2011-07-18 21:43:05 -07001517 /**
1518 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001519 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001520 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001521 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001522 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001523 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001524 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001525 // In the case where we've prebound the widget, we remove it from the DragLayer
1526 if (LOGD) {
1527 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1528 }
1529 getDragLayer().removeView(hostView);
1530
Adam Cohened66b2b2012-01-23 17:28:51 -08001531 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001532 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001533
1534 // Clear the boundWidget so that it doesn't get destroyed.
1535 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001536 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001537 // In this case, we either need to start an activity to get permission to bind
1538 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001539 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1540 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1541 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1542 this, info.componentName);
1543 } else {
1544 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1545 }
Adam Cohendd70d662012-10-04 16:53:44 -07001546 Bundle options = info.bindOptions;
1547
Sunny Goyalffe83f12014-08-14 17:39:34 -07001548 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1549 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001550 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001551 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001552 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001553 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001554 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001555 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001556 }
1557
Adam Cohendcd297f2013-06-18 13:13:40 -07001558 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001559 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001560 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001561 folderInfo.title = getText(R.string.folder_name);
1562
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001563 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001564 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001565
1566 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001567 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301568 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001569 // Force measure the new folder icon
1570 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1571 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001572 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001573 }
Romain Guycbb89e42009-06-08 15:52:54 -07001574
Winsonc0b52fe2015-09-09 16:38:15 -07001575 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001576 * Unbinds the view for the specified item, and removes the item and all its children.
1577 *
1578 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001579 * @param itemInfo the {@link ItemInfo} for this view.
1580 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001581 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001582 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001583 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001584 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001585 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1586 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001587 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001588 } else {
1589 mWorkspace.removeWorkspaceItem(v);
1590 }
Winsonc0b52fe2015-09-09 16:38:15 -07001591 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001592 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001593 }
1594 } else if (itemInfo instanceof FolderInfo) {
1595 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001596 if (v instanceof FolderIcon) {
1597 ((FolderIcon) v).removeListeners();
1598 }
Winsonc0b52fe2015-09-09 16:38:15 -07001599 mWorkspace.removeWorkspaceItem(v);
1600 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001601 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001602 }
1603 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1604 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001605 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001606 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07001607 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001608 }
1609 } else {
1610 return false;
1611 }
1612 return true;
1613 }
1614
1615 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07001616 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07001617 */
Sunny Goyal56c73602015-09-25 12:55:01 -07001618 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07001619 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07001620 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07001621 // Deleting an app widget ID is a void call but writes to disk before returning
1622 // to the caller...
1623 new AsyncTask<Void, Void, Void>() {
1624 public Void doInBackground(Void ... args) {
1625 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
1626 return null;
1627 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07001628 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07001629 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001630 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001631 }
1632
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001633 @Override
1634 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001635 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001636 }
1637
Joe Onorato88ec0992009-11-19 13:16:06 -08001638 @Override
1639 public void onBackPressed() {
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08001640 if (finishAutoCancelActionMode()) {
1641 return;
1642 }
Adam Cohen9211d422014-10-07 18:14:53 -07001643 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1644 return;
1645 }
1646
Sunny Goyal45478022015-06-08 16:52:41 -07001647 if (mDragController.isDragging()) {
1648 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001649 return;
1650 }
1651
Jon Mirandafeba90f2016-10-06 10:53:29 -07001652 // Note: There should be at most one log per method call. This is enforced implicitly
1653 // by using if-else statements.
1654 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001655 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
1656 if (topView != null) {
Sunny Goyal37920962017-09-28 13:43:24 -07001657 topView.onBackPressed();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001658 } else if (!isInState(NORMAL)) {
Sunny Goyal49bcf342018-01-11 13:59:53 -08001659 LauncherState lastState = mStateManager.getLastState();
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001660 ued.logActionCommand(Action.Command.BACK, mStateManager.getState().containerType,
1661 lastState.containerType);
Sunny Goyal49bcf342018-01-11 13:59:53 -08001662 mStateManager.goToState(lastState);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001663 } else {
1664 // Back button is a no-op here, but give at least some feedback for the button press
1665 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001666 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001667 }
1668
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001669 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001670 * Launches the intent referred by the clicked shortcut.
1671 *
1672 * @param v The view representing the clicked shortcut.
1673 */
1674 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001675 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1676 // view has detached (it's possible for this to happen if the view is removed mid touch).
1677 if (v.getWindowToken() == null) {
1678 return;
1679 }
1680
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001681 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001682 return;
1683 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001684
Adam Cohen1697b792013-09-17 19:08:21 -07001685 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001686 if (isInState(OVERVIEW)) {
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001687 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Touch.TAP,
Jon Miranda2e61fba2017-02-24 09:12:59 -08001688 LauncherLogProto.Action.Direction.NONE,
1689 LauncherLogProto.ContainerType.OVERVIEW, mWorkspace.getCurrentPage());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001690 mStateManager.goToState(NORMAL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001691 }
1692 return;
1693 }
1694
1695 if (v instanceof CellLayout) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001696 if (isInState(OVERVIEW)) {
Jon Mirandac6cf4932017-02-07 17:12:36 -08001697 int page = mWorkspace.indexOfChild(v);
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -08001698 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Touch.TAP,
Jon Mirandac6cf4932017-02-07 17:12:36 -08001699 LauncherLogProto.Action.Direction.NONE,
1700 LauncherLogProto.ContainerType.OVERVIEW, page);
1701 mWorkspace.snapToPageFromOverView(page);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001702 mStateManager.goToState(NORMAL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001703 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07001704 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001705 }
1706
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001707 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001708 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001709 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001710 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001711 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001712 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001713 }
Sunny Goyal508da152014-08-14 10:53:27 -07001714 } else if (tag instanceof AppInfo) {
1715 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07001716 } else if (tag instanceof LauncherAppWidgetInfo) {
1717 if (v instanceof PendingAppWidgetHostView) {
1718 onClickPendingWidget((PendingAppWidgetHostView) v);
1719 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001720 }
1721 }
1722
Sunny Goyal70660032015-05-14 00:07:08 -07001723 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07001724 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07001725 return false;
1726 }
1727
Michael Jurkaaf442092010-06-10 17:01:57 -07001728 /**
Sunny Goyalff572272014-07-23 13:58:07 -07001729 * Event handler for the app widget view which has not fully restored.
1730 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001731 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07001732 if (mIsSafeModeEnabled) {
1733 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
1734 return;
1735 }
1736
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001737 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07001738 if (v.isReadyForClickSetup()) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001739 LauncherAppWidgetProviderInfo appWidgetInfo =
1740 mAppWidgetManager.findProvider(info.providerName, info.user);
1741 if (appWidgetInfo == null) {
1742 return;
1743 }
1744 WidgetAddFlowHandler addFlowHandler = new WidgetAddFlowHandler(appWidgetInfo);
1745
Sunny Goyald478c832016-04-01 12:04:16 -07001746 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
1747 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
1748 // This should not happen, as we make sure that an Id is allocated during bind.
1749 return;
1750 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001751 addFlowHandler.startBindFlow(this, info.appWidgetId, info,
1752 REQUEST_BIND_PENDING_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -07001753 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001754 addFlowHandler.startConfigActivity(this, info, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07001755 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001756 } else {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001757 final String packageName = info.providerName.getPackageName();
Sunny Goyale6e72002017-01-12 16:55:36 -08001758 onClickPendingAppItem(v, packageName, info.installProgress >= 0);
Sunny Goyalff572272014-07-23 13:58:07 -07001759 }
1760 }
1761
Sunny Goyale6e72002017-01-12 16:55:36 -08001762 private void onClickPendingAppItem(final View v, final String packageName,
1763 boolean downloadStarted) {
1764 if (downloadStarted) {
1765 // If the download has started, simply direct to the market app.
1766 startMarketIntentForPackage(v, packageName);
1767 return;
1768 }
Sunny Goyale03b8122014-10-08 09:55:24 -07001769 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001770 .setTitle(R.string.abandoned_promises_title)
1771 .setMessage(R.string.abandoned_promise_explanation)
Sunny Goyale6e72002017-01-12 16:55:36 -08001772 .setPositiveButton(R.string.abandoned_search, new DialogInterface.OnClickListener() {
1773 @Override
1774 public void onClick(DialogInterface dialogInterface, int i) {
1775 startMarketIntentForPackage(v, packageName);
1776 }
1777 })
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001778 .setNeutralButton(R.string.abandoned_clean_this,
1779 new DialogInterface.OnClickListener() {
1780 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001781 final UserHandle user = Process.myUserHandle();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001782 mWorkspace.removeAbandonedPromise(packageName, user);
1783 }
1784 })
1785 .create().show();
Sunny Goyale6e72002017-01-12 16:55:36 -08001786 }
1787
1788 private void startMarketIntentForPackage(View v, String packageName) {
1789 ItemInfo item = (ItemInfo) v.getTag();
Sunny Goyal67e75e22018-01-22 15:14:00 -08001790 Intent intent = new PackageManagerHelper(v.getContext()).getMarketIntent(packageName);
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001791 startActivitySafely(v, intent, item);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001792 }
1793
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001794 /**
1795 * Event handler for an app shortcut click.
1796 *
1797 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
1798 */
Chris Wren40c5ed32014-06-24 18:24:23 -04001799 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001800 if (LOGD) Log.d(TAG, "onClickAppShortcut");
1801 Object tag = v.getTag();
1802 if (!(tag instanceof ShortcutInfo)) {
1803 throw new IllegalArgumentException("Input must be a Shortcut");
1804 }
1805
1806 // Open shortcut
1807 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001808
Sunny Goyal076839c2017-10-30 13:52:20 -07001809 if (shortcut.isDisabled()) {
Tony Makf6308652017-12-02 16:56:22 +00001810 final int disabledFlags = shortcut.runtimeStatusFlags & ShortcutInfo.FLAG_DISABLED_MASK;
1811 if ((disabledFlags &
Sunny Goyal076839c2017-10-30 13:52:20 -07001812 ~FLAG_DISABLED_SUSPENDED &
1813 ~FLAG_DISABLED_QUIET_USER) == 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001814 // If the app is only disabled because of the above flags, launch activity anyway.
1815 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00001816 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07001817 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
1818 // Use a message specific to this shortcut, if it has one.
1819 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
1820 return;
1821 }
1822 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00001823 int error = R.string.activity_not_available;
Sunny Goyal076839c2017-10-30 13:52:20 -07001824 if ((shortcut.runtimeStatusFlags & FLAG_DISABLED_SAFEMODE) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00001825 error = R.string.safemode_shortcut_error;
Sunny Goyal076839c2017-10-30 13:52:20 -07001826 } else if ((shortcut.runtimeStatusFlags & FLAG_DISABLED_BY_PUBLISHER) != 0 ||
1827 (shortcut.runtimeStatusFlags & FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07001828 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00001829 }
1830 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
1831 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001832 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001833 }
1834
Chris Wren40c5ed32014-06-24 18:24:23 -04001835 // Check for abandoned promise
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -07001836 if ((v instanceof BubbleTextView) && shortcut.hasPromiseIconUi()) {
Sunny Goyale6e72002017-01-12 16:55:36 -08001837 String packageName = shortcut.intent.getComponent() != null ?
1838 shortcut.intent.getComponent().getPackageName() : shortcut.intent.getPackage();
1839 if (!TextUtils.isEmpty(packageName)) {
1840 onClickPendingAppItem(v, packageName,
1841 shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE));
1842 return;
1843 }
Chris Wren40c5ed32014-06-24 18:24:23 -04001844 }
1845
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001846 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07001847 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04001848 }
1849
Sunny Goyala7ce1662016-05-31 15:01:35 -07001850 private void startAppShortcutOrInfoActivity(View v) {
1851 ItemInfo item = (ItemInfo) v.getTag();
Mario Bertschler08ffaae2017-03-20 11:30:27 -07001852 Intent intent;
1853 if (item instanceof PromiseAppInfo) {
1854 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) item;
Sunny Goyal67e75e22018-01-22 15:14:00 -08001855 intent = promiseAppInfo.getMarketIntent(this);
Mario Bertschler08ffaae2017-03-20 11:30:27 -07001856 } else {
1857 intent = item.getIntent();
1858 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07001859 if (intent == null) {
1860 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07001861 }
Mario Bertschler8d01cb42018-02-13 14:47:02 -08001862 if (item instanceof ShortcutInfo) {
1863 ShortcutInfo si = (ShortcutInfo) item;
1864 if (si.hasStatusFlag(ShortcutInfo.FLAG_SUPPORTS_WEB_UI)
1865 && intent.getAction() == Intent.ACTION_VIEW) {
1866 // make a copy of the intent that has the package set to null
1867 // we do this because the platform sometimes disables instant
1868 // apps temporarily (triggered by the user) and fallbacks to the
1869 // web ui. This only works though if the package isn't set
1870 intent = new Intent(intent);
1871 intent.setPackage(null);
1872 }
1873 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001874 startActivitySafely(v, intent, item);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001875 }
1876
1877 /**
1878 * Event handler for a folder icon click.
1879 *
1880 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
1881 */
1882 protected void onClickFolderIcon(View v) {
1883 if (LOGD) Log.d(TAG, "onClickFolder");
1884 if (!(v instanceof FolderIcon)){
1885 throw new IllegalArgumentException("Input must be a FolderIcon");
1886 }
1887
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001888 Folder folder = ((FolderIcon) v).getFolder();
1889 if (!folder.isOpen() && !folder.isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001890 // Open the requested folder
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001891 folder.animateOpen();
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001892 }
Michael Jurka5130e402011-10-13 04:55:35 -07001893 }
1894
Sandeep Siddharthad8058372014-01-28 10:41:15 -08001895 /**
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001896 * Event handler for the wallpaper picker button that appears after a long press
1897 * on the home screen.
1898 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001899 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07001900 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07001901 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
1902 return;
1903 }
1904
Tony Wickham785f7a52015-08-31 17:28:32 -07001905 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
1906 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal2100c782016-08-22 16:00:03 -07001907 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001908 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001909 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001910
1911 String pickerPackage = getString(R.string.wallpaper_picker_package);
Tony85efb4b2017-06-02 13:06:10 -07001912 boolean hasTargetPackage = !TextUtils.isEmpty(pickerPackage);
1913 if (hasTargetPackage) {
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001914 intent.setPackage(pickerPackage);
1915 }
1916
Sunny Goyala5ace712017-11-15 17:12:51 -08001917 final Bundle launchOptions;
1918 if (v != null) {
1919 intent.setSourceBounds(getViewBounds(v));
1920 // If there is no target package, use the default intent chooser animation
Jon Miranda17940f02018-01-18 14:46:34 -08001921 launchOptions = hasTargetPackage
1922 ? getActivityLaunchOptions(v, isInMultiWindowModeCompat())
1923 : null;
Sunny Goyala5ace712017-11-15 17:12:51 -08001924 } else {
1925 launchOptions = null;
1926 }
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001927 try {
Sunny Goyala5ace712017-11-15 17:12:51 -08001928 startActivityForResult(intent, REQUEST_PICK_WALLPAPER, launchOptions);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001929 } catch (ActivityNotFoundException e) {
1930 setWaitingForResult(null);
1931 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1932 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001933 }
1934
Sunny Goyala7ce1662016-05-31 15:01:35 -07001935 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001936 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001937 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
1938 try {
1939 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
1940 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
1941 // is enabled by default on NYC.
1942 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
1943 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001944
1945 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
1946 String id = ((ShortcutInfo) info).getDeepShortcutId();
1947 String packageName = intent.getPackage();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301948 DeepShortcutManager.getInstance(this).startShortcut(
Sunny Goyal9994b2b2016-06-23 14:17:24 -07001949 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001950 } else {
1951 // Could be launching some bookkeeping activity
1952 startActivity(intent, optsBundle);
1953 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07001954 } finally {
1955 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01001956 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001957 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001958 // Due to legacy reasons, direct call shortcuts require Launchers to have the
1959 // corresponding permission. Show the appropriate permission prompt if that
1960 // is the case.
1961 if (intent.getComponent() == null
1962 && Intent.ACTION_CALL.equals(intent.getAction())
1963 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
1964 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001965
1966 setWaitingForResult(PendingRequestArgs
1967 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07001968 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
1969 REQUEST_PERMISSION_CALL_PHONE);
1970 } else {
1971 // No idea why this was thrown.
1972 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07001973 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001974 }
1975 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001976
Sunny Goyalfe770c92016-09-27 14:38:58 -07001977 @TargetApi(Build.VERSION_CODES.M)
Jon Miranda17940f02018-01-18 14:46:34 -08001978 public Bundle getActivityLaunchOptions(View v, boolean useDefaultLaunchOptions) {
1979 return useDefaultLaunchOptions
Jon Miranda54441f52018-01-24 15:38:25 -08001980 ? mAppTransitionManager.getDefaultActivityLaunchOptions(this, v)
1981 : mAppTransitionManager.getActivityLaunchOptions(this, v);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001982 }
1983
Tonye3c59252017-05-02 21:32:27 -07001984 public Rect getViewBounds(View v) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001985 int[] pos = new int[2];
1986 v.getLocationOnScreen(pos);
1987 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
1988 }
1989
Sunny Goyala7ce1662016-05-31 15:01:35 -07001990 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07001991 mAppLaunchSuccess = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001992 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
1993 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07001994 return mAppLaunchSuccess;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001995 }
Jon Miranda17940f02018-01-18 14:46:34 -08001996
Sunny Goyala7ce1662016-05-31 15:01:35 -07001997 // Only launch using the new animation if the shortcut has not opted out (this is a
1998 // private contract between launcher and may be ignored in the future).
1999 boolean useLaunchAnimation = (v != null) &&
2000 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Jon Miranda17940f02018-01-18 14:46:34 -08002001 Bundle optsBundle = useLaunchAnimation
Jon Miranda84e71bf2018-02-13 15:36:20 -08002002 ? getActivityLaunchOptions(v, isInMultiWindowModeCompat())
Jon Miranda17940f02018-01-18 14:46:34 -08002003 : null;
Sunny Goyala7ce1662016-05-31 15:01:35 -07002004
Sunny Goyal24bb66a2017-03-21 15:12:01 -07002005 UserHandle user = item == null ? null : item.user;
Sunny Goyala7ce1662016-05-31 15:01:35 -07002006
2007 // Prepare intent
2008 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2009 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002010 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002011 }
2012 try {
Jon Miranda84e71bf2018-02-13 15:36:20 -08002013 boolean isShortcut = Utilities.ATLEAST_MARSHMALLOW
2014 && (item instanceof ShortcutInfo)
2015 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
2016 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
2017 && !((ShortcutInfo) item).isPromise();
Jon Miranda17940f02018-01-18 14:46:34 -08002018 if (isShortcut) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002019 // Shortcuts need some special checks due to legacy reasons.
2020 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002021 } else if (user == null || user.equals(Process.myUserHandle())) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002022 // Could be launching some bookkeeping activity
2023 startActivity(intent, optsBundle);
2024 } else {
2025 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2026 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2027 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002028
2029 if (v instanceof BubbleTextView) {
2030 // This is set to the view that launched the activity that navigated the user away
2031 // from launcher. Since there is no callback for when the activity has finished
2032 // launching, enable the press state and keep this reference to reset the press
2033 // state when we return to launcher.
2034 BubbleTextView btv = (BubbleTextView) v;
2035 btv.setStayPressed(true);
2036 setOnResumeCallback(btv);
2037 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002038 mAppLaunchSuccess = true;
2039 getUserEventDispatcher().logAppLaunch(v, intent); // TODO for discovered apps b/35802115
Sunny Goyala7ce1662016-05-31 15:01:35 -07002040 } catch (ActivityNotFoundException|SecurityException e) {
2041 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2042 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2043 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002044 return mAppLaunchSuccess;
Michael Jurka86a720e2012-05-09 11:23:23 -07002045 }
2046
Tony Wickhamdadb3042016-02-24 11:07:00 -08002047 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07002048 public boolean dispatchTouchEvent(MotionEvent ev) {
Sunny Goyal02424b22018-01-19 11:24:32 -08002049 mLastDispatchTouchEvent.set(ev.getX(), ev.getY());
Jon Miranda379198e2016-09-23 08:54:40 -07002050 return super.dispatchTouchEvent(ev);
2051 }
2052
2053 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002054 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002055 if (!isDraggingEnabled()) return false;
2056 if (isWorkspaceLocked()) return false;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002057 if (!isInState(NORMAL) && !isInState(OVERVIEW)) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002058
Jon Miranda51f037d2016-11-07 08:37:20 -08002059 boolean ignoreLongPressToOverview =
Sunny Goyal02424b22018-01-19 11:24:32 -08002060 mDeviceProfile.shouldIgnoreLongPressToOverview(mLastDispatchTouchEvent.x);
Jon Miranda379198e2016-09-23 08:54:40 -07002061
Adam Cohen1697b792013-09-17 19:08:21 -07002062 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002063 if (!isInState(OVERVIEW)) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002064 if (!mWorkspace.isTouchActive() && !ignoreLongPressToOverview) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302065 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2066 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002067 mWorkspace.getCurrentPage());
Sunny Goyal02424b22018-01-19 11:24:32 -08002068 UiFactory.onWorkspaceLongPress(this, mLastDispatchTouchEvent);
Adam Cohen93c97562013-09-26 13:48:01 -07002069 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2070 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2071 return true;
2072 } else {
2073 return false;
2074 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002075 } else {
2076 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002077 }
Adam Cohen1697b792013-09-17 19:08:21 -07002078 }
2079
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002080 CellLayout.CellInfo longClickCellInfo = null;
2081 View itemUnderLongClick = null;
2082 if (v.getTag() instanceof ItemInfo) {
2083 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002084 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002085 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07002086 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002087 }
2088
Winson Chung3d503fb2011-07-13 17:25:49 -07002089 // The hotseat touch handling does not go through Workspace, and we always allow long press
2090 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002091 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002092 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002093 // User long pressed on empty space
Sunny Goyalc99cb172017-10-19 16:15:09 -07002094 if (mWorkspace.isPageRearrangeEnabled()) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002095 mWorkspace.startReordering(v);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302096 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2097 Action.Direction.NONE, ContainerType.OVERVIEW);
Adam Cohend3ecce92013-09-16 14:58:00 -07002098 } else {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002099 if (ignoreLongPressToOverview) {
Jon Miranda379198e2016-09-23 08:54:40 -07002100 return false;
2101 }
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302102 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2103 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002104 mWorkspace.getCurrentPage());
Sunny Goyal02424b22018-01-19 11:24:32 -08002105 UiFactory.onWorkspaceLongPress(this, mLastDispatchTouchEvent);
Adam Cohendedbd962013-07-11 14:21:49 -07002106 }
Jon Miranda379198e2016-09-23 08:54:40 -07002107 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2108 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002109 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07002110 final boolean isAllAppsButton =
2111 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
2112 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
2113 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05002114 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002115 // User long pressed on an item
Tony Wickham10236d62016-09-28 12:49:25 -07002116 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002117 }
2118 }
2119 }
2120 return true;
2121 }
2122
Winson Chung3d503fb2011-07-13 17:25:49 -07002123 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08002124 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07002125 return mHotseat != null && layout != null &&
2126 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2127 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002128
Winson Chung3d503fb2011-07-13 17:25:49 -07002129 /**
2130 * Returns the CellLayout of the specified container at the specified screen.
2131 */
Sunny Goyal92820592015-03-02 11:31:35 -08002132 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002133 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2134 if (mHotseat != null) {
2135 return mHotseat.getLayout();
2136 } else {
2137 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002138 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002139 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002140 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002141 }
2142 }
2143
Michael Jurkae326f182011-11-21 14:05:46 -08002144 @Override
2145 public void onTrimMemory(int level) {
2146 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07002147 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2148 // The widget preview db can result in holding onto over
2149 // 3MB of memory for caching which isn't necessary.
2150 SQLiteDatabase.releaseMemory();
2151
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08002152 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07002153 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08002154 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01002155 if (mLauncherCallbacks != null) {
2156 mLauncherCallbacks.onTrimMemory(level);
2157 }
Michael Jurkae326f182011-11-21 14:05:46 -08002158 }
2159
Michael Jurkad7c28052012-04-27 15:43:36 -07002160 @Override
2161 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07002162 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07002163 final List<CharSequence> text = event.getText();
2164 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07002165 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002166 // TODO: When can workspace be null?
2167 text.add(mWorkspace == null
2168 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07002169 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07002170 return result;
2171 }
2172
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002173 public void setOnResumeCallback(OnResumeCallback callback) {
2174 if (mOnResumeCallback != null) {
2175 mOnResumeCallback.onLauncherResume();
2176 }
2177 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07002178 }
2179
Michael Jurka7607c2f2013-04-03 14:33:19 -07002180 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002181 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002182 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002183 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002184 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002185 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002186 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002187 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07002188 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07002189 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002190 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002191
Joe Onorato9c1289c2009-08-17 11:03:03 -04002192 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07002193 * Clear any pending bind callbacks. This is called when is loader is planning to
2194 * perform a full rebind from scratch.
2195 */
2196 @Override
2197 public void clearPendingBinds() {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002198 if (mPendingExecutor != null) {
2199 mPendingExecutor.markCompleted();
2200 mPendingExecutor = null;
2201 }
2202 }
2203
2204 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002205 * Refreshes the shortcuts shown on the workspace.
2206 *
2207 * Implementation of the method from LauncherModel.Callbacks.
2208 */
2209 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002210 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002211 // Floating panels (except the full widget sheet) are associated with individual icons. If
2212 // we are starting a fresh bind, close all such panels as all the icons are about
2213 // to go away.
2214 AbstractFloatingView.closeOpenViews(this, true,
Sunny Goyal7ede6112017-12-05 15:11:21 -08002215 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_REBIND_SAFE);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08002216
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002217 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08002218
Winson Chungd64d1762013-08-20 14:37:16 -07002219 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07002220 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07002221 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07002222
Winson Chung3d503fb2011-07-13 17:25:49 -07002223 if (mHotseat != null) {
2224 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002225 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002226 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002227 }
2228
Adam Cohendcd297f2013-06-18 13:13:40 -07002229 @Override
2230 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002231 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002232 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
2233 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002234 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
2235 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002236 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002237 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
2238 // If there are no screens, we need to have an empty screen
2239 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07002240 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002241 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07002242
Winsonc7d2e832016-07-28 12:24:55 -07002243 // After we have added all the screens, if the wallpaper was locked to the default state,
2244 // then notify to indicate that it can be released and a proper wallpaper offset can be
2245 // computed before the next layout
2246 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07002247 }
2248
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002249 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002250 int count = orderedScreenIds.size();
2251 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002252 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002253 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002254 // No need to bind the first screen, as its always bound.
2255 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
2256 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002257 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002258 }
2259
Sunny Goyalb23980c2017-08-17 07:45:25 -07002260 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002261 public void bindAppsAdded(ArrayList<Long> newScreens, ArrayList<ItemInfo> addNotAnimated,
2262 ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07002263 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08002264 if (newScreens != null) {
2265 bindAddScreens(newScreens);
2266 }
Winson Chungd64d1762013-08-20 14:37:16 -07002267
2268 // We add the items without animation on non-visible pages, and with
2269 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08002270 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002271 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002272 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002273 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002274 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07002275 }
Winson Chungc58497e2013-09-03 17:48:37 -07002276
Winson Chung87412982013-10-03 18:34:14 -07002277 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07002278 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002279 }
2280
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002281 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002282 * Bind the items start-end from the list.
2283 *
2284 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002285 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07002286 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07002287 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002288 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07002289 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002290 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07002291 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07002292 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002293 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07002294 int end = items.size();
2295 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002296 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002297
2298 // Short circuit if we are loading dock items for a configuration which has no dock
2299 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2300 mHotseat == null) {
2301 continue;
2302 }
2303
Sunny Goyal639e9062015-08-19 19:17:06 -07002304 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002305 switch (item.itemType) {
2306 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2307 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08002308 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07002309 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07002310 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002311 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002312 }
2313 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07002314 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002315 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002316 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002317 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002318 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07002319 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2320 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07002321 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002322 if (view == null) {
2323 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002324 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08002325 break;
2326 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002327 default:
2328 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002329 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002330
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002331 /*
2332 * Remove colliding items.
2333 */
2334 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
2335 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
2336 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
2337 View v = cl.getChildAt(item.cellX, item.cellY);
2338 Object tag = v.getTag();
2339 String desc = "Collision while binding workspace item: " + item
2340 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08002341 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002342 throw (new RuntimeException(desc));
2343 } else {
2344 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002345 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002346 continue;
2347 }
2348 }
2349 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302350 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07002351 if (animateIcons) {
2352 // Animate all the applications up now
2353 view.setAlpha(0f);
2354 view.setScaleX(0f);
2355 view.setScaleY(0f);
2356 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08002357 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07002358 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002359 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07002360
Winson Chung997a9232013-07-24 15:33:46 -07002361 if (animateIcons) {
2362 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08002363 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07002364 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08002365 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07002366 final Runnable startBounceAnimRunnable = new Runnable() {
2367 public void run() {
2368 anim.playTogether(bounceAnims);
2369 anim.start();
2370 }
2371 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08002372 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07002373 // We post the animation slightly delayed to prevent slowdowns
2374 // when we are loading right after we return to launcher.
2375 mWorkspace.postDelayed(new Runnable() {
2376 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00002377 if (mWorkspace != null) {
2378 mWorkspace.snapToPage(newScreenIndex);
2379 mWorkspace.postDelayed(startBounceAnimRunnable,
2380 NEW_APPS_ANIMATION_DELAY);
2381 }
Winson Chung94d67682013-09-25 16:29:40 -07002382 }
2383 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07002384 } else {
2385 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07002386 }
2387 }
Winson Chung64359a52013-07-08 17:17:08 -07002388 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002389 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002390 }
2391
Joe Onorato9c1289c2009-08-17 11:03:03 -04002392 /**
2393 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002394 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07002395 public void bindAppWidget(LauncherAppWidgetInfo item) {
2396 View view = inflateAppWidget(item);
2397 if (view != null) {
2398 mWorkspace.addInScreen(view, item);
2399 mWorkspace.requestLayout();
2400 }
2401 }
2402
2403 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002404 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302405 PendingAppWidgetHostView view =
2406 new PendingAppWidgetHostView(this, item, mIconCache, true);
2407 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002408 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002409 }
2410
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002411 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002412
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002413 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08002414
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002415 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2416 // If the provider is not ready, bind as a pending widget.
2417 appWidgetInfo = null;
2418 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2419 // The widget id is not valid. Try to find the widget based on the provider info.
2420 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
2421 } else {
2422 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
2423 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002424
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002425 // If the provider is ready, but the width is not yet restored, try to restore it.
2426 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
2427 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07002428 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002429 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
2430 + " belongs to component " + item.providerName
2431 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002432 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002433 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002434 }
Sunny Goyalff572272014-07-23 13:58:07 -07002435
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002436 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002437 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07002438 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2439 // Id has not been allocated yet. Allocate a new id.
2440 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
2441 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07002442
Sunny Goyald478c832016-04-01 12:04:16 -07002443 // Also try to bind the widget. If the bind fails, the user will be shown
2444 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08002445 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07002446 pendingInfo.spanX = item.spanX;
2447 pendingInfo.spanY = item.spanY;
2448 pendingInfo.minSpanX = item.minSpanX;
2449 pendingInfo.minSpanY = item.minSpanY;
2450 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07002451
2452 boolean isDirectConfig =
2453 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2454 if (isDirectConfig && item.bindOptions != null) {
2455 Bundle newOptions = item.bindOptions.getExtras();
2456 if (options != null) {
2457 newOptions.putAll(options);
2458 }
2459 options = newOptions;
2460 }
Sunny Goyald478c832016-04-01 12:04:16 -07002461 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2462 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002463
Sunny Goyal86df1382016-08-10 15:03:22 -07002464 // We tried to bind once. If we were not able to bind, we would need to
2465 // go through the permission dialog, which means we cannot skip the config
2466 // activity.
2467 item.bindOptions = null;
2468 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2469
Sunny Goyald478c832016-04-01 12:04:16 -07002470 // Bind succeeded
2471 if (success) {
2472 // If the widget has a configure activity, it is still needs to set it up,
2473 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002474 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002475 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2476 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002477 }
Sunny Goyald478c832016-04-01 12:04:16 -07002478
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002479 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002480 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002481 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002482 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002483 // The widget was marked as UI not ready, but there is no configure activity to
2484 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002485 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002486 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002487 }
Sunny Goyalff572272014-07-23 13:58:07 -07002488 }
2489
Sunny Goyald5462aa2016-11-22 16:52:39 +05302490 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002491 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002492 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002493 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002494 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07002495 deleteWidgetInfo(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002496 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002497 }
2498
Sunny Goyal233ee962015-08-03 13:05:01 -07002499 item.minSpanX = appWidgetInfo.minSpanX;
2500 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302501 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002502 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302503 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002504 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302505 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002506
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002507 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002508 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002509 }
2510
Sunny Goyalff572272014-07-23 13:58:07 -07002511 /**
2512 * Restores a pending widget.
2513 *
2514 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002515 */
Sunny Goyald478c832016-04-01 12:04:16 -07002516 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002517 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2518 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2519 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002520 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002521 }
2522
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002523 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002524 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002525 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2526 info.pendingItemInfo = null;
2527 }
Sunny Goyalff572272014-07-23 13:58:07 -07002528
Sunny Goyalba47faa2017-10-04 16:50:57 -07002529 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002530 view.reInflate();
Sunny Goyalba47faa2017-10-04 16:50:57 -07002531 }
2532
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002533 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002534 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002535 }
2536
Adam Cohen1462de32012-07-24 22:34:36 -07002537 public void onPageBoundSynchronously(int page) {
Sunny Goyalf4aceab2017-12-06 16:16:31 -08002538 mSynchronouslyBoundPage = page;
Adam Cohen1462de32012-07-24 22:34:36 -07002539 }
2540
Sunny Goyal527c7d32015-08-28 15:19:36 -07002541 @Override
2542 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2543 if (mPendingExecutor != null) {
2544 mPendingExecutor.markCompleted();
2545 }
2546 mPendingExecutor = executor;
Sunny Goyal60180b02018-02-07 12:56:30 -08002547 if (!isInState(ALL_APPS)) {
2548 mAppsView.getAppsStore().setDeferUpdates(true);
2549 mPendingExecutor.execute(() -> mAppsView.getAppsStore().setDeferUpdates(false));
2550 }
2551
Sunny Goyal527c7d32015-08-28 15:19:36 -07002552 executor.attachTo(this);
2553 }
2554
2555 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2556 if (mPendingExecutor == executor) {
2557 mPendingExecutor = null;
2558 }
2559 }
2560
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002561 @Override
2562 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002563 if (mDragLayer.getAlpha() < 1) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002564 mDragLayer.animate().alpha(1).withEndAction(
2565 executor == null ? null : executor::onLoadAnimationCompleted).start();
2566 } else if (executor != null) {
2567 executor.onLoadAnimationCompleted();
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002568 }
2569 }
2570
Joe Onorato9c1289c2009-08-17 11:03:03 -04002571 /**
2572 * Callback saying that there aren't any more items to bind.
2573 *
2574 * Implementation of the method from LauncherModel.Callbacks.
2575 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002576 public void finishBindingItems() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002577 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002578 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002579
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002580 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002581
Sunny Goyal2100c782016-08-22 16:00:03 -07002582 if (mPendingActivityResult != null) {
2583 handleActivityResult(mPendingActivityResult.requestCode,
2584 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2585 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002586 }
2587
Sunny Goyala474a9b2017-05-04 16:47:11 -07002588 InstallShortcutReceiver.disableAndFlushInstallQueue(
2589 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002590
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002591 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002592 }
2593
Winson Chunga2413752012-04-03 14:22:34 -07002594 private boolean canRunNewAppsAnimation() {
2595 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002596 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002597 }
2598
Winson Chungc9168342013-06-26 14:54:55 -07002599 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002600 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07002601 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
2602 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002603 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002604 return bounceAnim;
2605 }
2606
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002607 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002608 * Add the icons for all apps.
2609 *
2610 * Implementation of the method from LauncherModel.Callbacks.
2611 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002612 public void bindAllApplications(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002613 mAppsView.getAppsStore().setApps(apps);
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002614
Adam Cohen9211d422014-10-07 18:14:53 -07002615 if (mLauncherCallbacks != null) {
2616 mLauncherCallbacks.bindAllApplications(apps);
2617 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002618 }
2619
2620 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002621 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
2622 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2623 */
2624 @Override
2625 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002626 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002627 }
2628
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002629 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002630 * A package was updated.
2631 *
2632 * Implementation of the method from LauncherModel.Callbacks.
2633 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002634 @Override
2635 public void bindAppsAddedOrUpdated(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002636 mAppsView.getAppsStore().addOrUpdateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002637 }
2638
Sunny Goyal4390ace2014-10-13 11:33:11 -07002639 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002640 public void bindPromiseAppProgressUpdated(PromiseAppInfo app) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002641 mAppsView.getAppsStore().updatePromiseAppProgress(app);
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002642 }
2643
2644 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002645 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002646 mWorkspace.widgetsRestored(widgets);
2647 }
2648
Joe Onorato9c1289c2009-08-17 11:03:03 -04002649 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002650 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002651 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002652 *
2653 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002654 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002655 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002656 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002657 if (!updated.isEmpty()) {
2658 mWorkspace.updateShortcuts(updated);
2659 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002660 }
2661
2662 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002663 * Update the state of a package, typically related to install state.
2664 *
2665 * Implementation of the method from LauncherModel.Callbacks.
2666 */
Sunny Goyale755d462014-07-22 13:48:29 -07002667 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002668 public void bindRestoreItemsChange(HashSet<ItemInfo> updates) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07002669 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07002670 }
2671
2672 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002673 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07002674 * in addition to specific applications to remove, the reason being that
2675 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002676 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07002677 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002678 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07002679 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002680 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002681 mWorkspace.removeItemsByMatcher(matcher);
2682 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002683 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002684
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002685 @Override
2686 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002687 mAppsView.getAppsStore().removeApps(appInfos);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002688 }
Joe Onoratobe386092009-11-17 17:32:16 -08002689
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002690 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002691 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
2692 mPopupDataProvider.setAllWidgets(allWidgets);
Tony Wickham26b17462017-03-20 17:12:24 -07002693 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
2694 if (topView != null) {
2695 topView.onWidgetsBound();
2696 }
2697 }
2698
Tony Wickham86222d22017-03-29 15:30:43 -07002699 /**
2700 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
2701 * refreshes the widgets and shortcuts associated with the given package/user
2702 */
2703 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07002704 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002705 }
2706
Sunny Goyalc99cb172017-10-19 16:15:09 -07002707 public boolean isRotationEnabled () {
2708 return mRotationEnabled;
Winson Chung400438b2011-01-16 17:53:48 -08002709 }
2710
Winson Chung80baf5a2010-08-09 16:03:15 -07002711 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002712 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08002713 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002714 @Override
2715 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
2716 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002717
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002718 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
2719 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07002720 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002721 writer.println(prefix + " Homescreen " + i);
2722
2723 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
2724 for (int j = 0; j < layout.getChildCount(); j++) {
2725 Object tag = layout.getChildAt(j).getTag();
2726 if (tag != null) {
2727 writer.println(prefix + " " + tag.toString());
2728 }
2729 }
2730 }
2731
2732 writer.println(prefix + " Hotseat");
2733 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07002734 for (int j = 0; j < layout.getChildCount(); j++) {
2735 Object tag = layout.getChildAt(j).getTag();
2736 if (tag != null) {
2737 writer.println(prefix + " " + tag.toString());
2738 }
Adam Cohen4caf2982013-08-20 18:54:31 -07002739 }
Sunny Goyala1365452015-10-01 15:46:24 -07002740
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002741 try {
2742 FileLog.flushAll(writer);
2743 } catch (Exception e) {
2744 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07002745 }
2746 }
2747
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002748 writer.println(prefix + "Misc:");
2749 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
2750 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
2751 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
2752
2753 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002754
Adam Cohen9211d422014-10-07 18:14:53 -07002755 if (mLauncherCallbacks != null) {
2756 mLauncherCallbacks.dump(prefix, fd, writer, args);
2757 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002758 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002759
Sunny Goyal66b24572016-09-21 15:57:55 -07002760 @Override
2761 @TargetApi(Build.VERSION_CODES.N)
2762 public void onProvideKeyboardShortcuts(
2763 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
2764
2765 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002766 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002767 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
2768 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
2769 }
2770 View currentFocus = getCurrentFocus();
2771 if (new CustomActionsPopup(this, currentFocus).canShow()) {
2772 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
2773 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
2774 }
Tony18c4aa42017-05-10 21:23:57 -05002775 if (currentFocus.getTag() instanceof ItemInfo
2776 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002777 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
2778 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
2779 }
2780 if (!shortcutInfos.isEmpty()) {
2781 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
2782 }
2783
2784 super.onProvideKeyboardShortcuts(data, menu, deviceId);
2785 }
2786
2787 @Override
2788 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
2789 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
2790 switch (keyCode) {
2791 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002792 if (isInState(NORMAL)) {
2793 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07002794 return true;
2795 }
2796 break;
2797 case KeyEvent.KEYCODE_S: {
2798 View focusedView = getCurrentFocus();
2799 if (focusedView instanceof BubbleTextView
2800 && focusedView.getTag() instanceof ItemInfo
2801 && mAccessibilityDelegate.performAction(focusedView,
2802 (ItemInfo) focusedView.getTag(),
2803 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal00ac9202017-11-09 15:24:06 -08002804 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07002805 return true;
2806 }
2807 break;
2808 }
2809 case KeyEvent.KEYCODE_O:
2810 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
2811 return true;
2812 }
2813 break;
2814 }
2815 }
2816 return super.onKeyShortcut(keyCode, event);
2817 }
2818
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002819 @Override
2820 public boolean onKeyUp(int keyCode, KeyEvent event) {
2821 if (keyCode == KeyEvent.KEYCODE_MENU) {
2822 // KEYCODE_MENU is sent by some tests, for example
2823 // LauncherJankTests#testWidgetsContainerFling. Don't just remove its handling.
2824 if (!mDragController.isDragging() && !mWorkspace.isSwitchingState() &&
2825 isInState(NORMAL)) {
2826 // Close any open floating views.
2827 AbstractFloatingView.closeAllOpenViews(this);
2828
2829 // Setting the touch point to (-1, -1) will show the options popup in the center of
2830 // the screen.
2831 mLastDispatchTouchEvent.set(-1, -1);
2832 UiFactory.onWorkspaceLongPress(this, mLastDispatchTouchEvent);
2833 }
2834 return true;
2835 }
2836 return super.onKeyUp(keyCode, event);
2837 }
2838
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002839 public static Launcher getLauncher(Context context) {
2840 if (context instanceof Launcher) {
2841 return (Launcher) context;
2842 }
2843 return ((Launcher) ((ContextWrapper) context).getBaseContext());
2844 }
2845
Sunny Goyal5a81c382017-03-20 15:08:06 -07002846 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener {
Sunny Goyal745bad92016-05-02 10:54:12 -07002847
2848 @Override
2849 public void onSharedPreferenceChanged(
2850 SharedPreferences sharedPreferences, String key) {
2851 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
Sunny Goyale13d4642017-05-22 15:58:42 -07002852 // Recreate the activity so that it initializes the rotation preference again.
2853 recreate();
Sunny Goyal745bad92016-05-02 10:54:12 -07002854 }
2855 }
Sunny Goyal745bad92016-05-02 10:54:12 -07002856 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002857
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08002858 @Override
2859 public void onActionModeStarted(ActionMode mode) {
2860 super.onActionModeStarted(mode);
2861 mCurrentActionMode = mode;
2862 }
2863
2864 @Override
2865 public void onActionModeFinished(ActionMode mode) {
2866 super.onActionModeFinished(mode);
2867 mCurrentActionMode = null;
2868 }
2869
2870 public boolean finishAutoCancelActionMode() {
2871 if (mCurrentActionMode != null && AUTO_CANCEL_ACTION_MODE == mCurrentActionMode.getTag()) {
2872 mCurrentActionMode.finish();
2873 return true;
2874 }
2875 return false;
2876 }
2877
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002878 /**
2879 * Callback for listening for onResume
2880 */
2881 public interface OnResumeCallback {
2882
2883 void onLauncherResume();
2884 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002885}