blob: d262db69c456c5d6c7767df01acd7e116000da96 [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
Adam Cohen9211d422014-10-07 18:14:53 -0700297 if (mLauncherCallbacks != null) {
298 mLauncherCallbacks.preOnCreate();
299 }
300
Mario Bertschler27288382017-05-24 15:35:09 -0700301 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
302 wallpaperColorInfo.setOnThemeChangeListener(this);
Mario Bertschlera6936942017-05-31 14:48:19 -0700303 overrideTheme(wallpaperColorInfo.isDark(), wallpaperColorInfo.supportsDarkText());
Mario Bertschler27288382017-05-24 15:35:09 -0700304
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800305 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700306 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400307
Sunny Goyal87f784c2017-01-11 10:48:34 -0800308 LauncherAppState app = LauncherAppState.getInstance(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800309 mOldConfig = new Configuration(getResources().getConfiguration());
310 initDeviceProfile(app.getInvariantDeviceProfile());
Winson Chung6e1c0d32013-10-25 15:24:24 -0700311
Sunny Goyalf7258242015-10-19 16:59:07 -0700312 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700313 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800314 mModel = app.setLauncher(this);
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800315 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout());
Joe Onorato0589f0f2010-02-08 13:44:00 -0800316 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700317 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700318
Joe Onorato41a12d22009-10-31 18:30:00 -0400319 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700320 mAllAppsController = new AllAppsTransitionController(this);
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800321 mStateManager = new LauncherStateManager(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700322
Sunny Goyalffe83f12014-08-14 17:39:34 -0700323 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700324
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700325 mAppWidgetHost = new LauncherAppWidgetHost(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700326 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700327
Sunny Goyal60820d72017-05-09 12:40:11 -0700328 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700329
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800330 setupViews();
Tony Wickham010d2552017-01-20 08:15:28 -0800331 mPopupDataProvider = new PopupDataProvider(this);
332
Sunny Goyald3864fa2017-11-14 15:06:36 -0800333 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
334 // In case we are on a device with locked rotation, we should look at preferences to check
335 // if the user has specifically allowed rotation.
336 if (!mRotationEnabled) {
337 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
338 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
339 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
340 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800341
Sunny Goyald3864fa2017-11-14 15:06:36 -0800342 boolean internalStateHandled = InternalStateHandler.handleCreate(this, getIntent());
343 if (internalStateHandled) {
344 // Temporarily enable the rotation
345 mRotationEnabled = true;
346
347 if (savedInstanceState != null) {
348 // InternalStateHandler has already set the appropriate state.
349 // We dont need to do anything.
350 savedInstanceState.remove(RUNTIME_STATE);
351 }
352 }
353 restoreState(savedInstanceState);
Winson Chungb63b44c2017-11-10 17:54:44 -0800354
Sunny Goyal2100c782016-08-22 16:00:03 -0700355 // We only load the page synchronously if the user rotates (or triggers a
356 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700357 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
358 if (savedInstanceState != null) {
359 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
360 }
Sunny Goyald3864fa2017-11-14 15:06:36 -0800361
Sunny Goyalfe770c92016-09-27 14:38:58 -0700362 if (!mModel.startLoader(currentScreen)) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800363 if (!internalStateHandled) {
364 // If we are not binding synchronously, show a fade in animation when
365 // the first page bind completes.
366 mDragLayer.setAlpha(0);
367 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700368 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700369 // Pages bound synchronously.
370 mWorkspace.setCurrentPage(currentScreen);
371
Sunny Goyal2100c782016-08-22 16:00:03 -0700372 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800373 }
374
375 // For handling default keys
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800376 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800377
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700378 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
379 // we want the screen to auto-rotate based on the current orientation
Sunny Goyalc99cb172017-10-19 16:15:09 -0700380 setRequestedOrientation(mRotationEnabled
381 ? SCREEN_ORIENTATION_UNSPECIFIED : SCREEN_ORIENTATION_NOSENSOR);
Winson Chungaf40f202013-09-18 18:26:31 -0700382
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800383 setContentView(mLauncherView);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800384 getRootView().dispatchInsets();
Mario Bertschlera6936942017-05-31 14:48:19 -0700385
Jon Miranda7fda2852017-07-19 16:07:01 -0700386 // Listen for broadcasts
387 IntentFilter filter = new IntentFilter();
388 filter.addAction(Intent.ACTION_SCREEN_OFF);
389 filter.addAction(Intent.ACTION_USER_PRESENT); // When the device wakes up + keyguard is gone
390 registerReceiver(mReceiver, filter);
391 mShouldFadeInScrim = true;
Sunny Goyala616d2b2017-06-12 13:54:01 -0700392
Sunny Goyal8392c822017-06-20 10:03:56 -0700393 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
394 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700395
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800396 mAppTransitionManager = LauncherAppTransitionManager.newInstance(this);
Jon Miranda7853bdb2018-01-22 17:32:30 -0800397
Sunny Goyal14b32402017-07-31 10:03:28 -0700398 if (mLauncherCallbacks != null) {
399 mLauncherCallbacks.onCreate(savedInstanceState);
400 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700401
402 TraceHelper.endSection("Launcher-onCreate");
Adam Cohen9211d422014-10-07 18:14:53 -0700403 }
404
Sunny Goyal7779d622015-06-11 16:18:39 -0700405 @Override
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800406 public void onConfigurationChanged(Configuration newConfig) {
407 int diff = newConfig.diff(mOldConfig);
408 if ((diff & (CONFIG_ORIENTATION | CONFIG_SCREEN_SIZE)) != 0) {
409 mUserEventDispatcher = null;
410 initDeviceProfile(mDeviceProfile.inv);
Sunny Goyalfde55052018-02-01 14:46:13 -0800411 dispatchDeviceProfileChanged();
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800412
413 getRootView().dispatchInsets();
414 getStateManager().reapplyState();
415
416 // TODO: We can probably avoid rebind when only screen size changed.
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800417 int currentPage = mWorkspace.getNextPage();
418 if (mModel.startLoader(currentPage)) {
419 mWorkspace.setCurrentPage(currentPage);
420 setWorkspaceLoading(true);
421 }
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800422 }
423
424 mOldConfig.setTo(newConfig);
425 super.onConfigurationChanged(newConfig);
426 }
427
428 private void initDeviceProfile(InvariantDeviceProfile idp) {
429 // Load configuration-specific DeviceProfile
430 mDeviceProfile = idp.getDeviceProfile(this);
431 if (isInMultiWindowModeCompat()) {
432 Display display = getWindowManager().getDefaultDisplay();
433 Point mwSize = new Point();
434 display.getSize(mwSize);
435 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
436 }
437 }
438
439 @Override
Mario Bertschler27288382017-05-24 15:35:09 -0700440 public void onThemeChanged() {
441 recreate();
442 }
443
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700444 public LauncherStateManager getStateManager() {
445 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700446 }
447
Tonydc76f8e2018-02-02 21:45:39 -0600448 public LauncherAppTransitionManager getAppTransitionManager() {
449 return mAppTransitionManager;
450 }
451
Mario Bertschlera6936942017-05-31 14:48:19 -0700452 protected void overrideTheme(boolean isDark, boolean supportsDarkText) {
Mario Bertschler27288382017-05-24 15:35:09 -0700453 if (isDark) {
454 setTheme(R.style.LauncherThemeDark);
Mario Bertschlera6936942017-05-31 14:48:19 -0700455 } else if (supportsDarkText) {
456 setTheme(R.style.LauncherThemeDarkText);
Mario Bertschler27288382017-05-24 15:35:09 -0700457 }
458 }
459
460 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700461 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800462 return mLauncherView.findViewById(id);
463 }
464
465 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700466 public void onAppWidgetHostReset() {
467 if (mAppWidgetHost != null) {
468 mAppWidgetHost.startListening();
469 }
470 }
471
Adam Cohen9211d422014-10-07 18:14:53 -0700472 private LauncherCallbacks mLauncherCallbacks;
473
Sunny Goyal32554d12015-12-03 15:31:25 -0800474 /**
475 * Call this after onCreate to set or clear overlay.
476 */
477 public void setLauncherOverlay(LauncherOverlay overlay) {
478 if (overlay != null) {
479 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
480 }
481 mWorkspace.setLauncherOverlay(overlay);
482 }
483
Adam Cohen9211d422014-10-07 18:14:53 -0700484 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
485 mLauncherCallbacks = callbacks;
486 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700487 }
488
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700489 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700490 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700491 if (mLauncherCallbacks != null) {
492 mLauncherCallbacks.onLauncherProviderChange();
493 }
494 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700495
Hyunyoung Song3f471442015-04-08 19:01:34 -0700496 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700497 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
498 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700499 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700500 }
501
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000502 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700503 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
504 // This cast is safe as long as the id < 0x00FFFFFF
505 // Since we jail all the dynamically generated views, there should be no clashes
506 // with any other views.
507 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000508 }
509
Tony Wickham010d2552017-01-20 08:15:28 -0800510 public PopupDataProvider getPopupDataProvider() {
511 return mPopupDataProvider;
512 }
513
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000514 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700515 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
516 * a configuration step, this allows the proper animations to run after other transitions.
517 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700518 private long completeAdd(
519 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
520 long screenId = info.screenId;
521 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700522 // When the screen id represents an actual screen (as opposed to a rank) we make sure
523 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700524 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700525 }
Adam Cohendb364c32014-05-20 14:23:40 -0700526
Sunny Goyal2100c782016-08-22 16:00:03 -0700527 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800528 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700529 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700530 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800531 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700532 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700533 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700534 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700535 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700536 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700537 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700538 int widgetId = appWidgetId;
539 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700540 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700541 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700542 // Since the view was just bound, also launch the configure activity if needed
543 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
544 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800545 if (provider != null) {
546 new WidgetAddFlowHandler(provider)
547 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700548 }
549 }
550 break;
551 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800552 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700553
Adam Cohendb364c32014-05-20 14:23:40 -0700554 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800555 }
556
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800557 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700558 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700559 if (isWorkspaceLoading()) {
560 // process the result once the workspace has loaded.
561 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
562 return;
563 }
564 mPendingActivityResult = null;
565
Winson Chunge341d302013-08-16 14:31:00 -0700566 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700567 final PendingRequestArgs requestArgs = mPendingRequestArgs;
568 setWaitingForResult(null);
569 if (requestArgs == null) {
570 return;
571 }
572
573 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700574
Adam Cohenad4e15c2013-10-17 16:21:35 -0700575 Runnable exitSpringLoaded = new Runnable() {
576 @Override
577 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700578 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700579 }
580 };
581
Michael Jurka8b805b12012-04-18 14:23:14 -0700582 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700583 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700584 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700585 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
586 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700587 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700588 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700589 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700590 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700591 addAppWidgetImpl(
592 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800593 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700594 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700595 }
596 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200597 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700598 if (resultCode == RESULT_OK && isInState(OVERVIEW)) {
Tony Wickhame3054412015-09-09 09:05:25 -0700599 // User could have free-scrolled between pages before picking a wallpaper; make sure
600 // we move to the closest one now.
601 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700602 mStateManager.goToState(NORMAL, false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200603 }
604 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700605 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200606
Adam Cohened66b2b2012-01-23 17:28:51 -0800607 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700608 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700609
Adam Cohened66b2b2012-01-23 17:28:51 -0800610 // We have special handling for widgets
611 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800612 final int appWidgetId;
613 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
614 : -1;
615 if (widgetId < 0) {
616 appWidgetId = pendingAddWidgetId;
617 } else {
618 appWidgetId = widgetId;
619 }
620
Adam Cohenad4e15c2013-10-17 16:21:35 -0700621 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800622 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700623 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
624 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700625 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700626 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700627 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700628 @Override
629 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700630 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700631 }
632 };
Adam Cohendb364c32014-05-20 14:23:40 -0700633
Sunny Goyal2100c782016-08-22 16:00:03 -0700634 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
635 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
636 } else {
637 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
638 // When the screen id represents an actual screen (as opposed to a rank)
639 // we make sure that the drop page actually exists.
640 requestArgs.screenId =
641 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700642 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700643 final CellLayout dropLayout =
644 mWorkspace.getScreenWithId(requestArgs.screenId);
645
646 dropLayout.setDropPending(true);
647 final Runnable onComplete = new Runnable() {
648 @Override
649 public void run() {
650 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
651 dropLayout.setDropPending(false);
652 }
653 };
654 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
655 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700656 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800657 return;
658 }
659
Sunny Goyald478c832016-04-01 12:04:16 -0700660 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
661 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700662 if (resultCode == RESULT_OK) {
663 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700664 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700665 }
666 // Leave the widget in the pending state if the user canceled the configure.
667 return;
668 }
669
Sunny Goyalaad90582015-07-09 14:22:50 -0700670 if (requestCode == REQUEST_CREATE_SHORTCUT) {
671 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700672 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
673 completeAdd(requestCode, data, -1, requestArgs);
674 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
675 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
676
Sunny Goyalaad90582015-07-09 14:22:50 -0700677 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700678 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
679 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800680 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800681 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800682 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800683 }
684
685 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700686 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800687 final int requestCode, final int resultCode, final Intent data) {
688 handleActivityResult(requestCode, resultCode, data);
689 if (mLauncherCallbacks != null) {
690 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
691 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800692 }
693
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700694 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700695 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600696 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700697 PendingRequestArgs pendingArgs = mPendingRequestArgs;
698 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
699 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
700 setWaitingForResult(null);
701
Sunny Goyal28c6b962015-10-12 11:42:05 -0700702 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700703 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700704 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700705 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700706 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700707 Intent intent = pendingArgs.getPendingIntent();
708
Sunny Goyal28c6b962015-10-12 11:42:05 -0700709 if (grantResults.length > 0
710 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700711 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700712 } else {
713 // TODO: Show a snack bar with link to settings
714 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700715 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700716 }
717 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600718 if (mLauncherCallbacks != null) {
719 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
720 grantResults);
721 }
722 }
723
Adam Cohendb364c32014-05-20 14:23:40 -0700724 /**
725 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
726 *
727 * @param screenId the screen id to check
728 * @return the new screen, or screenId if it exists
729 */
730 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800731 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700732 if (dropLayout == null) {
733 // it's possible that the add screen was removed because it was
734 // empty and a re-bind occurred
735 mWorkspace.addExtraEmptyScreen();
736 return mWorkspace.commitExtraEmptyScreen();
737 } else {
738 return screenId;
739 }
740 }
741
Sunny Goyal2100c782016-08-22 16:00:03 -0700742 @Thunk void completeTwoStageWidgetDrop(
743 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
744 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800745 Runnable onCompleteRunnable = null;
746 int animationType = 0;
747
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700748 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800749 if (resultCode == RESULT_OK) {
750 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
751 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800752 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700753 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800754 onCompleteRunnable = new Runnable() {
755 @Override
756 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700757 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700758 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800759 }
760 };
761 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800762 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800763 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800764 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700765 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700766 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700767 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
768 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700769 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700770 // The animated view may be null in the case of a rotation during widget configuration
771 onCompleteRunnable.run();
772 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800773 }
774
775 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700776 protected void onStop() {
777 super.onStop();
778 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700779
780 if (mLauncherCallbacks != null) {
781 mLauncherCallbacks.onStop();
782 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800783 mAppWidgetHost.setListenIfResumed(false);
Tony Wickham010d2552017-01-20 08:15:28 -0800784
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700785 if (!mAppLaunchSuccess) {
786 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
Sunny Goyalea609262017-10-25 15:47:38 -0700787 mStateManager.getState().containerType);
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700788 }
Tony Wickham010d2552017-01-20 08:15:28 -0800789 NotificationListener.removeNotificationsChangedListener();
Sunny Goyalaf161fd2018-02-05 16:34:57 -0800790 getStateManager().moveToRestState();
Michael Jurkacd496d72013-04-11 11:32:45 -0700791 }
792
793 @Override
794 protected void onStart() {
795 super.onStart();
796 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700797
798 if (mLauncherCallbacks != null) {
799 mLauncherCallbacks.onStart();
800 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800801 mAppWidgetHost.setListenIfResumed(true);
Sunny Goyalcac002d2018-01-04 09:51:45 -0800802 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
Jon Miranda2d89ea82017-05-04 11:47:53 -0700803
Jon Mirandade43a712018-01-18 11:35:10 -0800804 if (mShouldFadeInScrim && mLauncherView.getBackground() != null) {
Jon Miranda2d89ea82017-05-04 11:47:53 -0700805 if (mScrimAnimator != null) {
806 mScrimAnimator.cancel();
807 }
Jon Mirandade43a712018-01-18 11:35:10 -0800808 mLauncherView.getBackground().setAlpha(0);
809 mScrimAnimator = ObjectAnimator.ofInt(mLauncherView.getBackground(),
Jon Miranda2d89ea82017-05-04 11:47:53 -0700810 LauncherAnimUtils.DRAWABLE_ALPHA, 0, 255);
811 mScrimAnimator.addListener(new AnimatorListenerAdapter() {
812 @Override
813 public void onAnimationEnd(Animator animation) {
814 mScrimAnimator = null;
815 }
816 });
817 mScrimAnimator.setDuration(600);
818 mScrimAnimator.setStartDelay(getWindow().getTransitionBackgroundFadeDuration());
819 mScrimAnimator.start();
820 }
Jon Miranda7fda2852017-07-19 16:07:01 -0700821 mShouldFadeInScrim = false;
Michael Jurkacd496d72013-04-11 11:32:45 -0700822 }
823
824 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800825 protected void onResume() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700826 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800827 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700828 TraceHelper.partitionSection("ON_RESUME", "superCall");
829
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700830 mAppLaunchSuccess = false;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700831 getUserEventDispatcher().resetElapsedSessionMillis();
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700832 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700833 // Process any items that were added while Launcher was away.
834 InstallShortcutReceiver.disableAndFlushInstallQueue(
835 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700836
Sunny Goyala474a9b2017-05-04 16:47:11 -0700837 // Refresh shortcuts if the permission changed.
838 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700839
Sunny Goyal7ede6112017-12-05 15:11:21 -0800840 DiscoveryBounce.showIfNeeded(this);
Adam Cohen9211d422014-10-07 18:14:53 -0700841 if (mLauncherCallbacks != null) {
842 mLauncherCallbacks.onResume();
843 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800844
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700845 TraceHelper.endSection("ON_RESUME");
Adam Cohen06dff352012-06-01 17:17:08 -0700846 }
847
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800848 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700849 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700850 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700851 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700852
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700853 super.onPause();
Joe Onorato24b6fd82009-11-12 13:47:09 -0800854 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700855 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700856
Adam Cohen9211d422014-10-07 18:14:53 -0700857 if (mLauncherCallbacks != null) {
858 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700859 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700860 }
861
Adam Cohenc2d6e892014-10-16 09:49:24 -0700862 public interface LauncherOverlay {
863
864 /**
865 * Touch interaction leading to overscroll has begun
866 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700867 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700868
869 /**
870 * Touch interaction related to overscroll has ended
871 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700872 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700873
874 /**
875 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
876 * screen (or in the case of RTL, the rightmost screen).
877 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700878 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700879
880 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800881 * Called when the launcher is ready to use the overlay
882 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700883 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700884 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700885 }
886
887 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700888 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700889 }
890
891 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
892
Sunny Goyalc86df472016-02-25 09:19:38 -0800893 public void onScrollChanged(float progress) {
894 if (mWorkspace != null) {
895 mWorkspace.onOverlayScrollChanged(progress);
896 }
897 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700898 }
899
Sunny Goyal16764582017-11-06 16:00:34 -0800900 public boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -0700901 if (mLauncherCallbacks != null) {
902 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700903 } else {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800904 // On O and above we there is always some setting present settings (add icon to
905 // home screen or icon badging). On earlier APIs we will have the allow rotation
906 // setting, on devices with a locked orientation,
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700907 return Utilities.ATLEAST_OREO || !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -0700908 }
Jorim Jaggid017f882014-01-14 17:08:48 -0800909 }
910
Sunny Goyalf9403d92017-10-18 10:55:56 -0700911 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -0700912 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700913 }
914
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800915 /**
916 * Restores the previous state, if it exists.
917 *
918 * @param savedState The previous state.
919 */
920 private void restoreState(Bundle savedState) {
921 if (savedState == null) {
922 return;
923 }
924
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700925 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -0700926 LauncherState[] stateValues = LauncherState.values();
927 LauncherState state = stateValues[stateOrdinal];
Sunny Goyalbc683e92017-12-06 10:25:07 -0800928 if (!state.disableRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700929 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800930 }
931
Sunny Goyal2100c782016-08-22 16:00:03 -0700932 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
933 if (requestArgs != null) {
934 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800935 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700936
937 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700938
939 SparseArray<Parcelable> widgetsState =
940 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
941 if (widgetsState != null) {
942 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
943 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800944 }
945
946 /**
947 * Finds all the views we need and configure them properly.
948 */
949 private void setupViews() {
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800950 mDragLayer = findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700951 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -0700952 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700953 mWorkspace.initParentViews(mDragLayer);
Sunny Goyal16764582017-11-06 16:00:34 -0800954 mOverviewPanel = findViewById(R.id.overview_panel);
Craig Mautner360310b2012-10-26 15:13:08 -0700955
Sunny Goyal784f9c32016-03-23 16:56:54 -0700956 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
957 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
958 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800959
Winson Chung4c98d922011-05-31 16:50:48 -0700960 // Setup the drag layer
Sunny Goyal7ede6112017-12-05 15:11:21 -0800961 mDragLayer.setup(this, mDragController);
Winson Chung4c98d922011-05-31 16:50:48 -0700962
Winson Chung3d503fb2011-07-13 17:25:49 -0700963 // Setup the hotseat
964 mHotseat = (Hotseat) findViewById(R.id.hotseat);
965 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -0700966 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -0700967 }
968
Winson Chung4c98d922011-05-31 16:50:48 -0700969 // Setup the workspace
970 mWorkspace.setHapticFeedbackEnabled(false);
971 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700972 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -0700973 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
974 // default state, otherwise we will update to the wrong offsets in RTL
975 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700976 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700977 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700978
Tony Wickham34d2c912015-09-11 09:27:58 -0700979 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700980 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700981
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700982 // Setup Apps
983 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -0700984
Winson Chung3d503fb2011-07-13 17:25:49 -0700985 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -0700986 mDragController.setMoveTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700987 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -0400988
Hyunyoung Songd725f642017-08-17 22:26:35 -0700989 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800990 }
991
992 /**
993 * Creates a view representing a shortcut.
994 *
995 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800996 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800997 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700998 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800999 }
1000
1001 /**
1002 * Creates a view representing a shortcut inflated from the specified resource.
1003 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001004 * @param parent The group the shortcut belongs to.
1005 * @param info The data structure describing the shortcut.
1006 *
1007 * @return A View inflated from layoutResId.
1008 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001009 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -08001010 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
1011 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001012 favorite.applyFromShortcutInfo(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001013 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001014 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001015 return favorite;
1016 }
1017
1018 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -07001019 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001020 *
1021 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001022 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001023 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001024 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001025 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
1026 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001027 return;
1028 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001029
Jon Mirandac476d6e2017-05-04 16:30:01 -07001030 int[] cellXY = mTmpAddItemCellCoordinates;
1031 CellLayout layout = getCellLayout(container, screenId);
1032
Sunny Goyal782f0c92017-01-19 10:27:54 -08001033 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001034 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001035 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
1036 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001037 }
1038
1039 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001040 // Legacy shortcuts are only supported for primary profile.
1041 info = Process.myUserHandle().equals(args.user)
1042 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001043
Sunny Goyalb57645f2017-03-20 13:57:28 -07001044 if (info == null) {
1045 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1046 return;
1047 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001048 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001049 // The app is trying to add a shortcut without sufficient permissions
1050 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1051 return;
1052 }
1053 }
1054
Jon Mirandac476d6e2017-05-04 16:30:01 -07001055 if (container < 0) {
1056 // Adding a shortcut to the Workspace.
1057 final View view = createShortcut(info);
1058 boolean foundCellSpan = false;
1059 // First we check if we already know the exact location where we want to add this item.
1060 if (cellX >= 0 && cellY >= 0) {
1061 cellXY[0] = cellX;
1062 cellXY[1] = cellY;
1063 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001064
Jon Mirandac476d6e2017-05-04 16:30:01 -07001065 // If appropriate, either create a folder or add to an existing folder
1066 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001067 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001068 return;
1069 }
1070 DragObject dragObject = new DragObject();
1071 dragObject.dragInfo = info;
1072 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1073 true)) {
1074 return;
1075 }
1076 } else {
1077 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1078 }
1079
1080 if (!foundCellSpan) {
1081 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001082 return;
1083 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001084
1085 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1086 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001087 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001088 // Adding a shortcut to a Folder.
Sunny Goyale29897f2017-07-20 10:09:42 -07001089 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001090 if (folderIcon != null) {
1091 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1092 folderInfo.add(info, args.rank, false);
1093 } else {
Sunny Goyale29897f2017-07-20 10:09:42 -07001094 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001095 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001096 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001097 }
1098
Sunny Goyale29897f2017-07-20 10:09:42 -07001099 public FolderIcon findFolderIcon(final long folderIconId) {
1100 return (FolderIcon) mWorkspace.getFirstMatch(new ItemOperator() {
1101 @Override
1102 public boolean evaluate(ItemInfo info, View view) {
1103 return info != null && info.id == folderIconId;
1104 }
1105 });
1106 }
1107
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001108 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001109 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001110 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001111 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001112 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001113 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001114 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1115
Adam Cohened66b2b2012-01-23 17:28:51 -08001116 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001117 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001118 }
Romain Guycbb89e42009-06-08 15:52:54 -07001119
Adam Cohen59400422014-03-05 18:07:04 -08001120 LauncherAppWidgetInfo launcherInfo;
1121 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001122 launcherInfo.spanX = itemInfo.spanX;
1123 launcherInfo.spanY = itemInfo.spanY;
1124 launcherInfo.minSpanX = itemInfo.minSpanX;
1125 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001126 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001127
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001128 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001129 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001130
Sunny Goyal2100c782016-08-22 16:00:03 -07001131 if (hostView == null) {
1132 // Perform actual inflation because we're live
1133 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001134 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001135 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301136 prepareAppWidget(hostView, launcherInfo);
1137 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001138 }
Romain Guycbb89e42009-06-08 15:52:54 -07001139
Sunny Goyald5462aa2016-11-22 16:52:39 +05301140 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001141 hostView.setTag(item);
1142 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001143 hostView.setFocusable(true);
1144 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001145 }
1146
Adam Cohended9f8d2010-11-03 13:25:16 -07001147 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1148 @Override
1149 public void onReceive(Context context, Intent intent) {
1150 final String action = intent.getAction();
1151 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001152 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001153 // processing a multi-step drop
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001154 if (mAppsView != null && mPendingRequestArgs == null) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001155 mStateManager.goToState(NORMAL);
Winson Chung785d2eb2011-04-14 16:08:02 -07001156 }
Jon Miranda7fda2852017-07-19 16:07:01 -07001157 mShouldFadeInScrim = true;
1158 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1159 // ACTION_USER_PRESENT is sent after onStart/onResume. This covers the case where
1160 // the user unlocked and the Launcher is not in the foreground.
1161 mShouldFadeInScrim = false;
Adam Cohended9f8d2010-11-03 13:25:16 -07001162 }
1163 }
1164 };
1165
Tony Wickham010d2552017-01-20 08:15:28 -08001166 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
Sunny Goyal29947f02017-12-18 13:49:44 -08001167 mWorkspace.updateIconBadges(updatedBadges);
Sunny Goyal60180b02018-02-07 12:56:30 -08001168 mAppsView.getAppsStore().updateIconBadges(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001169
Sunny Goyal29947f02017-12-18 13:49:44 -08001170 PopupContainerWithArrow popup = PopupContainerWithArrow.getOpen(Launcher.this);
1171 if (popup != null) {
1172 popup.updateNotificationHeader(updatedBadges);
Tony Wickham010d2552017-01-20 08:15:28 -08001173 }
1174 }
1175
Adam Cohended9f8d2010-11-03 13:25:16 -07001176 @Override
1177 public void onAttachedToWindow() {
1178 super.onAttachedToWindow();
1179
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001180 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyalc86df472016-02-25 09:19:38 -08001181 if (mLauncherCallbacks != null) {
1182 mLauncherCallbacks.onAttachedToWindow();
1183 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001184 }
1185
1186 @Override
1187 public void onDetachedFromWindow() {
1188 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001189
1190 if (mLauncherCallbacks != null) {
1191 mLauncherCallbacks.onDetachedFromWindow();
1192 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001193 }
1194
Sunny Goyal3bac2c22018-02-07 14:44:05 -08001195 public void onQuickstepGestureStarted(boolean isVisible) {
1196 if (mLauncherCallbacks != null) {
1197 mLauncherCallbacks.onQuickstepGestureStarted(isVisible);
1198 }
1199 }
1200
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001201 public AllAppsTransitionController getAllAppsController() {
1202 return mAllAppsController;
1203 }
1204
Winson Chung1a341002018-01-17 10:00:23 -08001205 public LauncherRootView getRootView() {
1206 return (LauncherRootView) mLauncherView;
1207 }
1208
Winson Chunga6945242014-01-08 14:04:34 -08001209 public DragLayer getDragLayer() {
1210 return mDragLayer;
1211 }
1212
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001213 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001214 return mAppsView;
1215 }
1216
Winson Chunga6945242014-01-08 14:04:34 -08001217 public Workspace getWorkspace() {
1218 return mWorkspace;
1219 }
1220
1221 public Hotseat getHotseat() {
1222 return mHotseat;
1223 }
1224
Winson Chung8ae41982017-11-10 11:42:13 -08001225 public <T extends ViewGroup> T getOverviewPanel() {
1226 return (T) mOverviewPanel;
Winson Chunga6945242014-01-08 14:04:34 -08001227 }
1228
Sunny Goyal47328fd2016-05-25 18:56:41 -07001229 public DropTargetBar getDropTargetBar() {
1230 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001231 }
1232
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001233 public LauncherAppWidgetHost getAppWidgetHost() {
1234 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001235 }
Romain Guycbb89e42009-06-08 15:52:54 -07001236
Winson Chunga9abd0e2010-10-27 17:18:37 -07001237 public LauncherModel getModel() {
1238 return mModel;
1239 }
1240
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001241 public ModelWriter getModelWriter() {
1242 return mModelWriter;
1243 }
1244
Adam Cohen79d90c52016-04-22 13:29:20 -07001245 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001246 return mSharedPrefs;
1247 }
1248
Sunny Goyalf8d56fc2018-01-31 15:18:11 -08001249 public int getOrientation() { return mOldConfig.orientation; }
Jon Miranda6bed3502017-09-19 14:43:17 -07001250
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001251 @Override
1252 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001253 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001254 super.onNewIntent(intent);
1255
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001256 boolean alreadyOnHome = hasWindowFocus() && ((intent.getFlags() &
Winson15f8b172015-08-19 11:02:39 -07001257 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1258 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001259
1260 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001261 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001262 && AbstractFloatingView.getTopOpenView(this) == null;
Winson15f8b172015-08-19 11:02:39 -07001263 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001264 boolean internalStateHandled = InternalStateHandler
Sunny Goyal8b264562018-02-03 23:00:13 -08001265 .handleNewIntent(this, intent, isStarted());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001266
Winson15f8b172015-08-19 11:02:39 -07001267 if (isActionMain) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001268 if (!internalStateHandled) {
1269 // Note: There should be at most one log per method call. This is enforced
1270 // implicitly by using if-else statements.
1271 UserEventDispatcher ued = getUserEventDispatcher();
1272 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
1273 if (topOpenView != null) {
1274 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
1275 } else if (alreadyOnHome) {
1276 Target target = newContainerTarget(mStateManager.getState().containerType);
1277 target.pageIndex = mWorkspace.getCurrentPage();
1278 ued.logActionCommand(Action.Command.HOME_INTENT, target);
1279 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001280
Sunny Goyald3864fa2017-11-14 15:06:36 -08001281 // In all these cases, only animate if we're already on home
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001282 AbstractFloatingView.closeAllOpenViews(this, isStarted());
Jon Mirandafeba90f2016-10-06 10:53:29 -07001283
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001284 mStateManager.goToState(NORMAL);
Sunny Goyald3864fa2017-11-14 15:06:36 -08001285
1286 // Reset the apps view
1287 if (!alreadyOnHome && mAppsView != null) {
1288 mAppsView.reset();
1289 }
1290
1291 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()) {
1292 mWorkspace.post(mWorkspace::moveToDefaultScreen);
1293 }
1294 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001295
1296 final View v = getWindow().peekDecorView();
1297 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001298 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001299 }
1300
Adam Cohen9211d422014-10-07 18:14:53 -07001301 if (mLauncherCallbacks != null) {
1302 mLauncherCallbacks.onHomeIntent();
1303 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001304 }
Winson15f8b172015-08-19 11:02:39 -07001305
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001306 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001307 }
1308
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001309 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001310 public void onRestoreInstanceState(Bundle state) {
1311 super.onRestoreInstanceState(state);
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001312 mWorkspace.restoreInstanceStateForChild(mSynchronouslyBoundPage);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001313 }
1314
1315 @Override
1316 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001317 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001318 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001319
Adam Cohen21cd0022013-10-09 18:57:02 -07001320 }
Sunny Goyalea609262017-10-25 15:47:38 -07001321 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001322
1323
1324 AbstractFloatingView widgets = AbstractFloatingView
1325 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1326 if (widgets != null) {
1327 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1328 widgets.saveHierarchyState(widgetsState);
1329 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1330 } else {
1331 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1332 }
1333
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001334 // We close any open folders and shortcut containers since they will not be re-opened,
1335 // and we need to make sure this state is reflected.
1336 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001337
Sunny Goyal2100c782016-08-22 16:00:03 -07001338 if (mPendingRequestArgs != null) {
1339 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1340 }
1341 if (mPendingActivityResult != null) {
1342 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001343 }
1344
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001345 super.onSaveInstanceState(outState);
1346
Adam Cohen9211d422014-10-07 18:14:53 -07001347 if (mLauncherCallbacks != null) {
1348 mLauncherCallbacks.onSaveInstanceState(outState);
1349 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001350 }
1351
1352 @Override
1353 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001354 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001355
Sunny Goyal5cc7bbd2017-06-12 09:50:39 -07001356 unregisterReceiver(mReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001357 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001358
Winson Chungcd2b0142011-06-08 16:02:26 -07001359 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001360 // It's possible to receive onDestroy after a new Launcher activity has
1361 // been created. In this case, don't interfere with the new Launcher.
1362 if (mModel.isCurrentCallbacks(this)) {
1363 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001364 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001365 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001366
Sunny Goyal745bad92016-05-02 10:54:12 -07001367 if (mRotationPrefChangeHandler != null) {
1368 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1369 }
1370
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001371 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001372 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001373 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001374 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001375 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001376
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001377 TextKeyListener.getInstance().release();
Mario Bertschler27288382017-05-24 15:35:09 -07001378 WallpaperColorInfo.getInstance(this).setOnThemeChangeListener(null);
1379
Michael Jurka2ecf9952012-06-18 12:52:28 -07001380 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001381
Tony2917a8b2017-07-31 23:29:42 -07001382 clearPendingBinds();
1383
Adam Cohen9211d422014-10-07 18:14:53 -07001384 if (mLauncherCallbacks != null) {
1385 mLauncherCallbacks.onDestroy();
1386 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001387 }
1388
Sunny Goyalae502842016-06-17 08:43:56 -07001389 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1390 return mAccessibilityDelegate;
1391 }
1392
Adam Cohena9cf38f2011-05-02 15:36:58 -07001393 public DragController getDragController() {
1394 return mDragController;
1395 }
1396
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001397 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001398 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001399 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001400 }
1401
1402 @Override
1403 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1404 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001405 try {
1406 super.startIntentSenderForResult(intent, requestCode,
1407 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1408 } catch (IntentSender.SendIntentException e) {
1409 throw new ActivityNotFoundException();
1410 }
1411 }
1412
Winson Chung70d72102011-08-12 11:24:35 -07001413 /**
1414 * Indicates that we want global search for this activity by setting the globalSearch
1415 * argument for {@link #startSearch} to true.
1416 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001417 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001418 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001419 Bundle appSearchData, boolean globalSearch) {
1420 if (appSearchData == null) {
1421 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001422 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001423 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001424
Sunny Goyal6f28e712016-09-13 14:19:29 -07001425 if (mLauncherCallbacks == null ||
1426 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1427 // Starting search from the callbacks failed. Start the default global search.
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001428 super.startSearch(initialQuery, selectInitialQuery, appSearchData, true);
Ian Parkinson047036e2014-09-01 15:40:53 +01001429 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001430
1431 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001432 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001433 }
1434
Joe Onorato9c1289c2009-08-17 11:03:03 -04001435 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001436 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001437 }
1438
Adam Cohen517a7f52014-03-01 12:12:59 -08001439 public boolean isWorkspaceLoading() {
1440 return mWorkspaceLoading;
1441 }
1442
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001443 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001444 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001445 }
1446
Sunny Goyal04a324a2017-01-20 21:08:59 -08001447 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001448 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001449 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001450
Sunny Goyal2100c782016-08-22 16:00:03 -07001451 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001452 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001453 if (LOGD) {
1454 Log.d(TAG, "Adding widget from drop");
1455 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001456 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001457 }
1458
Sunny Goyal2100c782016-08-22 16:00:03 -07001459 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001460 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1461 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1462 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001463
Adam Cohenad4e15c2013-10-17 16:21:35 -07001464 Runnable onComplete = new Runnable() {
1465 @Override
1466 public void run() {
1467 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001468 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001469 }
1470 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001471 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001472 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001473 }
1474 }
Romain Guycbb89e42009-06-08 15:52:54 -07001475
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001476 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1477 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001478 info.container = container;
1479 info.screenId = screenId;
1480 if (cell != null) {
1481 info.cellX = cell[0];
1482 info.cellY = cell[1];
1483 }
1484 info.spanX = spanX;
1485 info.spanY = spanY;
1486
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001487 switch (info.itemType) {
1488 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1489 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001490 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001491 break;
1492 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001493 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001494 break;
1495 default:
1496 throw new IllegalStateException("Unknown item type: " + info.itemType);
1497 }
1498 }
1499
Adam Cohenfbba09b2011-07-18 21:43:05 -07001500 /**
1501 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001502 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001503 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001504 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1505 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001506 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1507 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1508 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001509 }
1510
Adam Cohenfbba09b2011-07-18 21:43:05 -07001511 /**
1512 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001513 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001514 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001515 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001516 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001517 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001518 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001519 // In the case where we've prebound the widget, we remove it from the DragLayer
1520 if (LOGD) {
1521 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1522 }
1523 getDragLayer().removeView(hostView);
1524
Adam Cohened66b2b2012-01-23 17:28:51 -08001525 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001526 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001527
1528 // Clear the boundWidget so that it doesn't get destroyed.
1529 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001530 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001531 // In this case, we either need to start an activity to get permission to bind
1532 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001533 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1534 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1535 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1536 this, info.componentName);
1537 } else {
1538 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1539 }
Adam Cohendd70d662012-10-04 16:53:44 -07001540 Bundle options = info.bindOptions;
1541
Sunny Goyalffe83f12014-08-14 17:39:34 -07001542 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1543 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001544 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001545 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001546 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001547 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001548 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001549 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001550 }
1551
Adam Cohendcd297f2013-06-18 13:13:40 -07001552 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001553 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001554 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001555 folderInfo.title = getText(R.string.folder_name);
1556
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001557 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001558 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001559
1560 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001561 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301562 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001563 // Force measure the new folder icon
1564 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1565 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001566 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001567 }
Romain Guycbb89e42009-06-08 15:52:54 -07001568
Winsonc0b52fe2015-09-09 16:38:15 -07001569 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001570 * Unbinds the view for the specified item, and removes the item and all its children.
1571 *
1572 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001573 * @param itemInfo the {@link ItemInfo} for this view.
1574 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001575 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001576 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001577 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001578 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001579 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1580 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001581 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001582 } else {
1583 mWorkspace.removeWorkspaceItem(v);
1584 }
Winsonc0b52fe2015-09-09 16:38:15 -07001585 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001586 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001587 }
1588 } else if (itemInfo instanceof FolderInfo) {
1589 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001590 if (v instanceof FolderIcon) {
1591 ((FolderIcon) v).removeListeners();
1592 }
Winsonc0b52fe2015-09-09 16:38:15 -07001593 mWorkspace.removeWorkspaceItem(v);
1594 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001595 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001596 }
1597 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1598 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001599 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001600 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07001601 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001602 }
1603 } else {
1604 return false;
1605 }
1606 return true;
1607 }
1608
1609 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07001610 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07001611 */
Sunny Goyal56c73602015-09-25 12:55:01 -07001612 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07001613 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07001614 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07001615 // Deleting an app widget ID is a void call but writes to disk before returning
1616 // to the caller...
1617 new AsyncTask<Void, Void, Void>() {
1618 public Void doInBackground(Void ... args) {
1619 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
1620 return null;
1621 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07001622 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07001623 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001624 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001625 }
1626
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001627 @Override
1628 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001629 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001630 }
1631
Joe Onorato88ec0992009-11-19 13:16:06 -08001632 @Override
1633 public void onBackPressed() {
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08001634 if (finishAutoCancelActionMode()) {
1635 return;
1636 }
Adam Cohen9211d422014-10-07 18:14:53 -07001637 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1638 return;
1639 }
1640
Sunny Goyal45478022015-06-08 16:52:41 -07001641 if (mDragController.isDragging()) {
1642 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001643 return;
1644 }
1645
Jon Mirandafeba90f2016-10-06 10:53:29 -07001646 // Note: There should be at most one log per method call. This is enforced implicitly
1647 // by using if-else statements.
1648 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001649 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
1650 if (topView != null) {
Sunny Goyal37920962017-09-28 13:43:24 -07001651 topView.onBackPressed();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001652 } else if (!isInState(NORMAL)) {
Sunny Goyal49bcf342018-01-11 13:59:53 -08001653 LauncherState lastState = mStateManager.getLastState();
Sunny Goyalea609262017-10-25 15:47:38 -07001654 ued.logActionCommand(Action.Command.BACK, mStateManager.getState().containerType);
Sunny Goyal49bcf342018-01-11 13:59:53 -08001655 mStateManager.goToState(lastState);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001656 } else {
1657 // Back button is a no-op here, but give at least some feedback for the button press
1658 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001659 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001660 }
1661
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001662 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001663 * Launches the intent referred by the clicked shortcut.
1664 *
1665 * @param v The view representing the clicked shortcut.
1666 */
1667 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001668 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1669 // view has detached (it's possible for this to happen if the view is removed mid touch).
1670 if (v.getWindowToken() == null) {
1671 return;
1672 }
1673
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001674 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001675 return;
1676 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001677
Adam Cohen1697b792013-09-17 19:08:21 -07001678 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001679 if (isInState(OVERVIEW)) {
Jon Miranda2e61fba2017-02-24 09:12:59 -08001680 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1681 LauncherLogProto.Action.Direction.NONE,
1682 LauncherLogProto.ContainerType.OVERVIEW, mWorkspace.getCurrentPage());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001683 mStateManager.goToState(NORMAL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001684 }
1685 return;
1686 }
1687
1688 if (v instanceof CellLayout) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001689 if (isInState(OVERVIEW)) {
Jon Mirandac6cf4932017-02-07 17:12:36 -08001690 int page = mWorkspace.indexOfChild(v);
1691 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1692 LauncherLogProto.Action.Direction.NONE,
1693 LauncherLogProto.ContainerType.OVERVIEW, page);
1694 mWorkspace.snapToPageFromOverView(page);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001695 mStateManager.goToState(NORMAL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001696 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07001697 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001698 }
1699
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001700 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001701 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001702 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001703 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001704 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001705 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001706 }
Sunny Goyal508da152014-08-14 10:53:27 -07001707 } else if (tag instanceof AppInfo) {
1708 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07001709 } else if (tag instanceof LauncherAppWidgetInfo) {
1710 if (v instanceof PendingAppWidgetHostView) {
1711 onClickPendingWidget((PendingAppWidgetHostView) v);
1712 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001713 }
1714 }
1715
Sunny Goyal70660032015-05-14 00:07:08 -07001716 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07001717 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07001718 return false;
1719 }
1720
Michael Jurkaaf442092010-06-10 17:01:57 -07001721 /**
Sunny Goyalff572272014-07-23 13:58:07 -07001722 * Event handler for the app widget view which has not fully restored.
1723 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001724 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07001725 if (mIsSafeModeEnabled) {
1726 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
1727 return;
1728 }
1729
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001730 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07001731 if (v.isReadyForClickSetup()) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001732 LauncherAppWidgetProviderInfo appWidgetInfo =
1733 mAppWidgetManager.findProvider(info.providerName, info.user);
1734 if (appWidgetInfo == null) {
1735 return;
1736 }
1737 WidgetAddFlowHandler addFlowHandler = new WidgetAddFlowHandler(appWidgetInfo);
1738
Sunny Goyald478c832016-04-01 12:04:16 -07001739 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
1740 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
1741 // This should not happen, as we make sure that an Id is allocated during bind.
1742 return;
1743 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001744 addFlowHandler.startBindFlow(this, info.appWidgetId, info,
1745 REQUEST_BIND_PENDING_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -07001746 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001747 addFlowHandler.startConfigActivity(this, info, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07001748 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001749 } else {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001750 final String packageName = info.providerName.getPackageName();
Sunny Goyale6e72002017-01-12 16:55:36 -08001751 onClickPendingAppItem(v, packageName, info.installProgress >= 0);
Sunny Goyalff572272014-07-23 13:58:07 -07001752 }
1753 }
1754
Sunny Goyale6e72002017-01-12 16:55:36 -08001755 private void onClickPendingAppItem(final View v, final String packageName,
1756 boolean downloadStarted) {
1757 if (downloadStarted) {
1758 // If the download has started, simply direct to the market app.
1759 startMarketIntentForPackage(v, packageName);
1760 return;
1761 }
Sunny Goyale03b8122014-10-08 09:55:24 -07001762 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001763 .setTitle(R.string.abandoned_promises_title)
1764 .setMessage(R.string.abandoned_promise_explanation)
Sunny Goyale6e72002017-01-12 16:55:36 -08001765 .setPositiveButton(R.string.abandoned_search, new DialogInterface.OnClickListener() {
1766 @Override
1767 public void onClick(DialogInterface dialogInterface, int i) {
1768 startMarketIntentForPackage(v, packageName);
1769 }
1770 })
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001771 .setNeutralButton(R.string.abandoned_clean_this,
1772 new DialogInterface.OnClickListener() {
1773 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001774 final UserHandle user = Process.myUserHandle();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001775 mWorkspace.removeAbandonedPromise(packageName, user);
1776 }
1777 })
1778 .create().show();
Sunny Goyale6e72002017-01-12 16:55:36 -08001779 }
1780
1781 private void startMarketIntentForPackage(View v, String packageName) {
1782 ItemInfo item = (ItemInfo) v.getTag();
Sunny Goyal67e75e22018-01-22 15:14:00 -08001783 Intent intent = new PackageManagerHelper(v.getContext()).getMarketIntent(packageName);
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001784 startActivitySafely(v, intent, item);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001785 }
1786
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001787 /**
1788 * Event handler for an app shortcut click.
1789 *
1790 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
1791 */
Chris Wren40c5ed32014-06-24 18:24:23 -04001792 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001793 if (LOGD) Log.d(TAG, "onClickAppShortcut");
1794 Object tag = v.getTag();
1795 if (!(tag instanceof ShortcutInfo)) {
1796 throw new IllegalArgumentException("Input must be a Shortcut");
1797 }
1798
1799 // Open shortcut
1800 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001801
Sunny Goyal076839c2017-10-30 13:52:20 -07001802 if (shortcut.isDisabled()) {
Tony Makf6308652017-12-02 16:56:22 +00001803 final int disabledFlags = shortcut.runtimeStatusFlags & ShortcutInfo.FLAG_DISABLED_MASK;
1804 if ((disabledFlags &
Sunny Goyal076839c2017-10-30 13:52:20 -07001805 ~FLAG_DISABLED_SUSPENDED &
1806 ~FLAG_DISABLED_QUIET_USER) == 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001807 // If the app is only disabled because of the above flags, launch activity anyway.
1808 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00001809 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07001810 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
1811 // Use a message specific to this shortcut, if it has one.
1812 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
1813 return;
1814 }
1815 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00001816 int error = R.string.activity_not_available;
Sunny Goyal076839c2017-10-30 13:52:20 -07001817 if ((shortcut.runtimeStatusFlags & FLAG_DISABLED_SAFEMODE) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00001818 error = R.string.safemode_shortcut_error;
Sunny Goyal076839c2017-10-30 13:52:20 -07001819 } else if ((shortcut.runtimeStatusFlags & FLAG_DISABLED_BY_PUBLISHER) != 0 ||
1820 (shortcut.runtimeStatusFlags & FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07001821 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00001822 }
1823 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
1824 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001825 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001826 }
1827
Chris Wren40c5ed32014-06-24 18:24:23 -04001828 // Check for abandoned promise
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -07001829 if ((v instanceof BubbleTextView) && shortcut.hasPromiseIconUi()) {
Sunny Goyale6e72002017-01-12 16:55:36 -08001830 String packageName = shortcut.intent.getComponent() != null ?
1831 shortcut.intent.getComponent().getPackageName() : shortcut.intent.getPackage();
1832 if (!TextUtils.isEmpty(packageName)) {
1833 onClickPendingAppItem(v, packageName,
1834 shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE));
1835 return;
1836 }
Chris Wren40c5ed32014-06-24 18:24:23 -04001837 }
1838
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001839 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07001840 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04001841 }
1842
Sunny Goyala7ce1662016-05-31 15:01:35 -07001843 private void startAppShortcutOrInfoActivity(View v) {
1844 ItemInfo item = (ItemInfo) v.getTag();
Mario Bertschler08ffaae2017-03-20 11:30:27 -07001845 Intent intent;
1846 if (item instanceof PromiseAppInfo) {
1847 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) item;
Sunny Goyal67e75e22018-01-22 15:14:00 -08001848 intent = promiseAppInfo.getMarketIntent(this);
Mario Bertschler08ffaae2017-03-20 11:30:27 -07001849 } else {
1850 intent = item.getIntent();
1851 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07001852 if (intent == null) {
1853 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07001854 }
Mario Bertschler8d01cb42018-02-13 14:47:02 -08001855 if (item instanceof ShortcutInfo) {
1856 ShortcutInfo si = (ShortcutInfo) item;
1857 if (si.hasStatusFlag(ShortcutInfo.FLAG_SUPPORTS_WEB_UI)
1858 && intent.getAction() == Intent.ACTION_VIEW) {
1859 // make a copy of the intent that has the package set to null
1860 // we do this because the platform sometimes disables instant
1861 // apps temporarily (triggered by the user) and fallbacks to the
1862 // web ui. This only works though if the package isn't set
1863 intent = new Intent(intent);
1864 intent.setPackage(null);
1865 }
1866 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001867 startActivitySafely(v, intent, item);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001868 }
1869
1870 /**
1871 * Event handler for a folder icon click.
1872 *
1873 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
1874 */
1875 protected void onClickFolderIcon(View v) {
1876 if (LOGD) Log.d(TAG, "onClickFolder");
1877 if (!(v instanceof FolderIcon)){
1878 throw new IllegalArgumentException("Input must be a FolderIcon");
1879 }
1880
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001881 Folder folder = ((FolderIcon) v).getFolder();
1882 if (!folder.isOpen() && !folder.isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001883 // Open the requested folder
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001884 folder.animateOpen();
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001885 }
Michael Jurka5130e402011-10-13 04:55:35 -07001886 }
1887
Sandeep Siddharthad8058372014-01-28 10:41:15 -08001888 /**
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001889 * Event handler for the wallpaper picker button that appears after a long press
1890 * on the home screen.
1891 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001892 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07001893 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07001894 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
1895 return;
1896 }
1897
Tony Wickham785f7a52015-08-31 17:28:32 -07001898 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
1899 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal2100c782016-08-22 16:00:03 -07001900 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001901 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001902 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001903
1904 String pickerPackage = getString(R.string.wallpaper_picker_package);
Tony85efb4b2017-06-02 13:06:10 -07001905 boolean hasTargetPackage = !TextUtils.isEmpty(pickerPackage);
1906 if (hasTargetPackage) {
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001907 intent.setPackage(pickerPackage);
1908 }
1909
Sunny Goyala5ace712017-11-15 17:12:51 -08001910 final Bundle launchOptions;
1911 if (v != null) {
1912 intent.setSourceBounds(getViewBounds(v));
1913 // If there is no target package, use the default intent chooser animation
Jon Miranda17940f02018-01-18 14:46:34 -08001914 launchOptions = hasTargetPackage
1915 ? getActivityLaunchOptions(v, isInMultiWindowModeCompat())
1916 : null;
Sunny Goyala5ace712017-11-15 17:12:51 -08001917 } else {
1918 launchOptions = null;
1919 }
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001920 try {
Sunny Goyala5ace712017-11-15 17:12:51 -08001921 startActivityForResult(intent, REQUEST_PICK_WALLPAPER, launchOptions);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001922 } catch (ActivityNotFoundException e) {
1923 setWaitingForResult(null);
1924 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1925 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001926 }
1927
Sunny Goyala7ce1662016-05-31 15:01:35 -07001928 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001929 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001930 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
1931 try {
1932 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
1933 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
1934 // is enabled by default on NYC.
1935 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
1936 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001937
1938 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
1939 String id = ((ShortcutInfo) info).getDeepShortcutId();
1940 String packageName = intent.getPackage();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301941 DeepShortcutManager.getInstance(this).startShortcut(
Sunny Goyal9994b2b2016-06-23 14:17:24 -07001942 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001943 } else {
1944 // Could be launching some bookkeeping activity
1945 startActivity(intent, optsBundle);
1946 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07001947 } finally {
1948 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01001949 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001950 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001951 // Due to legacy reasons, direct call shortcuts require Launchers to have the
1952 // corresponding permission. Show the appropriate permission prompt if that
1953 // is the case.
1954 if (intent.getComponent() == null
1955 && Intent.ACTION_CALL.equals(intent.getAction())
1956 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
1957 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001958
1959 setWaitingForResult(PendingRequestArgs
1960 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07001961 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
1962 REQUEST_PERMISSION_CALL_PHONE);
1963 } else {
1964 // No idea why this was thrown.
1965 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07001966 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001967 }
1968 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001969
Sunny Goyalfe770c92016-09-27 14:38:58 -07001970 @TargetApi(Build.VERSION_CODES.M)
Jon Miranda17940f02018-01-18 14:46:34 -08001971 public Bundle getActivityLaunchOptions(View v, boolean useDefaultLaunchOptions) {
1972 return useDefaultLaunchOptions
Jon Miranda54441f52018-01-24 15:38:25 -08001973 ? mAppTransitionManager.getDefaultActivityLaunchOptions(this, v)
1974 : mAppTransitionManager.getActivityLaunchOptions(this, v);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001975 }
1976
Tonye3c59252017-05-02 21:32:27 -07001977 public Rect getViewBounds(View v) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001978 int[] pos = new int[2];
1979 v.getLocationOnScreen(pos);
1980 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
1981 }
1982
Sunny Goyala7ce1662016-05-31 15:01:35 -07001983 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07001984 mAppLaunchSuccess = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001985 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
1986 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07001987 return mAppLaunchSuccess;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001988 }
Jon Miranda17940f02018-01-18 14:46:34 -08001989
1990 boolean isShortcut = Utilities.ATLEAST_MARSHMALLOW
1991 && (item instanceof ShortcutInfo)
1992 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
1993 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
1994 && !((ShortcutInfo) item).isPromise();
1995
Sunny Goyala7ce1662016-05-31 15:01:35 -07001996 // Only launch using the new animation if the shortcut has not opted out (this is a
1997 // private contract between launcher and may be ignored in the future).
1998 boolean useLaunchAnimation = (v != null) &&
1999 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Jon Miranda17940f02018-01-18 14:46:34 -08002000 Bundle optsBundle = useLaunchAnimation
2001 ? getActivityLaunchOptions(v, isShortcut || isInMultiWindowModeCompat())
2002 : null;
Sunny Goyala7ce1662016-05-31 15:01:35 -07002003
Sunny Goyal24bb66a2017-03-21 15:12:01 -07002004 UserHandle user = item == null ? null : item.user;
Sunny Goyala7ce1662016-05-31 15:01:35 -07002005
2006 // Prepare intent
2007 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2008 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002009 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002010 }
2011 try {
Jon Miranda17940f02018-01-18 14:46:34 -08002012 if (isShortcut) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002013 // Shortcuts need some special checks due to legacy reasons.
2014 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002015 } else if (user == null || user.equals(Process.myUserHandle())) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002016 // Could be launching some bookkeeping activity
2017 startActivity(intent, optsBundle);
2018 } else {
2019 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2020 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2021 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002022
2023 if (v instanceof BubbleTextView) {
2024 // This is set to the view that launched the activity that navigated the user away
2025 // from launcher. Since there is no callback for when the activity has finished
2026 // launching, enable the press state and keep this reference to reset the press
2027 // state when we return to launcher.
2028 BubbleTextView btv = (BubbleTextView) v;
2029 btv.setStayPressed(true);
2030 setOnResumeCallback(btv);
2031 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002032 mAppLaunchSuccess = true;
2033 getUserEventDispatcher().logAppLaunch(v, intent); // TODO for discovered apps b/35802115
Sunny Goyala7ce1662016-05-31 15:01:35 -07002034 } catch (ActivityNotFoundException|SecurityException e) {
2035 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2036 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2037 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002038 return mAppLaunchSuccess;
Michael Jurka86a720e2012-05-09 11:23:23 -07002039 }
2040
Tony Wickhamdadb3042016-02-24 11:07:00 -08002041 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07002042 public boolean dispatchTouchEvent(MotionEvent ev) {
Sunny Goyal02424b22018-01-19 11:24:32 -08002043 mLastDispatchTouchEvent.set(ev.getX(), ev.getY());
Jon Miranda379198e2016-09-23 08:54:40 -07002044 return super.dispatchTouchEvent(ev);
2045 }
2046
2047 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002048 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002049 if (!isDraggingEnabled()) return false;
2050 if (isWorkspaceLocked()) return false;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002051 if (!isInState(NORMAL) && !isInState(OVERVIEW)) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002052
Jon Miranda51f037d2016-11-07 08:37:20 -08002053 boolean ignoreLongPressToOverview =
Sunny Goyal02424b22018-01-19 11:24:32 -08002054 mDeviceProfile.shouldIgnoreLongPressToOverview(mLastDispatchTouchEvent.x);
Jon Miranda379198e2016-09-23 08:54:40 -07002055
Adam Cohen1697b792013-09-17 19:08:21 -07002056 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002057 if (!isInState(OVERVIEW)) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002058 if (!mWorkspace.isTouchActive() && !ignoreLongPressToOverview) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302059 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2060 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002061 mWorkspace.getCurrentPage());
Sunny Goyal02424b22018-01-19 11:24:32 -08002062 UiFactory.onWorkspaceLongPress(this, mLastDispatchTouchEvent);
Adam Cohen93c97562013-09-26 13:48:01 -07002063 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2064 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2065 return true;
2066 } else {
2067 return false;
2068 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002069 } else {
2070 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002071 }
Adam Cohen1697b792013-09-17 19:08:21 -07002072 }
2073
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002074 CellLayout.CellInfo longClickCellInfo = null;
2075 View itemUnderLongClick = null;
2076 if (v.getTag() instanceof ItemInfo) {
2077 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002078 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002079 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07002080 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002081 }
2082
Winson Chung3d503fb2011-07-13 17:25:49 -07002083 // The hotseat touch handling does not go through Workspace, and we always allow long press
2084 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002085 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002086 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002087 // User long pressed on empty space
Sunny Goyalc99cb172017-10-19 16:15:09 -07002088 if (mWorkspace.isPageRearrangeEnabled()) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002089 mWorkspace.startReordering(v);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302090 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2091 Action.Direction.NONE, ContainerType.OVERVIEW);
Adam Cohend3ecce92013-09-16 14:58:00 -07002092 } else {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002093 if (ignoreLongPressToOverview) {
Jon Miranda379198e2016-09-23 08:54:40 -07002094 return false;
2095 }
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302096 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2097 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002098 mWorkspace.getCurrentPage());
Sunny Goyal02424b22018-01-19 11:24:32 -08002099 UiFactory.onWorkspaceLongPress(this, mLastDispatchTouchEvent);
Adam Cohendedbd962013-07-11 14:21:49 -07002100 }
Jon Miranda379198e2016-09-23 08:54:40 -07002101 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2102 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002103 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07002104 final boolean isAllAppsButton =
2105 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
2106 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
2107 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05002108 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002109 // User long pressed on an item
Tony Wickham10236d62016-09-28 12:49:25 -07002110 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002111 }
2112 }
2113 }
2114 return true;
2115 }
2116
Winson Chung3d503fb2011-07-13 17:25:49 -07002117 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08002118 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07002119 return mHotseat != null && layout != null &&
2120 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2121 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002122
Winson Chung3d503fb2011-07-13 17:25:49 -07002123 /**
2124 * Returns the CellLayout of the specified container at the specified screen.
2125 */
Sunny Goyal92820592015-03-02 11:31:35 -08002126 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002127 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2128 if (mHotseat != null) {
2129 return mHotseat.getLayout();
2130 } else {
2131 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002132 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002133 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002134 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002135 }
2136 }
2137
Michael Jurkae326f182011-11-21 14:05:46 -08002138 @Override
2139 public void onTrimMemory(int level) {
2140 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07002141 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2142 // The widget preview db can result in holding onto over
2143 // 3MB of memory for caching which isn't necessary.
2144 SQLiteDatabase.releaseMemory();
2145
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08002146 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07002147 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08002148 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01002149 if (mLauncherCallbacks != null) {
2150 mLauncherCallbacks.onTrimMemory(level);
2151 }
Michael Jurkae326f182011-11-21 14:05:46 -08002152 }
2153
Michael Jurkad7c28052012-04-27 15:43:36 -07002154 @Override
2155 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07002156 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07002157 final List<CharSequence> text = event.getText();
2158 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07002159 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002160 // TODO: When can workspace be null?
2161 text.add(mWorkspace == null
2162 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07002163 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07002164 return result;
2165 }
2166
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002167 public void setOnResumeCallback(OnResumeCallback callback) {
2168 if (mOnResumeCallback != null) {
2169 mOnResumeCallback.onLauncherResume();
2170 }
2171 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07002172 }
2173
Michael Jurka7607c2f2013-04-03 14:33:19 -07002174 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002175 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002176 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002177 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002178 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002179 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002180 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002181 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07002182 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07002183 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002184 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002185
Joe Onorato9c1289c2009-08-17 11:03:03 -04002186 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07002187 * Clear any pending bind callbacks. This is called when is loader is planning to
2188 * perform a full rebind from scratch.
2189 */
2190 @Override
2191 public void clearPendingBinds() {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002192 if (mPendingExecutor != null) {
2193 mPendingExecutor.markCompleted();
2194 mPendingExecutor = null;
2195 }
2196 }
2197
2198 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002199 * Refreshes the shortcuts shown on the workspace.
2200 *
2201 * Implementation of the method from LauncherModel.Callbacks.
2202 */
2203 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002204 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002205 // Floating panels (except the full widget sheet) are associated with individual icons. If
2206 // we are starting a fresh bind, close all such panels as all the icons are about
2207 // to go away.
2208 AbstractFloatingView.closeOpenViews(this, true,
Sunny Goyal7ede6112017-12-05 15:11:21 -08002209 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_REBIND_SAFE);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08002210
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002211 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08002212
Winson Chungd64d1762013-08-20 14:37:16 -07002213 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07002214 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07002215 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07002216
Winson Chung3d503fb2011-07-13 17:25:49 -07002217 if (mHotseat != null) {
2218 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002219 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002220 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002221 }
2222
Adam Cohendcd297f2013-06-18 13:13:40 -07002223 @Override
2224 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002225 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002226 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
2227 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002228 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
2229 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002230 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002231 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
2232 // If there are no screens, we need to have an empty screen
2233 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07002234 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002235 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07002236
Winsonc7d2e832016-07-28 12:24:55 -07002237 // After we have added all the screens, if the wallpaper was locked to the default state,
2238 // then notify to indicate that it can be released and a proper wallpaper offset can be
2239 // computed before the next layout
2240 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07002241 }
2242
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002243 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002244 int count = orderedScreenIds.size();
2245 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002246 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002247 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002248 // No need to bind the first screen, as its always bound.
2249 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
2250 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002251 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002252 }
2253
Sunny Goyalb23980c2017-08-17 07:45:25 -07002254 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002255 public void bindAppsAdded(ArrayList<Long> newScreens, ArrayList<ItemInfo> addNotAnimated,
2256 ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07002257 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08002258 if (newScreens != null) {
2259 bindAddScreens(newScreens);
2260 }
Winson Chungd64d1762013-08-20 14:37:16 -07002261
2262 // We add the items without animation on non-visible pages, and with
2263 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08002264 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002265 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002266 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002267 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002268 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07002269 }
Winson Chungc58497e2013-09-03 17:48:37 -07002270
Winson Chung87412982013-10-03 18:34:14 -07002271 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07002272 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002273 }
2274
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002275 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002276 * Bind the items start-end from the list.
2277 *
2278 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002279 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07002280 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07002281 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002282 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07002283 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002284 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07002285 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07002286 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002287 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07002288 int end = items.size();
2289 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002290 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002291
2292 // Short circuit if we are loading dock items for a configuration which has no dock
2293 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2294 mHotseat == null) {
2295 continue;
2296 }
2297
Sunny Goyal639e9062015-08-19 19:17:06 -07002298 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002299 switch (item.itemType) {
2300 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2301 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08002302 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07002303 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07002304 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002305 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002306 }
2307 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07002308 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002309 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002310 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002311 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002312 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07002313 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2314 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07002315 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002316 if (view == null) {
2317 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002318 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08002319 break;
2320 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002321 default:
2322 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002323 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002324
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002325 /*
2326 * Remove colliding items.
2327 */
2328 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
2329 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
2330 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
2331 View v = cl.getChildAt(item.cellX, item.cellY);
2332 Object tag = v.getTag();
2333 String desc = "Collision while binding workspace item: " + item
2334 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08002335 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002336 throw (new RuntimeException(desc));
2337 } else {
2338 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002339 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002340 continue;
2341 }
2342 }
2343 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302344 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07002345 if (animateIcons) {
2346 // Animate all the applications up now
2347 view.setAlpha(0f);
2348 view.setScaleX(0f);
2349 view.setScaleY(0f);
2350 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08002351 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07002352 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002353 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07002354
Winson Chung997a9232013-07-24 15:33:46 -07002355 if (animateIcons) {
2356 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08002357 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07002358 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08002359 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07002360 final Runnable startBounceAnimRunnable = new Runnable() {
2361 public void run() {
2362 anim.playTogether(bounceAnims);
2363 anim.start();
2364 }
2365 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08002366 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07002367 // We post the animation slightly delayed to prevent slowdowns
2368 // when we are loading right after we return to launcher.
2369 mWorkspace.postDelayed(new Runnable() {
2370 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00002371 if (mWorkspace != null) {
2372 mWorkspace.snapToPage(newScreenIndex);
2373 mWorkspace.postDelayed(startBounceAnimRunnable,
2374 NEW_APPS_ANIMATION_DELAY);
2375 }
Winson Chung94d67682013-09-25 16:29:40 -07002376 }
2377 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07002378 } else {
2379 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07002380 }
2381 }
Winson Chung64359a52013-07-08 17:17:08 -07002382 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002383 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002384 }
2385
Joe Onorato9c1289c2009-08-17 11:03:03 -04002386 /**
2387 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002388 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07002389 public void bindAppWidget(LauncherAppWidgetInfo item) {
2390 View view = inflateAppWidget(item);
2391 if (view != null) {
2392 mWorkspace.addInScreen(view, item);
2393 mWorkspace.requestLayout();
2394 }
2395 }
2396
2397 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002398 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302399 PendingAppWidgetHostView view =
2400 new PendingAppWidgetHostView(this, item, mIconCache, true);
2401 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002402 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002403 }
2404
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002405 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002406
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002407 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08002408
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002409 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2410 // If the provider is not ready, bind as a pending widget.
2411 appWidgetInfo = null;
2412 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2413 // The widget id is not valid. Try to find the widget based on the provider info.
2414 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
2415 } else {
2416 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
2417 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002418
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002419 // If the provider is ready, but the width is not yet restored, try to restore it.
2420 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
2421 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07002422 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002423 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
2424 + " belongs to component " + item.providerName
2425 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002426 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002427 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002428 }
Sunny Goyalff572272014-07-23 13:58:07 -07002429
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002430 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002431 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07002432 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2433 // Id has not been allocated yet. Allocate a new id.
2434 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
2435 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07002436
Sunny Goyald478c832016-04-01 12:04:16 -07002437 // Also try to bind the widget. If the bind fails, the user will be shown
2438 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08002439 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07002440 pendingInfo.spanX = item.spanX;
2441 pendingInfo.spanY = item.spanY;
2442 pendingInfo.minSpanX = item.minSpanX;
2443 pendingInfo.minSpanY = item.minSpanY;
2444 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07002445
2446 boolean isDirectConfig =
2447 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2448 if (isDirectConfig && item.bindOptions != null) {
2449 Bundle newOptions = item.bindOptions.getExtras();
2450 if (options != null) {
2451 newOptions.putAll(options);
2452 }
2453 options = newOptions;
2454 }
Sunny Goyald478c832016-04-01 12:04:16 -07002455 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2456 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002457
Sunny Goyal86df1382016-08-10 15:03:22 -07002458 // We tried to bind once. If we were not able to bind, we would need to
2459 // go through the permission dialog, which means we cannot skip the config
2460 // activity.
2461 item.bindOptions = null;
2462 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2463
Sunny Goyald478c832016-04-01 12:04:16 -07002464 // Bind succeeded
2465 if (success) {
2466 // If the widget has a configure activity, it is still needs to set it up,
2467 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002468 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002469 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2470 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002471 }
Sunny Goyald478c832016-04-01 12:04:16 -07002472
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002473 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002474 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002475 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002476 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002477 // The widget was marked as UI not ready, but there is no configure activity to
2478 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002479 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002480 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002481 }
Sunny Goyalff572272014-07-23 13:58:07 -07002482 }
2483
Sunny Goyald5462aa2016-11-22 16:52:39 +05302484 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002485 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002486 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002487 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002488 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07002489 deleteWidgetInfo(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002490 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002491 }
2492
Sunny Goyal233ee962015-08-03 13:05:01 -07002493 item.minSpanX = appWidgetInfo.minSpanX;
2494 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302495 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002496 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302497 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002498 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302499 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002500
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002501 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002502 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002503 }
2504
Sunny Goyalff572272014-07-23 13:58:07 -07002505 /**
2506 * Restores a pending widget.
2507 *
2508 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002509 */
Sunny Goyald478c832016-04-01 12:04:16 -07002510 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002511 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2512 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2513 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002514 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002515 }
2516
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002517 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002518 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002519 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2520 info.pendingItemInfo = null;
2521 }
Sunny Goyalff572272014-07-23 13:58:07 -07002522
Sunny Goyalba47faa2017-10-04 16:50:57 -07002523 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002524 view.reInflate();
Sunny Goyalba47faa2017-10-04 16:50:57 -07002525 }
2526
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002527 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002528 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002529 }
2530
Adam Cohen1462de32012-07-24 22:34:36 -07002531 public void onPageBoundSynchronously(int page) {
Sunny Goyalf4aceab2017-12-06 16:16:31 -08002532 mSynchronouslyBoundPage = page;
Adam Cohen1462de32012-07-24 22:34:36 -07002533 }
2534
Sunny Goyal527c7d32015-08-28 15:19:36 -07002535 @Override
2536 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2537 if (mPendingExecutor != null) {
2538 mPendingExecutor.markCompleted();
2539 }
2540 mPendingExecutor = executor;
Sunny Goyal60180b02018-02-07 12:56:30 -08002541 if (!isInState(ALL_APPS)) {
2542 mAppsView.getAppsStore().setDeferUpdates(true);
2543 mPendingExecutor.execute(() -> mAppsView.getAppsStore().setDeferUpdates(false));
2544 }
2545
Sunny Goyal527c7d32015-08-28 15:19:36 -07002546 executor.attachTo(this);
2547 }
2548
2549 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2550 if (mPendingExecutor == executor) {
2551 mPendingExecutor = null;
2552 }
2553 }
2554
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002555 @Override
2556 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002557 if (mDragLayer.getAlpha() < 1) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002558 mDragLayer.animate().alpha(1).withEndAction(
2559 executor == null ? null : executor::onLoadAnimationCompleted).start();
2560 } else if (executor != null) {
2561 executor.onLoadAnimationCompleted();
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002562 }
2563 }
2564
Joe Onorato9c1289c2009-08-17 11:03:03 -04002565 /**
2566 * Callback saying that there aren't any more items to bind.
2567 *
2568 * Implementation of the method from LauncherModel.Callbacks.
2569 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002570 public void finishBindingItems() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002571 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002572 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002573
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002574 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002575
Sunny Goyal2100c782016-08-22 16:00:03 -07002576 if (mPendingActivityResult != null) {
2577 handleActivityResult(mPendingActivityResult.requestCode,
2578 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2579 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002580 }
2581
Sunny Goyala474a9b2017-05-04 16:47:11 -07002582 InstallShortcutReceiver.disableAndFlushInstallQueue(
2583 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002584
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002585 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002586 }
2587
Winson Chunga2413752012-04-03 14:22:34 -07002588 private boolean canRunNewAppsAnimation() {
2589 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002590 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002591 }
2592
Winson Chungc9168342013-06-26 14:54:55 -07002593 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002594 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07002595 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
2596 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002597 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002598 return bounceAnim;
2599 }
2600
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002601 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002602 * Add the icons for all apps.
2603 *
2604 * Implementation of the method from LauncherModel.Callbacks.
2605 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002606 public void bindAllApplications(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002607 mAppsView.getAppsStore().setApps(apps);
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002608
Adam Cohen9211d422014-10-07 18:14:53 -07002609 if (mLauncherCallbacks != null) {
2610 mLauncherCallbacks.bindAllApplications(apps);
2611 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002612 }
2613
2614 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002615 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
2616 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2617 */
2618 @Override
2619 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002620 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002621 }
2622
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002623 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002624 * A package was updated.
2625 *
2626 * Implementation of the method from LauncherModel.Callbacks.
2627 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002628 @Override
2629 public void bindAppsAddedOrUpdated(ArrayList<AppInfo> apps) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002630 mAppsView.getAppsStore().addOrUpdateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002631 }
2632
Sunny Goyal4390ace2014-10-13 11:33:11 -07002633 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002634 public void bindPromiseAppProgressUpdated(PromiseAppInfo app) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002635 mAppsView.getAppsStore().updatePromiseAppProgress(app);
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002636 }
2637
2638 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002639 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002640 mWorkspace.widgetsRestored(widgets);
2641 }
2642
Joe Onorato9c1289c2009-08-17 11:03:03 -04002643 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002644 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002645 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002646 *
2647 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002648 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002649 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002650 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002651 if (!updated.isEmpty()) {
2652 mWorkspace.updateShortcuts(updated);
2653 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002654 }
2655
2656 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002657 * Update the state of a package, typically related to install state.
2658 *
2659 * Implementation of the method from LauncherModel.Callbacks.
2660 */
Sunny Goyale755d462014-07-22 13:48:29 -07002661 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002662 public void bindRestoreItemsChange(HashSet<ItemInfo> updates) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07002663 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07002664 }
2665
2666 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002667 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07002668 * in addition to specific applications to remove, the reason being that
2669 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002670 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07002671 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002672 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07002673 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002674 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002675 mWorkspace.removeItemsByMatcher(matcher);
2676 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002677 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002678
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002679 @Override
2680 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
Sunny Goyal60180b02018-02-07 12:56:30 -08002681 mAppsView.getAppsStore().removeApps(appInfos);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002682 }
Joe Onoratobe386092009-11-17 17:32:16 -08002683
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002684 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002685 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
2686 mPopupDataProvider.setAllWidgets(allWidgets);
Tony Wickham26b17462017-03-20 17:12:24 -07002687 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
2688 if (topView != null) {
2689 topView.onWidgetsBound();
2690 }
2691 }
2692
Tony Wickham86222d22017-03-29 15:30:43 -07002693 /**
2694 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
2695 * refreshes the widgets and shortcuts associated with the given package/user
2696 */
2697 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07002698 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002699 }
2700
Sunny Goyalc99cb172017-10-19 16:15:09 -07002701 public boolean isRotationEnabled () {
2702 return mRotationEnabled;
Winson Chung400438b2011-01-16 17:53:48 -08002703 }
2704
Winson Chung80baf5a2010-08-09 16:03:15 -07002705 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002706 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08002707 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002708 @Override
2709 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
2710 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002711
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002712 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
2713 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07002714 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002715 writer.println(prefix + " Homescreen " + i);
2716
2717 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
2718 for (int j = 0; j < layout.getChildCount(); j++) {
2719 Object tag = layout.getChildAt(j).getTag();
2720 if (tag != null) {
2721 writer.println(prefix + " " + tag.toString());
2722 }
2723 }
2724 }
2725
2726 writer.println(prefix + " Hotseat");
2727 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07002728 for (int j = 0; j < layout.getChildCount(); j++) {
2729 Object tag = layout.getChildAt(j).getTag();
2730 if (tag != null) {
2731 writer.println(prefix + " " + tag.toString());
2732 }
Adam Cohen4caf2982013-08-20 18:54:31 -07002733 }
Sunny Goyala1365452015-10-01 15:46:24 -07002734
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002735 try {
2736 FileLog.flushAll(writer);
2737 } catch (Exception e) {
2738 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07002739 }
2740 }
2741
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002742 writer.println(prefix + "Misc:");
2743 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
2744 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
2745 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
2746
2747 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002748
Adam Cohen9211d422014-10-07 18:14:53 -07002749 if (mLauncherCallbacks != null) {
2750 mLauncherCallbacks.dump(prefix, fd, writer, args);
2751 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002752 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002753
Sunny Goyal66b24572016-09-21 15:57:55 -07002754 @Override
2755 @TargetApi(Build.VERSION_CODES.N)
2756 public void onProvideKeyboardShortcuts(
2757 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
2758
2759 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002760 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002761 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
2762 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
2763 }
2764 View currentFocus = getCurrentFocus();
2765 if (new CustomActionsPopup(this, currentFocus).canShow()) {
2766 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
2767 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
2768 }
Tony18c4aa42017-05-10 21:23:57 -05002769 if (currentFocus.getTag() instanceof ItemInfo
2770 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002771 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
2772 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
2773 }
2774 if (!shortcutInfos.isEmpty()) {
2775 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
2776 }
2777
2778 super.onProvideKeyboardShortcuts(data, menu, deviceId);
2779 }
2780
2781 @Override
2782 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
2783 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
2784 switch (keyCode) {
2785 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002786 if (isInState(NORMAL)) {
2787 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07002788 return true;
2789 }
2790 break;
2791 case KeyEvent.KEYCODE_S: {
2792 View focusedView = getCurrentFocus();
2793 if (focusedView instanceof BubbleTextView
2794 && focusedView.getTag() instanceof ItemInfo
2795 && mAccessibilityDelegate.performAction(focusedView,
2796 (ItemInfo) focusedView.getTag(),
2797 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal00ac9202017-11-09 15:24:06 -08002798 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07002799 return true;
2800 }
2801 break;
2802 }
2803 case KeyEvent.KEYCODE_O:
2804 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
2805 return true;
2806 }
2807 break;
2808 }
2809 }
2810 return super.onKeyShortcut(keyCode, event);
2811 }
2812
Vadim Tryshev646a0dd2018-02-05 16:27:50 -08002813 @Override
2814 public boolean onKeyUp(int keyCode, KeyEvent event) {
2815 if (keyCode == KeyEvent.KEYCODE_MENU) {
2816 // KEYCODE_MENU is sent by some tests, for example
2817 // LauncherJankTests#testWidgetsContainerFling. Don't just remove its handling.
2818 if (!mDragController.isDragging() && !mWorkspace.isSwitchingState() &&
2819 isInState(NORMAL)) {
2820 // Close any open floating views.
2821 AbstractFloatingView.closeAllOpenViews(this);
2822
2823 // Setting the touch point to (-1, -1) will show the options popup in the center of
2824 // the screen.
2825 mLastDispatchTouchEvent.set(-1, -1);
2826 UiFactory.onWorkspaceLongPress(this, mLastDispatchTouchEvent);
2827 }
2828 return true;
2829 }
2830 return super.onKeyUp(keyCode, event);
2831 }
2832
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002833 public static Launcher getLauncher(Context context) {
2834 if (context instanceof Launcher) {
2835 return (Launcher) context;
2836 }
2837 return ((Launcher) ((ContextWrapper) context).getBaseContext());
2838 }
2839
Sunny Goyal5a81c382017-03-20 15:08:06 -07002840 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener {
Sunny Goyal745bad92016-05-02 10:54:12 -07002841
2842 @Override
2843 public void onSharedPreferenceChanged(
2844 SharedPreferences sharedPreferences, String key) {
2845 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
Sunny Goyale13d4642017-05-22 15:58:42 -07002846 // Recreate the activity so that it initializes the rotation preference again.
2847 recreate();
Sunny Goyal745bad92016-05-02 10:54:12 -07002848 }
2849 }
Sunny Goyal745bad92016-05-02 10:54:12 -07002850 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002851
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08002852 @Override
2853 public void onActionModeStarted(ActionMode mode) {
2854 super.onActionModeStarted(mode);
2855 mCurrentActionMode = mode;
2856 }
2857
2858 @Override
2859 public void onActionModeFinished(ActionMode mode) {
2860 super.onActionModeFinished(mode);
2861 mCurrentActionMode = null;
2862 }
2863
2864 public boolean finishAutoCancelActionMode() {
2865 if (mCurrentActionMode != null && AUTO_CANCEL_ACTION_MODE == mCurrentActionMode.getTag()) {
2866 mCurrentActionMode.finish();
2867 return true;
2868 }
2869 return false;
2870 }
2871
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002872 /**
2873 * Callback for listening for onResume
2874 */
2875 public interface OnResumeCallback {
2876
2877 void onLauncherResume();
2878 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002879}