blob: a91907d44e9be6fb7cc4df6ebff8a48334ead95d [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 Goyalc99cb172017-10-19 16:15:09 -070019import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_NOSENSOR;
20import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
21
Sunny Goyal076839c2017-10-30 13:52:20 -070022import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_BY_PUBLISHER;
23import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_LOCKED_USER;
24import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_QUIET_USER;
25import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_SAFEMODE;
26import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_SUSPENDED;
Sunny Goyal4c7f2152017-10-17 17:17:16 -070027import static com.android.launcher3.LauncherAnimUtils.SPRING_LOADED_EXIT_DELAY;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -070028import static com.android.launcher3.LauncherState.ALL_APPS;
29import static com.android.launcher3.LauncherState.NORMAL;
30import static com.android.launcher3.LauncherState.OVERVIEW;
Sunny Goyalaeb16432017-10-16 11:46:41 -070031import static com.android.launcher3.logging.LoggerUtils.newContainerTarget;
Sunny Goyal326403e2017-10-02 12:45:10 -070032
Sunny Goyal28c6b962015-10-12 11:42:05 -070033import android.Manifest;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070034import android.animation.Animator;
Jon Miranda2d89ea82017-05-04 11:47:53 -070035import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070036import android.animation.AnimatorSet;
Jon Miranda2d89ea82017-05-04 11:47:53 -070037import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070038import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070039import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000040import android.annotation.TargetApi;
Winson Chungc7450e32012-04-17 17:34:08 -070041import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040042import android.app.AlertDialog;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080043import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070044import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080046import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080047import android.content.ComponentCallbacks2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.Context;
Andrew Sappersteinabef55a2016-06-19 12:49:00 -070049import android.content.ContextWrapper;
Chris Wren40c5ed32014-06-24 18:24:23 -040050import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070052import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070053import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070054import android.content.SharedPreferences;
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;
Adam Cohen3f9c9712014-10-31 11:48:25 -070057import android.database.sqlite.SQLiteDatabase;
Jon Mirandacc42c5b2016-10-27 17:15:27 -070058import android.graphics.Point;
Sunny Goyal02424b22018-01-19 11:24:32 -080059import android.graphics.PointF;
Michael Jurkaaf442092010-06-10 17:01:57 -070060import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070061import android.graphics.drawable.Drawable;
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;
Tony2917a8b2017-07-31 23:29:42 -0700153import java.util.concurrent.Executor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700154
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155/**
156 * Default launcher application.
157 */
Sunny Goyal27835952017-01-13 12:15:53 -0800158public class Launcher extends BaseActivity
Adam Cohen79d90c52016-04-22 13:29:20 -0700159 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
160 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Mario Bertschler27288382017-05-24 15:35:09 -0700161 WallpaperColorInfo.OnThemeChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700162 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700163 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164
Winson Chunga2413752012-04-03 14:22:34 -0700165 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700166
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700168 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700169
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700170 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700171 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172
Michael Jurka8b805b12012-04-18 14:23:14 -0700173 private static final int REQUEST_BIND_APPWIDGET = 11;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700174 private static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
175 private static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700176
Jon Mirandac476d6e2017-05-04 16:30:01 -0700177 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700178
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700179 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
180
Mathew Inwood876a8462013-06-14 14:12:41 +0100181 /**
182 * IntentStarter uses request codes starting with this. This must be greater than all activity
183 * request codes used internally.
184 */
185 protected static final int REQUEST_LAST = 100;
186
Winson Chung2672ff92012-05-04 16:22:30 -0700187 // The Intent extra that defines whether to ignore the launch animation
188 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400189 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700190
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191 // Type: int
192 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700193 // Type: int
194 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700195 // Type: PendingRequestArgs
196 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
197 // Type: ActivityResultInfo
198 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700199 // Type: SparseArray<Parcelable>
200 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800201
Sunny Goyal7c8a65e2017-12-22 11:11:33 -0800202 // When starting an action mode, setting this tag will cause the action mode to be cancelled
203 // automatically when user interacts with the launcher.
204 public static final Object AUTO_CANCEL_ACTION_MODE = new Object();
205
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700206 private LauncherStateManager mStateManager;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700207
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700208 private boolean mIsSafeModeEnabled;
209
Adam Cohenad4e15c2013-10-17 16:21:35 -0700210 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211
Winson Chunga2413752012-04-03 14:22:34 -0700212 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700213 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
214 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
215 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700216
Jon Miranda54441f52018-01-24 15:38:25 -0800217 private LauncherAppTransitionManager mAppTransitionManager;
218
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
Jon Miranda6bed3502017-09-19 14:43:17 -0700240 // We need to store the orientation Launcher was created with, due to a bug (b/64916689)
241 // that results in widgets being inflated in the wrong orientation.
242 private int mOrientation;
243
Adam Cohen091440a2015-03-18 14:16:05 -0700244 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400245
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700246 private OnResumeCallback mOnResumeCallback;
247
Sunny Goyal527c7d32015-08-28 15:19:36 -0700248 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700249
Joe Onorato9c1289c2009-08-17 11:03:03 -0400250 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800251 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800252 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700253 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400254
Jon Miranda2d89ea82017-05-04 11:47:53 -0700255 private ObjectAnimator mScrimAnimator;
Jon Miranda7fda2852017-07-19 16:07:01 -0700256 private boolean mShouldFadeInScrim;
Jon Miranda2d89ea82017-05-04 11:47:53 -0700257
Tony Wickham010d2552017-01-20 08:15:28 -0800258 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700259
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800260 private int mSynchronouslyBoundPage = PagedView.INVALID_PAGE;
Adam Cohen1462de32012-07-24 22:34:36 -0700261
Winson Chung46353de2012-02-16 14:05:10 -0800262 // We only want to get the SharedPreferences once since it does an FS stat each time we get
263 // it from the context.
264 private SharedPreferences mSharedPrefs;
265
Sunny Goyal2100c782016-08-22 16:00:03 -0700266 // Activity result which needs to be processed after workspace has loaded.
267 private ActivityResultInfo mPendingActivityResult;
268 /**
269 * Holds extra information required to handle a result from an external call, like
270 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
271 */
272 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800273
Sunny Goyal02424b22018-01-19 11:24:32 -0800274 private final PointF mLastDispatchTouchEvent = new PointF();
Jon Miranda379198e2016-09-23 08:54:40 -0700275
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700276 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700277 private boolean mRotationEnabled = false;
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700278 private boolean mAppLaunchSuccess;
279
Sunny Goyal745bad92016-05-02 10:54:12 -0700280 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7c8a65e2017-12-22 11:11:33 -0800281 private ActionMode mCurrentActionMode;
Sunny Goyal7779d622015-06-11 16:18:39 -0700282
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800283 @Override
284 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700285 if (DEBUG_STRICT_MODE) {
286 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
287 .detectDiskReads()
288 .detectDiskWrites()
289 .detectNetwork() // or .detectAll() for all detectable problems
290 .penaltyLog()
291 .build());
292 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
293 .detectLeakedSqlLiteObjects()
294 .detectLeakedClosableObjects()
295 .penaltyLog()
296 .penaltyDeath()
297 .build());
298 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700299 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700300
Adam Cohen9211d422014-10-07 18:14:53 -0700301 if (mLauncherCallbacks != null) {
302 mLauncherCallbacks.preOnCreate();
303 }
304
Mario Bertschler27288382017-05-24 15:35:09 -0700305 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
306 wallpaperColorInfo.setOnThemeChangeListener(this);
Mario Bertschlera6936942017-05-31 14:48:19 -0700307 overrideTheme(wallpaperColorInfo.isDark(), wallpaperColorInfo.supportsDarkText());
Mario Bertschler27288382017-05-24 15:35:09 -0700308
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800309 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700310 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400311
Sunny Goyal87f784c2017-01-11 10:48:34 -0800312 LauncherAppState app = LauncherAppState.getInstance(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700313
Adam Cohen2e6da152015-05-06 11:42:25 -0700314 // Load configuration-specific DeviceProfile
Sunny Goyal27835952017-01-13 12:15:53 -0800315 mDeviceProfile = app.getInvariantDeviceProfile().getDeviceProfile(this);
Jon Mirandafe964322017-03-22 10:25:17 -0700316 if (isInMultiWindowModeCompat()) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700317 Display display = getWindowManager().getDefaultDisplay();
318 Point mwSize = new Point();
319 display.getSize(mwSize);
320 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
321 }
Sunny Goyalc6205602015-05-21 20:46:33 -0700322
Jon Miranda6bed3502017-09-19 14:43:17 -0700323 mOrientation = getResources().getConfiguration().orientation;
Sunny Goyalf7258242015-10-19 16:59:07 -0700324 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700325 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800326 mModel = app.setLauncher(this);
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800327 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout());
Joe Onorato0589f0f2010-02-08 13:44:00 -0800328 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700329 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700330
Joe Onorato41a12d22009-10-31 18:30:00 -0400331 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700332 mAllAppsController = new AllAppsTransitionController(this);
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800333 mStateManager = new LauncherStateManager(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700334
Sunny Goyalffe83f12014-08-14 17:39:34 -0700335 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700336
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700337 mAppWidgetHost = new LauncherAppWidgetHost(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700338 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700339
Sunny Goyal60820d72017-05-09 12:40:11 -0700340 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700341
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800342 setupViews();
Tony Wickham010d2552017-01-20 08:15:28 -0800343 mPopupDataProvider = new PopupDataProvider(this);
344
Sunny Goyald3864fa2017-11-14 15:06:36 -0800345 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
346 // In case we are on a device with locked rotation, we should look at preferences to check
347 // if the user has specifically allowed rotation.
348 if (!mRotationEnabled) {
349 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
350 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
351 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
352 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800353
Sunny Goyald3864fa2017-11-14 15:06:36 -0800354 boolean internalStateHandled = InternalStateHandler.handleCreate(this, getIntent());
355 if (internalStateHandled) {
356 // Temporarily enable the rotation
357 mRotationEnabled = true;
358
359 if (savedInstanceState != null) {
360 // InternalStateHandler has already set the appropriate state.
361 // We dont need to do anything.
362 savedInstanceState.remove(RUNTIME_STATE);
363 }
364 }
365 restoreState(savedInstanceState);
Winson Chungb63b44c2017-11-10 17:54:44 -0800366
Sunny Goyal2100c782016-08-22 16:00:03 -0700367 // We only load the page synchronously if the user rotates (or triggers a
368 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700369 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
370 if (savedInstanceState != null) {
371 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
372 }
Sunny Goyald3864fa2017-11-14 15:06:36 -0800373
Sunny Goyalfe770c92016-09-27 14:38:58 -0700374 if (!mModel.startLoader(currentScreen)) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800375 if (!internalStateHandled) {
376 // If we are not binding synchronously, show a fade in animation when
377 // the first page bind completes.
378 mDragLayer.setAlpha(0);
379 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700380 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700381 // Pages bound synchronously.
382 mWorkspace.setCurrentPage(currentScreen);
383
Sunny Goyal2100c782016-08-22 16:00:03 -0700384 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800385 }
386
387 // For handling default keys
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800388 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800389
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700390 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
391 // we want the screen to auto-rotate based on the current orientation
Sunny Goyalc99cb172017-10-19 16:15:09 -0700392 setRequestedOrientation(mRotationEnabled
393 ? SCREEN_ORIENTATION_UNSPECIFIED : SCREEN_ORIENTATION_NOSENSOR);
Winson Chungaf40f202013-09-18 18:26:31 -0700394
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800395 setContentView(mLauncherView);
Sunny Goyal07b69292018-01-08 14:19:34 -0800396 ((LauncherRootView) mLauncherView).dispatchInsets();
Mario Bertschlera6936942017-05-31 14:48:19 -0700397
Jon Miranda7fda2852017-07-19 16:07:01 -0700398 // Listen for broadcasts
399 IntentFilter filter = new IntentFilter();
400 filter.addAction(Intent.ACTION_SCREEN_OFF);
401 filter.addAction(Intent.ACTION_USER_PRESENT); // When the device wakes up + keyguard is gone
402 registerReceiver(mReceiver, filter);
403 mShouldFadeInScrim = true;
Sunny Goyala616d2b2017-06-12 13:54:01 -0700404
Sunny Goyal8392c822017-06-20 10:03:56 -0700405 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
406 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700407
Jon Miranda54441f52018-01-24 15:38:25 -0800408 mAppTransitionManager = Utilities.getOverrideObject(LauncherAppTransitionManager.class,
409 this, R.string.app_transition_manager_class);
410
Jon Miranda7853bdb2018-01-22 17:32:30 -0800411 if (!isInMultiWindowModeCompat()) {
Jon Miranda54441f52018-01-24 15:38:25 -0800412 mAppTransitionManager.registerRemoteAnimations();
Jon Miranda7853bdb2018-01-22 17:32:30 -0800413 }
414
Sunny Goyal14b32402017-07-31 10:03:28 -0700415 if (mLauncherCallbacks != null) {
416 mLauncherCallbacks.onCreate(savedInstanceState);
417 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700418
419 TraceHelper.endSection("Launcher-onCreate");
Adam Cohen9211d422014-10-07 18:14:53 -0700420 }
421
Sunny Goyal7779d622015-06-11 16:18:39 -0700422 @Override
Mario Bertschler27288382017-05-24 15:35:09 -0700423 public void onThemeChanged() {
424 recreate();
425 }
426
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700427 public LauncherStateManager getStateManager() {
428 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700429 }
430
Mario Bertschlera6936942017-05-31 14:48:19 -0700431 protected void overrideTheme(boolean isDark, boolean supportsDarkText) {
Mario Bertschler27288382017-05-24 15:35:09 -0700432 if (isDark) {
433 setTheme(R.style.LauncherThemeDark);
Mario Bertschlera6936942017-05-31 14:48:19 -0700434 } else if (supportsDarkText) {
435 setTheme(R.style.LauncherThemeDarkText);
Mario Bertschler27288382017-05-24 15:35:09 -0700436 }
437 }
438
439 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700440 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800441 return mLauncherView.findViewById(id);
442 }
443
444 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700445 public void onAppWidgetHostReset() {
446 if (mAppWidgetHost != null) {
447 mAppWidgetHost.startListening();
448 }
449 }
450
Adam Cohen9211d422014-10-07 18:14:53 -0700451 private LauncherCallbacks mLauncherCallbacks;
452
Sunny Goyal32554d12015-12-03 15:31:25 -0800453 /**
454 * Call this after onCreate to set or clear overlay.
455 */
456 public void setLauncherOverlay(LauncherOverlay overlay) {
457 if (overlay != null) {
458 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
459 }
460 mWorkspace.setLauncherOverlay(overlay);
461 }
462
Adam Cohen9211d422014-10-07 18:14:53 -0700463 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
464 mLauncherCallbacks = callbacks;
465 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700466 }
467
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700468 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700469 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700470 if (mLauncherCallbacks != null) {
471 mLauncherCallbacks.onLauncherProviderChange();
472 }
473 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700474
Hyunyoung Song3f471442015-04-08 19:01:34 -0700475 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700476 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
477 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700478 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700479 }
480
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000481 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700482 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
483 // This cast is safe as long as the id < 0x00FFFFFF
484 // Since we jail all the dynamically generated views, there should be no clashes
485 // with any other views.
486 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000487 }
488
Tony Wickham010d2552017-01-20 08:15:28 -0800489 public PopupDataProvider getPopupDataProvider() {
490 return mPopupDataProvider;
491 }
492
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000493 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700494 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
495 * a configuration step, this allows the proper animations to run after other transitions.
496 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700497 private long completeAdd(
498 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
499 long screenId = info.screenId;
500 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700501 // When the screen id represents an actual screen (as opposed to a rank) we make sure
502 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700503 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700504 }
Adam Cohendb364c32014-05-20 14:23:40 -0700505
Sunny Goyal2100c782016-08-22 16:00:03 -0700506 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800507 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700508 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700509 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800510 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700511 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700512 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700513 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700514 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700515 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700516 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700517 int widgetId = appWidgetId;
518 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700519 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700520 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700521 // Since the view was just bound, also launch the configure activity if needed
522 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
523 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800524 if (provider != null) {
525 new WidgetAddFlowHandler(provider)
526 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700527 }
528 }
529 break;
530 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800531 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700532
Adam Cohendb364c32014-05-20 14:23:40 -0700533 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800534 }
535
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800536 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700537 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700538 if (isWorkspaceLoading()) {
539 // process the result once the workspace has loaded.
540 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
541 return;
542 }
543 mPendingActivityResult = null;
544
Winson Chunge341d302013-08-16 14:31:00 -0700545 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700546 final PendingRequestArgs requestArgs = mPendingRequestArgs;
547 setWaitingForResult(null);
548 if (requestArgs == null) {
549 return;
550 }
551
552 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700553
Adam Cohenad4e15c2013-10-17 16:21:35 -0700554 Runnable exitSpringLoaded = new Runnable() {
555 @Override
556 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700557 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700558 }
559 };
560
Michael Jurka8b805b12012-04-18 14:23:14 -0700561 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700562 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700563 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700564 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
565 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700566 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700567 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700568 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700569 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700570 addAppWidgetImpl(
571 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800572 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700573 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700574 }
575 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200576 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700577 if (resultCode == RESULT_OK && isInState(OVERVIEW)) {
Tony Wickhame3054412015-09-09 09:05:25 -0700578 // User could have free-scrolled between pages before picking a wallpaper; make sure
579 // we move to the closest one now.
580 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700581 mStateManager.goToState(NORMAL, false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200582 }
583 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700584 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200585
Adam Cohened66b2b2012-01-23 17:28:51 -0800586 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700587 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700588
Adam Cohened66b2b2012-01-23 17:28:51 -0800589 // We have special handling for widgets
590 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800591 final int appWidgetId;
592 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
593 : -1;
594 if (widgetId < 0) {
595 appWidgetId = pendingAddWidgetId;
596 } else {
597 appWidgetId = widgetId;
598 }
599
Adam Cohenad4e15c2013-10-17 16:21:35 -0700600 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800601 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700602 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
603 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700604 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700605 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700606 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700607 @Override
608 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700609 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700610 }
611 };
Adam Cohendb364c32014-05-20 14:23:40 -0700612
Sunny Goyal2100c782016-08-22 16:00:03 -0700613 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
614 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
615 } else {
616 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
617 // When the screen id represents an actual screen (as opposed to a rank)
618 // we make sure that the drop page actually exists.
619 requestArgs.screenId =
620 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700621 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700622 final CellLayout dropLayout =
623 mWorkspace.getScreenWithId(requestArgs.screenId);
624
625 dropLayout.setDropPending(true);
626 final Runnable onComplete = new Runnable() {
627 @Override
628 public void run() {
629 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
630 dropLayout.setDropPending(false);
631 }
632 };
633 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
634 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700635 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800636 return;
637 }
638
Sunny Goyald478c832016-04-01 12:04:16 -0700639 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
640 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700641 if (resultCode == RESULT_OK) {
642 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700643 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700644 }
645 // Leave the widget in the pending state if the user canceled the configure.
646 return;
647 }
648
Sunny Goyalaad90582015-07-09 14:22:50 -0700649 if (requestCode == REQUEST_CREATE_SHORTCUT) {
650 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700651 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
652 completeAdd(requestCode, data, -1, requestArgs);
653 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
654 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
655
Sunny Goyalaad90582015-07-09 14:22:50 -0700656 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700657 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
658 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800659 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800661 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800662 }
663
664 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700665 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800666 final int requestCode, final int resultCode, final Intent data) {
667 handleActivityResult(requestCode, resultCode, data);
668 if (mLauncherCallbacks != null) {
669 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
670 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800671 }
672
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700673 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700674 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600675 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700676 PendingRequestArgs pendingArgs = mPendingRequestArgs;
677 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
678 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
679 setWaitingForResult(null);
680
Sunny Goyal28c6b962015-10-12 11:42:05 -0700681 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700682 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700683 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700684 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700685 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700686 Intent intent = pendingArgs.getPendingIntent();
687
Sunny Goyal28c6b962015-10-12 11:42:05 -0700688 if (grantResults.length > 0
689 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700690 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700691 } else {
692 // TODO: Show a snack bar with link to settings
693 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700694 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700695 }
696 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600697 if (mLauncherCallbacks != null) {
698 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
699 grantResults);
700 }
701 }
702
Adam Cohendb364c32014-05-20 14:23:40 -0700703 /**
704 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
705 *
706 * @param screenId the screen id to check
707 * @return the new screen, or screenId if it exists
708 */
709 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800710 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700711 if (dropLayout == null) {
712 // it's possible that the add screen was removed because it was
713 // empty and a re-bind occurred
714 mWorkspace.addExtraEmptyScreen();
715 return mWorkspace.commitExtraEmptyScreen();
716 } else {
717 return screenId;
718 }
719 }
720
Sunny Goyal2100c782016-08-22 16:00:03 -0700721 @Thunk void completeTwoStageWidgetDrop(
722 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
723 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800724 Runnable onCompleteRunnable = null;
725 int animationType = 0;
726
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700727 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800728 if (resultCode == RESULT_OK) {
729 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
730 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800731 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700732 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800733 onCompleteRunnable = new Runnable() {
734 @Override
735 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700736 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700737 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800738 }
739 };
740 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800741 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800742 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800743 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700744 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700745 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700746 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
747 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700748 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700749 // The animated view may be null in the case of a rotation during widget configuration
750 onCompleteRunnable.run();
751 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800752 }
753
754 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700755 protected void onStop() {
756 super.onStop();
757 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700758
759 if (mLauncherCallbacks != null) {
760 mLauncherCallbacks.onStop();
761 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800762 mAppWidgetHost.setListenIfResumed(false);
Tony Wickham010d2552017-01-20 08:15:28 -0800763
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700764 if (!mAppLaunchSuccess) {
765 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
Sunny Goyalea609262017-10-25 15:47:38 -0700766 mStateManager.getState().containerType);
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700767 }
Tony Wickham010d2552017-01-20 08:15:28 -0800768 NotificationListener.removeNotificationsChangedListener();
Michael Jurkacd496d72013-04-11 11:32:45 -0700769 }
770
771 @Override
772 protected void onStart() {
773 super.onStart();
774 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700775
776 if (mLauncherCallbacks != null) {
777 mLauncherCallbacks.onStart();
778 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800779 mAppWidgetHost.setListenIfResumed(true);
Sunny Goyalcac002d2018-01-04 09:51:45 -0800780 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
Jon Miranda2d89ea82017-05-04 11:47:53 -0700781
Jon Mirandade43a712018-01-18 11:35:10 -0800782 if (mShouldFadeInScrim && mLauncherView.getBackground() != null) {
Jon Miranda2d89ea82017-05-04 11:47:53 -0700783 if (mScrimAnimator != null) {
784 mScrimAnimator.cancel();
785 }
Jon Mirandade43a712018-01-18 11:35:10 -0800786 mLauncherView.getBackground().setAlpha(0);
787 mScrimAnimator = ObjectAnimator.ofInt(mLauncherView.getBackground(),
Jon Miranda2d89ea82017-05-04 11:47:53 -0700788 LauncherAnimUtils.DRAWABLE_ALPHA, 0, 255);
789 mScrimAnimator.addListener(new AnimatorListenerAdapter() {
790 @Override
791 public void onAnimationEnd(Animator animation) {
792 mScrimAnimator = null;
793 }
794 });
795 mScrimAnimator.setDuration(600);
796 mScrimAnimator.setStartDelay(getWindow().getTransitionBackgroundFadeDuration());
797 mScrimAnimator.start();
798 }
Jon Miranda7fda2852017-07-19 16:07:01 -0700799 mShouldFadeInScrim = false;
Michael Jurkacd496d72013-04-11 11:32:45 -0700800 }
801
802 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800803 protected void onResume() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700804 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800805 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700806 TraceHelper.partitionSection("ON_RESUME", "superCall");
807
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700808 mAppLaunchSuccess = false;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700809 getUserEventDispatcher().resetElapsedSessionMillis();
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700810 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700811 // Process any items that were added while Launcher was away.
812 InstallShortcutReceiver.disableAndFlushInstallQueue(
813 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700814
Sunny Goyala474a9b2017-05-04 16:47:11 -0700815 // Refresh shortcuts if the permission changed.
816 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700817
Sunny Goyal7ede6112017-12-05 15:11:21 -0800818 DiscoveryBounce.showIfNeeded(this);
Adam Cohen9211d422014-10-07 18:14:53 -0700819 if (mLauncherCallbacks != null) {
820 mLauncherCallbacks.onResume();
821 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800822
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700823 TraceHelper.endSection("ON_RESUME");
Adam Cohen06dff352012-06-01 17:17:08 -0700824 }
825
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800826 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700827 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700828 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700829 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700830
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700831 super.onPause();
Joe Onorato24b6fd82009-11-12 13:47:09 -0800832 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700833 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700834
Adam Cohen9211d422014-10-07 18:14:53 -0700835 if (mLauncherCallbacks != null) {
836 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700837 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700838 }
839
Adam Cohenc2d6e892014-10-16 09:49:24 -0700840 public interface LauncherOverlay {
841
842 /**
843 * Touch interaction leading to overscroll has begun
844 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700845 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700846
847 /**
848 * Touch interaction related to overscroll has ended
849 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700850 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700851
852 /**
853 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
854 * screen (or in the case of RTL, the rightmost screen).
855 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700856 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700857
858 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800859 * Called when the launcher is ready to use the overlay
860 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700861 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700862 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700863 }
864
865 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700866 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700867 }
868
869 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
870
Sunny Goyalc86df472016-02-25 09:19:38 -0800871 public void onScrollChanged(float progress) {
872 if (mWorkspace != null) {
873 mWorkspace.onOverlayScrollChanged(progress);
874 }
875 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700876 }
877
Sunny Goyal16764582017-11-06 16:00:34 -0800878 public boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -0700879 if (mLauncherCallbacks != null) {
880 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700881 } else {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800882 // On O and above we there is always some setting present settings (add icon to
883 // home screen or icon badging). On earlier APIs we will have the allow rotation
884 // setting, on devices with a locked orientation,
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700885 return Utilities.ATLEAST_OREO || !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -0700886 }
Jorim Jaggid017f882014-01-14 17:08:48 -0800887 }
888
Sunny Goyalf9403d92017-10-18 10:55:56 -0700889 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -0700890 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700891 }
892
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800893 /**
894 * Restores the previous state, if it exists.
895 *
896 * @param savedState The previous state.
897 */
898 private void restoreState(Bundle savedState) {
899 if (savedState == null) {
900 return;
901 }
902
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700903 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -0700904 LauncherState[] stateValues = LauncherState.values();
905 LauncherState state = stateValues[stateOrdinal];
Sunny Goyalbc683e92017-12-06 10:25:07 -0800906 if (!state.disableRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700907 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800908 }
909
Sunny Goyal2100c782016-08-22 16:00:03 -0700910 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
911 if (requestArgs != null) {
912 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800913 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700914
915 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700916
917 SparseArray<Parcelable> widgetsState =
918 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
919 if (widgetsState != null) {
920 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
921 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800922 }
923
924 /**
925 * Finds all the views we need and configure them properly.
926 */
927 private void setupViews() {
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800928 mDragLayer = findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700929 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -0700930 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700931 mWorkspace.initParentViews(mDragLayer);
Sunny Goyal16764582017-11-06 16:00:34 -0800932 mOverviewPanel = findViewById(R.id.overview_panel);
Craig Mautner360310b2012-10-26 15:13:08 -0700933
Sunny Goyal784f9c32016-03-23 16:56:54 -0700934 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
935 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
936 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800937
Winson Chung4c98d922011-05-31 16:50:48 -0700938 // Setup the drag layer
Sunny Goyal7ede6112017-12-05 15:11:21 -0800939 mDragLayer.setup(this, mDragController);
Winson Chung4c98d922011-05-31 16:50:48 -0700940
Winson Chung3d503fb2011-07-13 17:25:49 -0700941 // Setup the hotseat
942 mHotseat = (Hotseat) findViewById(R.id.hotseat);
943 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -0700944 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -0700945 }
946
Winson Chung4c98d922011-05-31 16:50:48 -0700947 // Setup the workspace
948 mWorkspace.setHapticFeedbackEnabled(false);
949 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700950 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -0700951 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
952 // default state, otherwise we will update to the wrong offsets in RTL
953 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700954 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700955 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700956
Tony Wickham34d2c912015-09-11 09:27:58 -0700957 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700958 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700959
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700960 // Setup Apps
961 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -0700962
Winson Chung3d503fb2011-07-13 17:25:49 -0700963 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -0700964 mDragController.setMoveTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700965 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -0400966
Hyunyoung Songd725f642017-08-17 22:26:35 -0700967 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800968 }
969
970 /**
971 * Creates a view representing a shortcut.
972 *
973 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800974 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800975 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700976 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800977 }
978
979 /**
980 * Creates a view representing a shortcut inflated from the specified resource.
981 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800982 * @param parent The group the shortcut belongs to.
983 * @param info The data structure describing the shortcut.
984 *
985 * @return A View inflated from layoutResId.
986 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700987 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -0800988 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
989 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800990 favorite.applyFromShortcutInfo(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800991 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700992 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800993 return favorite;
994 }
995
996 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -0700997 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800998 *
999 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001000 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001001 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001002 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001003 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
1004 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001005 return;
1006 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001007
Jon Mirandac476d6e2017-05-04 16:30:01 -07001008 int[] cellXY = mTmpAddItemCellCoordinates;
1009 CellLayout layout = getCellLayout(container, screenId);
1010
Sunny Goyal782f0c92017-01-19 10:27:54 -08001011 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001012 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001013 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
1014 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001015 }
1016
1017 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001018 // Legacy shortcuts are only supported for primary profile.
1019 info = Process.myUserHandle().equals(args.user)
1020 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001021
Sunny Goyalb57645f2017-03-20 13:57:28 -07001022 if (info == null) {
1023 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1024 return;
1025 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001026 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001027 // The app is trying to add a shortcut without sufficient permissions
1028 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1029 return;
1030 }
1031 }
1032
Jon Mirandac476d6e2017-05-04 16:30:01 -07001033 if (container < 0) {
1034 // Adding a shortcut to the Workspace.
1035 final View view = createShortcut(info);
1036 boolean foundCellSpan = false;
1037 // First we check if we already know the exact location where we want to add this item.
1038 if (cellX >= 0 && cellY >= 0) {
1039 cellXY[0] = cellX;
1040 cellXY[1] = cellY;
1041 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001042
Jon Mirandac476d6e2017-05-04 16:30:01 -07001043 // If appropriate, either create a folder or add to an existing folder
1044 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001045 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001046 return;
1047 }
1048 DragObject dragObject = new DragObject();
1049 dragObject.dragInfo = info;
1050 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1051 true)) {
1052 return;
1053 }
1054 } else {
1055 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1056 }
1057
1058 if (!foundCellSpan) {
1059 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001060 return;
1061 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001062
1063 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1064 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001065 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001066 // Adding a shortcut to a Folder.
Sunny Goyale29897f2017-07-20 10:09:42 -07001067 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001068 if (folderIcon != null) {
1069 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1070 folderInfo.add(info, args.rank, false);
1071 } else {
Sunny Goyale29897f2017-07-20 10:09:42 -07001072 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001073 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001074 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001075 }
1076
Sunny Goyale29897f2017-07-20 10:09:42 -07001077 public FolderIcon findFolderIcon(final long folderIconId) {
1078 return (FolderIcon) mWorkspace.getFirstMatch(new ItemOperator() {
1079 @Override
1080 public boolean evaluate(ItemInfo info, View view) {
1081 return info != null && info.id == folderIconId;
1082 }
1083 });
1084 }
1085
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001086 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001087 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001088 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001089 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001091 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001092 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1093
Adam Cohened66b2b2012-01-23 17:28:51 -08001094 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001095 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001096 }
Romain Guycbb89e42009-06-08 15:52:54 -07001097
Adam Cohen59400422014-03-05 18:07:04 -08001098 LauncherAppWidgetInfo launcherInfo;
1099 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001100 launcherInfo.spanX = itemInfo.spanX;
1101 launcherInfo.spanY = itemInfo.spanY;
1102 launcherInfo.minSpanX = itemInfo.minSpanX;
1103 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001104 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001105
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001106 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001107 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001108
Sunny Goyal2100c782016-08-22 16:00:03 -07001109 if (hostView == null) {
1110 // Perform actual inflation because we're live
1111 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001112 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001113 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301114 prepareAppWidget(hostView, launcherInfo);
1115 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001116 }
Romain Guycbb89e42009-06-08 15:52:54 -07001117
Sunny Goyald5462aa2016-11-22 16:52:39 +05301118 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001119 hostView.setTag(item);
1120 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001121 hostView.setFocusable(true);
1122 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001123 }
1124
Adam Cohended9f8d2010-11-03 13:25:16 -07001125 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1126 @Override
1127 public void onReceive(Context context, Intent intent) {
1128 final String action = intent.getAction();
1129 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001130 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001131 // processing a multi-step drop
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001132 if (mAppsView != null && mPendingRequestArgs == null) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001133 mStateManager.goToState(NORMAL);
Winson Chung785d2eb2011-04-14 16:08:02 -07001134 }
Jon Miranda7fda2852017-07-19 16:07:01 -07001135 mShouldFadeInScrim = true;
1136 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1137 // ACTION_USER_PRESENT is sent after onStart/onResume. This covers the case where
1138 // the user unlocked and the Launcher is not in the foreground.
1139 mShouldFadeInScrim = false;
Adam Cohended9f8d2010-11-03 13:25:16 -07001140 }
1141 }
1142 };
1143
Tony Wickham010d2552017-01-20 08:15:28 -08001144 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
Sunny Goyal29947f02017-12-18 13:49:44 -08001145 mWorkspace.updateIconBadges(updatedBadges);
1146 mAppsView.updateIconBadges(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001147
Sunny Goyal29947f02017-12-18 13:49:44 -08001148 PopupContainerWithArrow popup = PopupContainerWithArrow.getOpen(Launcher.this);
1149 if (popup != null) {
1150 popup.updateNotificationHeader(updatedBadges);
Tony Wickham010d2552017-01-20 08:15:28 -08001151 }
1152 }
1153
Adam Cohended9f8d2010-11-03 13:25:16 -07001154 @Override
1155 public void onAttachedToWindow() {
1156 super.onAttachedToWindow();
1157
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001158 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyalc86df472016-02-25 09:19:38 -08001159 if (mLauncherCallbacks != null) {
1160 mLauncherCallbacks.onAttachedToWindow();
1161 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001162 }
1163
1164 @Override
1165 public void onDetachedFromWindow() {
1166 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001167
1168 if (mLauncherCallbacks != null) {
1169 mLauncherCallbacks.onDetachedFromWindow();
1170 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001171 }
1172
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001173 public AllAppsTransitionController getAllAppsController() {
1174 return mAllAppsController;
1175 }
1176
Winson Chunga6945242014-01-08 14:04:34 -08001177 public DragLayer getDragLayer() {
1178 return mDragLayer;
1179 }
1180
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001181 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001182 return mAppsView;
1183 }
1184
Winson Chunga6945242014-01-08 14:04:34 -08001185 public Workspace getWorkspace() {
1186 return mWorkspace;
1187 }
1188
1189 public Hotseat getHotseat() {
1190 return mHotseat;
1191 }
1192
Winson Chung8ae41982017-11-10 11:42:13 -08001193 public <T extends ViewGroup> T getOverviewPanel() {
1194 return (T) mOverviewPanel;
Winson Chunga6945242014-01-08 14:04:34 -08001195 }
1196
Sunny Goyal47328fd2016-05-25 18:56:41 -07001197 public DropTargetBar getDropTargetBar() {
1198 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001199 }
1200
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001201 public LauncherAppWidgetHost getAppWidgetHost() {
1202 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001203 }
Romain Guycbb89e42009-06-08 15:52:54 -07001204
Winson Chunga9abd0e2010-10-27 17:18:37 -07001205 public LauncherModel getModel() {
1206 return mModel;
1207 }
1208
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001209 public ModelWriter getModelWriter() {
1210 return mModelWriter;
1211 }
1212
Adam Cohen79d90c52016-04-22 13:29:20 -07001213 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001214 return mSharedPrefs;
1215 }
1216
Jon Miranda6bed3502017-09-19 14:43:17 -07001217 public int getOrientation() { return mOrientation; }
1218
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001219 @Override
1220 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001221 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001222 super.onNewIntent(intent);
1223
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001224 boolean alreadyOnHome = hasWindowFocus() && ((intent.getFlags() &
Winson15f8b172015-08-19 11:02:39 -07001225 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1226 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001227
1228 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001229 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001230 && AbstractFloatingView.getTopOpenView(this) == null;
Winson15f8b172015-08-19 11:02:39 -07001231 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001232 boolean internalStateHandled = InternalStateHandler
1233 .handleNewIntent(this, intent, alreadyOnHome);
1234
Winson15f8b172015-08-19 11:02:39 -07001235 if (isActionMain) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001236 if (!internalStateHandled) {
1237 // Note: There should be at most one log per method call. This is enforced
1238 // implicitly by using if-else statements.
1239 UserEventDispatcher ued = getUserEventDispatcher();
1240 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
1241 if (topOpenView != null) {
1242 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
1243 } else if (alreadyOnHome) {
1244 Target target = newContainerTarget(mStateManager.getState().containerType);
1245 target.pageIndex = mWorkspace.getCurrentPage();
1246 ued.logActionCommand(Action.Command.HOME_INTENT, target);
1247 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001248
Sunny Goyald3864fa2017-11-14 15:06:36 -08001249 // In all these cases, only animate if we're already on home
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001250 AbstractFloatingView.closeAllOpenViews(this, isStarted());
Jon Mirandafeba90f2016-10-06 10:53:29 -07001251
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001252 mStateManager.goToState(NORMAL);
Sunny Goyald3864fa2017-11-14 15:06:36 -08001253
1254 // Reset the apps view
1255 if (!alreadyOnHome && mAppsView != null) {
1256 mAppsView.reset();
1257 }
1258
1259 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()) {
1260 mWorkspace.post(mWorkspace::moveToDefaultScreen);
1261 }
1262 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001263
1264 final View v = getWindow().peekDecorView();
1265 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001266 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001267 }
1268
Adam Cohen9211d422014-10-07 18:14:53 -07001269 if (mLauncherCallbacks != null) {
1270 mLauncherCallbacks.onHomeIntent();
1271 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001272 }
Winson15f8b172015-08-19 11:02:39 -07001273
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001274 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001275 }
1276
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001277 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001278 public void onRestoreInstanceState(Bundle state) {
1279 super.onRestoreInstanceState(state);
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001280 mWorkspace.restoreInstanceStateForChild(mSynchronouslyBoundPage);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001281 }
1282
1283 @Override
1284 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001285 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001286 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001287
Adam Cohen21cd0022013-10-09 18:57:02 -07001288 }
Sunny Goyalea609262017-10-25 15:47:38 -07001289 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001290
1291
1292 AbstractFloatingView widgets = AbstractFloatingView
1293 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1294 if (widgets != null) {
1295 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1296 widgets.saveHierarchyState(widgetsState);
1297 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1298 } else {
1299 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1300 }
1301
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001302 // We close any open folders and shortcut containers since they will not be re-opened,
1303 // and we need to make sure this state is reflected.
1304 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001305
Sunny Goyal2100c782016-08-22 16:00:03 -07001306 if (mPendingRequestArgs != null) {
1307 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1308 }
1309 if (mPendingActivityResult != null) {
1310 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001311 }
1312
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001313 super.onSaveInstanceState(outState);
1314
Adam Cohen9211d422014-10-07 18:14:53 -07001315 if (mLauncherCallbacks != null) {
1316 mLauncherCallbacks.onSaveInstanceState(outState);
1317 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001318 }
1319
1320 @Override
1321 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001322 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001323
Sunny Goyal5cc7bbd2017-06-12 09:50:39 -07001324 unregisterReceiver(mReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001325 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001326
Winson Chungcd2b0142011-06-08 16:02:26 -07001327 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001328 // It's possible to receive onDestroy after a new Launcher activity has
1329 // been created. In this case, don't interfere with the new Launcher.
1330 if (mModel.isCurrentCallbacks(this)) {
1331 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001332 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001333 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001334
Sunny Goyal745bad92016-05-02 10:54:12 -07001335 if (mRotationPrefChangeHandler != null) {
1336 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1337 }
1338
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001339 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001340 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001341 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001342 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001343 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001344
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001345 TextKeyListener.getInstance().release();
Mario Bertschler27288382017-05-24 15:35:09 -07001346 WallpaperColorInfo.getInstance(this).setOnThemeChangeListener(null);
1347
Michael Jurka2ecf9952012-06-18 12:52:28 -07001348 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001349
Tony2917a8b2017-07-31 23:29:42 -07001350 clearPendingBinds();
1351
Adam Cohen9211d422014-10-07 18:14:53 -07001352 if (mLauncherCallbacks != null) {
1353 mLauncherCallbacks.onDestroy();
1354 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001355 }
1356
Sunny Goyalae502842016-06-17 08:43:56 -07001357 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1358 return mAccessibilityDelegate;
1359 }
1360
Adam Cohena9cf38f2011-05-02 15:36:58 -07001361 public DragController getDragController() {
1362 return mDragController;
1363 }
1364
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001365 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001366 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001367 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001368 }
1369
1370 @Override
1371 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1372 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001373 try {
1374 super.startIntentSenderForResult(intent, requestCode,
1375 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1376 } catch (IntentSender.SendIntentException e) {
1377 throw new ActivityNotFoundException();
1378 }
1379 }
1380
Winson Chung70d72102011-08-12 11:24:35 -07001381 /**
1382 * Indicates that we want global search for this activity by setting the globalSearch
1383 * argument for {@link #startSearch} to true.
1384 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001385 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001386 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001387 Bundle appSearchData, boolean globalSearch) {
1388 if (appSearchData == null) {
1389 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001390 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001391 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001392
Sunny Goyal6f28e712016-09-13 14:19:29 -07001393 if (mLauncherCallbacks == null ||
1394 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1395 // Starting search from the callbacks failed. Start the default global search.
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001396 super.startSearch(initialQuery, selectInitialQuery, appSearchData, true);
Ian Parkinson047036e2014-09-01 15:40:53 +01001397 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001398
1399 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001400 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001401 }
1402
Joe Onorato9c1289c2009-08-17 11:03:03 -04001403 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001404 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001405 }
1406
Adam Cohen517a7f52014-03-01 12:12:59 -08001407 public boolean isWorkspaceLoading() {
1408 return mWorkspaceLoading;
1409 }
1410
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001411 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001412 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001413 }
1414
Sunny Goyal04a324a2017-01-20 21:08:59 -08001415 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001416 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001417 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001418
Sunny Goyal2100c782016-08-22 16:00:03 -07001419 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001420 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001421 if (LOGD) {
1422 Log.d(TAG, "Adding widget from drop");
1423 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001424 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001425 }
1426
Sunny Goyal2100c782016-08-22 16:00:03 -07001427 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001428 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1429 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1430 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001431
Adam Cohenad4e15c2013-10-17 16:21:35 -07001432 Runnable onComplete = new Runnable() {
1433 @Override
1434 public void run() {
1435 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001436 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001437 }
1438 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001439 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001440 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001441 }
1442 }
Romain Guycbb89e42009-06-08 15:52:54 -07001443
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001444 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1445 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001446 info.container = container;
1447 info.screenId = screenId;
1448 if (cell != null) {
1449 info.cellX = cell[0];
1450 info.cellY = cell[1];
1451 }
1452 info.spanX = spanX;
1453 info.spanY = spanY;
1454
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001455 switch (info.itemType) {
1456 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1457 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001458 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001459 break;
1460 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001461 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001462 break;
1463 default:
1464 throw new IllegalStateException("Unknown item type: " + info.itemType);
1465 }
1466 }
1467
Adam Cohenfbba09b2011-07-18 21:43:05 -07001468 /**
1469 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001470 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001471 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001472 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1473 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001474 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1475 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1476 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001477 }
1478
Adam Cohenfbba09b2011-07-18 21:43:05 -07001479 /**
1480 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001481 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001482 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001483 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001484 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001485 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001486 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001487 // In the case where we've prebound the widget, we remove it from the DragLayer
1488 if (LOGD) {
1489 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1490 }
1491 getDragLayer().removeView(hostView);
1492
Adam Cohened66b2b2012-01-23 17:28:51 -08001493 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001494 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001495
1496 // Clear the boundWidget so that it doesn't get destroyed.
1497 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001498 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001499 // In this case, we either need to start an activity to get permission to bind
1500 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001501 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1502 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1503 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1504 this, info.componentName);
1505 } else {
1506 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1507 }
Adam Cohendd70d662012-10-04 16:53:44 -07001508 Bundle options = info.bindOptions;
1509
Sunny Goyalffe83f12014-08-14 17:39:34 -07001510 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1511 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001512 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001513 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001514 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001515 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001516 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001517 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001518 }
1519
Adam Cohendcd297f2013-06-18 13:13:40 -07001520 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001521 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001522 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001523 folderInfo.title = getText(R.string.folder_name);
1524
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001525 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001526 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001527
1528 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001529 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301530 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001531 // Force measure the new folder icon
1532 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1533 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001534 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001535 }
Romain Guycbb89e42009-06-08 15:52:54 -07001536
Winsonc0b52fe2015-09-09 16:38:15 -07001537 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001538 * Unbinds the view for the specified item, and removes the item and all its children.
1539 *
1540 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001541 * @param itemInfo the {@link ItemInfo} for this view.
1542 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001543 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001544 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001545 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001546 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001547 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1548 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001549 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001550 } else {
1551 mWorkspace.removeWorkspaceItem(v);
1552 }
Winsonc0b52fe2015-09-09 16:38:15 -07001553 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001554 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001555 }
1556 } else if (itemInfo instanceof FolderInfo) {
1557 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001558 if (v instanceof FolderIcon) {
1559 ((FolderIcon) v).removeListeners();
1560 }
Winsonc0b52fe2015-09-09 16:38:15 -07001561 mWorkspace.removeWorkspaceItem(v);
1562 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001563 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001564 }
1565 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1566 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001567 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001568 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07001569 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001570 }
1571 } else {
1572 return false;
1573 }
1574 return true;
1575 }
1576
1577 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07001578 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07001579 */
Sunny Goyal56c73602015-09-25 12:55:01 -07001580 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07001581 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07001582 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07001583 // Deleting an app widget ID is a void call but writes to disk before returning
1584 // to the caller...
1585 new AsyncTask<Void, Void, Void>() {
1586 public Void doInBackground(Void ... args) {
1587 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
1588 return null;
1589 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07001590 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07001591 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001592 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001593 }
1594
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001595 @Override
1596 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001597 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001598 }
1599
Joe Onorato88ec0992009-11-19 13:16:06 -08001600 @Override
1601 public void onBackPressed() {
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08001602 if (finishAutoCancelActionMode()) {
1603 return;
1604 }
Adam Cohen9211d422014-10-07 18:14:53 -07001605 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1606 return;
1607 }
1608
Sunny Goyal45478022015-06-08 16:52:41 -07001609 if (mDragController.isDragging()) {
1610 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001611 return;
1612 }
1613
Jon Mirandafeba90f2016-10-06 10:53:29 -07001614 // Note: There should be at most one log per method call. This is enforced implicitly
1615 // by using if-else statements.
1616 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001617 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
1618 if (topView != null) {
Sunny Goyal37920962017-09-28 13:43:24 -07001619 topView.onBackPressed();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001620 } else if (!isInState(NORMAL)) {
Sunny Goyal49bcf342018-01-11 13:59:53 -08001621 LauncherState lastState = mStateManager.getLastState();
Sunny Goyalea609262017-10-25 15:47:38 -07001622 ued.logActionCommand(Action.Command.BACK, mStateManager.getState().containerType);
Sunny Goyal49bcf342018-01-11 13:59:53 -08001623 mStateManager.goToState(lastState);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001624 } else {
1625 // Back button is a no-op here, but give at least some feedback for the button press
1626 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001627 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001628 }
1629
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001630 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001631 * Launches the intent referred by the clicked shortcut.
1632 *
1633 * @param v The view representing the clicked shortcut.
1634 */
1635 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001636 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1637 // view has detached (it's possible for this to happen if the view is removed mid touch).
1638 if (v.getWindowToken() == null) {
1639 return;
1640 }
1641
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001642 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001643 return;
1644 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001645
Adam Cohen1697b792013-09-17 19:08:21 -07001646 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001647 if (isInState(OVERVIEW)) {
Jon Miranda2e61fba2017-02-24 09:12:59 -08001648 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1649 LauncherLogProto.Action.Direction.NONE,
1650 LauncherLogProto.ContainerType.OVERVIEW, mWorkspace.getCurrentPage());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001651 mStateManager.goToState(NORMAL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001652 }
1653 return;
1654 }
1655
1656 if (v instanceof CellLayout) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001657 if (isInState(OVERVIEW)) {
Jon Mirandac6cf4932017-02-07 17:12:36 -08001658 int page = mWorkspace.indexOfChild(v);
1659 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1660 LauncherLogProto.Action.Direction.NONE,
1661 LauncherLogProto.ContainerType.OVERVIEW, page);
1662 mWorkspace.snapToPageFromOverView(page);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001663 mStateManager.goToState(NORMAL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001664 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07001665 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001666 }
1667
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001668 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001669 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001670 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001671 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001672 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001673 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001674 }
Sunny Goyal508da152014-08-14 10:53:27 -07001675 } else if (tag instanceof AppInfo) {
1676 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07001677 } else if (tag instanceof LauncherAppWidgetInfo) {
1678 if (v instanceof PendingAppWidgetHostView) {
1679 onClickPendingWidget((PendingAppWidgetHostView) v);
1680 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001681 }
1682 }
1683
Sunny Goyal70660032015-05-14 00:07:08 -07001684 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07001685 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07001686 return false;
1687 }
1688
Michael Jurkaaf442092010-06-10 17:01:57 -07001689 /**
Sunny Goyalff572272014-07-23 13:58:07 -07001690 * Event handler for the app widget view which has not fully restored.
1691 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001692 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07001693 if (mIsSafeModeEnabled) {
1694 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
1695 return;
1696 }
1697
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001698 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07001699 if (v.isReadyForClickSetup()) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001700 LauncherAppWidgetProviderInfo appWidgetInfo =
1701 mAppWidgetManager.findProvider(info.providerName, info.user);
1702 if (appWidgetInfo == null) {
1703 return;
1704 }
1705 WidgetAddFlowHandler addFlowHandler = new WidgetAddFlowHandler(appWidgetInfo);
1706
Sunny Goyald478c832016-04-01 12:04:16 -07001707 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
1708 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
1709 // This should not happen, as we make sure that an Id is allocated during bind.
1710 return;
1711 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001712 addFlowHandler.startBindFlow(this, info.appWidgetId, info,
1713 REQUEST_BIND_PENDING_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -07001714 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001715 addFlowHandler.startConfigActivity(this, info, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07001716 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001717 } else {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001718 final String packageName = info.providerName.getPackageName();
Sunny Goyale6e72002017-01-12 16:55:36 -08001719 onClickPendingAppItem(v, packageName, info.installProgress >= 0);
Sunny Goyalff572272014-07-23 13:58:07 -07001720 }
1721 }
1722
Sunny Goyale6e72002017-01-12 16:55:36 -08001723 private void onClickPendingAppItem(final View v, final String packageName,
1724 boolean downloadStarted) {
1725 if (downloadStarted) {
1726 // If the download has started, simply direct to the market app.
1727 startMarketIntentForPackage(v, packageName);
1728 return;
1729 }
Sunny Goyale03b8122014-10-08 09:55:24 -07001730 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001731 .setTitle(R.string.abandoned_promises_title)
1732 .setMessage(R.string.abandoned_promise_explanation)
Sunny Goyale6e72002017-01-12 16:55:36 -08001733 .setPositiveButton(R.string.abandoned_search, new DialogInterface.OnClickListener() {
1734 @Override
1735 public void onClick(DialogInterface dialogInterface, int i) {
1736 startMarketIntentForPackage(v, packageName);
1737 }
1738 })
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001739 .setNeutralButton(R.string.abandoned_clean_this,
1740 new DialogInterface.OnClickListener() {
1741 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001742 final UserHandle user = Process.myUserHandle();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001743 mWorkspace.removeAbandonedPromise(packageName, user);
1744 }
1745 })
1746 .create().show();
Sunny Goyale6e72002017-01-12 16:55:36 -08001747 }
1748
1749 private void startMarketIntentForPackage(View v, String packageName) {
1750 ItemInfo item = (ItemInfo) v.getTag();
Sunny Goyal67e75e22018-01-22 15:14:00 -08001751 Intent intent = new PackageManagerHelper(v.getContext()).getMarketIntent(packageName);
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001752 startActivitySafely(v, intent, item);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001753 }
1754
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001755 /**
1756 * Event handler for an app shortcut click.
1757 *
1758 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
1759 */
Chris Wren40c5ed32014-06-24 18:24:23 -04001760 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001761 if (LOGD) Log.d(TAG, "onClickAppShortcut");
1762 Object tag = v.getTag();
1763 if (!(tag instanceof ShortcutInfo)) {
1764 throw new IllegalArgumentException("Input must be a Shortcut");
1765 }
1766
1767 // Open shortcut
1768 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001769
Sunny Goyal076839c2017-10-30 13:52:20 -07001770 if (shortcut.isDisabled()) {
Tony Makf6308652017-12-02 16:56:22 +00001771 final int disabledFlags = shortcut.runtimeStatusFlags & ShortcutInfo.FLAG_DISABLED_MASK;
1772 if ((disabledFlags &
Sunny Goyal076839c2017-10-30 13:52:20 -07001773 ~FLAG_DISABLED_SUSPENDED &
1774 ~FLAG_DISABLED_QUIET_USER) == 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001775 // If the app is only disabled because of the above flags, launch activity anyway.
1776 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00001777 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07001778 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
1779 // Use a message specific to this shortcut, if it has one.
1780 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
1781 return;
1782 }
1783 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00001784 int error = R.string.activity_not_available;
Sunny Goyal076839c2017-10-30 13:52:20 -07001785 if ((shortcut.runtimeStatusFlags & FLAG_DISABLED_SAFEMODE) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00001786 error = R.string.safemode_shortcut_error;
Sunny Goyal076839c2017-10-30 13:52:20 -07001787 } else if ((shortcut.runtimeStatusFlags & FLAG_DISABLED_BY_PUBLISHER) != 0 ||
1788 (shortcut.runtimeStatusFlags & FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07001789 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00001790 }
1791 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
1792 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001793 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001794 }
1795
Chris Wren40c5ed32014-06-24 18:24:23 -04001796 // Check for abandoned promise
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -07001797 if ((v instanceof BubbleTextView) && shortcut.hasPromiseIconUi()) {
Sunny Goyale6e72002017-01-12 16:55:36 -08001798 String packageName = shortcut.intent.getComponent() != null ?
1799 shortcut.intent.getComponent().getPackageName() : shortcut.intent.getPackage();
1800 if (!TextUtils.isEmpty(packageName)) {
1801 onClickPendingAppItem(v, packageName,
1802 shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE));
1803 return;
1804 }
Chris Wren40c5ed32014-06-24 18:24:23 -04001805 }
1806
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001807 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07001808 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04001809 }
1810
Sunny Goyala7ce1662016-05-31 15:01:35 -07001811 private void startAppShortcutOrInfoActivity(View v) {
1812 ItemInfo item = (ItemInfo) v.getTag();
Mario Bertschler08ffaae2017-03-20 11:30:27 -07001813 Intent intent;
1814 if (item instanceof PromiseAppInfo) {
1815 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) item;
Sunny Goyal67e75e22018-01-22 15:14:00 -08001816 intent = promiseAppInfo.getMarketIntent(this);
Mario Bertschler08ffaae2017-03-20 11:30:27 -07001817 } else {
1818 intent = item.getIntent();
1819 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07001820 if (intent == null) {
1821 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07001822 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001823 startActivitySafely(v, intent, item);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001824 }
1825
1826 /**
1827 * Event handler for a folder icon click.
1828 *
1829 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
1830 */
1831 protected void onClickFolderIcon(View v) {
1832 if (LOGD) Log.d(TAG, "onClickFolder");
1833 if (!(v instanceof FolderIcon)){
1834 throw new IllegalArgumentException("Input must be a FolderIcon");
1835 }
1836
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001837 Folder folder = ((FolderIcon) v).getFolder();
1838 if (!folder.isOpen() && !folder.isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001839 // Open the requested folder
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001840 folder.animateOpen();
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001841 }
Michael Jurka5130e402011-10-13 04:55:35 -07001842 }
1843
Sandeep Siddharthad8058372014-01-28 10:41:15 -08001844 /**
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001845 * Event handler for the wallpaper picker button that appears after a long press
1846 * on the home screen.
1847 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001848 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07001849 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07001850 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
1851 return;
1852 }
1853
Tony Wickham785f7a52015-08-31 17:28:32 -07001854 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
1855 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal2100c782016-08-22 16:00:03 -07001856 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001857 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001858 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001859
1860 String pickerPackage = getString(R.string.wallpaper_picker_package);
Tony85efb4b2017-06-02 13:06:10 -07001861 boolean hasTargetPackage = !TextUtils.isEmpty(pickerPackage);
1862 if (hasTargetPackage) {
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001863 intent.setPackage(pickerPackage);
1864 }
1865
Sunny Goyala5ace712017-11-15 17:12:51 -08001866 final Bundle launchOptions;
1867 if (v != null) {
1868 intent.setSourceBounds(getViewBounds(v));
1869 // If there is no target package, use the default intent chooser animation
Jon Miranda17940f02018-01-18 14:46:34 -08001870 launchOptions = hasTargetPackage
1871 ? getActivityLaunchOptions(v, isInMultiWindowModeCompat())
1872 : null;
Sunny Goyala5ace712017-11-15 17:12:51 -08001873 } else {
1874 launchOptions = null;
1875 }
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001876 try {
Sunny Goyala5ace712017-11-15 17:12:51 -08001877 startActivityForResult(intent, REQUEST_PICK_WALLPAPER, launchOptions);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001878 } catch (ActivityNotFoundException e) {
1879 setWaitingForResult(null);
1880 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1881 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001882 }
1883
Sunny Goyala7ce1662016-05-31 15:01:35 -07001884 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001885 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001886 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
1887 try {
1888 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
1889 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
1890 // is enabled by default on NYC.
1891 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
1892 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001893
1894 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
1895 String id = ((ShortcutInfo) info).getDeepShortcutId();
1896 String packageName = intent.getPackage();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301897 DeepShortcutManager.getInstance(this).startShortcut(
Sunny Goyal9994b2b2016-06-23 14:17:24 -07001898 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001899 } else {
1900 // Could be launching some bookkeeping activity
1901 startActivity(intent, optsBundle);
1902 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07001903 } finally {
1904 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01001905 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001906 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001907 // Due to legacy reasons, direct call shortcuts require Launchers to have the
1908 // corresponding permission. Show the appropriate permission prompt if that
1909 // is the case.
1910 if (intent.getComponent() == null
1911 && Intent.ACTION_CALL.equals(intent.getAction())
1912 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
1913 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001914
1915 setWaitingForResult(PendingRequestArgs
1916 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07001917 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
1918 REQUEST_PERMISSION_CALL_PHONE);
1919 } else {
1920 // No idea why this was thrown.
1921 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07001922 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001923 }
1924 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001925
Sunny Goyalfe770c92016-09-27 14:38:58 -07001926 @TargetApi(Build.VERSION_CODES.M)
Jon Miranda17940f02018-01-18 14:46:34 -08001927 public Bundle getActivityLaunchOptions(View v, boolean useDefaultLaunchOptions) {
1928 return useDefaultLaunchOptions
Jon Miranda54441f52018-01-24 15:38:25 -08001929 ? mAppTransitionManager.getDefaultActivityLaunchOptions(this, v)
1930 : mAppTransitionManager.getActivityLaunchOptions(this, v);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001931 }
1932
Tonye3c59252017-05-02 21:32:27 -07001933 public Rect getViewBounds(View v) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001934 int[] pos = new int[2];
1935 v.getLocationOnScreen(pos);
1936 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
1937 }
1938
Sunny Goyala7ce1662016-05-31 15:01:35 -07001939 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07001940 mAppLaunchSuccess = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001941 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
1942 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07001943 return mAppLaunchSuccess;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001944 }
Jon Miranda17940f02018-01-18 14:46:34 -08001945
1946 boolean isShortcut = Utilities.ATLEAST_MARSHMALLOW
1947 && (item instanceof ShortcutInfo)
1948 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
1949 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
1950 && !((ShortcutInfo) item).isPromise();
1951
Sunny Goyala7ce1662016-05-31 15:01:35 -07001952 // Only launch using the new animation if the shortcut has not opted out (this is a
1953 // private contract between launcher and may be ignored in the future).
1954 boolean useLaunchAnimation = (v != null) &&
1955 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Jon Miranda17940f02018-01-18 14:46:34 -08001956 Bundle optsBundle = useLaunchAnimation
1957 ? getActivityLaunchOptions(v, isShortcut || isInMultiWindowModeCompat())
1958 : null;
Sunny Goyala7ce1662016-05-31 15:01:35 -07001959
Sunny Goyal24bb66a2017-03-21 15:12:01 -07001960 UserHandle user = item == null ? null : item.user;
Sunny Goyala7ce1662016-05-31 15:01:35 -07001961
1962 // Prepare intent
1963 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1964 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001965 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07001966 }
1967 try {
Jon Miranda17940f02018-01-18 14:46:34 -08001968 if (isShortcut) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001969 // Shortcuts need some special checks due to legacy reasons.
1970 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001971 } else if (user == null || user.equals(Process.myUserHandle())) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001972 // Could be launching some bookkeeping activity
1973 startActivity(intent, optsBundle);
1974 } else {
1975 LauncherAppsCompat.getInstance(this).startActivityForProfile(
1976 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
1977 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001978
1979 if (v instanceof BubbleTextView) {
1980 // This is set to the view that launched the activity that navigated the user away
1981 // from launcher. Since there is no callback for when the activity has finished
1982 // launching, enable the press state and keep this reference to reset the press
1983 // state when we return to launcher.
1984 BubbleTextView btv = (BubbleTextView) v;
1985 btv.setStayPressed(true);
1986 setOnResumeCallback(btv);
1987 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07001988 mAppLaunchSuccess = true;
1989 getUserEventDispatcher().logAppLaunch(v, intent); // TODO for discovered apps b/35802115
Sunny Goyala7ce1662016-05-31 15:01:35 -07001990 } catch (ActivityNotFoundException|SecurityException e) {
1991 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1992 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
1993 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07001994 return mAppLaunchSuccess;
Michael Jurka86a720e2012-05-09 11:23:23 -07001995 }
1996
Tony Wickhamdadb3042016-02-24 11:07:00 -08001997 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07001998 public boolean dispatchTouchEvent(MotionEvent ev) {
Sunny Goyal02424b22018-01-19 11:24:32 -08001999 mLastDispatchTouchEvent.set(ev.getX(), ev.getY());
Jon Miranda379198e2016-09-23 08:54:40 -07002000 return super.dispatchTouchEvent(ev);
2001 }
2002
2003 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002004 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002005 if (!isDraggingEnabled()) return false;
2006 if (isWorkspaceLocked()) return false;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002007 if (!isInState(NORMAL) && !isInState(OVERVIEW)) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002008
Jon Miranda51f037d2016-11-07 08:37:20 -08002009 boolean ignoreLongPressToOverview =
Sunny Goyal02424b22018-01-19 11:24:32 -08002010 mDeviceProfile.shouldIgnoreLongPressToOverview(mLastDispatchTouchEvent.x);
Jon Miranda379198e2016-09-23 08:54:40 -07002011
Adam Cohen1697b792013-09-17 19:08:21 -07002012 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002013 if (!isInState(OVERVIEW)) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002014 if (!mWorkspace.isTouchActive() && !ignoreLongPressToOverview) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302015 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2016 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002017 mWorkspace.getCurrentPage());
Sunny Goyal02424b22018-01-19 11:24:32 -08002018 UiFactory.onWorkspaceLongPress(this, mLastDispatchTouchEvent);
Adam Cohen93c97562013-09-26 13:48:01 -07002019 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2020 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2021 return true;
2022 } else {
2023 return false;
2024 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002025 } else {
2026 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002027 }
Adam Cohen1697b792013-09-17 19:08:21 -07002028 }
2029
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002030 CellLayout.CellInfo longClickCellInfo = null;
2031 View itemUnderLongClick = null;
2032 if (v.getTag() instanceof ItemInfo) {
2033 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002034 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002035 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07002036 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002037 }
2038
Winson Chung3d503fb2011-07-13 17:25:49 -07002039 // The hotseat touch handling does not go through Workspace, and we always allow long press
2040 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002041 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002042 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002043 // User long pressed on empty space
Sunny Goyalc99cb172017-10-19 16:15:09 -07002044 if (mWorkspace.isPageRearrangeEnabled()) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002045 mWorkspace.startReordering(v);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302046 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2047 Action.Direction.NONE, ContainerType.OVERVIEW);
Adam Cohend3ecce92013-09-16 14:58:00 -07002048 } else {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002049 if (ignoreLongPressToOverview) {
Jon Miranda379198e2016-09-23 08:54:40 -07002050 return false;
2051 }
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302052 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2053 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002054 mWorkspace.getCurrentPage());
Sunny Goyal02424b22018-01-19 11:24:32 -08002055 UiFactory.onWorkspaceLongPress(this, mLastDispatchTouchEvent);
Adam Cohendedbd962013-07-11 14:21:49 -07002056 }
Jon Miranda379198e2016-09-23 08:54:40 -07002057 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2058 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002059 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07002060 final boolean isAllAppsButton =
2061 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
2062 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
2063 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05002064 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002065 // User long pressed on an item
Tony Wickham10236d62016-09-28 12:49:25 -07002066 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002067 }
2068 }
2069 }
2070 return true;
2071 }
2072
Winson Chung3d503fb2011-07-13 17:25:49 -07002073 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08002074 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07002075 return mHotseat != null && layout != null &&
2076 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2077 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002078
Winson Chung3d503fb2011-07-13 17:25:49 -07002079 /**
2080 * Returns the CellLayout of the specified container at the specified screen.
2081 */
Sunny Goyal92820592015-03-02 11:31:35 -08002082 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002083 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2084 if (mHotseat != null) {
2085 return mHotseat.getLayout();
2086 } else {
2087 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002088 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002089 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002090 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002091 }
2092 }
2093
Michael Jurkae326f182011-11-21 14:05:46 -08002094 @Override
2095 public void onTrimMemory(int level) {
2096 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07002097 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2098 // The widget preview db can result in holding onto over
2099 // 3MB of memory for caching which isn't necessary.
2100 SQLiteDatabase.releaseMemory();
2101
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08002102 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07002103 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08002104 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01002105 if (mLauncherCallbacks != null) {
2106 mLauncherCallbacks.onTrimMemory(level);
2107 }
Michael Jurkae326f182011-11-21 14:05:46 -08002108 }
2109
Michael Jurkad7c28052012-04-27 15:43:36 -07002110 @Override
2111 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07002112 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07002113 final List<CharSequence> text = event.getText();
2114 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07002115 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002116 // TODO: When can workspace be null?
2117 text.add(mWorkspace == null
2118 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07002119 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07002120 return result;
2121 }
2122
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002123 public void setOnResumeCallback(OnResumeCallback callback) {
2124 if (mOnResumeCallback != null) {
2125 mOnResumeCallback.onLauncherResume();
2126 }
2127 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07002128 }
2129
Michael Jurka7607c2f2013-04-03 14:33:19 -07002130 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002131 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002132 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002133 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002134 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002135 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002136 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002137 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07002138 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07002139 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002140 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002141
Joe Onorato9c1289c2009-08-17 11:03:03 -04002142 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07002143 * Clear any pending bind callbacks. This is called when is loader is planning to
2144 * perform a full rebind from scratch.
2145 */
2146 @Override
2147 public void clearPendingBinds() {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002148 if (mPendingExecutor != null) {
2149 mPendingExecutor.markCompleted();
2150 mPendingExecutor = null;
2151 }
2152 }
2153
2154 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002155 * Refreshes the shortcuts shown on the workspace.
2156 *
2157 * Implementation of the method from LauncherModel.Callbacks.
2158 */
2159 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002160 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002161 // Floating panels (except the full widget sheet) are associated with individual icons. If
2162 // we are starting a fresh bind, close all such panels as all the icons are about
2163 // to go away.
2164 AbstractFloatingView.closeOpenViews(this, true,
Sunny Goyal7ede6112017-12-05 15:11:21 -08002165 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_REBIND_SAFE);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08002166
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002167 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08002168
Winson Chungd64d1762013-08-20 14:37:16 -07002169 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07002170 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07002171 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07002172
Winson Chung3d503fb2011-07-13 17:25:49 -07002173 if (mHotseat != null) {
2174 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002175 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002176 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002177 }
2178
Adam Cohendcd297f2013-06-18 13:13:40 -07002179 @Override
2180 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002181 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002182 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
2183 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002184 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
2185 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002186 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002187 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
2188 // If there are no screens, we need to have an empty screen
2189 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07002190 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002191 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07002192
Winsonc7d2e832016-07-28 12:24:55 -07002193 // After we have added all the screens, if the wallpaper was locked to the default state,
2194 // then notify to indicate that it can be released and a proper wallpaper offset can be
2195 // computed before the next layout
2196 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07002197 }
2198
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002199 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002200 int count = orderedScreenIds.size();
2201 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002202 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002203 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002204 // No need to bind the first screen, as its always bound.
2205 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
2206 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002207 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002208 }
2209
Sunny Goyalb23980c2017-08-17 07:45:25 -07002210 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002211 public void bindAppsAdded(ArrayList<Long> newScreens, ArrayList<ItemInfo> addNotAnimated,
2212 ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07002213 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08002214 if (newScreens != null) {
2215 bindAddScreens(newScreens);
2216 }
Winson Chungd64d1762013-08-20 14:37:16 -07002217
2218 // We add the items without animation on non-visible pages, and with
2219 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08002220 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002221 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002222 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002223 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002224 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07002225 }
Winson Chungc58497e2013-09-03 17:48:37 -07002226
Winson Chung87412982013-10-03 18:34:14 -07002227 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07002228 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002229 }
2230
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002231 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002232 * Bind the items start-end from the list.
2233 *
2234 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002235 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07002236 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07002237 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002238 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07002239 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002240 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07002241 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07002242 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002243 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07002244 int end = items.size();
2245 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002246 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002247
2248 // Short circuit if we are loading dock items for a configuration which has no dock
2249 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2250 mHotseat == null) {
2251 continue;
2252 }
2253
Sunny Goyal639e9062015-08-19 19:17:06 -07002254 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002255 switch (item.itemType) {
2256 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2257 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08002258 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07002259 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07002260 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002261 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002262 }
2263 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07002264 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002265 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002266 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002267 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002268 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07002269 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2270 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07002271 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002272 if (view == null) {
2273 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002274 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08002275 break;
2276 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002277 default:
2278 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002279 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002280
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002281 /*
2282 * Remove colliding items.
2283 */
2284 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
2285 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
2286 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
2287 View v = cl.getChildAt(item.cellX, item.cellY);
2288 Object tag = v.getTag();
2289 String desc = "Collision while binding workspace item: " + item
2290 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08002291 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002292 throw (new RuntimeException(desc));
2293 } else {
2294 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002295 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002296 continue;
2297 }
2298 }
2299 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302300 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07002301 if (animateIcons) {
2302 // Animate all the applications up now
2303 view.setAlpha(0f);
2304 view.setScaleX(0f);
2305 view.setScaleY(0f);
2306 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08002307 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07002308 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002309 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07002310
Winson Chung997a9232013-07-24 15:33:46 -07002311 if (animateIcons) {
2312 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08002313 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07002314 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08002315 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07002316 final Runnable startBounceAnimRunnable = new Runnable() {
2317 public void run() {
2318 anim.playTogether(bounceAnims);
2319 anim.start();
2320 }
2321 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08002322 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07002323 // We post the animation slightly delayed to prevent slowdowns
2324 // when we are loading right after we return to launcher.
2325 mWorkspace.postDelayed(new Runnable() {
2326 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00002327 if (mWorkspace != null) {
2328 mWorkspace.snapToPage(newScreenIndex);
2329 mWorkspace.postDelayed(startBounceAnimRunnable,
2330 NEW_APPS_ANIMATION_DELAY);
2331 }
Winson Chung94d67682013-09-25 16:29:40 -07002332 }
2333 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07002334 } else {
2335 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07002336 }
2337 }
Winson Chung64359a52013-07-08 17:17:08 -07002338 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002339 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002340 }
2341
Joe Onorato9c1289c2009-08-17 11:03:03 -04002342 /**
2343 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002344 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07002345 public void bindAppWidget(LauncherAppWidgetInfo item) {
2346 View view = inflateAppWidget(item);
2347 if (view != null) {
2348 mWorkspace.addInScreen(view, item);
2349 mWorkspace.requestLayout();
2350 }
2351 }
2352
2353 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002354 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302355 PendingAppWidgetHostView view =
2356 new PendingAppWidgetHostView(this, item, mIconCache, true);
2357 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002358 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002359 }
2360
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002361 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002362
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002363 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08002364
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002365 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2366 // If the provider is not ready, bind as a pending widget.
2367 appWidgetInfo = null;
2368 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2369 // The widget id is not valid. Try to find the widget based on the provider info.
2370 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
2371 } else {
2372 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
2373 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002374
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002375 // If the provider is ready, but the width is not yet restored, try to restore it.
2376 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
2377 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07002378 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002379 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
2380 + " belongs to component " + item.providerName
2381 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002382 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002383 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002384 }
Sunny Goyalff572272014-07-23 13:58:07 -07002385
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002386 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002387 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07002388 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2389 // Id has not been allocated yet. Allocate a new id.
2390 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
2391 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07002392
Sunny Goyald478c832016-04-01 12:04:16 -07002393 // Also try to bind the widget. If the bind fails, the user will be shown
2394 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08002395 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07002396 pendingInfo.spanX = item.spanX;
2397 pendingInfo.spanY = item.spanY;
2398 pendingInfo.minSpanX = item.minSpanX;
2399 pendingInfo.minSpanY = item.minSpanY;
2400 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07002401
2402 boolean isDirectConfig =
2403 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2404 if (isDirectConfig && item.bindOptions != null) {
2405 Bundle newOptions = item.bindOptions.getExtras();
2406 if (options != null) {
2407 newOptions.putAll(options);
2408 }
2409 options = newOptions;
2410 }
Sunny Goyald478c832016-04-01 12:04:16 -07002411 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2412 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002413
Sunny Goyal86df1382016-08-10 15:03:22 -07002414 // We tried to bind once. If we were not able to bind, we would need to
2415 // go through the permission dialog, which means we cannot skip the config
2416 // activity.
2417 item.bindOptions = null;
2418 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2419
Sunny Goyald478c832016-04-01 12:04:16 -07002420 // Bind succeeded
2421 if (success) {
2422 // If the widget has a configure activity, it is still needs to set it up,
2423 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002424 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002425 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2426 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002427 }
Sunny Goyald478c832016-04-01 12:04:16 -07002428
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002429 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002430 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002431 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002432 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002433 // The widget was marked as UI not ready, but there is no configure activity to
2434 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002435 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002436 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002437 }
Sunny Goyalff572272014-07-23 13:58:07 -07002438 }
2439
Sunny Goyald5462aa2016-11-22 16:52:39 +05302440 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002441 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002442 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002443 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002444 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07002445 deleteWidgetInfo(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002446 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002447 }
2448
Sunny Goyal233ee962015-08-03 13:05:01 -07002449 item.minSpanX = appWidgetInfo.minSpanX;
2450 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302451 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002452 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302453 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002454 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302455 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002456
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002457 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002458 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002459 }
2460
Sunny Goyalff572272014-07-23 13:58:07 -07002461 /**
2462 * Restores a pending widget.
2463 *
2464 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002465 */
Sunny Goyald478c832016-04-01 12:04:16 -07002466 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002467 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2468 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2469 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002470 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002471 }
2472
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002473 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002474 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002475 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2476 info.pendingItemInfo = null;
2477 }
Sunny Goyalff572272014-07-23 13:58:07 -07002478
Sunny Goyalba47faa2017-10-04 16:50:57 -07002479 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002480 view.reInflate();
Sunny Goyalba47faa2017-10-04 16:50:57 -07002481 }
2482
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002483 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002484 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002485 }
2486
Adam Cohen1462de32012-07-24 22:34:36 -07002487 public void onPageBoundSynchronously(int page) {
Sunny Goyalf4aceab2017-12-06 16:16:31 -08002488 mSynchronouslyBoundPage = page;
Adam Cohen1462de32012-07-24 22:34:36 -07002489 }
2490
Sunny Goyal527c7d32015-08-28 15:19:36 -07002491 @Override
2492 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2493 if (mPendingExecutor != null) {
2494 mPendingExecutor.markCompleted();
2495 }
2496 mPendingExecutor = executor;
2497 executor.attachTo(this);
2498 }
2499
2500 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2501 if (mPendingExecutor == executor) {
2502 mPendingExecutor = null;
2503 }
2504 }
2505
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002506 @Override
2507 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002508 if (mDragLayer.getAlpha() < 1) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002509 mDragLayer.animate().alpha(1).withEndAction(
2510 executor == null ? null : executor::onLoadAnimationCompleted).start();
2511 } else if (executor != null) {
2512 executor.onLoadAnimationCompleted();
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002513 }
2514 }
2515
Joe Onorato9c1289c2009-08-17 11:03:03 -04002516 /**
2517 * Callback saying that there aren't any more items to bind.
2518 *
2519 * Implementation of the method from LauncherModel.Callbacks.
2520 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002521 public void finishBindingItems() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002522 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002523 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002524
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002525 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002526
Sunny Goyal2100c782016-08-22 16:00:03 -07002527 if (mPendingActivityResult != null) {
2528 handleActivityResult(mPendingActivityResult.requestCode,
2529 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2530 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002531 }
2532
Sunny Goyala474a9b2017-05-04 16:47:11 -07002533 InstallShortcutReceiver.disableAndFlushInstallQueue(
2534 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002535
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002536 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002537 }
2538
Winson Chunga2413752012-04-03 14:22:34 -07002539 private boolean canRunNewAppsAnimation() {
2540 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002541 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002542 }
2543
Winson Chungc9168342013-06-26 14:54:55 -07002544 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002545 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07002546 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
2547 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002548 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002549 return bounceAnim;
2550 }
2551
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002552 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002553 * Add the icons for all apps.
2554 *
2555 * Implementation of the method from LauncherModel.Callbacks.
2556 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002557 public void bindAllApplications(ArrayList<AppInfo> apps) {
Winson Chungb745afb2015-03-02 11:51:23 -08002558 if (mAppsView != null) {
Tony2917a8b2017-07-31 23:29:42 -07002559 Executor pendingExecutor = getPendingExecutor();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002560 if (pendingExecutor != null && !isInState(ALL_APPS)) {
Tony2917a8b2017-07-31 23:29:42 -07002561 // Wait until the fade in animation has finished before setting all apps list.
Sunny Goyal29947f02017-12-18 13:49:44 -08002562 pendingExecutor.execute(() -> bindAllApplications(apps));
Tony2917a8b2017-07-31 23:29:42 -07002563 return;
2564 }
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002565
Winson Chungb745afb2015-03-02 11:51:23 -08002566 mAppsView.setApps(apps);
2567 }
Adam Cohen9211d422014-10-07 18:14:53 -07002568 if (mLauncherCallbacks != null) {
2569 mLauncherCallbacks.bindAllApplications(apps);
2570 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002571 }
2572
2573 /**
Tony2917a8b2017-07-31 23:29:42 -07002574 * Returns an Executor that will run after the launcher is first drawn (including after the
2575 * initial fade in animation). Returns null if the first draw has already occurred.
2576 */
2577 public @Nullable Executor getPendingExecutor() {
2578 return mPendingExecutor != null && mPendingExecutor.canQueue() ? mPendingExecutor : null;
2579 }
2580
2581 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002582 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
2583 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2584 */
2585 @Override
2586 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002587 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002588 }
2589
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002590 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002591 * A package was updated.
2592 *
2593 * Implementation of the method from LauncherModel.Callbacks.
2594 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002595 @Override
2596 public void bindAppsAddedOrUpdated(ArrayList<AppInfo> apps) {
Winson Chungb745afb2015-03-02 11:51:23 -08002597 if (mAppsView != null) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002598 mAppsView.addOrUpdateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07002599 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002600 }
2601
Sunny Goyal4390ace2014-10-13 11:33:11 -07002602 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002603 public void bindPromiseAppProgressUpdated(PromiseAppInfo app) {
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002604 if (mAppsView != null) {
2605 mAppsView.updatePromiseAppProgress(app);
2606 }
2607 }
2608
2609 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002610 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002611 mWorkspace.widgetsRestored(widgets);
2612 }
2613
Joe Onorato9c1289c2009-08-17 11:03:03 -04002614 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002615 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002616 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002617 *
2618 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002619 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002620 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002621 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002622 if (!updated.isEmpty()) {
2623 mWorkspace.updateShortcuts(updated);
2624 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002625 }
2626
2627 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002628 * Update the state of a package, typically related to install state.
2629 *
2630 * Implementation of the method from LauncherModel.Callbacks.
2631 */
Sunny Goyale755d462014-07-22 13:48:29 -07002632 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002633 public void bindRestoreItemsChange(HashSet<ItemInfo> updates) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07002634 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07002635 }
2636
2637 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002638 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07002639 * in addition to specific applications to remove, the reason being that
2640 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002641 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07002642 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002643 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07002644 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002645 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002646 mWorkspace.removeItemsByMatcher(matcher);
2647 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002648 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002649
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002650 @Override
2651 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
Winson Chungdf95eb12013-10-16 14:57:07 -07002652 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08002653 if (mAppsView != null) {
2654 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07002655 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002656 }
Joe Onoratobe386092009-11-17 17:32:16 -08002657
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002658 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002659 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
2660 mPopupDataProvider.setAllWidgets(allWidgets);
Tony Wickham26b17462017-03-20 17:12:24 -07002661 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
2662 if (topView != null) {
2663 topView.onWidgetsBound();
2664 }
2665 }
2666
Tony Wickham86222d22017-03-29 15:30:43 -07002667 /**
2668 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
2669 * refreshes the widgets and shortcuts associated with the given package/user
2670 */
2671 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07002672 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002673 }
2674
Sunny Goyalc99cb172017-10-19 16:15:09 -07002675 public boolean isRotationEnabled () {
2676 return mRotationEnabled;
Winson Chung400438b2011-01-16 17:53:48 -08002677 }
2678
Winson Chung80baf5a2010-08-09 16:03:15 -07002679 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002680 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08002681 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002682 @Override
2683 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
2684 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002685
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002686 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
2687 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07002688 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002689 writer.println(prefix + " Homescreen " + i);
2690
2691 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
2692 for (int j = 0; j < layout.getChildCount(); j++) {
2693 Object tag = layout.getChildAt(j).getTag();
2694 if (tag != null) {
2695 writer.println(prefix + " " + tag.toString());
2696 }
2697 }
2698 }
2699
2700 writer.println(prefix + " Hotseat");
2701 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07002702 for (int j = 0; j < layout.getChildCount(); j++) {
2703 Object tag = layout.getChildAt(j).getTag();
2704 if (tag != null) {
2705 writer.println(prefix + " " + tag.toString());
2706 }
Adam Cohen4caf2982013-08-20 18:54:31 -07002707 }
Sunny Goyala1365452015-10-01 15:46:24 -07002708
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002709 try {
2710 FileLog.flushAll(writer);
2711 } catch (Exception e) {
2712 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07002713 }
2714 }
2715
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002716 writer.println(prefix + "Misc:");
2717 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
2718 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
2719 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
2720
2721 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002722
Adam Cohen9211d422014-10-07 18:14:53 -07002723 if (mLauncherCallbacks != null) {
2724 mLauncherCallbacks.dump(prefix, fd, writer, args);
2725 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002726 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002727
Sunny Goyal66b24572016-09-21 15:57:55 -07002728 @Override
2729 @TargetApi(Build.VERSION_CODES.N)
2730 public void onProvideKeyboardShortcuts(
2731 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
2732
2733 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002734 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002735 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
2736 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
2737 }
2738 View currentFocus = getCurrentFocus();
2739 if (new CustomActionsPopup(this, currentFocus).canShow()) {
2740 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
2741 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
2742 }
Tony18c4aa42017-05-10 21:23:57 -05002743 if (currentFocus.getTag() instanceof ItemInfo
2744 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002745 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
2746 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
2747 }
2748 if (!shortcutInfos.isEmpty()) {
2749 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
2750 }
2751
2752 super.onProvideKeyboardShortcuts(data, menu, deviceId);
2753 }
2754
2755 @Override
2756 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
2757 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
2758 switch (keyCode) {
2759 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002760 if (isInState(NORMAL)) {
2761 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07002762 return true;
2763 }
2764 break;
2765 case KeyEvent.KEYCODE_S: {
2766 View focusedView = getCurrentFocus();
2767 if (focusedView instanceof BubbleTextView
2768 && focusedView.getTag() instanceof ItemInfo
2769 && mAccessibilityDelegate.performAction(focusedView,
2770 (ItemInfo) focusedView.getTag(),
2771 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal00ac9202017-11-09 15:24:06 -08002772 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07002773 return true;
2774 }
2775 break;
2776 }
2777 case KeyEvent.KEYCODE_O:
2778 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
2779 return true;
2780 }
2781 break;
2782 }
2783 }
2784 return super.onKeyShortcut(keyCode, event);
2785 }
2786
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002787 public static Launcher getLauncher(Context context) {
2788 if (context instanceof Launcher) {
2789 return (Launcher) context;
2790 }
2791 return ((Launcher) ((ContextWrapper) context).getBaseContext());
2792 }
2793
Sunny Goyal5a81c382017-03-20 15:08:06 -07002794 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener {
Sunny Goyal745bad92016-05-02 10:54:12 -07002795
2796 @Override
2797 public void onSharedPreferenceChanged(
2798 SharedPreferences sharedPreferences, String key) {
2799 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
Sunny Goyale13d4642017-05-22 15:58:42 -07002800 // Recreate the activity so that it initializes the rotation preference again.
2801 recreate();
Sunny Goyal745bad92016-05-02 10:54:12 -07002802 }
2803 }
Sunny Goyal745bad92016-05-02 10:54:12 -07002804 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002805
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08002806 @Override
2807 public void onActionModeStarted(ActionMode mode) {
2808 super.onActionModeStarted(mode);
2809 mCurrentActionMode = mode;
2810 }
2811
2812 @Override
2813 public void onActionModeFinished(ActionMode mode) {
2814 super.onActionModeFinished(mode);
2815 mCurrentActionMode = null;
2816 }
2817
2818 public boolean finishAutoCancelActionMode() {
2819 if (mCurrentActionMode != null && AUTO_CANCEL_ACTION_MODE == mCurrentActionMode.getTag()) {
2820 mCurrentActionMode.finish();
2821 return true;
2822 }
2823 return false;
2824 }
2825
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002826 /**
2827 * Callback for listening for onResume
2828 */
2829 public interface OnResumeCallback {
2830
2831 void onLauncherResume();
2832 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002833}