blob: 67c05789589136f8cbdfbc876753949e7a4f52da [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 -070032import static com.android.launcher3.util.RunnableWithId.RUNNABLE_ID_BIND_APPS;
33import static com.android.launcher3.util.RunnableWithId.RUNNABLE_ID_BIND_WIDGETS;
34
Sunny Goyal28c6b962015-10-12 11:42:05 -070035import android.Manifest;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070036import android.animation.Animator;
Jon Miranda2d89ea82017-05-04 11:47:53 -070037import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070038import android.animation.AnimatorSet;
Jon Miranda2d89ea82017-05-04 11:47:53 -070039import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070040import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070041import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000042import android.annotation.TargetApi;
Winson Chungc7450e32012-04-17 17:34:08 -070043import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040044import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080046import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070047import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080049import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080050import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040051import android.content.ComponentName;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052import android.content.Context;
Andrew Sappersteinabef55a2016-06-19 12:49:00 -070053import android.content.ContextWrapper;
Chris Wren40c5ed32014-06-24 18:24:23 -040054import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070056import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070057import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070058import android.content.SharedPreferences;
Sunny Goyal745bad92016-05-02 10:54:12 -070059import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.content.pm.PackageManager;
Adam Cohen3f9c9712014-10-31 11:48:25 -070061import android.database.sqlite.SQLiteDatabase;
Jon Mirandacc42c5b2016-10-27 17:15:27 -070062import android.graphics.Point;
Michael Jurkaaf442092010-06-10 17:01:57 -070063import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070064import android.graphics.drawable.Drawable;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070065import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020066import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.os.Bundle;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070068import android.os.Parcelable;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080069import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070070import android.os.StrictMode;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070071import android.os.UserHandle;
Tony Wickham86222d22017-03-29 15:30:43 -070072import android.support.annotation.Nullable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073import android.text.Selection;
74import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070075import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070077import android.util.Log;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070078import android.util.SparseArray;
Adam Cohen96d30a12013-07-16 18:13:21 -070079import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070080import android.view.HapticFeedbackConstants;
81import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070082import android.view.KeyboardShortcutGroup;
83import android.view.KeyboardShortcutInfo;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080084import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -070085import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070086import android.view.MotionEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070087import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080088import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070089import android.view.ViewGroup;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080090import android.view.accessibility.AccessibilityEvent;
Tony Wickhame2217252016-03-22 16:34:23 -070091import android.view.accessibility.AccessibilityManager;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070092import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070094import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070095
Sunny Goyal651077b2014-06-30 14:15:31 -070096import com.android.launcher3.DropTarget.DragObject;
Sunny Goyala7ce1662016-05-31 15:01:35 -070097import com.android.launcher3.LauncherSettings.Favorites;
Jon Mirandac476d6e2017-05-04 16:30:01 -070098import com.android.launcher3.Workspace.ItemOperator;
Sunny Goyalae502842016-06-17 08:43:56 -070099import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700100import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700101import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700102import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100103import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -0700104import com.android.launcher3.compat.LauncherAppsCompatVO;
Adam Cohen39933482017-09-29 16:43:51 -0700105import com.android.launcher3.compat.UserManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -0800106import com.android.launcher3.config.FeatureFlags;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700107import com.android.launcher3.dragndrop.DragController;
108import com.android.launcher3.dragndrop.DragLayer;
Sunny Goyal94b510c2016-08-16 15:36:48 -0700109import com.android.launcher3.dragndrop.DragOptions;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700110import com.android.launcher3.dragndrop.DragView;
Sunny Goyalb38fab72017-01-20 19:32:31 -0800111import com.android.launcher3.dragndrop.PinItemDragListener;
Mario Bertschler27288382017-05-24 15:35:09 -0700112import com.android.launcher3.dynamicui.WallpaperColorInfo;
Sunny Goyal26119432016-02-18 22:09:23 +0000113import com.android.launcher3.folder.Folder;
114import com.android.launcher3.folder.FolderIcon;
Sunny Goyal66b24572016-09-21 15:57:55 -0700115import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700116import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700117import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700118import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyala535ae42017-02-27 10:07:13 -0800119import com.android.launcher3.model.ModelWriter;
Sunny Goyala535ae42017-02-27 10:07:13 -0800120import com.android.launcher3.notification.NotificationListener;
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700121import com.android.launcher3.pageindicators.PageIndicator;
Sunny Goyal10a1bd02017-10-09 14:56:21 -0700122import com.android.launcher3.popup.BaseActionPopup;
Tony Wickham540913e2017-01-23 11:47:51 -0800123import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -0800124import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700125import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700126import com.android.launcher3.states.AllAppsState;
Jon Mirandac6cf4932017-02-07 17:12:36 -0800127import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530128import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
129import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
130import com.android.launcher3.userevent.nano.LauncherLogProto.ControlType;
Sunny Goyalaeb16432017-10-16 11:46:41 -0700131import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyal2100c782016-08-22 16:00:03 -0700132import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700133import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700134import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700135import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700136import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800137import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700138import com.android.launcher3.util.PendingRequestArgs;
Sunny Goyal326403e2017-10-02 12:45:10 -0700139import com.android.launcher3.util.RunnableWithId;
Sunny Goyal8392c822017-06-20 10:03:56 -0700140import com.android.launcher3.util.SystemUiController;
Mario Bertschlera6936942017-05-31 14:48:19 -0700141import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700142import com.android.launcher3.util.Thunk;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700143import com.android.launcher3.util.TraceHelper;
Sunny Goyal326403e2017-10-02 12:45:10 -0700144import com.android.launcher3.util.UiThreadHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700145import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800146import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700147import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800148import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700149import com.android.launcher3.widget.WidgetHostViewLoader;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700150import com.android.launcher3.widget.WidgetListRowEntry;
151import com.android.launcher3.widget.WidgetsFullSheet;
Sunny Goyal952e63d2017-08-16 04:59:08 -0700152import com.android.launcher3.widget.custom.CustomWidgetParser;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700153
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700154import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700155import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700156import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700157import java.util.Collection;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700158import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700159import java.util.List;
Tony Wickham010d2552017-01-20 08:15:28 -0800160import java.util.Set;
Tony2917a8b2017-07-31 23:29:42 -0700161import java.util.concurrent.Executor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700162
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163/**
164 * Default launcher application.
165 */
Sunny Goyal27835952017-01-13 12:15:53 -0800166public class Launcher extends BaseActivity
Adam Cohen79d90c52016-04-22 13:29:20 -0700167 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
168 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Mario Bertschler27288382017-05-24 15:35:09 -0700169 WallpaperColorInfo.OnThemeChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700170 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700171 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172
Winson Chunga2413752012-04-03 14:22:34 -0700173 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700174
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700176 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700177
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700178 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700179 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180
Michael Jurka8b805b12012-04-18 14:23:14 -0700181 private static final int REQUEST_BIND_APPWIDGET = 11;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700182 private static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
183 private static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700184
Jon Mirandac476d6e2017-05-04 16:30:01 -0700185 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700186
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700187 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
188
Mathew Inwood876a8462013-06-14 14:12:41 +0100189 /**
190 * IntentStarter uses request codes starting with this. This must be greater than all activity
191 * request codes used internally.
192 */
193 protected static final int REQUEST_LAST = 100;
194
Winson Chung2672ff92012-05-04 16:22:30 -0700195 // The Intent extra that defines whether to ignore the launch animation
196 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400197 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700198
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199 // Type: int
200 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700201 // Type: int
202 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700203 // Type: PendingRequestArgs
204 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
205 // Type: ActivityResultInfo
206 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700207 // Type: SparseArray<Parcelable>
208 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800209
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700210 private LauncherStateManager mStateManager;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700211
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700212 private boolean mIsSafeModeEnabled;
213
Adam Cohenad4e15c2013-10-17 16:21:35 -0700214 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800215
Winson Chunga2413752012-04-03 14:22:34 -0700216 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700217 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
218 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
219 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700220
Adam Cohen091440a2015-03-18 14:16:05 -0700221 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700222 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700223 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800224 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700225
226 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700227
Sunny Goyalffe83f12014-08-14 17:39:34 -0700228 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700229 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700230
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700231 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700232
Sunny Goyal316490e2015-06-02 09:38:28 -0700233 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800234 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700235
Michael Jurka838a4ca2011-02-07 13:33:06 -0800236 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700237
Sunny Goyal47328fd2016-05-25 18:56:41 -0700238 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700239
240 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700241 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700242 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700243
Jon Miranda6bed3502017-09-19 14:43:17 -0700244 // We need to store the orientation Launcher was created with, due to a bug (b/64916689)
245 // that results in widgets being inflated in the wrong orientation.
246 private int mOrientation;
247
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800248 private SpannableStringBuilder mDefaultKeySsb = null;
249
Adam Cohen091440a2015-03-18 14:16:05 -0700250 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400251
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800252 private boolean mPaused = true;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700253 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800254
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700255 private final ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<>();
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700256 private OnResumeCallback mOnResumeCallback;
257
Sunny Goyal527c7d32015-08-28 15:19:36 -0700258 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700259
Joe Onorato9c1289c2009-08-17 11:03:03 -0400260 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800261 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800262 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700263 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Sunny Goyal1acc56a2016-09-25 21:23:25 -0700264 private boolean mHasFocus = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400265
Jon Miranda2d89ea82017-05-04 11:47:53 -0700266 private ObjectAnimator mScrimAnimator;
Jon Miranda7fda2852017-07-19 16:07:01 -0700267 private boolean mShouldFadeInScrim;
Jon Miranda2d89ea82017-05-04 11:47:53 -0700268
Tony Wickham010d2552017-01-20 08:15:28 -0800269 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700270
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700271 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<>();
Adam Cohen1462de32012-07-24 22:34:36 -0700272
Winson Chung46353de2012-02-16 14:05:10 -0800273 // We only want to get the SharedPreferences once since it does an FS stat each time we get
274 // it from the context.
275 private SharedPreferences mSharedPrefs;
276
Sunny Goyal2100c782016-08-22 16:00:03 -0700277 // Activity result which needs to be processed after workspace has loaded.
278 private ActivityResultInfo mPendingActivityResult;
279 /**
280 * Holds extra information required to handle a result from an external call, like
281 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
282 */
283 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800284
Jon Miranda379198e2016-09-23 08:54:40 -0700285 private float mLastDispatchTouchEventX = 0.0f;
Jon Miranda379198e2016-09-23 08:54:40 -0700286
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700287 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700288 private boolean mRotationEnabled = false;
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700289 private boolean mAppLaunchSuccess;
290
Sunny Goyal745bad92016-05-02 10:54:12 -0700291 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700292
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800293 @Override
294 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700295 if (DEBUG_STRICT_MODE) {
296 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
297 .detectDiskReads()
298 .detectDiskWrites()
299 .detectNetwork() // or .detectAll() for all detectable problems
300 .penaltyLog()
301 .build());
302 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
303 .detectLeakedSqlLiteObjects()
304 .detectLeakedClosableObjects()
305 .penaltyLog()
306 .penaltyDeath()
307 .build());
308 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700309 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700310
Adam Cohen9211d422014-10-07 18:14:53 -0700311 if (mLauncherCallbacks != null) {
312 mLauncherCallbacks.preOnCreate();
313 }
314
Mario Bertschler27288382017-05-24 15:35:09 -0700315 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
316 wallpaperColorInfo.setOnThemeChangeListener(this);
Mario Bertschlera6936942017-05-31 14:48:19 -0700317 overrideTheme(wallpaperColorInfo.isDark(), wallpaperColorInfo.supportsDarkText());
Mario Bertschler27288382017-05-24 15:35:09 -0700318
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800319 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700320 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400321
Sunny Goyal87f784c2017-01-11 10:48:34 -0800322 LauncherAppState app = LauncherAppState.getInstance(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700323
Adam Cohen2e6da152015-05-06 11:42:25 -0700324 // Load configuration-specific DeviceProfile
Sunny Goyal27835952017-01-13 12:15:53 -0800325 mDeviceProfile = app.getInvariantDeviceProfile().getDeviceProfile(this);
Jon Mirandafe964322017-03-22 10:25:17 -0700326 if (isInMultiWindowModeCompat()) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700327 Display display = getWindowManager().getDefaultDisplay();
328 Point mwSize = new Point();
329 display.getSize(mwSize);
330 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
331 }
Sunny Goyalc6205602015-05-21 20:46:33 -0700332
Jon Miranda6bed3502017-09-19 14:43:17 -0700333 mOrientation = getResources().getConfiguration().orientation;
Sunny Goyalf7258242015-10-19 16:59:07 -0700334 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700335 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800336 mModel = app.setLauncher(this);
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800337 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout());
Joe Onorato0589f0f2010-02-08 13:44:00 -0800338 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700339 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700340
Joe Onorato41a12d22009-10-31 18:30:00 -0400341 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700342 mAllAppsController = new AllAppsTransitionController(this);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700343 mStateManager = new LauncherStateManager(this, mAllAppsController);
Romain Guycbb89e42009-06-08 15:52:54 -0700344
Sunny Goyalffe83f12014-08-14 17:39:34 -0700345 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700346
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700347 mAppWidgetHost = new LauncherAppWidgetHost(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700348 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700349
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700350 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
351 // this also ensures that any synchronous binding below doesn't re-trigger another
352 // LauncherModel load.
353 mPaused = false;
354
Sunny Goyal60820d72017-05-09 12:40:11 -0700355 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700356
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800357 setupViews();
Winson1f064272016-07-18 17:18:02 -0700358 mDeviceProfile.layout(this, false /* notifyListeners */);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800359
Tony Wickham010d2552017-01-20 08:15:28 -0800360 mPopupDataProvider = new PopupDataProvider(this);
361
Sunny Goyalfe770c92016-09-27 14:38:58 -0700362 restoreState(savedInstanceState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800363
Sunny Goyal2100c782016-08-22 16:00:03 -0700364 // We only load the page synchronously if the user rotates (or triggers a
365 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700366 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
367 if (savedInstanceState != null) {
368 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
369 }
370 if (!mModel.startLoader(currentScreen)) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700371 // If we are not binding synchronously, show a fade in animation when
372 // the first page bind completes.
373 mDragLayer.setAlpha(0);
374 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700375 // Pages bound synchronously.
376 mWorkspace.setCurrentPage(currentScreen);
377
Sunny Goyal2100c782016-08-22 16:00:03 -0700378 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800379 }
380
381 // For handling default keys
382 mDefaultKeySsb = new SpannableStringBuilder();
383 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800384
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800385 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700386 // In case we are on a device with locked rotation, we should look at preferences to check
387 // if the user has specifically allowed rotation.
388 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700389 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700390 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
391 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700392 }
393
Sunny Goyal5a81c382017-03-20 15:08:06 -0700394 if (PinItemDragListener.handleDragRequest(this, getIntent())) {
395 // Temporarily enable the rotation
396 mRotationEnabled = true;
397 }
398
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700399 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
400 // we want the screen to auto-rotate based on the current orientation
Sunny Goyalc99cb172017-10-19 16:15:09 -0700401 setRequestedOrientation(mRotationEnabled
402 ? SCREEN_ORIENTATION_UNSPECIFIED : SCREEN_ORIENTATION_NOSENSOR);
Winson Chungaf40f202013-09-18 18:26:31 -0700403
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800404 setContentView(mLauncherView);
Mario Bertschlera6936942017-05-31 14:48:19 -0700405
Jon Miranda7fda2852017-07-19 16:07:01 -0700406 // Listen for broadcasts
407 IntentFilter filter = new IntentFilter();
408 filter.addAction(Intent.ACTION_SCREEN_OFF);
409 filter.addAction(Intent.ACTION_USER_PRESENT); // When the device wakes up + keyguard is gone
410 registerReceiver(mReceiver, filter);
411 mShouldFadeInScrim = true;
Sunny Goyala616d2b2017-06-12 13:54:01 -0700412
Sunny Goyal8392c822017-06-20 10:03:56 -0700413 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
414 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700415
416 if (mLauncherCallbacks != null) {
417 mLauncherCallbacks.onCreate(savedInstanceState);
418 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700419
420 TraceHelper.endSection("Launcher-onCreate");
Adam Cohen9211d422014-10-07 18:14:53 -0700421 }
422
Sunny Goyal7779d622015-06-11 16:18:39 -0700423 @Override
Mario Bertschler27288382017-05-24 15:35:09 -0700424 public void onThemeChanged() {
425 recreate();
426 }
427
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700428 public LauncherStateManager getStateManager() {
429 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700430 }
431
Mario Bertschlera6936942017-05-31 14:48:19 -0700432 protected void overrideTheme(boolean isDark, boolean supportsDarkText) {
Mario Bertschler27288382017-05-24 15:35:09 -0700433 if (isDark) {
434 setTheme(R.style.LauncherThemeDark);
Mario Bertschlera6936942017-05-31 14:48:19 -0700435 } else if (supportsDarkText) {
436 setTheme(R.style.LauncherThemeDarkText);
Mario Bertschler27288382017-05-24 15:35:09 -0700437 }
438 }
439
440 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700441 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800442 return mLauncherView.findViewById(id);
443 }
444
445 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700446 public void onAppWidgetHostReset() {
447 if (mAppWidgetHost != null) {
448 mAppWidgetHost.startListening();
449 }
450 }
451
Adam Cohen9211d422014-10-07 18:14:53 -0700452 private LauncherCallbacks mLauncherCallbacks;
453
Winson1f064272016-07-18 17:18:02 -0700454 public void onInsetsChanged(Rect insets) {
455 mDeviceProfile.updateInsets(insets);
456 mDeviceProfile.layout(this, true /* notifyListeners */);
457 }
458
Sunny Goyal32554d12015-12-03 15:31:25 -0800459 /**
460 * Call this after onCreate to set or clear overlay.
461 */
462 public void setLauncherOverlay(LauncherOverlay overlay) {
463 if (overlay != null) {
464 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
465 }
466 mWorkspace.setLauncherOverlay(overlay);
467 }
468
Adam Cohen9211d422014-10-07 18:14:53 -0700469 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
470 mLauncherCallbacks = callbacks;
471 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700472 }
473
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700474 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700475 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700476 if (mLauncherCallbacks != null) {
477 mLauncherCallbacks.onLauncherProviderChange();
478 }
479 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700480
Hyunyoung Song3f471442015-04-08 19:01:34 -0700481 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700482 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
483 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700484 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700485 }
486
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000487 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700488 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
489 // This cast is safe as long as the id < 0x00FFFFFF
490 // Since we jail all the dynamically generated views, there should be no clashes
491 // with any other views.
492 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000493 }
494
Tony Wickham010d2552017-01-20 08:15:28 -0800495 public PopupDataProvider getPopupDataProvider() {
496 return mPopupDataProvider;
497 }
498
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000499 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700500 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
501 * a configuration step, this allows the proper animations to run after other transitions.
502 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700503 private long completeAdd(
504 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
505 long screenId = info.screenId;
506 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700507 // When the screen id represents an actual screen (as opposed to a rank) we make sure
508 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700509 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700510 }
Adam Cohendb364c32014-05-20 14:23:40 -0700511
Sunny Goyal2100c782016-08-22 16:00:03 -0700512 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800513 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700514 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700515 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800516 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700517 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700518 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700519 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700520 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700521 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700522 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700523 int widgetId = appWidgetId;
524 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700525 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700526 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700527 // Since the view was just bound, also launch the configure activity if needed
528 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
529 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800530 if (provider != null) {
531 new WidgetAddFlowHandler(provider)
532 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700533 }
534 }
535 break;
536 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800537 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700538
Adam Cohendb364c32014-05-20 14:23:40 -0700539 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800540 }
541
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800542 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700543 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700544 if (isWorkspaceLoading()) {
545 // process the result once the workspace has loaded.
546 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
547 return;
548 }
549 mPendingActivityResult = null;
550
Winson Chunge341d302013-08-16 14:31:00 -0700551 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700552 final PendingRequestArgs requestArgs = mPendingRequestArgs;
553 setWaitingForResult(null);
554 if (requestArgs == null) {
555 return;
556 }
557
558 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700559
Adam Cohenad4e15c2013-10-17 16:21:35 -0700560 Runnable exitSpringLoaded = new Runnable() {
561 @Override
562 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700563 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700564 }
565 };
566
Michael Jurka8b805b12012-04-18 14:23:14 -0700567 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700568 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700569 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700570 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
571 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700572 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700573 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700574 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700575 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700576 addAppWidgetImpl(
577 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800578 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700579 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700580 }
581 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200582 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700583 if (resultCode == RESULT_OK && isInState(OVERVIEW)) {
Tony Wickhame3054412015-09-09 09:05:25 -0700584 // User could have free-scrolled between pages before picking a wallpaper; make sure
585 // we move to the closest one now.
586 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700587 mStateManager.goToState(NORMAL, false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200588 }
589 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700590 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200591
Adam Cohened66b2b2012-01-23 17:28:51 -0800592 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700593 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700594
Adam Cohened66b2b2012-01-23 17:28:51 -0800595 // We have special handling for widgets
596 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800597 final int appWidgetId;
598 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
599 : -1;
600 if (widgetId < 0) {
601 appWidgetId = pendingAddWidgetId;
602 } else {
603 appWidgetId = widgetId;
604 }
605
Adam Cohenad4e15c2013-10-17 16:21:35 -0700606 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800607 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700608 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
609 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700610 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700611 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700612 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700613 @Override
614 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700615 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700616 }
617 };
Adam Cohendb364c32014-05-20 14:23:40 -0700618
Sunny Goyal2100c782016-08-22 16:00:03 -0700619 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
620 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
621 } else {
622 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
623 // When the screen id represents an actual screen (as opposed to a rank)
624 // we make sure that the drop page actually exists.
625 requestArgs.screenId =
626 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700627 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700628 final CellLayout dropLayout =
629 mWorkspace.getScreenWithId(requestArgs.screenId);
630
631 dropLayout.setDropPending(true);
632 final Runnable onComplete = new Runnable() {
633 @Override
634 public void run() {
635 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
636 dropLayout.setDropPending(false);
637 }
638 };
639 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
640 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700641 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800642 return;
643 }
644
Sunny Goyald478c832016-04-01 12:04:16 -0700645 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
646 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700647 if (resultCode == RESULT_OK) {
648 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700649 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700650 }
651 // Leave the widget in the pending state if the user canceled the configure.
652 return;
653 }
654
Sunny Goyalaad90582015-07-09 14:22:50 -0700655 if (requestCode == REQUEST_CREATE_SHORTCUT) {
656 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700657 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
658 completeAdd(requestCode, data, -1, requestArgs);
659 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
660 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
661
Sunny Goyalaad90582015-07-09 14:22:50 -0700662 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700663 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
664 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800665 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800666 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800667 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800668 }
669
670 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700671 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800672 final int requestCode, final int resultCode, final Intent data) {
673 handleActivityResult(requestCode, resultCode, data);
674 if (mLauncherCallbacks != null) {
675 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
676 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800677 }
678
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700679 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700680 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600681 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700682 PendingRequestArgs pendingArgs = mPendingRequestArgs;
683 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
684 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
685 setWaitingForResult(null);
686
Sunny Goyal28c6b962015-10-12 11:42:05 -0700687 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700688 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700689 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700690 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700691 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700692 Intent intent = pendingArgs.getPendingIntent();
693
Sunny Goyal28c6b962015-10-12 11:42:05 -0700694 if (grantResults.length > 0
695 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700696 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700697 } else {
698 // TODO: Show a snack bar with link to settings
699 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700700 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700701 }
702 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600703 if (mLauncherCallbacks != null) {
704 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
705 grantResults);
706 }
707 }
708
Adam Cohendb364c32014-05-20 14:23:40 -0700709 /**
710 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
711 *
712 * @param screenId the screen id to check
713 * @return the new screen, or screenId if it exists
714 */
715 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800716 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700717 if (dropLayout == null) {
718 // it's possible that the add screen was removed because it was
719 // empty and a re-bind occurred
720 mWorkspace.addExtraEmptyScreen();
721 return mWorkspace.commitExtraEmptyScreen();
722 } else {
723 return screenId;
724 }
725 }
726
Sunny Goyal2100c782016-08-22 16:00:03 -0700727 @Thunk void completeTwoStageWidgetDrop(
728 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
729 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800730 Runnable onCompleteRunnable = null;
731 int animationType = 0;
732
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700733 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800734 if (resultCode == RESULT_OK) {
735 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
736 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800737 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700738 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800739 onCompleteRunnable = new Runnable() {
740 @Override
741 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700742 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700743 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800744 }
745 };
746 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800747 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800748 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800749 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700750 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700751 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700752 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
753 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700754 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700755 // The animated view may be null in the case of a rotation during widget configuration
756 onCompleteRunnable.run();
757 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800758 }
759
760 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700761 protected void onStop() {
762 super.onStop();
763 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700764
765 if (mLauncherCallbacks != null) {
766 mLauncherCallbacks.onStop();
767 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700768
Sunny Goyalf5e37442016-11-02 10:31:24 -0700769 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal5da78f42016-06-17 14:00:22 -0700770 mAppWidgetHost.stopListening();
771 }
Tony Wickham010d2552017-01-20 08:15:28 -0800772
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700773 if (!mAppLaunchSuccess) {
774 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
Sunny Goyalea609262017-10-25 15:47:38 -0700775 mStateManager.getState().containerType);
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700776 }
Tony Wickham010d2552017-01-20 08:15:28 -0800777 NotificationListener.removeNotificationsChangedListener();
Michael Jurkacd496d72013-04-11 11:32:45 -0700778 }
779
780 @Override
781 protected void onStart() {
782 super.onStart();
783 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700784
785 if (mLauncherCallbacks != null) {
786 mLauncherCallbacks.onStart();
787 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700788
Sunny Goyalf5e37442016-11-02 10:31:24 -0700789 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal5da78f42016-06-17 14:00:22 -0700790 mAppWidgetHost.startListening();
791 }
Tony Wickham010d2552017-01-20 08:15:28 -0800792
793 if (!isWorkspaceLoading()) {
794 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
795 }
Jon Miranda2d89ea82017-05-04 11:47:53 -0700796
Jon Miranda7fda2852017-07-19 16:07:01 -0700797 if (mShouldFadeInScrim && mDragLayer.getBackground() != null) {
Jon Miranda2d89ea82017-05-04 11:47:53 -0700798 if (mScrimAnimator != null) {
799 mScrimAnimator.cancel();
800 }
801 mDragLayer.getBackground().setAlpha(0);
802 mScrimAnimator = ObjectAnimator.ofInt(mDragLayer.getBackground(),
803 LauncherAnimUtils.DRAWABLE_ALPHA, 0, 255);
804 mScrimAnimator.addListener(new AnimatorListenerAdapter() {
805 @Override
806 public void onAnimationEnd(Animator animation) {
807 mScrimAnimator = null;
808 }
809 });
810 mScrimAnimator.setDuration(600);
811 mScrimAnimator.setStartDelay(getWindow().getTransitionBackgroundFadeDuration());
812 mScrimAnimator.start();
813 }
Jon Miranda7fda2852017-07-19 16:07:01 -0700814 mShouldFadeInScrim = false;
Michael Jurkacd496d72013-04-11 11:32:45 -0700815 }
816
817 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800818 protected void onResume() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700819 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800820 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700821 TraceHelper.partitionSection("ON_RESUME", "superCall");
822
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700823 mAppLaunchSuccess = false;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700824 getUserEventDispatcher().resetElapsedSessionMillis();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800825 mPaused = false;
Sunny Goyal2100c782016-08-22 16:00:03 -0700826 if (mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700827 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -0700828 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700829 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800830 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700831 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200832 // We might have postponed some bind calls until onResume (see waitUntilResume) --
833 // execute them here
Michael Jurka1e2f4652013-07-08 18:03:46 -0700834 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
835 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200836 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700837 mBindOnResumeCallbacks.clear();
Michael Jurka447bf842013-05-15 14:52:15 +0200838 }
Winson Chunge4e50662012-01-23 14:45:13 -0800839
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700840 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700841 // Process any items that were added while Launcher was away.
842 InstallShortcutReceiver.disableAndFlushInstallQueue(
843 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700844
Sunny Goyala474a9b2017-05-04 16:47:11 -0700845 // Refresh shortcuts if the permission changed.
846 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700847
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700848 if (shouldShowDiscoveryBounce()) {
849 mAllAppsController.showDiscoveryBounce();
850 }
Adam Cohen9211d422014-10-07 18:14:53 -0700851 if (mLauncherCallbacks != null) {
852 mLauncherCallbacks.onResume();
853 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800854
Sunny Goyala502aa32017-10-02 16:04:06 -0700855 clearTypedText();
856
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700857 TraceHelper.endSection("ON_RESUME");
Adam Cohen06dff352012-06-01 17:17:08 -0700858 }
859
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800860 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700861 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700862 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700863 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700864
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700865 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700866 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800867 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700868 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700869
Adam Cohen9211d422014-10-07 18:14:53 -0700870 if (mLauncherCallbacks != null) {
871 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700872 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700873 }
874
Adam Cohenc2d6e892014-10-16 09:49:24 -0700875 public interface LauncherOverlay {
876
877 /**
878 * Touch interaction leading to overscroll has begun
879 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700880 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700881
882 /**
883 * Touch interaction related to overscroll has ended
884 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700885 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700886
887 /**
888 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
889 * screen (or in the case of RTL, the rightmost screen).
890 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700891 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700892
893 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800894 * Called when the launcher is ready to use the overlay
895 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700896 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700897 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700898 }
899
900 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700901 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700902 }
903
904 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
905
Sunny Goyalc86df472016-02-25 09:19:38 -0800906 public void onScrollChanged(float progress) {
907 if (mWorkspace != null) {
908 mWorkspace.onOverlayScrollChanged(progress);
909 }
910 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700911 }
912
Jorim Jaggid017f882014-01-14 17:08:48 -0800913 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -0700914 if (mLauncherCallbacks != null) {
915 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700916 } else {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800917 // On O and above we there is always some setting present settings (add icon to
918 // home screen or icon badging). On earlier APIs we will have the allow rotation
919 // setting, on devices with a locked orientation,
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700920 return Utilities.ATLEAST_OREO || !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -0700921 }
Jorim Jaggid017f882014-01-14 17:08:48 -0800922 }
923
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700924 @Override
925 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400926 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -0700927 if (mModel.isCurrentCallbacks(this)) {
928 mModel.stopLoader();
929 }
Hyunyoung Song3f471442015-04-08 19:01:34 -0700930 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
931
Romain Guy13c2e7b2010-03-10 19:45:00 -0800932 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700933 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700934
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800935 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800936 @Override
937 public void onWindowFocusChanged(boolean hasFocus) {
938 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -0700939 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -0700940
941 if (mLauncherCallbacks != null) {
942 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
943 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800944 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800945
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800946 private boolean acceptFilter() {
947 final InputMethodManager inputManager = (InputMethodManager)
948 getSystemService(Context.INPUT_METHOD_SERVICE);
949 return !inputManager.isFullscreenMode();
950 }
951
952 @Override
953 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700954 final int uniChar = event.getUnicodeChar();
955 final boolean handled = super.onKeyDown(keyCode, event);
956 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
957 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800958 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
959 keyCode, event);
960 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700961 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700962 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700963 // showSearchDialog()
964 // If there are multiple keystrokes before the search dialog takes focus,
965 // onSearchRequested() will be called for every keystroke,
966 // but it is idempotent, so it's fine.
967 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800968 }
969 }
970
Joe Onorato8a9625e2010-01-28 15:55:35 -0800971 // Eat the long press event so the keyboard doesn't come up.
972 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
973 return true;
974 }
975
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800976 return handled;
977 }
978
Winson46163472015-09-22 17:31:56 -0700979 @Override
980 public boolean onKeyUp(int keyCode, KeyEvent event) {
981 if (keyCode == KeyEvent.KEYCODE_MENU) {
982 // Ignore the menu key if we are currently dragging or are on the custom content screen
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700983 if (!mDragController.isDragging()) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700984 // Close any open floating view
985 AbstractFloatingView.closeAllOpenViews(this);
Tony Wickham49c8d292016-07-01 11:44:34 -0700986
Winson46163472015-09-22 17:31:56 -0700987 // Show the overview mode if we are on the workspace
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700988 if (isInState(NORMAL) && !mWorkspace.isSwitchingState()) {
989 mStateManager.goToState(OVERVIEW, true /* animate */, new Runnable() {
990 @Override
991 public void run() {
992 // Hitting the menu button when in touch mode does not trigger touch
993 // mode to be disabled, so if requested, force focus on one of the
994 // overview panel buttons.
995 mOverviewPanel.requestFocusFromTouch();
996 }
997 });
Winson46163472015-09-22 17:31:56 -0700998 }
999 }
1000 return true;
1001 }
1002 return super.onKeyUp(keyCode, event);
1003 }
1004
Karl Rosaen138a0412009-04-23 19:00:21 -07001005 private String getTypedText() {
1006 return mDefaultKeySsb.toString();
1007 }
1008
Sunny Goyal6f28e712016-09-13 14:19:29 -07001009 @Override
1010 public void clearTypedText() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001011 mDefaultKeySsb.clear();
1012 mDefaultKeySsb.clearSpans();
1013 Selection.setSelection(mDefaultKeySsb, 0);
1014 }
1015
Sunny Goyalf9403d92017-10-18 10:55:56 -07001016 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -07001017 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -07001018 }
1019
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001020 /**
1021 * Restores the previous state, if it exists.
1022 *
1023 * @param savedState The previous state.
1024 */
1025 private void restoreState(Bundle savedState) {
1026 if (savedState == null) {
1027 return;
1028 }
1029
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001030 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -07001031 LauncherState[] stateValues = LauncherState.values();
1032 LauncherState state = stateValues[stateOrdinal];
1033 if (!state.doNotRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001034 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001035 }
1036
Sunny Goyal2100c782016-08-22 16:00:03 -07001037 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
1038 if (requestArgs != null) {
1039 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001040 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001041
1042 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001043
1044 SparseArray<Parcelable> widgetsState =
1045 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
1046 if (widgetsState != null) {
1047 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
1048 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001049 }
1050
1051 /**
1052 * Finds all the views we need and configure them properly.
1053 */
1054 private void setupViews() {
Winson Chung4c98d922011-05-31 16:50:48 -07001055 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001056 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -07001057 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001058 mWorkspace.initParentViews(mDragLayer);
Craig Mautner360310b2012-10-26 15:13:08 -07001059
Sunny Goyal784f9c32016-03-23 16:56:54 -07001060 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1061 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1062 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001063
Winson Chung4c98d922011-05-31 16:50:48 -07001064 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001065 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001066
Winson Chung3d503fb2011-07-13 17:25:49 -07001067 // Setup the hotseat
1068 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1069 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001070 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001071 }
1072
Winsone9f27272015-10-13 10:47:51 -07001073 // Setup the overview panel
1074 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001075
Winson Chung4c98d922011-05-31 16:50:48 -07001076 // Setup the workspace
1077 mWorkspace.setHapticFeedbackEnabled(false);
1078 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001079 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001080 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1081 // default state, otherwise we will update to the wrong offsets in RTL
1082 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001083 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001084 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001085
Tony Wickham34d2c912015-09-11 09:27:58 -07001086 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001087 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001088
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001089 // Setup Apps
1090 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001091
Winson Chung3d503fb2011-07-13 17:25:49 -07001092 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001093 mDragController.setMoveTarget(mWorkspace);
1094 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001095 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001096
Hyunyoung Songd725f642017-08-17 22:26:35 -07001097 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001098 }
1099
Winsone9f27272015-10-13 10:47:51 -07001100 private void setupOverviewPanel() {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001101 mOverviewPanel = findViewById(R.id.overview_panel);
Winsone9f27272015-10-13 10:47:51 -07001102
Winsone9f27272015-10-13 10:47:51 -07001103 // Bind wallpaper button actions
1104 View wallpaperButton = findViewById(R.id.wallpaper_button);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301105 new OverviewButtonClickListener(ControlType.WALLPAPER_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001106 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001107 public void handleViewClick(View view) {
1108 onClickWallpaperPicker(view);
Winsone9f27272015-10-13 10:47:51 -07001109 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001110 }.attachTo(wallpaperButton);
Winsone9f27272015-10-13 10:47:51 -07001111
1112 // Bind widget button actions
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301113 new OverviewButtonClickListener(ControlType.WIDGETS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001114 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001115 public void handleViewClick(View view) {
1116 onClickAddWidgetButton(view);
Winsone9f27272015-10-13 10:47:51 -07001117 }
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001118 }.attachTo(findViewById(R.id.widget_button));
Winsone9f27272015-10-13 10:47:51 -07001119
1120 // Bind settings actions
1121 View settingsButton = findViewById(R.id.settings_button);
1122 boolean hasSettings = hasSettings();
1123 if (hasSettings) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301124 new OverviewButtonClickListener(ControlType.SETTINGS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001125 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001126 public void handleViewClick(View view) {
1127 onClickSettingsButton(view);
Winsone9f27272015-10-13 10:47:51 -07001128 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001129 }.attachTo(settingsButton);
Winsone9f27272015-10-13 10:47:51 -07001130 } else {
1131 settingsButton.setVisibility(View.GONE);
1132 }
1133
1134 mOverviewPanel.setAlpha(0f);
1135 }
1136
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001137 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001138 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001139 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001140 */
1141 public void setAllAppsButton(View allAppsButton) {
1142 mAllAppsButton = allAppsButton;
1143 }
1144
Anjali Koppal5ad44842014-03-10 20:34:39 -07001145 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001146 * Creates a view representing a shortcut.
1147 *
1148 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001149 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001150 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001151 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001152 }
1153
1154 /**
1155 * Creates a view representing a shortcut inflated from the specified resource.
1156 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001157 * @param parent The group the shortcut belongs to.
1158 * @param info The data structure describing the shortcut.
1159 *
1160 * @return A View inflated from layoutResId.
1161 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001162 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -08001163 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
1164 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001165 favorite.applyFromShortcutInfo(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001166 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001167 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001168 return favorite;
1169 }
1170
1171 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -07001172 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001173 *
1174 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001175 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001176 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001177 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001178 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
1179 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001180 return;
1181 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001182
Jon Mirandac476d6e2017-05-04 16:30:01 -07001183 int[] cellXY = mTmpAddItemCellCoordinates;
1184 CellLayout layout = getCellLayout(container, screenId);
1185
Sunny Goyal782f0c92017-01-19 10:27:54 -08001186 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001187 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001188 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
1189 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001190 }
1191
1192 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001193 // Legacy shortcuts are only supported for primary profile.
1194 info = Process.myUserHandle().equals(args.user)
1195 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001196
Sunny Goyalb57645f2017-03-20 13:57:28 -07001197 if (info == null) {
1198 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1199 return;
1200 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001201 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001202 // The app is trying to add a shortcut without sufficient permissions
1203 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1204 return;
1205 }
1206 }
1207
Jon Mirandac476d6e2017-05-04 16:30:01 -07001208 if (container < 0) {
1209 // Adding a shortcut to the Workspace.
1210 final View view = createShortcut(info);
1211 boolean foundCellSpan = false;
1212 // First we check if we already know the exact location where we want to add this item.
1213 if (cellX >= 0 && cellY >= 0) {
1214 cellXY[0] = cellX;
1215 cellXY[1] = cellY;
1216 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001217
Jon Mirandac476d6e2017-05-04 16:30:01 -07001218 // If appropriate, either create a folder or add to an existing folder
1219 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001220 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001221 return;
1222 }
1223 DragObject dragObject = new DragObject();
1224 dragObject.dragInfo = info;
1225 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1226 true)) {
1227 return;
1228 }
1229 } else {
1230 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1231 }
1232
1233 if (!foundCellSpan) {
1234 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001235 return;
1236 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001237
1238 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1239 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001240 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001241 // Adding a shortcut to a Folder.
Sunny Goyale29897f52017-07-20 10:09:42 -07001242 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001243 if (folderIcon != null) {
1244 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1245 folderInfo.add(info, args.rank, false);
1246 } else {
Sunny Goyale29897f52017-07-20 10:09:42 -07001247 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001248 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001249 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001250 }
1251
Sunny Goyale29897f52017-07-20 10:09:42 -07001252 public FolderIcon findFolderIcon(final long folderIconId) {
1253 return (FolderIcon) mWorkspace.getFirstMatch(new ItemOperator() {
1254 @Override
1255 public boolean evaluate(ItemInfo info, View view) {
1256 return info != null && info.id == folderIconId;
1257 }
1258 });
1259 }
1260
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001261 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001262 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001263 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001264 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001265 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001266 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001267 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1268
Adam Cohened66b2b2012-01-23 17:28:51 -08001269 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001270 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001271 }
Romain Guycbb89e42009-06-08 15:52:54 -07001272
Adam Cohen59400422014-03-05 18:07:04 -08001273 LauncherAppWidgetInfo launcherInfo;
1274 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001275 launcherInfo.spanX = itemInfo.spanX;
1276 launcherInfo.spanY = itemInfo.spanY;
1277 launcherInfo.minSpanX = itemInfo.minSpanX;
1278 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001279 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001280
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001281 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001282 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001283
Sunny Goyal2100c782016-08-22 16:00:03 -07001284 if (hostView == null) {
1285 // Perform actual inflation because we're live
1286 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001287 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001288 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301289 prepareAppWidget(hostView, launcherInfo);
1290 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001291 }
Romain Guycbb89e42009-06-08 15:52:54 -07001292
Sunny Goyald5462aa2016-11-22 16:52:39 +05301293 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001294 hostView.setTag(item);
1295 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001296 hostView.setFocusable(true);
1297 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001298 }
1299
Adam Cohended9f8d2010-11-03 13:25:16 -07001300 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1301 @Override
1302 public void onReceive(Context context, Intent intent) {
1303 final String action = intent.getAction();
1304 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001305 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001306 // processing a multi-step drop
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001307 if (mAppsView != null && mPendingRequestArgs == null) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001308 mStateManager.goToState(NORMAL);
Winson Chung785d2eb2011-04-14 16:08:02 -07001309 }
Jon Miranda7fda2852017-07-19 16:07:01 -07001310 mShouldFadeInScrim = true;
1311 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1312 // ACTION_USER_PRESENT is sent after onStart/onResume. This covers the case where
1313 // the user unlocked and the Launcher is not in the foreground.
1314 mShouldFadeInScrim = false;
Adam Cohended9f8d2010-11-03 13:25:16 -07001315 }
1316 }
1317 };
1318
Tony Wickham010d2552017-01-20 08:15:28 -08001319 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
1320 Runnable r = new Runnable() {
1321 @Override
1322 public void run() {
1323 mWorkspace.updateIconBadges(updatedBadges);
1324 mAppsView.updateIconBadges(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001325
Sunny Goyal10a1bd02017-10-09 14:56:21 -07001326 BaseActionPopup popup = BaseActionPopup.getOpen(Launcher.this);
1327 if (popup instanceof PopupContainerWithArrow) {
1328 ((PopupContainerWithArrow) popup).updateNotificationHeader(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001329 }
Tony Wickham010d2552017-01-20 08:15:28 -08001330 }
1331 };
1332 if (!waitUntilResume(r)) {
1333 r.run();
1334 }
1335 }
1336
Adam Cohended9f8d2010-11-03 13:25:16 -07001337 @Override
1338 public void onAttachedToWindow() {
1339 super.onAttachedToWindow();
1340
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001341 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyalc86df472016-02-25 09:19:38 -08001342 if (mLauncherCallbacks != null) {
1343 mLauncherCallbacks.onAttachedToWindow();
1344 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001345 }
1346
1347 @Override
1348 public void onDetachedFromWindow() {
1349 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001350
1351 if (mLauncherCallbacks != null) {
1352 mLauncherCallbacks.onDetachedFromWindow();
1353 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001354 }
1355
Winson Chunga6945242014-01-08 14:04:34 -08001356 public DragLayer getDragLayer() {
1357 return mDragLayer;
1358 }
1359
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001360 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001361 return mAppsView;
1362 }
1363
Winson Chunga6945242014-01-08 14:04:34 -08001364 public Workspace getWorkspace() {
1365 return mWorkspace;
1366 }
1367
1368 public Hotseat getHotseat() {
1369 return mHotseat;
1370 }
1371
Jorim Jaggid017f882014-01-14 17:08:48 -08001372 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001373 return mOverviewPanel;
1374 }
1375
Sunny Goyal47328fd2016-05-25 18:56:41 -07001376 public DropTargetBar getDropTargetBar() {
1377 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001378 }
1379
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001380 public LauncherAppWidgetHost getAppWidgetHost() {
1381 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001382 }
Romain Guycbb89e42009-06-08 15:52:54 -07001383
Winson Chunga9abd0e2010-10-27 17:18:37 -07001384 public LauncherModel getModel() {
1385 return mModel;
1386 }
1387
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001388 public ModelWriter getModelWriter() {
1389 return mModelWriter;
1390 }
1391
Adam Cohen79d90c52016-04-22 13:29:20 -07001392 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001393 return mSharedPrefs;
1394 }
1395
Jon Miranda6bed3502017-09-19 14:43:17 -07001396 public int getOrientation() { return mOrientation; }
1397
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001398 @Override
1399 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001400 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001401 super.onNewIntent(intent);
1402
Winson15f8b172015-08-19 11:02:39 -07001403 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1404 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1405 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001406
1407 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001408 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001409 && AbstractFloatingView.getTopOpenView(this) == null;
Sunny Goyal85313732016-09-28 12:00:07 -07001410
Winson15f8b172015-08-19 11:02:39 -07001411 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1412 if (isActionMain) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001413 if (mWorkspace == null) {
1414 // Can be cases where mWorkspace is null, this prevents a NPE
1415 return;
1416 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001417
1418 // Note: There should be at most one log per method call. This is enforced implicitly
1419 // by using if-else statements.
1420 UserEventDispatcher ued = getUserEventDispatcher();
Jon Mirandafeba90f2016-10-06 10:53:29 -07001421 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
Sunny Goyal37920962017-09-28 13:43:24 -07001422 if (topOpenView != null) {
1423 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
Jon Mirandafeba90f2016-10-06 10:53:29 -07001424 } else if (alreadyOnHome) {
Sunny Goyalea609262017-10-25 15:47:38 -07001425 Target target = newContainerTarget(mStateManager.getState().containerType);
Sunny Goyalaeb16432017-10-16 11:46:41 -07001426 target.pageIndex = mWorkspace.getCurrentPage();
1427 ued.logActionCommand(Action.Command.HOME_INTENT, target);
Jon Mirandafeba90f2016-10-06 10:53:29 -07001428 }
1429
1430 // In all these cases, only animate if we're already on home
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001431 AbstractFloatingView.closeAllOpenViews(this, alreadyOnHome);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001432 mStateManager.goToState(NORMAL, alreadyOnHome /* animated */);
Adam Cohen6fecd412013-10-02 17:41:50 -07001433
1434 final View v = getWindow().peekDecorView();
1435 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001436 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001437 }
1438
Winson Chungb745afb2015-03-02 11:51:23 -08001439 // Reset the apps view
1440 if (!alreadyOnHome && mAppsView != null) {
Sunny Goyal161f96b2017-05-07 11:56:00 -07001441 mAppsView.reset();
Winson Chungb745afb2015-03-02 11:51:23 -08001442 }
1443
Adam Cohen9211d422014-10-07 18:14:53 -07001444 if (mLauncherCallbacks != null) {
1445 mLauncherCallbacks.onHomeIntent();
1446 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001447 }
Sunny Goyal5a81c382017-03-20 15:08:06 -07001448 PinItemDragListener.handleDragRequest(this, intent);
Adam Cohened307df2013-10-02 09:37:31 -07001449
Adam Cohen9211d422014-10-07 18:14:53 -07001450 if (mLauncherCallbacks != null) {
1451 mLauncherCallbacks.onNewIntent(intent);
1452 }
Winson15f8b172015-08-19 11:02:39 -07001453
1454 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1455 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1456 // animation.
1457 if (isActionMain) {
Sunny Goyal00c95b82017-10-03 10:29:23 -07001458 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001459
Winson15f8b172015-08-19 11:02:39 -07001460 mWorkspace.post(new Runnable() {
1461 @Override
1462 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001463 if (mWorkspace != null) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001464 mWorkspace.moveToDefaultScreen();
Tonyc17390962015-10-25 17:39:37 -07001465 }
Winson15f8b172015-08-19 11:02:39 -07001466 }
1467 });
1468 }
1469 }
1470
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001471 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001472 }
1473
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001474 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001475 public void onRestoreInstanceState(Bundle state) {
1476 super.onRestoreInstanceState(state);
1477 for (int page: mSynchronouslyBoundPages) {
1478 mWorkspace.restoreInstanceStateForChild(page);
1479 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001480 }
1481
1482 @Override
1483 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001484 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001485 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001486
Adam Cohen21cd0022013-10-09 18:57:02 -07001487 }
Sunny Goyalea609262017-10-25 15:47:38 -07001488 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001489
1490
1491 AbstractFloatingView widgets = AbstractFloatingView
1492 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1493 if (widgets != null) {
1494 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1495 widgets.saveHierarchyState(widgetsState);
1496 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1497 } else {
1498 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1499 }
1500
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001501 // We close any open folders and shortcut containers since they will not be re-opened,
1502 // and we need to make sure this state is reflected.
1503 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001504
Sunny Goyal2100c782016-08-22 16:00:03 -07001505 if (mPendingRequestArgs != null) {
1506 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1507 }
1508 if (mPendingActivityResult != null) {
1509 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001510 }
1511
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001512 super.onSaveInstanceState(outState);
1513
Adam Cohen9211d422014-10-07 18:14:53 -07001514 if (mLauncherCallbacks != null) {
1515 mLauncherCallbacks.onSaveInstanceState(outState);
1516 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001517 }
1518
1519 @Override
1520 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001521 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001522
Sunny Goyal5cc7bbd2017-06-12 09:50:39 -07001523 unregisterReceiver(mReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001524 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001525
Winson Chungcd2b0142011-06-08 16:02:26 -07001526 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001527 // It's possible to receive onDestroy after a new Launcher activity has
1528 // been created. In this case, don't interfere with the new Launcher.
1529 if (mModel.isCurrentCallbacks(this)) {
1530 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001531 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001532 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001533
Sunny Goyal745bad92016-05-02 10:54:12 -07001534 if (mRotationPrefChangeHandler != null) {
1535 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1536 }
1537
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001538 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001539 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001540 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001541 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001542 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001543 mAppWidgetHost = null;
1544
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001545 TextKeyListener.getInstance().release();
Mario Bertschler27288382017-05-24 15:35:09 -07001546 WallpaperColorInfo.getInstance(this).setOnThemeChangeListener(null);
1547
Michael Jurka2ecf9952012-06-18 12:52:28 -07001548 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001549
Tony2917a8b2017-07-31 23:29:42 -07001550 clearPendingBinds();
1551
Adam Cohen9211d422014-10-07 18:14:53 -07001552 if (mLauncherCallbacks != null) {
1553 mLauncherCallbacks.onDestroy();
1554 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001555 }
1556
Sunny Goyalae502842016-06-17 08:43:56 -07001557 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1558 return mAccessibilityDelegate;
1559 }
1560
Adam Cohena9cf38f2011-05-02 15:36:58 -07001561 public DragController getDragController() {
1562 return mDragController;
1563 }
1564
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001565 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001566 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001567 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001568 }
1569
1570 @Override
1571 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1572 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001573 try {
1574 super.startIntentSenderForResult(intent, requestCode,
1575 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1576 } catch (IntentSender.SendIntentException e) {
1577 throw new ActivityNotFoundException();
1578 }
1579 }
1580
Winson Chung70d72102011-08-12 11:24:35 -07001581 /**
1582 * Indicates that we want global search for this activity by setting the globalSearch
1583 * argument for {@link #startSearch} to true.
1584 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001585 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001586 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001587 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001588
Karl Rosaen138a0412009-04-23 19:00:21 -07001589 if (initialQuery == null) {
1590 // Use any text typed in the launcher as the initial query
1591 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001592 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001593 if (appSearchData == null) {
1594 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001595 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001596 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001597
Sunny Goyal6f28e712016-09-13 14:19:29 -07001598 if (mLauncherCallbacks == null ||
1599 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1600 // Starting search from the callbacks failed. Start the default global search.
1601 startGlobalSearch(initialQuery, selectInitialQuery, appSearchData, null);
Ian Parkinson047036e2014-09-01 15:40:53 +01001602 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001603
1604 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001605 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001606 }
1607
Ian Parkinson047036e2014-09-01 15:40:53 +01001608 /**
Michael Jurkaa33411c2012-06-14 16:18:21 -07001609 * Starts the global search activity. This code is a copied from SearchManager
1610 */
Sunny Goyal6f28e712016-09-13 14:19:29 -07001611 public void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001612 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001613 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001614 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1615 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1616 if (globalSearchActivity == null) {
1617 Log.w(TAG, "No global search activity found.");
1618 return;
1619 }
1620 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1621 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1622 intent.setComponent(globalSearchActivity);
1623 // Make sure that we have a Bundle to put source in
1624 if (appSearchData == null) {
1625 appSearchData = new Bundle();
1626 } else {
1627 appSearchData = new Bundle(appSearchData);
1628 }
Adam Cohen9211d422014-10-07 18:14:53 -07001629 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07001630 if (!appSearchData.containsKey("source")) {
1631 appSearchData.putString("source", getPackageName());
1632 }
1633 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1634 if (!TextUtils.isEmpty(initialQuery)) {
1635 intent.putExtra(SearchManager.QUERY, initialQuery);
1636 }
1637 if (selectInitialQuery) {
1638 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1639 }
1640 intent.setSourceBounds(sourceBounds);
1641 try {
1642 startActivity(intent);
1643 } catch (ActivityNotFoundException ex) {
1644 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1645 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001646 }
1647
Winson Chung70d72102011-08-12 11:24:35 -07001648 @Override
Winson Chung70d72102011-08-12 11:24:35 -07001649 public boolean onPrepareOptionsMenu(Menu menu) {
1650 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07001651 if (mLauncherCallbacks != null) {
1652 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
1653 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02001654 return false;
Winson Chung70d72102011-08-12 11:24:35 -07001655 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001656
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001657 @Override
1658 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001659 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001660 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001661 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001662 }
1663
Joe Onorato9c1289c2009-08-17 11:03:03 -04001664 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001665 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001666 }
1667
Adam Cohen517a7f52014-03-01 12:12:59 -08001668 public boolean isWorkspaceLoading() {
1669 return mWorkspaceLoading;
1670 }
1671
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001672 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001673 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001674 }
1675
Sunny Goyal04a324a2017-01-20 21:08:59 -08001676 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001677 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001678 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001679
Sunny Goyal2100c782016-08-22 16:00:03 -07001680 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001681 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001682 if (LOGD) {
1683 Log.d(TAG, "Adding widget from drop");
1684 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001685 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001686 }
1687
Sunny Goyal2100c782016-08-22 16:00:03 -07001688 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001689 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1690 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1691 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001692
Adam Cohenad4e15c2013-10-17 16:21:35 -07001693 Runnable onComplete = new Runnable() {
1694 @Override
1695 public void run() {
1696 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001697 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001698 }
1699 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001700 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001701 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001702 }
1703 }
Romain Guycbb89e42009-06-08 15:52:54 -07001704
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001705 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1706 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001707 info.container = container;
1708 info.screenId = screenId;
1709 if (cell != null) {
1710 info.cellX = cell[0];
1711 info.cellY = cell[1];
1712 }
1713 info.spanX = spanX;
1714 info.spanY = spanY;
1715
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001716 switch (info.itemType) {
1717 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1718 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001719 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001720 break;
1721 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001722 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001723 break;
1724 default:
1725 throw new IllegalStateException("Unknown item type: " + info.itemType);
1726 }
1727 }
1728
Adam Cohenfbba09b2011-07-18 21:43:05 -07001729 /**
1730 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001731 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001732 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001733 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1734 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001735 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1736 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1737 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001738 }
1739
Adam Cohenfbba09b2011-07-18 21:43:05 -07001740 /**
1741 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001742 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001743 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001744 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001745 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001746 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001747 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001748 // In the case where we've prebound the widget, we remove it from the DragLayer
1749 if (LOGD) {
1750 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1751 }
1752 getDragLayer().removeView(hostView);
1753
Adam Cohened66b2b2012-01-23 17:28:51 -08001754 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001755 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001756
1757 // Clear the boundWidget so that it doesn't get destroyed.
1758 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001759 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001760 // In this case, we either need to start an activity to get permission to bind
1761 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001762 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1763 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1764 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1765 this, info.componentName);
1766 } else {
1767 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1768 }
Adam Cohendd70d662012-10-04 16:53:44 -07001769 Bundle options = info.bindOptions;
1770
Sunny Goyalffe83f12014-08-14 17:39:34 -07001771 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1772 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001773 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001774 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001775 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001776 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001777 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001778 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001779 }
1780
Adam Cohendcd297f2013-06-18 13:13:40 -07001781 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001782 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001783 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001784 folderInfo.title = getText(R.string.folder_name);
1785
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001786 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001787 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001788
1789 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001790 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301791 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001792 // Force measure the new folder icon
1793 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1794 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001795 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001796 }
Romain Guycbb89e42009-06-08 15:52:54 -07001797
Winsonc0b52fe2015-09-09 16:38:15 -07001798 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001799 * Unbinds the view for the specified item, and removes the item and all its children.
1800 *
1801 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001802 * @param itemInfo the {@link ItemInfo} for this view.
1803 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001804 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001805 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001806 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001807 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001808 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1809 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001810 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001811 } else {
1812 mWorkspace.removeWorkspaceItem(v);
1813 }
Winsonc0b52fe2015-09-09 16:38:15 -07001814 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001815 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001816 }
1817 } else if (itemInfo instanceof FolderInfo) {
1818 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001819 if (v instanceof FolderIcon) {
1820 ((FolderIcon) v).removeListeners();
1821 }
Winsonc0b52fe2015-09-09 16:38:15 -07001822 mWorkspace.removeWorkspaceItem(v);
1823 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001824 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001825 }
1826 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1827 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001828 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001829 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07001830 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001831 }
1832 } else {
1833 return false;
1834 }
1835 return true;
1836 }
1837
1838 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07001839 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07001840 */
Sunny Goyal56c73602015-09-25 12:55:01 -07001841 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07001842 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07001843 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07001844 // Deleting an app widget ID is a void call but writes to disk before returning
1845 // to the caller...
1846 new AsyncTask<Void, Void, Void>() {
1847 public Void doInBackground(Void ... args) {
1848 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
1849 return null;
1850 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07001851 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07001852 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001853 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001854 }
1855
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001856 @Override
1857 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001858 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001859 }
1860
Joe Onorato88ec0992009-11-19 13:16:06 -08001861 @Override
1862 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07001863 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1864 return;
1865 }
1866
Sunny Goyal45478022015-06-08 16:52:41 -07001867 if (mDragController.isDragging()) {
1868 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001869 return;
1870 }
1871
Jon Mirandafeba90f2016-10-06 10:53:29 -07001872 // Note: There should be at most one log per method call. This is enforced implicitly
1873 // by using if-else statements.
1874 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001875 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
1876 if (topView != null) {
Sunny Goyal37920962017-09-28 13:43:24 -07001877 topView.onBackPressed();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001878 } else if (!isInState(NORMAL)) {
Sunny Goyalea609262017-10-25 15:47:38 -07001879 ued.logActionCommand(Action.Command.BACK, mStateManager.getState().containerType);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001880 mStateManager.goToState(NORMAL);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001881 } else {
1882 // Back button is a no-op here, but give at least some feedback for the button press
1883 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001884 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001885 }
1886
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001887 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001888 * Launches the intent referred by the clicked shortcut.
1889 *
1890 * @param v The view representing the clicked shortcut.
1891 */
1892 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001893 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1894 // view has detached (it's possible for this to happen if the view is removed mid touch).
1895 if (v.getWindowToken() == null) {
1896 return;
1897 }
1898
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001899 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001900 return;
1901 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001902
Adam Cohen1697b792013-09-17 19:08:21 -07001903 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001904 if (isInState(OVERVIEW)) {
Jon Miranda2e61fba2017-02-24 09:12:59 -08001905 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1906 LauncherLogProto.Action.Direction.NONE,
1907 LauncherLogProto.ContainerType.OVERVIEW, mWorkspace.getCurrentPage());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001908 mStateManager.goToState(NORMAL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001909 }
1910 return;
1911 }
1912
1913 if (v instanceof CellLayout) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001914 if (isInState(OVERVIEW)) {
Jon Mirandac6cf4932017-02-07 17:12:36 -08001915 int page = mWorkspace.indexOfChild(v);
1916 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1917 LauncherLogProto.Action.Direction.NONE,
1918 LauncherLogProto.ContainerType.OVERVIEW, page);
1919 mWorkspace.snapToPageFromOverView(page);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001920 mStateManager.goToState(NORMAL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001921 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07001922 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001923 }
1924
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001925 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001926 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001927 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001928 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001929 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001930 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001931 }
Hyunyoung Songd725f642017-08-17 22:26:35 -07001932 } else if ((v instanceof PageIndicator) ||
Adam Cohenbf78f3c2017-07-14 15:58:48 -07001933 (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001934 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07001935 } else if (tag instanceof AppInfo) {
1936 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07001937 } else if (tag instanceof LauncherAppWidgetInfo) {
1938 if (v instanceof PendingAppWidgetHostView) {
1939 onClickPendingWidget((PendingAppWidgetHostView) v);
1940 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001941 }
1942 }
1943
Sunny Goyal70660032015-05-14 00:07:08 -07001944 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07001945 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07001946 return false;
1947 }
1948
Michael Jurkaaf442092010-06-10 17:01:57 -07001949 /**
Sunny Goyalff572272014-07-23 13:58:07 -07001950 * Event handler for the app widget view which has not fully restored.
1951 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001952 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07001953 if (mIsSafeModeEnabled) {
1954 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
1955 return;
1956 }
1957
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001958 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07001959 if (v.isReadyForClickSetup()) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001960 LauncherAppWidgetProviderInfo appWidgetInfo =
1961 mAppWidgetManager.findProvider(info.providerName, info.user);
1962 if (appWidgetInfo == null) {
1963 return;
1964 }
1965 WidgetAddFlowHandler addFlowHandler = new WidgetAddFlowHandler(appWidgetInfo);
1966
Sunny Goyald478c832016-04-01 12:04:16 -07001967 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
1968 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
1969 // This should not happen, as we make sure that an Id is allocated during bind.
1970 return;
1971 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001972 addFlowHandler.startBindFlow(this, info.appWidgetId, info,
1973 REQUEST_BIND_PENDING_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -07001974 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001975 addFlowHandler.startConfigActivity(this, info, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07001976 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001977 } else {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001978 final String packageName = info.providerName.getPackageName();
Sunny Goyale6e72002017-01-12 16:55:36 -08001979 onClickPendingAppItem(v, packageName, info.installProgress >= 0);
Sunny Goyalff572272014-07-23 13:58:07 -07001980 }
1981 }
1982
1983 /**
Adam Cohenbf78f3c2017-07-14 15:58:48 -07001984 * Event handler for the "grid" button or "caret" that appears on the home screen, which
1985 * enters all apps mode. In verticalBarLayout the caret can be seen when all apps is open, and
1986 * so in that case reverses the action.
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001987 *
1988 * @param v The view that was clicked.
1989 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001990 protected void onClickAllAppsButton(View v) {
1991 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001992 if (!isInState(ALL_APPS)) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301993 getUserEventDispatcher().logActionOnControl(Action.Touch.TAP,
1994 ControlType.ALL_APPS_BUTTON);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001995 mStateManager.goToState(ALL_APPS);
Adam Cohen5441a9d2017-07-14 14:22:05 -07001996 } else {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001997 mStateManager.goToState(NORMAL);
Adam Cohen5441a9d2017-07-14 14:22:05 -07001998 }
1999 }
2000
Sunny Goyale6e72002017-01-12 16:55:36 -08002001 private void onClickPendingAppItem(final View v, final String packageName,
2002 boolean downloadStarted) {
2003 if (downloadStarted) {
2004 // If the download has started, simply direct to the market app.
2005 startMarketIntentForPackage(v, packageName);
2006 return;
2007 }
Sunny Goyale03b8122014-10-08 09:55:24 -07002008 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002009 .setTitle(R.string.abandoned_promises_title)
2010 .setMessage(R.string.abandoned_promise_explanation)
Sunny Goyale6e72002017-01-12 16:55:36 -08002011 .setPositiveButton(R.string.abandoned_search, new DialogInterface.OnClickListener() {
2012 @Override
2013 public void onClick(DialogInterface dialogInterface, int i) {
2014 startMarketIntentForPackage(v, packageName);
2015 }
2016 })
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002017 .setNeutralButton(R.string.abandoned_clean_this,
2018 new DialogInterface.OnClickListener() {
2019 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002020 final UserHandle user = Process.myUserHandle();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002021 mWorkspace.removeAbandonedPromise(packageName, user);
2022 }
2023 })
2024 .create().show();
Sunny Goyale6e72002017-01-12 16:55:36 -08002025 }
2026
2027 private void startMarketIntentForPackage(View v, String packageName) {
2028 ItemInfo item = (ItemInfo) v.getTag();
2029 Intent intent = PackageManagerHelper.getMarketIntent(packageName);
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002030 startActivitySafely(v, intent, item);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002031 }
2032
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002033 /**
2034 * Event handler for an app shortcut click.
2035 *
2036 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2037 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002038 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002039 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2040 Object tag = v.getTag();
2041 if (!(tag instanceof ShortcutInfo)) {
2042 throw new IllegalArgumentException("Input must be a Shortcut");
2043 }
2044
2045 // Open shortcut
2046 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002047
Sunny Goyal076839c2017-10-30 13:52:20 -07002048 if (shortcut.isDisabled()) {
2049 if ((shortcut.runtimeStatusFlags &
2050 ~FLAG_DISABLED_SUSPENDED &
2051 ~FLAG_DISABLED_QUIET_USER) == 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002052 // If the app is only disabled because of the above flags, launch activity anyway.
2053 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00002054 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07002055 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
2056 // Use a message specific to this shortcut, if it has one.
2057 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
2058 return;
2059 }
2060 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00002061 int error = R.string.activity_not_available;
Sunny Goyal076839c2017-10-30 13:52:20 -07002062 if ((shortcut.runtimeStatusFlags & FLAG_DISABLED_SAFEMODE) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00002063 error = R.string.safemode_shortcut_error;
Sunny Goyal076839c2017-10-30 13:52:20 -07002064 } else if ((shortcut.runtimeStatusFlags & FLAG_DISABLED_BY_PUBLISHER) != 0 ||
2065 (shortcut.runtimeStatusFlags & FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002066 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00002067 }
2068 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2069 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002070 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002071 }
2072
Chris Wren40c5ed32014-06-24 18:24:23 -04002073 // Check for abandoned promise
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -07002074 if ((v instanceof BubbleTextView) && shortcut.hasPromiseIconUi()) {
Sunny Goyale6e72002017-01-12 16:55:36 -08002075 String packageName = shortcut.intent.getComponent() != null ?
2076 shortcut.intent.getComponent().getPackageName() : shortcut.intent.getPackage();
2077 if (!TextUtils.isEmpty(packageName)) {
2078 onClickPendingAppItem(v, packageName,
2079 shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE));
2080 return;
2081 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002082 }
2083
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002084 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002085 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002086 }
2087
Sunny Goyala7ce1662016-05-31 15:01:35 -07002088 private void startAppShortcutOrInfoActivity(View v) {
2089 ItemInfo item = (ItemInfo) v.getTag();
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002090 Intent intent;
2091 if (item instanceof PromiseAppInfo) {
2092 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) item;
2093 intent = promiseAppInfo.getMarketIntent();
2094 } else {
2095 intent = item.getIntent();
2096 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002097 if (intent == null) {
2098 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002099 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002100 startActivitySafely(v, intent, item);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002101 }
2102
2103 /**
2104 * Event handler for a folder icon click.
2105 *
2106 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2107 */
2108 protected void onClickFolderIcon(View v) {
2109 if (LOGD) Log.d(TAG, "onClickFolder");
2110 if (!(v instanceof FolderIcon)){
2111 throw new IllegalArgumentException("Input must be a FolderIcon");
2112 }
2113
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002114 Folder folder = ((FolderIcon) v).getFolder();
2115 if (!folder.isOpen() && !folder.isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002116 // Open the requested folder
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002117 folder.animateOpen();
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002118 }
Michael Jurka5130e402011-10-13 04:55:35 -07002119 }
2120
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002121 /**
2122 * Event handler for the (Add) Widgets button that appears after a long press
2123 * on the home screen.
2124 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002125 public void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002126 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002127 if (mIsSafeModeEnabled) {
2128 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2129 } else {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002130 WidgetsFullSheet.show(this, true /* animated */);
Adam Cohen9211d422014-10-07 18:14:53 -07002131 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002132 }
2133
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002134 /**
2135 * Event handler for the wallpaper picker button that appears after a long press
2136 * on the home screen.
2137 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002138 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07002139 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002140 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2141 return;
2142 }
2143
Tony Wickham785f7a52015-08-31 17:28:32 -07002144 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2145 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal2100c782016-08-22 16:00:03 -07002146 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002147 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002148 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002149
2150 String pickerPackage = getString(R.string.wallpaper_picker_package);
Tony85efb4b2017-06-02 13:06:10 -07002151 boolean hasTargetPackage = !TextUtils.isEmpty(pickerPackage);
2152 if (hasTargetPackage) {
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002153 intent.setPackage(pickerPackage);
2154 }
2155
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002156 intent.setSourceBounds(getViewBounds(v));
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002157 try {
2158 startActivityForResult(intent, REQUEST_PICK_WALLPAPER,
2159 // If there is no target package, use the default intent chooser animation
2160 hasTargetPackage ? getActivityLaunchOptions(v) : null);
2161 } catch (ActivityNotFoundException e) {
2162 setWaitingForResult(null);
2163 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2164 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002165 }
2166
2167 /**
2168 * Event handler for a click on the settings button that appears after a long press
2169 * on the home screen.
2170 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002171 public void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002172 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyalf3a09f92016-08-25 09:45:14 -07002173 Intent intent = new Intent(Intent.ACTION_APPLICATION_PREFERENCES)
2174 .setPackage(getPackageName());
2175 intent.setSourceBounds(getViewBounds(v));
Sunny Goyal53f93b92017-05-04 11:23:29 -07002176 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Sunny Goyalf3a09f92016-08-25 09:45:14 -07002177 startActivity(intent, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002178 }
2179
Sunny Goyala7ce1662016-05-31 15:01:35 -07002180 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002181 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002182 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2183 try {
2184 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2185 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2186 // is enabled by default on NYC.
2187 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2188 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002189
2190 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2191 String id = ((ShortcutInfo) info).getDeepShortcutId();
2192 String packageName = intent.getPackage();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05302193 DeepShortcutManager.getInstance(this).startShortcut(
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002194 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002195 } else {
2196 // Could be launching some bookkeeping activity
2197 startActivity(intent, optsBundle);
2198 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002199 } finally {
2200 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002201 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002202 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002203 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2204 // corresponding permission. Show the appropriate permission prompt if that
2205 // is the case.
2206 if (intent.getComponent() == null
2207 && Intent.ACTION_CALL.equals(intent.getAction())
2208 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2209 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002210
2211 setWaitingForResult(PendingRequestArgs
2212 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002213 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2214 REQUEST_PERMISSION_CALL_PHONE);
2215 } else {
2216 // No idea why this was thrown.
2217 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002218 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002219 }
2220 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002221
Sunny Goyalfe770c92016-09-27 14:38:58 -07002222 @TargetApi(Build.VERSION_CODES.M)
Tonye3c59252017-05-02 21:32:27 -07002223 public Bundle getActivityLaunchOptions(View v) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002224 if (Utilities.ATLEAST_MARSHMALLOW) {
2225 int left = 0, top = 0;
2226 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07002227 if (v instanceof BubbleTextView) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002228 // Launch from center of icon, not entire view
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07002229 Drawable icon = ((BubbleTextView) v).getIcon();
Sunny Goyala7ce1662016-05-31 15:01:35 -07002230 if (icon != null) {
2231 Rect bounds = icon.getBounds();
2232 left = (width - bounds.width()) / 2;
2233 top = v.getPaddingTop();
2234 width = bounds.width();
2235 height = bounds.height();
2236 }
2237 }
2238 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2239 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2240 // On L devices, we use the device default slide-up transition.
2241 // On L MR1 devices, we use a custom version of the slide-up transition which
2242 // doesn't have the delay present in the device default.
2243 return ActivityOptions.makeCustomAnimation(
2244 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2245 }
2246 return null;
2247 }
2248
Tonye3c59252017-05-02 21:32:27 -07002249 public Rect getViewBounds(View v) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002250 int[] pos = new int[2];
2251 v.getLocationOnScreen(pos);
2252 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2253 }
2254
Sunny Goyala7ce1662016-05-31 15:01:35 -07002255 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002256 mAppLaunchSuccess = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002257 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2258 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002259 return mAppLaunchSuccess;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002260 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002261 // Only launch using the new animation if the shortcut has not opted out (this is a
2262 // private contract between launcher and may be ignored in the future).
2263 boolean useLaunchAnimation = (v != null) &&
2264 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2265 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2266
Sunny Goyal24bb66a2017-03-21 15:12:01 -07002267 UserHandle user = item == null ? null : item.user;
Sunny Goyala7ce1662016-05-31 15:01:35 -07002268
2269 // Prepare intent
2270 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2271 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002272 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002273 }
2274 try {
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002275 if (Utilities.ATLEAST_MARSHMALLOW
2276 && (item instanceof ShortcutInfo)
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002277 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002278 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyale6e72002017-01-12 16:55:36 -08002279 && !((ShortcutInfo) item).isPromise()) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002280 // Shortcuts need some special checks due to legacy reasons.
2281 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002282 } else if (user == null || user.equals(Process.myUserHandle())) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002283 // Could be launching some bookkeeping activity
2284 startActivity(intent, optsBundle);
2285 } else {
2286 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2287 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2288 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002289
2290 if (v instanceof BubbleTextView) {
2291 // This is set to the view that launched the activity that navigated the user away
2292 // from launcher. Since there is no callback for when the activity has finished
2293 // launching, enable the press state and keep this reference to reset the press
2294 // state when we return to launcher.
2295 BubbleTextView btv = (BubbleTextView) v;
2296 btv.setStayPressed(true);
2297 setOnResumeCallback(btv);
2298 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002299 mAppLaunchSuccess = true;
2300 getUserEventDispatcher().logAppLaunch(v, intent); // TODO for discovered apps b/35802115
Sunny Goyala7ce1662016-05-31 15:01:35 -07002301 } catch (ActivityNotFoundException|SecurityException e) {
2302 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2303 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2304 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002305 return mAppLaunchSuccess;
Michael Jurka86a720e2012-05-09 11:23:23 -07002306 }
2307
Tony Wickhamdadb3042016-02-24 11:07:00 -08002308 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07002309 public boolean dispatchTouchEvent(MotionEvent ev) {
2310 mLastDispatchTouchEventX = ev.getX();
2311 return super.dispatchTouchEvent(ev);
2312 }
2313
2314 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002315 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002316 if (!isDraggingEnabled()) return false;
2317 if (isWorkspaceLocked()) return false;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002318 if (!isInState(NORMAL) && !isInState(OVERVIEW)) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002319
Jon Miranda51f037d2016-11-07 08:37:20 -08002320 boolean ignoreLongPressToOverview =
2321 mDeviceProfile.shouldIgnoreLongPressToOverview(mLastDispatchTouchEventX);
Jon Miranda379198e2016-09-23 08:54:40 -07002322
Adam Cohen1697b792013-09-17 19:08:21 -07002323 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002324 if (!isInState(OVERVIEW)) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002325 if (!mWorkspace.isTouchActive() && !ignoreLongPressToOverview) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302326 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2327 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002328 mWorkspace.getCurrentPage());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002329 getStateManager().goToState(OVERVIEW);
Adam Cohen93c97562013-09-26 13:48:01 -07002330 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2331 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2332 return true;
2333 } else {
2334 return false;
2335 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002336 } else {
2337 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002338 }
Adam Cohen1697b792013-09-17 19:08:21 -07002339 }
2340
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002341 CellLayout.CellInfo longClickCellInfo = null;
2342 View itemUnderLongClick = null;
2343 if (v.getTag() instanceof ItemInfo) {
2344 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002345 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002346 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07002347 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002348 }
2349
Winson Chung3d503fb2011-07-13 17:25:49 -07002350 // The hotseat touch handling does not go through Workspace, and we always allow long press
2351 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002352 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002353 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002354 // User long pressed on empty space
Sunny Goyalc99cb172017-10-19 16:15:09 -07002355 if (mWorkspace.isPageRearrangeEnabled()) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002356 mWorkspace.startReordering(v);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302357 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2358 Action.Direction.NONE, ContainerType.OVERVIEW);
Adam Cohend3ecce92013-09-16 14:58:00 -07002359 } else {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002360 if (ignoreLongPressToOverview) {
Jon Miranda379198e2016-09-23 08:54:40 -07002361 return false;
2362 }
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302363 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2364 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002365 mWorkspace.getCurrentPage());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002366 getStateManager().goToState(OVERVIEW);
Adam Cohendedbd962013-07-11 14:21:49 -07002367 }
Jon Miranda379198e2016-09-23 08:54:40 -07002368 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2369 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002370 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07002371 final boolean isAllAppsButton =
2372 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
2373 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
2374 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05002375 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002376 // User long pressed on an item
Tony Wickham10236d62016-09-28 12:49:25 -07002377 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002378 }
2379 }
2380 }
2381 return true;
2382 }
2383
Winson Chung3d503fb2011-07-13 17:25:49 -07002384 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08002385 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07002386 return mHotseat != null && layout != null &&
2387 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2388 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002389
Winson Chung3d503fb2011-07-13 17:25:49 -07002390 /**
2391 * Returns the CellLayout of the specified container at the specified screen.
2392 */
Sunny Goyal92820592015-03-02 11:31:35 -08002393 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002394 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2395 if (mHotseat != null) {
2396 return mHotseat.getLayout();
2397 } else {
2398 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002399 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002400 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002401 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002402 }
2403 }
2404
Michael Jurkae326f182011-11-21 14:05:46 -08002405 @Override
2406 public void onTrimMemory(int level) {
2407 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07002408 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2409 // The widget preview db can result in holding onto over
2410 // 3MB of memory for caching which isn't necessary.
2411 SQLiteDatabase.releaseMemory();
2412
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08002413 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07002414 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08002415 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01002416 if (mLauncherCallbacks != null) {
2417 mLauncherCallbacks.onTrimMemory(level);
2418 }
Michael Jurkae326f182011-11-21 14:05:46 -08002419 }
2420
Michael Jurkad7c28052012-04-27 15:43:36 -07002421 @Override
2422 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07002423 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07002424 final List<CharSequence> text = event.getText();
2425 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07002426 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002427 // TODO: When can workspace be null?
2428 text.add(mWorkspace == null
2429 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07002430 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07002431 return result;
2432 }
2433
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002434 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07002435 * If the activity is currently paused, signal that we need to run the passed Runnable
2436 * in onResume.
2437 *
2438 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07002439 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
2440 * wrong when we're not running, and if the activity comes back to what the configuration was
2441 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07002442 *
2443 * Implementation of the method from LauncherModel.Callbacks.
2444 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07002445 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07002446 */
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002447 @Thunk boolean waitUntilResume(Runnable run) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002448 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002449 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002450 if (run instanceof RunnableWithId) {
2451 // Remove any runnables which have the same id
2452 while (mBindOnResumeCallbacks.remove(run)) { }
Michael Jurkac402cd92013-05-20 15:49:32 +02002453 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07002454 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07002455 return true;
2456 } else {
2457 return false;
2458 }
2459 }
2460
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002461 public void setOnResumeCallback(OnResumeCallback callback) {
2462 if (mOnResumeCallback != null) {
2463 mOnResumeCallback.onLauncherResume();
2464 }
2465 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07002466 }
2467
Michael Jurka7607c2f2013-04-03 14:33:19 -07002468 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002469 * If the activity is currently paused, signal that we need to re-run the loader
2470 * in onResume.
2471 *
2472 * This needs to be called from incoming places where resources might have been loaded
2473 * while we are paused. That is becaues the Configuration might be wrong
2474 * when we're not running, and if it comes back to what it was when we
2475 * were paused, we are not restarted.
2476 *
2477 * Implementation of the method from LauncherModel.Callbacks.
2478 *
2479 * @return true if we are currently paused. The caller might be able to
2480 * skip some work in that case since we will come back again.
2481 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002482 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002483 public boolean setLoadOnResume() {
2484 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002485 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002486 mOnResumeNeedsLoad = true;
2487 return true;
2488 } else {
2489 return false;
2490 }
2491 }
2492
2493 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002494 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002495 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002496 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002497 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002498 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002499 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002500 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07002501 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07002502 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002503 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002504
Joe Onorato9c1289c2009-08-17 11:03:03 -04002505 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07002506 * Clear any pending bind callbacks. This is called when is loader is planning to
2507 * perform a full rebind from scratch.
2508 */
2509 @Override
2510 public void clearPendingBinds() {
2511 mBindOnResumeCallbacks.clear();
2512 if (mPendingExecutor != null) {
2513 mPendingExecutor.markCompleted();
2514 mPendingExecutor = null;
2515 }
2516 }
2517
2518 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002519 * Refreshes the shortcuts shown on the workspace.
2520 *
2521 * Implementation of the method from LauncherModel.Callbacks.
2522 */
2523 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002524 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002525 // Floating panels (except the full widget sheet) are associated with individual icons. If
2526 // we are starting a fresh bind, close all such panels as all the icons are about
2527 // to go away.
2528 AbstractFloatingView.closeOpenViews(this, true,
2529 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08002530
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002531 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08002532
Winson Chungd64d1762013-08-20 14:37:16 -07002533 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07002534 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07002535 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07002536
Winson Chung3d503fb2011-07-13 17:25:49 -07002537 if (mHotseat != null) {
2538 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002539 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002540 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002541 }
2542
Adam Cohendcd297f2013-06-18 13:13:40 -07002543 @Override
2544 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002545 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002546 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
2547 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002548 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
2549 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002550 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002551 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
2552 // If there are no screens, we need to have an empty screen
2553 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07002554 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002555 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07002556
Winsonc7d2e832016-07-28 12:24:55 -07002557 // After we have added all the screens, if the wallpaper was locked to the default state,
2558 // then notify to indicate that it can be released and a proper wallpaper offset can be
2559 // computed before the next layout
2560 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07002561 }
2562
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002563 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002564 int count = orderedScreenIds.size();
2565 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002566 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002567 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002568 // No need to bind the first screen, as its always bound.
2569 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
2570 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002571 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002572 }
2573
Sunny Goyalb23980c2017-08-17 07:45:25 -07002574 @Override
Winson Chungd64d1762013-08-20 14:37:16 -07002575 public void bindAppsAdded(final ArrayList<Long> newScreens,
2576 final ArrayList<ItemInfo> addNotAnimated,
Sunny Goyalb23980c2017-08-17 07:45:25 -07002577 final ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07002578 Runnable r = new Runnable() {
2579 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002580 bindAppsAdded(newScreens, addNotAnimated, addAnimated);
Winson Chungd64d1762013-08-20 14:37:16 -07002581 }
2582 };
2583 if (waitUntilResume(r)) {
2584 return;
2585 }
2586
Winson Chungd64d1762013-08-20 14:37:16 -07002587 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08002588 if (newScreens != null) {
2589 bindAddScreens(newScreens);
2590 }
Winson Chungd64d1762013-08-20 14:37:16 -07002591
2592 // We add the items without animation on non-visible pages, and with
2593 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08002594 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002595 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002596 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002597 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002598 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07002599 }
Winson Chungc58497e2013-09-03 17:48:37 -07002600
Winson Chung87412982013-10-03 18:34:14 -07002601 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07002602 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002603 }
2604
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002605 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002606 * Bind the items start-end from the list.
2607 *
2608 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002609 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07002610 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07002611 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07002612 Runnable r = new Runnable() {
2613 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002614 bindItems(items, forceAnimateIcons);
Winson Chungd64d1762013-08-20 14:37:16 -07002615 }
2616 };
2617 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002618 return;
2619 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002620
Sunny Goyal3be633b2016-12-08 09:59:25 -08002621 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07002622 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002623 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07002624 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07002625 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002626 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07002627 int end = items.size();
2628 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002629 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002630
2631 // Short circuit if we are loading dock items for a configuration which has no dock
2632 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2633 mHotseat == null) {
2634 continue;
2635 }
2636
Sunny Goyal639e9062015-08-19 19:17:06 -07002637 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002638 switch (item.itemType) {
2639 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2640 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08002641 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07002642 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07002643 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002644 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002645 }
2646 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07002647 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002648 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002649 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002650 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002651 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07002652 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2653 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07002654 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002655 if (view == null) {
2656 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002657 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08002658 break;
2659 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002660 default:
2661 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002662 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002663
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002664 /*
2665 * Remove colliding items.
2666 */
2667 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
2668 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
2669 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
2670 View v = cl.getChildAt(item.cellX, item.cellY);
2671 Object tag = v.getTag();
2672 String desc = "Collision while binding workspace item: " + item
2673 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08002674 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002675 throw (new RuntimeException(desc));
2676 } else {
2677 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002678 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002679 continue;
2680 }
2681 }
2682 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302683 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07002684 if (animateIcons) {
2685 // Animate all the applications up now
2686 view.setAlpha(0f);
2687 view.setScaleX(0f);
2688 view.setScaleY(0f);
2689 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08002690 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07002691 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002692 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07002693
Winson Chung997a9232013-07-24 15:33:46 -07002694 if (animateIcons) {
2695 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08002696 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07002697 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08002698 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07002699 final Runnable startBounceAnimRunnable = new Runnable() {
2700 public void run() {
2701 anim.playTogether(bounceAnims);
2702 anim.start();
2703 }
2704 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08002705 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07002706 // We post the animation slightly delayed to prevent slowdowns
2707 // when we are loading right after we return to launcher.
2708 mWorkspace.postDelayed(new Runnable() {
2709 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00002710 if (mWorkspace != null) {
2711 mWorkspace.snapToPage(newScreenIndex);
2712 mWorkspace.postDelayed(startBounceAnimRunnable,
2713 NEW_APPS_ANIMATION_DELAY);
2714 }
Winson Chung94d67682013-09-25 16:29:40 -07002715 }
2716 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07002717 } else {
2718 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07002719 }
2720 }
Winson Chung64359a52013-07-08 17:17:08 -07002721 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002722 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002723 }
2724
Joe Onorato9c1289c2009-08-17 11:03:03 -04002725 /**
2726 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002727 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07002728 public void bindAppWidget(LauncherAppWidgetInfo item) {
2729 View view = inflateAppWidget(item);
2730 if (view != null) {
2731 mWorkspace.addInScreen(view, item);
2732 mWorkspace.requestLayout();
2733 }
2734 }
2735
2736 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002737 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302738 PendingAppWidgetHostView view =
2739 new PendingAppWidgetHostView(this, item, mIconCache, true);
2740 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002741 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002742 }
2743
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002744 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002745
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002746 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08002747
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002748 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2749 // If the provider is not ready, bind as a pending widget.
2750 appWidgetInfo = null;
2751 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2752 // The widget id is not valid. Try to find the widget based on the provider info.
2753 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
2754 } else {
2755 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
2756 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002757
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002758 // If the provider is ready, but the width is not yet restored, try to restore it.
2759 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
2760 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07002761 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002762 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
2763 + " belongs to component " + item.providerName
2764 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002765 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002766 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002767 }
Sunny Goyalff572272014-07-23 13:58:07 -07002768
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002769 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002770 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07002771 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2772 // Id has not been allocated yet. Allocate a new id.
2773 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
2774 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07002775
Sunny Goyald478c832016-04-01 12:04:16 -07002776 // Also try to bind the widget. If the bind fails, the user will be shown
2777 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08002778 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07002779 pendingInfo.spanX = item.spanX;
2780 pendingInfo.spanY = item.spanY;
2781 pendingInfo.minSpanX = item.minSpanX;
2782 pendingInfo.minSpanY = item.minSpanY;
2783 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07002784
2785 boolean isDirectConfig =
2786 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2787 if (isDirectConfig && item.bindOptions != null) {
2788 Bundle newOptions = item.bindOptions.getExtras();
2789 if (options != null) {
2790 newOptions.putAll(options);
2791 }
2792 options = newOptions;
2793 }
Sunny Goyald478c832016-04-01 12:04:16 -07002794 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2795 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002796
Sunny Goyal86df1382016-08-10 15:03:22 -07002797 // We tried to bind once. If we were not able to bind, we would need to
2798 // go through the permission dialog, which means we cannot skip the config
2799 // activity.
2800 item.bindOptions = null;
2801 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2802
Sunny Goyald478c832016-04-01 12:04:16 -07002803 // Bind succeeded
2804 if (success) {
2805 // If the widget has a configure activity, it is still needs to set it up,
2806 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002807 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002808 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2809 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002810 }
Sunny Goyald478c832016-04-01 12:04:16 -07002811
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002812 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002813 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002814 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002815 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002816 // The widget was marked as UI not ready, but there is no configure activity to
2817 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002818 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002819 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002820 }
Sunny Goyalff572272014-07-23 13:58:07 -07002821 }
2822
Sunny Goyald5462aa2016-11-22 16:52:39 +05302823 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002824 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002825 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002826 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002827 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07002828 deleteWidgetInfo(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002829 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002830 }
2831
Sunny Goyal233ee962015-08-03 13:05:01 -07002832 item.minSpanX = appWidgetInfo.minSpanX;
2833 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302834 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002835 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302836 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002837 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302838 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002839
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002840 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002841 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002842 }
2843
Sunny Goyalff572272014-07-23 13:58:07 -07002844 /**
2845 * Restores a pending widget.
2846 *
2847 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002848 */
Sunny Goyald478c832016-04-01 12:04:16 -07002849 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002850 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2851 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2852 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002853 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002854 }
2855
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002856 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002857 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002858 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2859 info.pendingItemInfo = null;
2860 }
Sunny Goyalff572272014-07-23 13:58:07 -07002861
Sunny Goyalba47faa2017-10-04 16:50:57 -07002862 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
2863 view.reinflate();
2864 }
2865
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002866 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002867 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002868 }
2869
Adam Cohen1462de32012-07-24 22:34:36 -07002870 public void onPageBoundSynchronously(int page) {
2871 mSynchronouslyBoundPages.add(page);
2872 }
2873
Sunny Goyal527c7d32015-08-28 15:19:36 -07002874 @Override
2875 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2876 if (mPendingExecutor != null) {
2877 mPendingExecutor.markCompleted();
2878 }
2879 mPendingExecutor = executor;
2880 executor.attachTo(this);
2881 }
2882
2883 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2884 if (mPendingExecutor == executor) {
2885 mPendingExecutor = null;
2886 }
2887 }
2888
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002889 @Override
2890 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
2891 Runnable r = new Runnable() {
2892 public void run() {
2893 finishFirstPageBind(executor);
2894 }
2895 };
2896 if (waitUntilResume(r)) {
2897 return;
2898 }
2899
2900 Runnable onComplete = new Runnable() {
2901 @Override
2902 public void run() {
2903 if (executor != null) {
2904 executor.onLoadAnimationCompleted();
2905 }
2906 }
2907 };
2908 if (mDragLayer.getAlpha() < 1) {
2909 mDragLayer.animate().alpha(1).withEndAction(onComplete).start();
2910 } else {
2911 onComplete.run();
2912 }
2913 }
2914
Joe Onorato9c1289c2009-08-17 11:03:03 -04002915 /**
2916 * Callback saying that there aren't any more items to bind.
2917 *
2918 * Implementation of the method from LauncherModel.Callbacks.
2919 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002920 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07002921 Runnable r = new Runnable() {
2922 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002923 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07002924 }
2925 };
2926 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002927 return;
2928 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002929 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002930 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002931
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002932 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002933
Sunny Goyal2100c782016-08-22 16:00:03 -07002934 if (mPendingActivityResult != null) {
2935 handleActivityResult(mPendingActivityResult.requestCode,
2936 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2937 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002938 }
2939
Sunny Goyala474a9b2017-05-04 16:47:11 -07002940 InstallShortcutReceiver.disableAndFlushInstallQueue(
2941 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002942
Tony Wickham010d2552017-01-20 08:15:28 -08002943 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002944 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002945 }
2946
Winson Chunga2413752012-04-03 14:22:34 -07002947 private boolean canRunNewAppsAnimation() {
2948 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002949 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002950 }
2951
Winson Chungc9168342013-06-26 14:54:55 -07002952 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002953 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07002954 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
2955 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002956 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002957 return bounceAnim;
2958 }
2959
Adam Cohen27772732013-11-11 14:00:56 +00002960 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07002961 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00002962 }
2963
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002964 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002965 * Add the icons for all apps.
2966 *
2967 * Implementation of the method from LauncherModel.Callbacks.
2968 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002969 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002970 Runnable r = new RunnableWithId(RUNNABLE_ID_BIND_APPS) {
2971 public void run() {
2972 bindAllApplications(apps);
2973 }
2974 };
2975 if (waitUntilResume(r)) {
Winson Chungbb785c62015-05-05 10:05:08 -07002976 return;
2977 }
2978
Winson Chungb745afb2015-03-02 11:51:23 -08002979 if (mAppsView != null) {
Tony2917a8b2017-07-31 23:29:42 -07002980 Executor pendingExecutor = getPendingExecutor();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002981 if (pendingExecutor != null && !isInState(ALL_APPS)) {
Tony2917a8b2017-07-31 23:29:42 -07002982 // Wait until the fade in animation has finished before setting all apps list.
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002983 pendingExecutor.execute(r);
Tony2917a8b2017-07-31 23:29:42 -07002984 return;
2985 }
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002986
Winson Chungb745afb2015-03-02 11:51:23 -08002987 mAppsView.setApps(apps);
2988 }
Adam Cohen9211d422014-10-07 18:14:53 -07002989 if (mLauncherCallbacks != null) {
2990 mLauncherCallbacks.bindAllApplications(apps);
2991 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002992 }
2993
2994 /**
Tony2917a8b2017-07-31 23:29:42 -07002995 * Returns an Executor that will run after the launcher is first drawn (including after the
2996 * initial fade in animation). Returns null if the first draw has already occurred.
2997 */
2998 public @Nullable Executor getPendingExecutor() {
2999 return mPendingExecutor != null && mPendingExecutor.canQueue() ? mPendingExecutor : null;
3000 }
3001
3002 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003003 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
3004 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
3005 */
3006 @Override
3007 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08003008 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07003009 }
3010
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003011 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003012 * A package was updated.
3013 *
3014 * Implementation of the method from LauncherModel.Callbacks.
3015 */
Sunny Goyalb23980c2017-08-17 07:45:25 -07003016 public void bindAppsAddedOrUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003017 Runnable r = new Runnable() {
3018 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07003019 bindAppsAddedOrUpdated(apps);
Winson Chungd64d1762013-08-20 14:37:16 -07003020 }
3021 };
3022 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003023 return;
3024 }
3025
Winson Chungb745afb2015-03-02 11:51:23 -08003026 if (mAppsView != null) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07003027 mAppsView.addOrUpdateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07003028 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003029 }
3030
Sunny Goyal4390ace2014-10-13 11:33:11 -07003031 @Override
Mario Bertschler08ffaae2017-03-20 11:30:27 -07003032 public void bindPromiseAppProgressUpdated(final PromiseAppInfo app) {
3033 Runnable r = new Runnable() {
3034 public void run() {
3035 bindPromiseAppProgressUpdated(app);
3036 }
3037 };
3038 if (waitUntilResume(r)) {
3039 return;
3040 }
3041
3042 if (mAppsView != null) {
3043 mAppsView.updatePromiseAppProgress(app);
3044 }
3045 }
3046
3047 @Override
Sunny Goyal4390ace2014-10-13 11:33:11 -07003048 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
3049 Runnable r = new Runnable() {
3050 public void run() {
3051 bindWidgetsRestored(widgets);
3052 }
3053 };
3054 if (waitUntilResume(r)) {
3055 return;
3056 }
3057 mWorkspace.widgetsRestored(widgets);
3058 }
3059
Joe Onorato9c1289c2009-08-17 11:03:03 -04003060 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003061 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003062 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003063 *
3064 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003065 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07003066 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003067 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003068 Runnable r = new Runnable() {
3069 public void run() {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003070 bindShortcutsChanged(updated, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003071 }
3072 };
3073 if (waitUntilResume(r)) {
3074 return;
3075 }
3076
Sunny Goyal4390ace2014-10-13 11:33:11 -07003077 if (!updated.isEmpty()) {
3078 mWorkspace.updateShortcuts(updated);
3079 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003080 }
3081
3082 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003083 * Update the state of a package, typically related to install state.
3084 *
3085 * Implementation of the method from LauncherModel.Callbacks.
3086 */
Sunny Goyale755d462014-07-22 13:48:29 -07003087 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07003088 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
3089 Runnable r = new Runnable() {
3090 public void run() {
3091 bindRestoreItemsChange(updates);
3092 }
3093 };
3094 if (waitUntilResume(r)) {
3095 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003096 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003097
Sunny Goyal756adbc2015-04-16 15:20:51 -07003098 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07003099 }
3100
3101 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003102 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07003103 * in addition to specific applications to remove, the reason being that
3104 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003105 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07003106 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003107 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07003108 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003109 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Winson Chungd64d1762013-08-20 14:37:16 -07003110 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003111 public void run() {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003112 bindWorkspaceComponentsRemoved(matcher);
Winson Chung83892cc2013-05-01 16:53:33 -07003113 }
Winson Chungd64d1762013-08-20 14:37:16 -07003114 };
3115 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003116 return;
3117 }
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003118 mWorkspace.removeItemsByMatcher(matcher);
3119 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003120 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003121
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003122 @Override
3123 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
3124 Runnable r = new Runnable() {
3125 public void run() {
3126 bindAppInfosRemoved(appInfos);
3127 }
3128 };
3129 if (waitUntilResume(r)) {
3130 return;
Joe Onorato36115782010-06-17 13:28:48 -04003131 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003132
Winson Chungdf95eb12013-10-16 14:57:07 -07003133 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08003134 if (mAppsView != null) {
3135 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07003136 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003137 }
Joe Onoratobe386092009-11-17 17:32:16 -08003138
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003139 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07003140 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
3141 mPopupDataProvider.setAllWidgets(allWidgets);
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003142 Runnable r = new RunnableWithId(RUNNABLE_ID_BIND_WIDGETS) {
3143 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07003144 public void run() {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003145 bindAllWidgets(allWidgets);
Winson Chung83892cc2013-05-01 16:53:33 -07003146 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003147 };
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003148 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003149 return;
3150 }
3151
Tony Wickham26b17462017-03-20 17:12:24 -07003152 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
3153 if (topView != null) {
3154 topView.onWidgetsBound();
3155 }
3156 }
3157
Tony Wickham86222d22017-03-29 15:30:43 -07003158 /**
3159 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
3160 * refreshes the widgets and shortcuts associated with the given package/user
3161 */
3162 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07003163 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003164 }
3165
Sunny Goyalc99cb172017-10-19 16:15:09 -07003166 public boolean isRotationEnabled () {
3167 return mRotationEnabled;
Winson Chung400438b2011-01-16 17:53:48 -08003168 }
3169
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003170 private boolean shouldShowDiscoveryBounce() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07003171 return isInState(NORMAL)
Sunny Goyalbe93f262017-10-20 17:05:27 -07003172 && !mSharedPrefs.getBoolean(AllAppsState.APPS_VIEW_SHOWN, false)
Sunny Goyalf9403d92017-10-18 10:55:56 -07003173 && !UserManagerCompat.getInstance(this).isDemoUser();
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003174 }
3175
Winson Chung80baf5a2010-08-09 16:03:15 -07003176 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003177 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08003178 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003179 @Override
3180 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3181 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003182
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003183 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
3184 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07003185 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003186 writer.println(prefix + " Homescreen " + i);
3187
3188 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
3189 for (int j = 0; j < layout.getChildCount(); j++) {
3190 Object tag = layout.getChildAt(j).getTag();
3191 if (tag != null) {
3192 writer.println(prefix + " " + tag.toString());
3193 }
3194 }
3195 }
3196
3197 writer.println(prefix + " Hotseat");
3198 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07003199 for (int j = 0; j < layout.getChildCount(); j++) {
3200 Object tag = layout.getChildAt(j).getTag();
3201 if (tag != null) {
3202 writer.println(prefix + " " + tag.toString());
3203 }
Adam Cohen4caf2982013-08-20 18:54:31 -07003204 }
Sunny Goyala1365452015-10-01 15:46:24 -07003205
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003206 try {
3207 FileLog.flushAll(writer);
3208 } catch (Exception e) {
3209 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07003210 }
3211 }
3212
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003213 writer.println(prefix + "Misc:");
3214 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
3215 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
3216 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
3217
3218 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003219
Adam Cohen9211d422014-10-07 18:14:53 -07003220 if (mLauncherCallbacks != null) {
3221 mLauncherCallbacks.dump(prefix, fd, writer, args);
3222 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003223 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003224
Sunny Goyal66b24572016-09-21 15:57:55 -07003225 @Override
3226 @TargetApi(Build.VERSION_CODES.N)
3227 public void onProvideKeyboardShortcuts(
3228 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
3229
3230 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07003231 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07003232 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
3233 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
3234 }
3235 View currentFocus = getCurrentFocus();
3236 if (new CustomActionsPopup(this, currentFocus).canShow()) {
3237 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
3238 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
3239 }
Tony18c4aa42017-05-10 21:23:57 -05003240 if (currentFocus.getTag() instanceof ItemInfo
3241 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
Sunny Goyal66b24572016-09-21 15:57:55 -07003242 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
3243 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
3244 }
3245 if (!shortcutInfos.isEmpty()) {
3246 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
3247 }
3248
3249 super.onProvideKeyboardShortcuts(data, menu, deviceId);
3250 }
3251
3252 @Override
3253 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
3254 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
3255 switch (keyCode) {
3256 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07003257 if (isInState(NORMAL)) {
3258 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07003259 return true;
3260 }
3261 break;
3262 case KeyEvent.KEYCODE_S: {
3263 View focusedView = getCurrentFocus();
3264 if (focusedView instanceof BubbleTextView
3265 && focusedView.getTag() instanceof ItemInfo
3266 && mAccessibilityDelegate.performAction(focusedView,
3267 (ItemInfo) focusedView.getTag(),
3268 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal10a1bd02017-10-09 14:56:21 -07003269 BaseActionPopup.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07003270 return true;
3271 }
3272 break;
3273 }
3274 case KeyEvent.KEYCODE_O:
3275 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
3276 return true;
3277 }
3278 break;
3279 }
3280 }
3281 return super.onKeyShortcut(keyCode, event);
3282 }
3283
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07003284 public static Launcher getLauncher(Context context) {
3285 if (context instanceof Launcher) {
3286 return (Launcher) context;
3287 }
3288 return ((Launcher) ((ContextWrapper) context).getBaseContext());
3289 }
3290
Sunny Goyal5a81c382017-03-20 15:08:06 -07003291 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener {
Sunny Goyal745bad92016-05-02 10:54:12 -07003292
3293 @Override
3294 public void onSharedPreferenceChanged(
3295 SharedPreferences sharedPreferences, String key) {
3296 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
Sunny Goyale13d4642017-05-22 15:58:42 -07003297 // Recreate the activity so that it initializes the rotation preference again.
3298 recreate();
Sunny Goyal745bad92016-05-02 10:54:12 -07003299 }
3300 }
Sunny Goyal745bad92016-05-02 10:54:12 -07003301 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07003302
3303 /**
3304 * Callback for listening for onResume
3305 */
3306 public interface OnResumeCallback {
3307
3308 void onLauncherResume();
3309 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003310}