blob: 2e930662d7503ead1de7bffbc00eb4577b532dfa [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;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070091import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070093import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070094
Sunny Goyal651077b2014-06-30 14:15:31 -070095import com.android.launcher3.DropTarget.DragObject;
Sunny Goyala7ce1662016-05-31 15:01:35 -070096import com.android.launcher3.LauncherSettings.Favorites;
Jon Mirandac476d6e2017-05-04 16:30:01 -070097import com.android.launcher3.Workspace.ItemOperator;
Sunny Goyalae502842016-06-17 08:43:56 -070098import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070099import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700100import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700101import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100102import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -0700103import com.android.launcher3.compat.LauncherAppsCompatVO;
Adam Cohen39933482017-09-29 16:43:51 -0700104import com.android.launcher3.compat.UserManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -0800105import com.android.launcher3.config.FeatureFlags;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700106import com.android.launcher3.dragndrop.DragController;
107import com.android.launcher3.dragndrop.DragLayer;
Sunny Goyal94b510c2016-08-16 15:36:48 -0700108import com.android.launcher3.dragndrop.DragOptions;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700109import com.android.launcher3.dragndrop.DragView;
Sunny Goyalb38fab72017-01-20 19:32:31 -0800110import com.android.launcher3.dragndrop.PinItemDragListener;
Mario Bertschler27288382017-05-24 15:35:09 -0700111import com.android.launcher3.dynamicui.WallpaperColorInfo;
Sunny Goyal26119432016-02-18 22:09:23 +0000112import com.android.launcher3.folder.Folder;
113import com.android.launcher3.folder.FolderIcon;
Sunny Goyal66b24572016-09-21 15:57:55 -0700114import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700115import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700116import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700117import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyala535ae42017-02-27 10:07:13 -0800118import com.android.launcher3.model.ModelWriter;
Sunny Goyala535ae42017-02-27 10:07:13 -0800119import com.android.launcher3.notification.NotificationListener;
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700120import com.android.launcher3.pageindicators.PageIndicator;
Sunny Goyal10a1bd02017-10-09 14:56:21 -0700121import com.android.launcher3.popup.BaseActionPopup;
Tony Wickham540913e2017-01-23 11:47:51 -0800122import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -0800123import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700124import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700125import com.android.launcher3.states.AllAppsState;
Sunny Goyalf8088ee2017-11-10 14:52:00 -0800126import com.android.launcher3.states.InternalStateHandler;
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
Sunny Goyalffe83f12014-08-14 17:39:34 -0700226 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700227 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700228
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700229 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700230
Sunny Goyal316490e2015-06-02 09:38:28 -0700231 @Thunk Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700232
Michael Jurka838a4ca2011-02-07 13:33:06 -0800233 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700234
Sunny Goyal47328fd2016-05-25 18:56:41 -0700235 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700236
237 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700238 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700239 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700240
Winson Chung8ae41982017-11-10 11:42:13 -0800241 // UI and state for the overview panel
242 private ViewGroup mOverviewPanel;
243
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 Goyalc4fa8c32017-11-07 12:23:58 -0800343 mStateManager = new LauncherStateManager(this);
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
Winson Chungb63b44c2017-11-10 17:54:44 -0800364 InternalStateHandler.handleCreate(this, getIntent());
365
Sunny Goyal2100c782016-08-22 16:00:03 -0700366 // We only load the page synchronously if the user rotates (or triggers a
367 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700368 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
369 if (savedInstanceState != null) {
370 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
371 }
372 if (!mModel.startLoader(currentScreen)) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700373 // If we are not binding synchronously, show a fade in animation when
374 // the first page bind completes.
375 mDragLayer.setAlpha(0);
376 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700377 // Pages bound synchronously.
378 mWorkspace.setCurrentPage(currentScreen);
379
Sunny Goyal2100c782016-08-22 16:00:03 -0700380 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800381 }
382
383 // For handling default keys
384 mDefaultKeySsb = new SpannableStringBuilder();
385 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800386
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800387 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700388 // In case we are on a device with locked rotation, we should look at preferences to check
389 // if the user has specifically allowed rotation.
390 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700391 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700392 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
393 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700394 }
395
Sunny Goyal5a81c382017-03-20 15:08:06 -0700396 if (PinItemDragListener.handleDragRequest(this, getIntent())) {
397 // Temporarily enable the rotation
398 mRotationEnabled = true;
399 }
400
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700401 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
402 // we want the screen to auto-rotate based on the current orientation
Sunny Goyalc99cb172017-10-19 16:15:09 -0700403 setRequestedOrientation(mRotationEnabled
404 ? SCREEN_ORIENTATION_UNSPECIFIED : SCREEN_ORIENTATION_NOSENSOR);
Winson Chungaf40f202013-09-18 18:26:31 -0700405
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800406 setContentView(mLauncherView);
Mario Bertschlera6936942017-05-31 14:48:19 -0700407
Jon Miranda7fda2852017-07-19 16:07:01 -0700408 // Listen for broadcasts
409 IntentFilter filter = new IntentFilter();
410 filter.addAction(Intent.ACTION_SCREEN_OFF);
411 filter.addAction(Intent.ACTION_USER_PRESENT); // When the device wakes up + keyguard is gone
412 registerReceiver(mReceiver, filter);
413 mShouldFadeInScrim = true;
Sunny Goyala616d2b2017-06-12 13:54:01 -0700414
Sunny Goyal8392c822017-06-20 10:03:56 -0700415 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
416 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700417
418 if (mLauncherCallbacks != null) {
419 mLauncherCallbacks.onCreate(savedInstanceState);
420 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700421
422 TraceHelper.endSection("Launcher-onCreate");
Adam Cohen9211d422014-10-07 18:14:53 -0700423 }
424
Sunny Goyal7779d622015-06-11 16:18:39 -0700425 @Override
Mario Bertschler27288382017-05-24 15:35:09 -0700426 public void onThemeChanged() {
427 recreate();
428 }
429
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700430 public LauncherStateManager getStateManager() {
431 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700432 }
433
Mario Bertschlera6936942017-05-31 14:48:19 -0700434 protected void overrideTheme(boolean isDark, boolean supportsDarkText) {
Mario Bertschler27288382017-05-24 15:35:09 -0700435 if (isDark) {
436 setTheme(R.style.LauncherThemeDark);
Mario Bertschlera6936942017-05-31 14:48:19 -0700437 } else if (supportsDarkText) {
438 setTheme(R.style.LauncherThemeDarkText);
Mario Bertschler27288382017-05-24 15:35:09 -0700439 }
440 }
441
442 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700443 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800444 return mLauncherView.findViewById(id);
445 }
446
447 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700448 public void onAppWidgetHostReset() {
449 if (mAppWidgetHost != null) {
450 mAppWidgetHost.startListening();
451 }
452 }
453
Adam Cohen9211d422014-10-07 18:14:53 -0700454 private LauncherCallbacks mLauncherCallbacks;
455
Winson1f064272016-07-18 17:18:02 -0700456 public void onInsetsChanged(Rect insets) {
457 mDeviceProfile.updateInsets(insets);
458 mDeviceProfile.layout(this, true /* notifyListeners */);
459 }
460
Sunny Goyal32554d12015-12-03 15:31:25 -0800461 /**
462 * Call this after onCreate to set or clear overlay.
463 */
464 public void setLauncherOverlay(LauncherOverlay overlay) {
465 if (overlay != null) {
466 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
467 }
468 mWorkspace.setLauncherOverlay(overlay);
469 }
470
Adam Cohen9211d422014-10-07 18:14:53 -0700471 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
472 mLauncherCallbacks = callbacks;
473 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700474 }
475
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700476 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700477 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700478 if (mLauncherCallbacks != null) {
479 mLauncherCallbacks.onLauncherProviderChange();
480 }
481 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700482
Hyunyoung Song3f471442015-04-08 19:01:34 -0700483 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700484 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
485 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700486 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700487 }
488
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000489 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700490 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
491 // This cast is safe as long as the id < 0x00FFFFFF
492 // Since we jail all the dynamically generated views, there should be no clashes
493 // with any other views.
494 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000495 }
496
Tony Wickham010d2552017-01-20 08:15:28 -0800497 public PopupDataProvider getPopupDataProvider() {
498 return mPopupDataProvider;
499 }
500
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000501 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700502 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
503 * a configuration step, this allows the proper animations to run after other transitions.
504 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700505 private long completeAdd(
506 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
507 long screenId = info.screenId;
508 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700509 // When the screen id represents an actual screen (as opposed to a rank) we make sure
510 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700511 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700512 }
Adam Cohendb364c32014-05-20 14:23:40 -0700513
Sunny Goyal2100c782016-08-22 16:00:03 -0700514 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800515 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700516 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700517 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800518 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700519 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700520 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700521 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700522 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700523 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700524 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700525 int widgetId = appWidgetId;
526 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700527 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700528 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700529 // Since the view was just bound, also launch the configure activity if needed
530 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
531 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800532 if (provider != null) {
533 new WidgetAddFlowHandler(provider)
534 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700535 }
536 }
537 break;
538 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800539 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700540
Adam Cohendb364c32014-05-20 14:23:40 -0700541 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800542 }
543
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800544 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700545 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700546 if (isWorkspaceLoading()) {
547 // process the result once the workspace has loaded.
548 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
549 return;
550 }
551 mPendingActivityResult = null;
552
Winson Chunge341d302013-08-16 14:31:00 -0700553 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700554 final PendingRequestArgs requestArgs = mPendingRequestArgs;
555 setWaitingForResult(null);
556 if (requestArgs == null) {
557 return;
558 }
559
560 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700561
Adam Cohenad4e15c2013-10-17 16:21:35 -0700562 Runnable exitSpringLoaded = new Runnable() {
563 @Override
564 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700565 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700566 }
567 };
568
Michael Jurka8b805b12012-04-18 14:23:14 -0700569 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700570 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700571 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700572 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
573 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700574 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700575 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700576 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700577 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700578 addAppWidgetImpl(
579 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800580 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700581 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700582 }
583 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200584 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700585 if (resultCode == RESULT_OK && isInState(OVERVIEW)) {
Tony Wickhame3054412015-09-09 09:05:25 -0700586 // User could have free-scrolled between pages before picking a wallpaper; make sure
587 // we move to the closest one now.
588 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700589 mStateManager.goToState(NORMAL, false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200590 }
591 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700592 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200593
Adam Cohened66b2b2012-01-23 17:28:51 -0800594 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700595 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700596
Adam Cohened66b2b2012-01-23 17:28:51 -0800597 // We have special handling for widgets
598 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800599 final int appWidgetId;
600 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
601 : -1;
602 if (widgetId < 0) {
603 appWidgetId = pendingAddWidgetId;
604 } else {
605 appWidgetId = widgetId;
606 }
607
Adam Cohenad4e15c2013-10-17 16:21:35 -0700608 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800609 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700610 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
611 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700612 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700613 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700614 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700615 @Override
616 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700617 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700618 }
619 };
Adam Cohendb364c32014-05-20 14:23:40 -0700620
Sunny Goyal2100c782016-08-22 16:00:03 -0700621 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
622 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
623 } else {
624 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
625 // When the screen id represents an actual screen (as opposed to a rank)
626 // we make sure that the drop page actually exists.
627 requestArgs.screenId =
628 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700629 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700630 final CellLayout dropLayout =
631 mWorkspace.getScreenWithId(requestArgs.screenId);
632
633 dropLayout.setDropPending(true);
634 final Runnable onComplete = new Runnable() {
635 @Override
636 public void run() {
637 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
638 dropLayout.setDropPending(false);
639 }
640 };
641 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
642 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700643 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800644 return;
645 }
646
Sunny Goyald478c832016-04-01 12:04:16 -0700647 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
648 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700649 if (resultCode == RESULT_OK) {
650 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700651 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700652 }
653 // Leave the widget in the pending state if the user canceled the configure.
654 return;
655 }
656
Sunny Goyalaad90582015-07-09 14:22:50 -0700657 if (requestCode == REQUEST_CREATE_SHORTCUT) {
658 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700659 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
660 completeAdd(requestCode, data, -1, requestArgs);
661 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
662 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
663
Sunny Goyalaad90582015-07-09 14:22:50 -0700664 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700665 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
666 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800667 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800669 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800670 }
671
672 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700673 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800674 final int requestCode, final int resultCode, final Intent data) {
675 handleActivityResult(requestCode, resultCode, data);
676 if (mLauncherCallbacks != null) {
677 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
678 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800679 }
680
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700681 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700682 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600683 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700684 PendingRequestArgs pendingArgs = mPendingRequestArgs;
685 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
686 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
687 setWaitingForResult(null);
688
Sunny Goyal28c6b962015-10-12 11:42:05 -0700689 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700690 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700691 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700692 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700693 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700694 Intent intent = pendingArgs.getPendingIntent();
695
Sunny Goyal28c6b962015-10-12 11:42:05 -0700696 if (grantResults.length > 0
697 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700698 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700699 } else {
700 // TODO: Show a snack bar with link to settings
701 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700702 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700703 }
704 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600705 if (mLauncherCallbacks != null) {
706 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
707 grantResults);
708 }
709 }
710
Adam Cohendb364c32014-05-20 14:23:40 -0700711 /**
712 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
713 *
714 * @param screenId the screen id to check
715 * @return the new screen, or screenId if it exists
716 */
717 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800718 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700719 if (dropLayout == null) {
720 // it's possible that the add screen was removed because it was
721 // empty and a re-bind occurred
722 mWorkspace.addExtraEmptyScreen();
723 return mWorkspace.commitExtraEmptyScreen();
724 } else {
725 return screenId;
726 }
727 }
728
Sunny Goyal2100c782016-08-22 16:00:03 -0700729 @Thunk void completeTwoStageWidgetDrop(
730 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
731 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800732 Runnable onCompleteRunnable = null;
733 int animationType = 0;
734
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700735 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800736 if (resultCode == RESULT_OK) {
737 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
738 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800739 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700740 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800741 onCompleteRunnable = new Runnable() {
742 @Override
743 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700744 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700745 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800746 }
747 };
748 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800749 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800750 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800751 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700752 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700753 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700754 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
755 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700756 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700757 // The animated view may be null in the case of a rotation during widget configuration
758 onCompleteRunnable.run();
759 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800760 }
761
762 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700763 protected void onStop() {
764 super.onStop();
765 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700766
767 if (mLauncherCallbacks != null) {
768 mLauncherCallbacks.onStop();
769 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700770
Sunny Goyalf5e37442016-11-02 10:31:24 -0700771 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal5da78f42016-06-17 14:00:22 -0700772 mAppWidgetHost.stopListening();
773 }
Tony Wickham010d2552017-01-20 08:15:28 -0800774
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700775 if (!mAppLaunchSuccess) {
776 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
Sunny Goyalea609262017-10-25 15:47:38 -0700777 mStateManager.getState().containerType);
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700778 }
Tony Wickham010d2552017-01-20 08:15:28 -0800779 NotificationListener.removeNotificationsChangedListener();
Michael Jurkacd496d72013-04-11 11:32:45 -0700780 }
781
782 @Override
783 protected void onStart() {
784 super.onStart();
785 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700786
787 if (mLauncherCallbacks != null) {
788 mLauncherCallbacks.onStart();
789 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700790
Sunny Goyalf5e37442016-11-02 10:31:24 -0700791 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal5da78f42016-06-17 14:00:22 -0700792 mAppWidgetHost.startListening();
793 }
Tony Wickham010d2552017-01-20 08:15:28 -0800794
795 if (!isWorkspaceLoading()) {
796 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
797 }
Jon Miranda2d89ea82017-05-04 11:47:53 -0700798
Jon Miranda7fda2852017-07-19 16:07:01 -0700799 if (mShouldFadeInScrim && mDragLayer.getBackground() != null) {
Jon Miranda2d89ea82017-05-04 11:47:53 -0700800 if (mScrimAnimator != null) {
801 mScrimAnimator.cancel();
802 }
803 mDragLayer.getBackground().setAlpha(0);
804 mScrimAnimator = ObjectAnimator.ofInt(mDragLayer.getBackground(),
805 LauncherAnimUtils.DRAWABLE_ALPHA, 0, 255);
806 mScrimAnimator.addListener(new AnimatorListenerAdapter() {
807 @Override
808 public void onAnimationEnd(Animator animation) {
809 mScrimAnimator = null;
810 }
811 });
812 mScrimAnimator.setDuration(600);
813 mScrimAnimator.setStartDelay(getWindow().getTransitionBackgroundFadeDuration());
814 mScrimAnimator.start();
815 }
Jon Miranda7fda2852017-07-19 16:07:01 -0700816 mShouldFadeInScrim = false;
Michael Jurkacd496d72013-04-11 11:32:45 -0700817 }
818
819 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800820 protected void onResume() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700821 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800822 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700823 TraceHelper.partitionSection("ON_RESUME", "superCall");
824
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700825 mAppLaunchSuccess = false;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700826 getUserEventDispatcher().resetElapsedSessionMillis();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800827 mPaused = false;
Sunny Goyal2100c782016-08-22 16:00:03 -0700828 if (mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700829 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -0700830 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700831 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800832 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700833 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200834 // We might have postponed some bind calls until onResume (see waitUntilResume) --
835 // execute them here
Michael Jurka1e2f4652013-07-08 18:03:46 -0700836 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
837 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200838 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700839 mBindOnResumeCallbacks.clear();
Michael Jurka447bf842013-05-15 14:52:15 +0200840 }
Winson Chunge4e50662012-01-23 14:45:13 -0800841
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700842 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700843 // Process any items that were added while Launcher was away.
844 InstallShortcutReceiver.disableAndFlushInstallQueue(
845 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700846
Sunny Goyala474a9b2017-05-04 16:47:11 -0700847 // Refresh shortcuts if the permission changed.
848 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700849
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700850 if (shouldShowDiscoveryBounce()) {
851 mAllAppsController.showDiscoveryBounce();
852 }
Adam Cohen9211d422014-10-07 18:14:53 -0700853 if (mLauncherCallbacks != null) {
854 mLauncherCallbacks.onResume();
855 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800856
Sunny Goyala502aa32017-10-02 16:04:06 -0700857 clearTypedText();
858
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700859 TraceHelper.endSection("ON_RESUME");
Adam Cohen06dff352012-06-01 17:17:08 -0700860 }
861
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800862 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700863 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700864 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700865 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700866
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700867 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700868 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800869 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700870 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700871
Adam Cohen9211d422014-10-07 18:14:53 -0700872 if (mLauncherCallbacks != null) {
873 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700874 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700875 }
876
Adam Cohenc2d6e892014-10-16 09:49:24 -0700877 public interface LauncherOverlay {
878
879 /**
880 * Touch interaction leading to overscroll has begun
881 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700882 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700883
884 /**
885 * Touch interaction related to overscroll has ended
886 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700887 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700888
889 /**
890 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
891 * screen (or in the case of RTL, the rightmost screen).
892 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700893 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700894
895 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800896 * Called when the launcher is ready to use the overlay
897 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700898 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700899 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700900 }
901
902 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700903 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700904 }
905
906 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
907
Sunny Goyalc86df472016-02-25 09:19:38 -0800908 public void onScrollChanged(float progress) {
909 if (mWorkspace != null) {
910 mWorkspace.onOverlayScrollChanged(progress);
911 }
912 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700913 }
914
Sunny Goyal16764582017-11-06 16:00:34 -0800915 public boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -0700916 if (mLauncherCallbacks != null) {
917 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700918 } else {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800919 // On O and above we there is always some setting present settings (add icon to
920 // home screen or icon badging). On earlier APIs we will have the allow rotation
921 // setting, on devices with a locked orientation,
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700922 return Utilities.ATLEAST_OREO || !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -0700923 }
Jorim Jaggid017f882014-01-14 17:08:48 -0800924 }
925
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700926 @Override
927 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400928 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -0700929 if (mModel.isCurrentCallbacks(this)) {
930 mModel.stopLoader();
931 }
Hyunyoung Song3f471442015-04-08 19:01:34 -0700932 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
933
Romain Guy13c2e7b2010-03-10 19:45:00 -0800934 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700935 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700936
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800937 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800938 @Override
939 public void onWindowFocusChanged(boolean hasFocus) {
940 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -0700941 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -0700942
943 if (mLauncherCallbacks != null) {
944 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
945 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800946 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800947
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800948 private boolean acceptFilter() {
949 final InputMethodManager inputManager = (InputMethodManager)
950 getSystemService(Context.INPUT_METHOD_SERVICE);
951 return !inputManager.isFullscreenMode();
952 }
953
954 @Override
955 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700956 final int uniChar = event.getUnicodeChar();
957 final boolean handled = super.onKeyDown(keyCode, event);
958 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
959 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800960 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
961 keyCode, event);
962 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700963 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700964 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700965 // showSearchDialog()
966 // If there are multiple keystrokes before the search dialog takes focus,
967 // onSearchRequested() will be called for every keystroke,
968 // but it is idempotent, so it's fine.
969 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800970 }
971 }
972
Joe Onorato8a9625e2010-01-28 15:55:35 -0800973 // Eat the long press event so the keyboard doesn't come up.
974 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
975 return true;
976 }
977
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800978 return handled;
979 }
980
Karl Rosaen138a0412009-04-23 19:00:21 -0700981 private String getTypedText() {
982 return mDefaultKeySsb.toString();
983 }
984
Sunny Goyal6f28e712016-09-13 14:19:29 -0700985 @Override
986 public void clearTypedText() {
Karl Rosaen138a0412009-04-23 19:00:21 -0700987 mDefaultKeySsb.clear();
988 mDefaultKeySsb.clearSpans();
989 Selection.setSelection(mDefaultKeySsb, 0);
990 }
991
Sunny Goyalf9403d92017-10-18 10:55:56 -0700992 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -0700993 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700994 }
995
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800996 /**
997 * Restores the previous state, if it exists.
998 *
999 * @param savedState The previous state.
1000 */
1001 private void restoreState(Bundle savedState) {
1002 if (savedState == null) {
1003 return;
1004 }
1005
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001006 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -07001007 LauncherState[] stateValues = LauncherState.values();
1008 LauncherState state = stateValues[stateOrdinal];
1009 if (!state.doNotRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001010 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001011 }
1012
Sunny Goyal2100c782016-08-22 16:00:03 -07001013 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
1014 if (requestArgs != null) {
1015 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001016 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001017
1018 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001019
1020 SparseArray<Parcelable> widgetsState =
1021 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
1022 if (widgetsState != null) {
1023 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
1024 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001025 }
1026
1027 /**
1028 * Finds all the views we need and configure them properly.
1029 */
1030 private void setupViews() {
Winson Chung4c98d922011-05-31 16:50:48 -07001031 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001032 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -07001033 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001034 mWorkspace.initParentViews(mDragLayer);
Sunny Goyal16764582017-11-06 16:00:34 -08001035 mOverviewPanel = findViewById(R.id.overview_panel);
Craig Mautner360310b2012-10-26 15:13:08 -07001036
Sunny Goyal784f9c32016-03-23 16:56:54 -07001037 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1038 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1039 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001040
Winson Chung4c98d922011-05-31 16:50:48 -07001041 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001042 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001043
Winson Chung3d503fb2011-07-13 17:25:49 -07001044 // Setup the hotseat
1045 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1046 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001047 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001048 }
1049
Winson Chung4c98d922011-05-31 16:50:48 -07001050 // Setup the workspace
1051 mWorkspace.setHapticFeedbackEnabled(false);
1052 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001053 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001054 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1055 // default state, otherwise we will update to the wrong offsets in RTL
1056 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001057 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001058 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001059
Tony Wickham34d2c912015-09-11 09:27:58 -07001060 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001061 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001062
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001063 // Setup Apps
1064 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001065
Winson Chung3d503fb2011-07-13 17:25:49 -07001066 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001067 mDragController.setMoveTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001068 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001069
Hyunyoung Songd725f642017-08-17 22:26:35 -07001070 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001071 }
1072
1073 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001074 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001075 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001076 */
1077 public void setAllAppsButton(View allAppsButton) {
1078 mAllAppsButton = allAppsButton;
1079 }
1080
Anjali Koppal5ad44842014-03-10 20:34:39 -07001081 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001082 * Creates a view representing a shortcut.
1083 *
1084 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001085 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001086 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001087 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001088 }
1089
1090 /**
1091 * Creates a view representing a shortcut inflated from the specified resource.
1092 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001093 * @param parent The group the shortcut belongs to.
1094 * @param info The data structure describing the shortcut.
1095 *
1096 * @return A View inflated from layoutResId.
1097 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001098 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -08001099 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
1100 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001101 favorite.applyFromShortcutInfo(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001102 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001103 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001104 return favorite;
1105 }
1106
1107 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -07001108 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001109 *
1110 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001111 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001112 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001113 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001114 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
1115 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001116 return;
1117 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001118
Jon Mirandac476d6e2017-05-04 16:30:01 -07001119 int[] cellXY = mTmpAddItemCellCoordinates;
1120 CellLayout layout = getCellLayout(container, screenId);
1121
Sunny Goyal782f0c92017-01-19 10:27:54 -08001122 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001123 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001124 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
1125 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001126 }
1127
1128 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001129 // Legacy shortcuts are only supported for primary profile.
1130 info = Process.myUserHandle().equals(args.user)
1131 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001132
Sunny Goyalb57645f2017-03-20 13:57:28 -07001133 if (info == null) {
1134 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1135 return;
1136 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001137 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001138 // The app is trying to add a shortcut without sufficient permissions
1139 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1140 return;
1141 }
1142 }
1143
Jon Mirandac476d6e2017-05-04 16:30:01 -07001144 if (container < 0) {
1145 // Adding a shortcut to the Workspace.
1146 final View view = createShortcut(info);
1147 boolean foundCellSpan = false;
1148 // First we check if we already know the exact location where we want to add this item.
1149 if (cellX >= 0 && cellY >= 0) {
1150 cellXY[0] = cellX;
1151 cellXY[1] = cellY;
1152 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001153
Jon Mirandac476d6e2017-05-04 16:30:01 -07001154 // If appropriate, either create a folder or add to an existing folder
1155 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001156 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001157 return;
1158 }
1159 DragObject dragObject = new DragObject();
1160 dragObject.dragInfo = info;
1161 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1162 true)) {
1163 return;
1164 }
1165 } else {
1166 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1167 }
1168
1169 if (!foundCellSpan) {
1170 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001171 return;
1172 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001173
1174 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1175 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001176 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001177 // Adding a shortcut to a Folder.
Sunny Goyale29897f52017-07-20 10:09:42 -07001178 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001179 if (folderIcon != null) {
1180 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1181 folderInfo.add(info, args.rank, false);
1182 } else {
Sunny Goyale29897f52017-07-20 10:09:42 -07001183 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001184 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001185 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001186 }
1187
Sunny Goyale29897f52017-07-20 10:09:42 -07001188 public FolderIcon findFolderIcon(final long folderIconId) {
1189 return (FolderIcon) mWorkspace.getFirstMatch(new ItemOperator() {
1190 @Override
1191 public boolean evaluate(ItemInfo info, View view) {
1192 return info != null && info.id == folderIconId;
1193 }
1194 });
1195 }
1196
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001197 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001198 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001199 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001200 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001201 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001202 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001203 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1204
Adam Cohened66b2b2012-01-23 17:28:51 -08001205 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001206 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001207 }
Romain Guycbb89e42009-06-08 15:52:54 -07001208
Adam Cohen59400422014-03-05 18:07:04 -08001209 LauncherAppWidgetInfo launcherInfo;
1210 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001211 launcherInfo.spanX = itemInfo.spanX;
1212 launcherInfo.spanY = itemInfo.spanY;
1213 launcherInfo.minSpanX = itemInfo.minSpanX;
1214 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001215 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001216
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001217 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001218 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001219
Sunny Goyal2100c782016-08-22 16:00:03 -07001220 if (hostView == null) {
1221 // Perform actual inflation because we're live
1222 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001223 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001224 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301225 prepareAppWidget(hostView, launcherInfo);
1226 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001227 }
Romain Guycbb89e42009-06-08 15:52:54 -07001228
Sunny Goyald5462aa2016-11-22 16:52:39 +05301229 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001230 hostView.setTag(item);
1231 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001232 hostView.setFocusable(true);
1233 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001234 }
1235
Adam Cohended9f8d2010-11-03 13:25:16 -07001236 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1237 @Override
1238 public void onReceive(Context context, Intent intent) {
1239 final String action = intent.getAction();
1240 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001241 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001242 // processing a multi-step drop
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001243 if (mAppsView != null && mPendingRequestArgs == null) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001244 mStateManager.goToState(NORMAL);
Winson Chung785d2eb2011-04-14 16:08:02 -07001245 }
Jon Miranda7fda2852017-07-19 16:07:01 -07001246 mShouldFadeInScrim = true;
1247 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1248 // ACTION_USER_PRESENT is sent after onStart/onResume. This covers the case where
1249 // the user unlocked and the Launcher is not in the foreground.
1250 mShouldFadeInScrim = false;
Adam Cohended9f8d2010-11-03 13:25:16 -07001251 }
1252 }
1253 };
1254
Tony Wickham010d2552017-01-20 08:15:28 -08001255 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
1256 Runnable r = new Runnable() {
1257 @Override
1258 public void run() {
1259 mWorkspace.updateIconBadges(updatedBadges);
1260 mAppsView.updateIconBadges(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001261
Sunny Goyal10a1bd02017-10-09 14:56:21 -07001262 BaseActionPopup popup = BaseActionPopup.getOpen(Launcher.this);
1263 if (popup instanceof PopupContainerWithArrow) {
1264 ((PopupContainerWithArrow) popup).updateNotificationHeader(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001265 }
Tony Wickham010d2552017-01-20 08:15:28 -08001266 }
1267 };
1268 if (!waitUntilResume(r)) {
1269 r.run();
1270 }
1271 }
1272
Adam Cohended9f8d2010-11-03 13:25:16 -07001273 @Override
1274 public void onAttachedToWindow() {
1275 super.onAttachedToWindow();
1276
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001277 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyalc86df472016-02-25 09:19:38 -08001278 if (mLauncherCallbacks != null) {
1279 mLauncherCallbacks.onAttachedToWindow();
1280 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001281 }
1282
1283 @Override
1284 public void onDetachedFromWindow() {
1285 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001286
1287 if (mLauncherCallbacks != null) {
1288 mLauncherCallbacks.onDetachedFromWindow();
1289 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001290 }
1291
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001292 public AllAppsTransitionController getAllAppsController() {
1293 return mAllAppsController;
1294 }
1295
Winson Chunga6945242014-01-08 14:04:34 -08001296 public DragLayer getDragLayer() {
1297 return mDragLayer;
1298 }
1299
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001300 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001301 return mAppsView;
1302 }
1303
Winson Chunga6945242014-01-08 14:04:34 -08001304 public Workspace getWorkspace() {
1305 return mWorkspace;
1306 }
1307
1308 public Hotseat getHotseat() {
1309 return mHotseat;
1310 }
1311
Winson Chung8ae41982017-11-10 11:42:13 -08001312 public <T extends ViewGroup> T getOverviewPanel() {
1313 return (T) mOverviewPanel;
Winson Chunga6945242014-01-08 14:04:34 -08001314 }
1315
Sunny Goyal47328fd2016-05-25 18:56:41 -07001316 public DropTargetBar getDropTargetBar() {
1317 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001318 }
1319
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001320 public LauncherAppWidgetHost getAppWidgetHost() {
1321 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001322 }
Romain Guycbb89e42009-06-08 15:52:54 -07001323
Winson Chunga9abd0e2010-10-27 17:18:37 -07001324 public LauncherModel getModel() {
1325 return mModel;
1326 }
1327
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001328 public ModelWriter getModelWriter() {
1329 return mModelWriter;
1330 }
1331
Adam Cohen79d90c52016-04-22 13:29:20 -07001332 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001333 return mSharedPrefs;
1334 }
1335
Jon Miranda6bed3502017-09-19 14:43:17 -07001336 public int getOrientation() { return mOrientation; }
1337
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001338 @Override
1339 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001340 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001341 super.onNewIntent(intent);
1342
Winson15f8b172015-08-19 11:02:39 -07001343 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1344 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1345 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001346
1347 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001348 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001349 && AbstractFloatingView.getTopOpenView(this) == null;
Winson15f8b172015-08-19 11:02:39 -07001350 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1351 if (isActionMain) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001352 if (mWorkspace == null) {
1353 // Can be cases where mWorkspace is null, this prevents a NPE
1354 return;
1355 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001356
1357 // Note: There should be at most one log per method call. This is enforced implicitly
1358 // by using if-else statements.
1359 UserEventDispatcher ued = getUserEventDispatcher();
Jon Mirandafeba90f2016-10-06 10:53:29 -07001360 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
Sunny Goyal37920962017-09-28 13:43:24 -07001361 if (topOpenView != null) {
1362 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
Jon Mirandafeba90f2016-10-06 10:53:29 -07001363 } else if (alreadyOnHome) {
Sunny Goyalea609262017-10-25 15:47:38 -07001364 Target target = newContainerTarget(mStateManager.getState().containerType);
Sunny Goyalaeb16432017-10-16 11:46:41 -07001365 target.pageIndex = mWorkspace.getCurrentPage();
1366 ued.logActionCommand(Action.Command.HOME_INTENT, target);
Jon Mirandafeba90f2016-10-06 10:53:29 -07001367 }
1368
1369 // In all these cases, only animate if we're already on home
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001370 AbstractFloatingView.closeAllOpenViews(this, alreadyOnHome);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001371 mStateManager.goToState(NORMAL, alreadyOnHome /* animated */);
Adam Cohen6fecd412013-10-02 17:41:50 -07001372
1373 final View v = getWindow().peekDecorView();
1374 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001375 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001376 }
1377
Winson Chungb745afb2015-03-02 11:51:23 -08001378 // Reset the apps view
1379 if (!alreadyOnHome && mAppsView != null) {
Sunny Goyal161f96b2017-05-07 11:56:00 -07001380 mAppsView.reset();
Winson Chungb745afb2015-03-02 11:51:23 -08001381 }
1382
Adam Cohen9211d422014-10-07 18:14:53 -07001383 if (mLauncherCallbacks != null) {
1384 mLauncherCallbacks.onHomeIntent();
1385 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001386 }
Sunny Goyal5a81c382017-03-20 15:08:06 -07001387 PinItemDragListener.handleDragRequest(this, intent);
Adam Cohened307df2013-10-02 09:37:31 -07001388
Adam Cohen9211d422014-10-07 18:14:53 -07001389 if (mLauncherCallbacks != null) {
1390 mLauncherCallbacks.onNewIntent(intent);
1391 }
Winson15f8b172015-08-19 11:02:39 -07001392
1393 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1394 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1395 // animation.
1396 if (isActionMain) {
Sunny Goyal00c95b82017-10-03 10:29:23 -07001397 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001398
Winson15f8b172015-08-19 11:02:39 -07001399 mWorkspace.post(new Runnable() {
1400 @Override
1401 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001402 if (mWorkspace != null) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001403 mWorkspace.moveToDefaultScreen();
Tonyc17390962015-10-25 17:39:37 -07001404 }
Winson15f8b172015-08-19 11:02:39 -07001405 }
1406 });
1407 }
1408 }
Winson Chungb63b44c2017-11-10 17:54:44 -08001409 InternalStateHandler.handleNewIntent(this, intent, alreadyOnHome);
Winson15f8b172015-08-19 11:02:39 -07001410
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001411 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001412 }
1413
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001414 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001415 public void onRestoreInstanceState(Bundle state) {
1416 super.onRestoreInstanceState(state);
1417 for (int page: mSynchronouslyBoundPages) {
1418 mWorkspace.restoreInstanceStateForChild(page);
1419 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001420 }
1421
1422 @Override
1423 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001424 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001425 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001426
Adam Cohen21cd0022013-10-09 18:57:02 -07001427 }
Sunny Goyalea609262017-10-25 15:47:38 -07001428 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001429
1430
1431 AbstractFloatingView widgets = AbstractFloatingView
1432 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1433 if (widgets != null) {
1434 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1435 widgets.saveHierarchyState(widgetsState);
1436 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1437 } else {
1438 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1439 }
1440
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001441 // We close any open folders and shortcut containers since they will not be re-opened,
1442 // and we need to make sure this state is reflected.
1443 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001444
Sunny Goyal2100c782016-08-22 16:00:03 -07001445 if (mPendingRequestArgs != null) {
1446 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1447 }
1448 if (mPendingActivityResult != null) {
1449 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001450 }
1451
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001452 super.onSaveInstanceState(outState);
1453
Adam Cohen9211d422014-10-07 18:14:53 -07001454 if (mLauncherCallbacks != null) {
1455 mLauncherCallbacks.onSaveInstanceState(outState);
1456 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001457 }
1458
1459 @Override
1460 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001461 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001462
Sunny Goyal5cc7bbd2017-06-12 09:50:39 -07001463 unregisterReceiver(mReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001464 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001465
Winson Chungcd2b0142011-06-08 16:02:26 -07001466 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001467 // It's possible to receive onDestroy after a new Launcher activity has
1468 // been created. In this case, don't interfere with the new Launcher.
1469 if (mModel.isCurrentCallbacks(this)) {
1470 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001471 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001472 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001473
Sunny Goyal745bad92016-05-02 10:54:12 -07001474 if (mRotationPrefChangeHandler != null) {
1475 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1476 }
1477
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001478 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001479 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001480 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001481 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001482 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001483 mAppWidgetHost = null;
1484
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485 TextKeyListener.getInstance().release();
Mario Bertschler27288382017-05-24 15:35:09 -07001486 WallpaperColorInfo.getInstance(this).setOnThemeChangeListener(null);
1487
Michael Jurka2ecf9952012-06-18 12:52:28 -07001488 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001489
Tony2917a8b2017-07-31 23:29:42 -07001490 clearPendingBinds();
1491
Adam Cohen9211d422014-10-07 18:14:53 -07001492 if (mLauncherCallbacks != null) {
1493 mLauncherCallbacks.onDestroy();
1494 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001495 }
1496
Sunny Goyalae502842016-06-17 08:43:56 -07001497 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1498 return mAccessibilityDelegate;
1499 }
1500
Adam Cohena9cf38f2011-05-02 15:36:58 -07001501 public DragController getDragController() {
1502 return mDragController;
1503 }
1504
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001505 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001506 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001507 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001508 }
1509
1510 @Override
1511 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1512 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001513 try {
1514 super.startIntentSenderForResult(intent, requestCode,
1515 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1516 } catch (IntentSender.SendIntentException e) {
1517 throw new ActivityNotFoundException();
1518 }
1519 }
1520
Winson Chung70d72102011-08-12 11:24:35 -07001521 /**
1522 * Indicates that we want global search for this activity by setting the globalSearch
1523 * argument for {@link #startSearch} to true.
1524 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001525 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001526 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001527 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001528
Karl Rosaen138a0412009-04-23 19:00:21 -07001529 if (initialQuery == null) {
1530 // Use any text typed in the launcher as the initial query
1531 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001532 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001533 if (appSearchData == null) {
1534 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001535 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001536 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001537
Sunny Goyal6f28e712016-09-13 14:19:29 -07001538 if (mLauncherCallbacks == null ||
1539 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1540 // Starting search from the callbacks failed. Start the default global search.
1541 startGlobalSearch(initialQuery, selectInitialQuery, appSearchData, null);
Ian Parkinson047036e2014-09-01 15:40:53 +01001542 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001543
1544 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001545 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001546 }
1547
Ian Parkinson047036e2014-09-01 15:40:53 +01001548 /**
Michael Jurkaa33411c2012-06-14 16:18:21 -07001549 * Starts the global search activity. This code is a copied from SearchManager
1550 */
Sunny Goyal6f28e712016-09-13 14:19:29 -07001551 public void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001552 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001553 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001554 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1555 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1556 if (globalSearchActivity == null) {
1557 Log.w(TAG, "No global search activity found.");
1558 return;
1559 }
1560 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1561 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1562 intent.setComponent(globalSearchActivity);
1563 // Make sure that we have a Bundle to put source in
1564 if (appSearchData == null) {
1565 appSearchData = new Bundle();
1566 } else {
1567 appSearchData = new Bundle(appSearchData);
1568 }
Adam Cohen9211d422014-10-07 18:14:53 -07001569 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07001570 if (!appSearchData.containsKey("source")) {
1571 appSearchData.putString("source", getPackageName());
1572 }
1573 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1574 if (!TextUtils.isEmpty(initialQuery)) {
1575 intent.putExtra(SearchManager.QUERY, initialQuery);
1576 }
1577 if (selectInitialQuery) {
1578 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1579 }
1580 intent.setSourceBounds(sourceBounds);
1581 try {
1582 startActivity(intent);
1583 } catch (ActivityNotFoundException ex) {
1584 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1585 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001586 }
1587
Winson Chung70d72102011-08-12 11:24:35 -07001588 @Override
Winson Chung70d72102011-08-12 11:24:35 -07001589 public boolean onPrepareOptionsMenu(Menu menu) {
1590 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07001591 if (mLauncherCallbacks != null) {
1592 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
1593 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02001594 return false;
Winson Chung70d72102011-08-12 11:24:35 -07001595 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001596
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001597 @Override
1598 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001599 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001600 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001601 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001602 }
1603
Joe Onorato9c1289c2009-08-17 11:03:03 -04001604 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001605 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001606 }
1607
Adam Cohen517a7f52014-03-01 12:12:59 -08001608 public boolean isWorkspaceLoading() {
1609 return mWorkspaceLoading;
1610 }
1611
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001612 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001613 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001614 }
1615
Sunny Goyal04a324a2017-01-20 21:08:59 -08001616 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001617 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001618 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001619
Sunny Goyal2100c782016-08-22 16:00:03 -07001620 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001621 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001622 if (LOGD) {
1623 Log.d(TAG, "Adding widget from drop");
1624 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001625 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001626 }
1627
Sunny Goyal2100c782016-08-22 16:00:03 -07001628 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001629 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1630 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1631 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001632
Adam Cohenad4e15c2013-10-17 16:21:35 -07001633 Runnable onComplete = new Runnable() {
1634 @Override
1635 public void run() {
1636 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001637 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001638 }
1639 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001640 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001641 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001642 }
1643 }
Romain Guycbb89e42009-06-08 15:52:54 -07001644
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001645 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1646 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001647 info.container = container;
1648 info.screenId = screenId;
1649 if (cell != null) {
1650 info.cellX = cell[0];
1651 info.cellY = cell[1];
1652 }
1653 info.spanX = spanX;
1654 info.spanY = spanY;
1655
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001656 switch (info.itemType) {
1657 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1658 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001659 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001660 break;
1661 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001662 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001663 break;
1664 default:
1665 throw new IllegalStateException("Unknown item type: " + info.itemType);
1666 }
1667 }
1668
Adam Cohenfbba09b2011-07-18 21:43:05 -07001669 /**
1670 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001671 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001672 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001673 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1674 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001675 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1676 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1677 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001678 }
1679
Adam Cohenfbba09b2011-07-18 21:43:05 -07001680 /**
1681 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001682 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001683 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001684 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001685 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001686 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001687 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001688 // In the case where we've prebound the widget, we remove it from the DragLayer
1689 if (LOGD) {
1690 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1691 }
1692 getDragLayer().removeView(hostView);
1693
Adam Cohened66b2b2012-01-23 17:28:51 -08001694 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001695 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001696
1697 // Clear the boundWidget so that it doesn't get destroyed.
1698 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001699 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001700 // In this case, we either need to start an activity to get permission to bind
1701 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001702 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1703 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1704 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1705 this, info.componentName);
1706 } else {
1707 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1708 }
Adam Cohendd70d662012-10-04 16:53:44 -07001709 Bundle options = info.bindOptions;
1710
Sunny Goyalffe83f12014-08-14 17:39:34 -07001711 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1712 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001713 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001714 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001715 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001716 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001717 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001718 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001719 }
1720
Adam Cohendcd297f2013-06-18 13:13:40 -07001721 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001722 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001723 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001724 folderInfo.title = getText(R.string.folder_name);
1725
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001726 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001727 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001728
1729 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001730 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301731 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001732 // Force measure the new folder icon
1733 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1734 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001735 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001736 }
Romain Guycbb89e42009-06-08 15:52:54 -07001737
Winsonc0b52fe2015-09-09 16:38:15 -07001738 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001739 * Unbinds the view for the specified item, and removes the item and all its children.
1740 *
1741 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001742 * @param itemInfo the {@link ItemInfo} for this view.
1743 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001744 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001745 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001746 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001747 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001748 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1749 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001750 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001751 } else {
1752 mWorkspace.removeWorkspaceItem(v);
1753 }
Winsonc0b52fe2015-09-09 16:38:15 -07001754 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001755 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001756 }
1757 } else if (itemInfo instanceof FolderInfo) {
1758 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001759 if (v instanceof FolderIcon) {
1760 ((FolderIcon) v).removeListeners();
1761 }
Winsonc0b52fe2015-09-09 16:38:15 -07001762 mWorkspace.removeWorkspaceItem(v);
1763 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001764 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001765 }
1766 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1767 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001768 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001769 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07001770 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001771 }
1772 } else {
1773 return false;
1774 }
1775 return true;
1776 }
1777
1778 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07001779 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07001780 */
Sunny Goyal56c73602015-09-25 12:55:01 -07001781 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07001782 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07001783 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07001784 // Deleting an app widget ID is a void call but writes to disk before returning
1785 // to the caller...
1786 new AsyncTask<Void, Void, Void>() {
1787 public Void doInBackground(Void ... args) {
1788 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
1789 return null;
1790 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07001791 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07001792 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001793 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001794 }
1795
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001796 @Override
1797 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001798 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001799 }
1800
Joe Onorato88ec0992009-11-19 13:16:06 -08001801 @Override
1802 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07001803 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1804 return;
1805 }
1806
Sunny Goyal45478022015-06-08 16:52:41 -07001807 if (mDragController.isDragging()) {
1808 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001809 return;
1810 }
1811
Jon Mirandafeba90f2016-10-06 10:53:29 -07001812 // Note: There should be at most one log per method call. This is enforced implicitly
1813 // by using if-else statements.
1814 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001815 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
1816 if (topView != null) {
Sunny Goyal37920962017-09-28 13:43:24 -07001817 topView.onBackPressed();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001818 } else if (!isInState(NORMAL)) {
Sunny Goyalea609262017-10-25 15:47:38 -07001819 ued.logActionCommand(Action.Command.BACK, mStateManager.getState().containerType);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001820 mStateManager.goToState(NORMAL);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001821 } else {
1822 // Back button is a no-op here, but give at least some feedback for the button press
1823 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001824 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001825 }
1826
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001827 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001828 * Launches the intent referred by the clicked shortcut.
1829 *
1830 * @param v The view representing the clicked shortcut.
1831 */
1832 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001833 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1834 // view has detached (it's possible for this to happen if the view is removed mid touch).
1835 if (v.getWindowToken() == null) {
1836 return;
1837 }
1838
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001839 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001840 return;
1841 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001842
Adam Cohen1697b792013-09-17 19:08:21 -07001843 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001844 if (isInState(OVERVIEW)) {
Jon Miranda2e61fba2017-02-24 09:12:59 -08001845 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1846 LauncherLogProto.Action.Direction.NONE,
1847 LauncherLogProto.ContainerType.OVERVIEW, mWorkspace.getCurrentPage());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001848 mStateManager.goToState(NORMAL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001849 }
1850 return;
1851 }
1852
1853 if (v instanceof CellLayout) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001854 if (isInState(OVERVIEW)) {
Jon Mirandac6cf4932017-02-07 17:12:36 -08001855 int page = mWorkspace.indexOfChild(v);
1856 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1857 LauncherLogProto.Action.Direction.NONE,
1858 LauncherLogProto.ContainerType.OVERVIEW, page);
1859 mWorkspace.snapToPageFromOverView(page);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001860 mStateManager.goToState(NORMAL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001861 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07001862 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001863 }
1864
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001865 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001866 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001867 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001868 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001869 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001870 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001871 }
Hyunyoung Songd725f642017-08-17 22:26:35 -07001872 } else if ((v instanceof PageIndicator) ||
Adam Cohenbf78f3c2017-07-14 15:58:48 -07001873 (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001874 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07001875 } else if (tag instanceof AppInfo) {
1876 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07001877 } else if (tag instanceof LauncherAppWidgetInfo) {
1878 if (v instanceof PendingAppWidgetHostView) {
1879 onClickPendingWidget((PendingAppWidgetHostView) v);
1880 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001881 }
1882 }
1883
Sunny Goyal70660032015-05-14 00:07:08 -07001884 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07001885 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07001886 return false;
1887 }
1888
Michael Jurkaaf442092010-06-10 17:01:57 -07001889 /**
Sunny Goyalff572272014-07-23 13:58:07 -07001890 * Event handler for the app widget view which has not fully restored.
1891 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001892 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07001893 if (mIsSafeModeEnabled) {
1894 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
1895 return;
1896 }
1897
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001898 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07001899 if (v.isReadyForClickSetup()) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001900 LauncherAppWidgetProviderInfo appWidgetInfo =
1901 mAppWidgetManager.findProvider(info.providerName, info.user);
1902 if (appWidgetInfo == null) {
1903 return;
1904 }
1905 WidgetAddFlowHandler addFlowHandler = new WidgetAddFlowHandler(appWidgetInfo);
1906
Sunny Goyald478c832016-04-01 12:04:16 -07001907 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
1908 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
1909 // This should not happen, as we make sure that an Id is allocated during bind.
1910 return;
1911 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001912 addFlowHandler.startBindFlow(this, info.appWidgetId, info,
1913 REQUEST_BIND_PENDING_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -07001914 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001915 addFlowHandler.startConfigActivity(this, info, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07001916 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001917 } else {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001918 final String packageName = info.providerName.getPackageName();
Sunny Goyale6e72002017-01-12 16:55:36 -08001919 onClickPendingAppItem(v, packageName, info.installProgress >= 0);
Sunny Goyalff572272014-07-23 13:58:07 -07001920 }
1921 }
1922
1923 /**
Adam Cohenbf78f3c2017-07-14 15:58:48 -07001924 * Event handler for the "grid" button or "caret" that appears on the home screen, which
1925 * enters all apps mode. In verticalBarLayout the caret can be seen when all apps is open, and
1926 * so in that case reverses the action.
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001927 *
1928 * @param v The view that was clicked.
1929 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001930 protected void onClickAllAppsButton(View v) {
1931 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001932 if (!isInState(ALL_APPS)) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301933 getUserEventDispatcher().logActionOnControl(Action.Touch.TAP,
1934 ControlType.ALL_APPS_BUTTON);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001935 mStateManager.goToState(ALL_APPS);
Adam Cohen5441a9d2017-07-14 14:22:05 -07001936 } else {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001937 mStateManager.goToState(NORMAL);
Adam Cohen5441a9d2017-07-14 14:22:05 -07001938 }
1939 }
1940
Sunny Goyale6e72002017-01-12 16:55:36 -08001941 private void onClickPendingAppItem(final View v, final String packageName,
1942 boolean downloadStarted) {
1943 if (downloadStarted) {
1944 // If the download has started, simply direct to the market app.
1945 startMarketIntentForPackage(v, packageName);
1946 return;
1947 }
Sunny Goyale03b8122014-10-08 09:55:24 -07001948 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001949 .setTitle(R.string.abandoned_promises_title)
1950 .setMessage(R.string.abandoned_promise_explanation)
Sunny Goyale6e72002017-01-12 16:55:36 -08001951 .setPositiveButton(R.string.abandoned_search, new DialogInterface.OnClickListener() {
1952 @Override
1953 public void onClick(DialogInterface dialogInterface, int i) {
1954 startMarketIntentForPackage(v, packageName);
1955 }
1956 })
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001957 .setNeutralButton(R.string.abandoned_clean_this,
1958 new DialogInterface.OnClickListener() {
1959 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001960 final UserHandle user = Process.myUserHandle();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001961 mWorkspace.removeAbandonedPromise(packageName, user);
1962 }
1963 })
1964 .create().show();
Sunny Goyale6e72002017-01-12 16:55:36 -08001965 }
1966
1967 private void startMarketIntentForPackage(View v, String packageName) {
1968 ItemInfo item = (ItemInfo) v.getTag();
1969 Intent intent = PackageManagerHelper.getMarketIntent(packageName);
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001970 startActivitySafely(v, intent, item);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001971 }
1972
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001973 /**
1974 * Event handler for an app shortcut click.
1975 *
1976 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
1977 */
Chris Wren40c5ed32014-06-24 18:24:23 -04001978 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001979 if (LOGD) Log.d(TAG, "onClickAppShortcut");
1980 Object tag = v.getTag();
1981 if (!(tag instanceof ShortcutInfo)) {
1982 throw new IllegalArgumentException("Input must be a Shortcut");
1983 }
1984
1985 // Open shortcut
1986 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001987
Sunny Goyal076839c2017-10-30 13:52:20 -07001988 if (shortcut.isDisabled()) {
1989 if ((shortcut.runtimeStatusFlags &
1990 ~FLAG_DISABLED_SUSPENDED &
1991 ~FLAG_DISABLED_QUIET_USER) == 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001992 // If the app is only disabled because of the above flags, launch activity anyway.
1993 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00001994 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07001995 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
1996 // Use a message specific to this shortcut, if it has one.
1997 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
1998 return;
1999 }
2000 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00002001 int error = R.string.activity_not_available;
Sunny Goyal076839c2017-10-30 13:52:20 -07002002 if ((shortcut.runtimeStatusFlags & FLAG_DISABLED_SAFEMODE) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00002003 error = R.string.safemode_shortcut_error;
Sunny Goyal076839c2017-10-30 13:52:20 -07002004 } else if ((shortcut.runtimeStatusFlags & FLAG_DISABLED_BY_PUBLISHER) != 0 ||
2005 (shortcut.runtimeStatusFlags & FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002006 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00002007 }
2008 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2009 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002010 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002011 }
2012
Chris Wren40c5ed32014-06-24 18:24:23 -04002013 // Check for abandoned promise
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -07002014 if ((v instanceof BubbleTextView) && shortcut.hasPromiseIconUi()) {
Sunny Goyale6e72002017-01-12 16:55:36 -08002015 String packageName = shortcut.intent.getComponent() != null ?
2016 shortcut.intent.getComponent().getPackageName() : shortcut.intent.getPackage();
2017 if (!TextUtils.isEmpty(packageName)) {
2018 onClickPendingAppItem(v, packageName,
2019 shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE));
2020 return;
2021 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002022 }
2023
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002024 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002025 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002026 }
2027
Sunny Goyala7ce1662016-05-31 15:01:35 -07002028 private void startAppShortcutOrInfoActivity(View v) {
2029 ItemInfo item = (ItemInfo) v.getTag();
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002030 Intent intent;
2031 if (item instanceof PromiseAppInfo) {
2032 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) item;
2033 intent = promiseAppInfo.getMarketIntent();
2034 } else {
2035 intent = item.getIntent();
2036 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002037 if (intent == null) {
2038 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002039 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002040 startActivitySafely(v, intent, item);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002041 }
2042
2043 /**
2044 * Event handler for a folder icon click.
2045 *
2046 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2047 */
2048 protected void onClickFolderIcon(View v) {
2049 if (LOGD) Log.d(TAG, "onClickFolder");
2050 if (!(v instanceof FolderIcon)){
2051 throw new IllegalArgumentException("Input must be a FolderIcon");
2052 }
2053
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002054 Folder folder = ((FolderIcon) v).getFolder();
2055 if (!folder.isOpen() && !folder.isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002056 // Open the requested folder
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002057 folder.animateOpen();
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002058 }
Michael Jurka5130e402011-10-13 04:55:35 -07002059 }
2060
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002061 /**
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002062 * Event handler for the wallpaper picker button that appears after a long press
2063 * on the home screen.
2064 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002065 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07002066 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002067 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2068 return;
2069 }
2070
Tony Wickham785f7a52015-08-31 17:28:32 -07002071 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2072 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal2100c782016-08-22 16:00:03 -07002073 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002074 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002075 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002076
2077 String pickerPackage = getString(R.string.wallpaper_picker_package);
Tony85efb4b2017-06-02 13:06:10 -07002078 boolean hasTargetPackage = !TextUtils.isEmpty(pickerPackage);
2079 if (hasTargetPackage) {
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002080 intent.setPackage(pickerPackage);
2081 }
2082
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002083 intent.setSourceBounds(getViewBounds(v));
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002084 try {
2085 startActivityForResult(intent, REQUEST_PICK_WALLPAPER,
2086 // If there is no target package, use the default intent chooser animation
2087 hasTargetPackage ? getActivityLaunchOptions(v) : null);
2088 } catch (ActivityNotFoundException e) {
2089 setWaitingForResult(null);
2090 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2091 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002092 }
2093
Sunny Goyala7ce1662016-05-31 15:01:35 -07002094 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002095 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002096 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2097 try {
2098 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2099 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2100 // is enabled by default on NYC.
2101 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2102 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002103
2104 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2105 String id = ((ShortcutInfo) info).getDeepShortcutId();
2106 String packageName = intent.getPackage();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05302107 DeepShortcutManager.getInstance(this).startShortcut(
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002108 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002109 } else {
2110 // Could be launching some bookkeeping activity
2111 startActivity(intent, optsBundle);
2112 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002113 } finally {
2114 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002115 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002116 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002117 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2118 // corresponding permission. Show the appropriate permission prompt if that
2119 // is the case.
2120 if (intent.getComponent() == null
2121 && Intent.ACTION_CALL.equals(intent.getAction())
2122 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2123 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002124
2125 setWaitingForResult(PendingRequestArgs
2126 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002127 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2128 REQUEST_PERMISSION_CALL_PHONE);
2129 } else {
2130 // No idea why this was thrown.
2131 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002132 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002133 }
2134 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002135
Sunny Goyalfe770c92016-09-27 14:38:58 -07002136 @TargetApi(Build.VERSION_CODES.M)
Tonye3c59252017-05-02 21:32:27 -07002137 public Bundle getActivityLaunchOptions(View v) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002138 if (Utilities.ATLEAST_MARSHMALLOW) {
2139 int left = 0, top = 0;
2140 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07002141 if (v instanceof BubbleTextView) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002142 // Launch from center of icon, not entire view
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07002143 Drawable icon = ((BubbleTextView) v).getIcon();
Sunny Goyala7ce1662016-05-31 15:01:35 -07002144 if (icon != null) {
2145 Rect bounds = icon.getBounds();
2146 left = (width - bounds.width()) / 2;
2147 top = v.getPaddingTop();
2148 width = bounds.width();
2149 height = bounds.height();
2150 }
2151 }
2152 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2153 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2154 // On L devices, we use the device default slide-up transition.
2155 // On L MR1 devices, we use a custom version of the slide-up transition which
2156 // doesn't have the delay present in the device default.
2157 return ActivityOptions.makeCustomAnimation(
2158 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2159 }
2160 return null;
2161 }
2162
Tonye3c59252017-05-02 21:32:27 -07002163 public Rect getViewBounds(View v) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002164 int[] pos = new int[2];
2165 v.getLocationOnScreen(pos);
2166 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2167 }
2168
Sunny Goyala7ce1662016-05-31 15:01:35 -07002169 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002170 mAppLaunchSuccess = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002171 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2172 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002173 return mAppLaunchSuccess;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002174 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002175 // Only launch using the new animation if the shortcut has not opted out (this is a
2176 // private contract between launcher and may be ignored in the future).
2177 boolean useLaunchAnimation = (v != null) &&
2178 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2179 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2180
Sunny Goyal24bb66a2017-03-21 15:12:01 -07002181 UserHandle user = item == null ? null : item.user;
Sunny Goyala7ce1662016-05-31 15:01:35 -07002182
2183 // Prepare intent
2184 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2185 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002186 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002187 }
2188 try {
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002189 if (Utilities.ATLEAST_MARSHMALLOW
2190 && (item instanceof ShortcutInfo)
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002191 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002192 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyale6e72002017-01-12 16:55:36 -08002193 && !((ShortcutInfo) item).isPromise()) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002194 // Shortcuts need some special checks due to legacy reasons.
2195 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002196 } else if (user == null || user.equals(Process.myUserHandle())) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002197 // Could be launching some bookkeeping activity
2198 startActivity(intent, optsBundle);
2199 } else {
2200 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2201 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2202 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002203
2204 if (v instanceof BubbleTextView) {
2205 // This is set to the view that launched the activity that navigated the user away
2206 // from launcher. Since there is no callback for when the activity has finished
2207 // launching, enable the press state and keep this reference to reset the press
2208 // state when we return to launcher.
2209 BubbleTextView btv = (BubbleTextView) v;
2210 btv.setStayPressed(true);
2211 setOnResumeCallback(btv);
2212 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002213 mAppLaunchSuccess = true;
2214 getUserEventDispatcher().logAppLaunch(v, intent); // TODO for discovered apps b/35802115
Sunny Goyala7ce1662016-05-31 15:01:35 -07002215 } catch (ActivityNotFoundException|SecurityException e) {
2216 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2217 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2218 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002219 return mAppLaunchSuccess;
Michael Jurka86a720e2012-05-09 11:23:23 -07002220 }
2221
Tony Wickhamdadb3042016-02-24 11:07:00 -08002222 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07002223 public boolean dispatchTouchEvent(MotionEvent ev) {
2224 mLastDispatchTouchEventX = ev.getX();
2225 return super.dispatchTouchEvent(ev);
2226 }
2227
2228 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002229 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002230 if (!isDraggingEnabled()) return false;
2231 if (isWorkspaceLocked()) return false;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002232 if (!isInState(NORMAL) && !isInState(OVERVIEW)) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002233
Jon Miranda51f037d2016-11-07 08:37:20 -08002234 boolean ignoreLongPressToOverview =
2235 mDeviceProfile.shouldIgnoreLongPressToOverview(mLastDispatchTouchEventX);
Jon Miranda379198e2016-09-23 08:54:40 -07002236
Adam Cohen1697b792013-09-17 19:08:21 -07002237 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002238 if (!isInState(OVERVIEW)) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002239 if (!mWorkspace.isTouchActive() && !ignoreLongPressToOverview) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302240 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2241 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002242 mWorkspace.getCurrentPage());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002243 getStateManager().goToState(OVERVIEW);
Adam Cohen93c97562013-09-26 13:48:01 -07002244 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2245 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2246 return true;
2247 } else {
2248 return false;
2249 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002250 } else {
2251 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002252 }
Adam Cohen1697b792013-09-17 19:08:21 -07002253 }
2254
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002255 CellLayout.CellInfo longClickCellInfo = null;
2256 View itemUnderLongClick = null;
2257 if (v.getTag() instanceof ItemInfo) {
2258 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002259 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002260 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07002261 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002262 }
2263
Winson Chung3d503fb2011-07-13 17:25:49 -07002264 // The hotseat touch handling does not go through Workspace, and we always allow long press
2265 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002266 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002267 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002268 // User long pressed on empty space
Sunny Goyalc99cb172017-10-19 16:15:09 -07002269 if (mWorkspace.isPageRearrangeEnabled()) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002270 mWorkspace.startReordering(v);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302271 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2272 Action.Direction.NONE, ContainerType.OVERVIEW);
Adam Cohend3ecce92013-09-16 14:58:00 -07002273 } else {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002274 if (ignoreLongPressToOverview) {
Jon Miranda379198e2016-09-23 08:54:40 -07002275 return false;
2276 }
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302277 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2278 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002279 mWorkspace.getCurrentPage());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002280 getStateManager().goToState(OVERVIEW);
Adam Cohendedbd962013-07-11 14:21:49 -07002281 }
Jon Miranda379198e2016-09-23 08:54:40 -07002282 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2283 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002284 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07002285 final boolean isAllAppsButton =
2286 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
2287 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
2288 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05002289 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002290 // User long pressed on an item
Tony Wickham10236d62016-09-28 12:49:25 -07002291 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002292 }
2293 }
2294 }
2295 return true;
2296 }
2297
Winson Chung3d503fb2011-07-13 17:25:49 -07002298 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08002299 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07002300 return mHotseat != null && layout != null &&
2301 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2302 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002303
Winson Chung3d503fb2011-07-13 17:25:49 -07002304 /**
2305 * Returns the CellLayout of the specified container at the specified screen.
2306 */
Sunny Goyal92820592015-03-02 11:31:35 -08002307 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002308 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2309 if (mHotseat != null) {
2310 return mHotseat.getLayout();
2311 } else {
2312 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002313 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002314 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002315 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002316 }
2317 }
2318
Michael Jurkae326f182011-11-21 14:05:46 -08002319 @Override
2320 public void onTrimMemory(int level) {
2321 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07002322 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2323 // The widget preview db can result in holding onto over
2324 // 3MB of memory for caching which isn't necessary.
2325 SQLiteDatabase.releaseMemory();
2326
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08002327 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07002328 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08002329 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01002330 if (mLauncherCallbacks != null) {
2331 mLauncherCallbacks.onTrimMemory(level);
2332 }
Michael Jurkae326f182011-11-21 14:05:46 -08002333 }
2334
Michael Jurkad7c28052012-04-27 15:43:36 -07002335 @Override
2336 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07002337 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07002338 final List<CharSequence> text = event.getText();
2339 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07002340 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002341 // TODO: When can workspace be null?
2342 text.add(mWorkspace == null
2343 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07002344 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07002345 return result;
2346 }
2347
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002348 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07002349 * If the activity is currently paused, signal that we need to run the passed Runnable
2350 * in onResume.
2351 *
2352 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07002353 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
2354 * wrong when we're not running, and if the activity comes back to what the configuration was
2355 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07002356 *
2357 * Implementation of the method from LauncherModel.Callbacks.
2358 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07002359 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07002360 */
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002361 @Thunk boolean waitUntilResume(Runnable run) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002362 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002363 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002364 if (run instanceof RunnableWithId) {
2365 // Remove any runnables which have the same id
2366 while (mBindOnResumeCallbacks.remove(run)) { }
Michael Jurkac402cd92013-05-20 15:49:32 +02002367 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07002368 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07002369 return true;
2370 } else {
2371 return false;
2372 }
2373 }
2374
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002375 public void setOnResumeCallback(OnResumeCallback callback) {
2376 if (mOnResumeCallback != null) {
2377 mOnResumeCallback.onLauncherResume();
2378 }
2379 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07002380 }
2381
Michael Jurka7607c2f2013-04-03 14:33:19 -07002382 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002383 * If the activity is currently paused, signal that we need to re-run the loader
2384 * in onResume.
2385 *
2386 * This needs to be called from incoming places where resources might have been loaded
2387 * while we are paused. That is becaues the Configuration might be wrong
2388 * when we're not running, and if it comes back to what it was when we
2389 * were paused, we are not restarted.
2390 *
2391 * Implementation of the method from LauncherModel.Callbacks.
2392 *
2393 * @return true if we are currently paused. The caller might be able to
2394 * skip some work in that case since we will come back again.
2395 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002396 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002397 public boolean setLoadOnResume() {
2398 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002399 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002400 mOnResumeNeedsLoad = true;
2401 return true;
2402 } else {
2403 return false;
2404 }
2405 }
2406
2407 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002408 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002409 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002410 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002411 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002412 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002413 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002414 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07002415 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07002416 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002417 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002418
Joe Onorato9c1289c2009-08-17 11:03:03 -04002419 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07002420 * Clear any pending bind callbacks. This is called when is loader is planning to
2421 * perform a full rebind from scratch.
2422 */
2423 @Override
2424 public void clearPendingBinds() {
2425 mBindOnResumeCallbacks.clear();
2426 if (mPendingExecutor != null) {
2427 mPendingExecutor.markCompleted();
2428 mPendingExecutor = null;
2429 }
2430 }
2431
2432 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002433 * Refreshes the shortcuts shown on the workspace.
2434 *
2435 * Implementation of the method from LauncherModel.Callbacks.
2436 */
2437 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002438 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002439 // Floating panels (except the full widget sheet) are associated with individual icons. If
2440 // we are starting a fresh bind, close all such panels as all the icons are about
2441 // to go away.
2442 AbstractFloatingView.closeOpenViews(this, true,
2443 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08002444
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002445 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08002446
Winson Chungd64d1762013-08-20 14:37:16 -07002447 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07002448 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07002449 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07002450
Winson Chung3d503fb2011-07-13 17:25:49 -07002451 if (mHotseat != null) {
2452 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002453 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002454 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002455 }
2456
Adam Cohendcd297f2013-06-18 13:13:40 -07002457 @Override
2458 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002459 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002460 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
2461 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002462 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
2463 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002464 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002465 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
2466 // If there are no screens, we need to have an empty screen
2467 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07002468 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002469 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07002470
Winsonc7d2e832016-07-28 12:24:55 -07002471 // After we have added all the screens, if the wallpaper was locked to the default state,
2472 // then notify to indicate that it can be released and a proper wallpaper offset can be
2473 // computed before the next layout
2474 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07002475 }
2476
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002477 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002478 int count = orderedScreenIds.size();
2479 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002480 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002481 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002482 // No need to bind the first screen, as its always bound.
2483 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
2484 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002485 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002486 }
2487
Sunny Goyalb23980c2017-08-17 07:45:25 -07002488 @Override
Winson Chungd64d1762013-08-20 14:37:16 -07002489 public void bindAppsAdded(final ArrayList<Long> newScreens,
2490 final ArrayList<ItemInfo> addNotAnimated,
Sunny Goyalb23980c2017-08-17 07:45:25 -07002491 final ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07002492 Runnable r = new Runnable() {
2493 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002494 bindAppsAdded(newScreens, addNotAnimated, addAnimated);
Winson Chungd64d1762013-08-20 14:37:16 -07002495 }
2496 };
2497 if (waitUntilResume(r)) {
2498 return;
2499 }
2500
Winson Chungd64d1762013-08-20 14:37:16 -07002501 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08002502 if (newScreens != null) {
2503 bindAddScreens(newScreens);
2504 }
Winson Chungd64d1762013-08-20 14:37:16 -07002505
2506 // We add the items without animation on non-visible pages, and with
2507 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08002508 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002509 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002510 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002511 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002512 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07002513 }
Winson Chungc58497e2013-09-03 17:48:37 -07002514
Winson Chung87412982013-10-03 18:34:14 -07002515 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07002516 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002517 }
2518
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002519 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002520 * Bind the items start-end from the list.
2521 *
2522 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002523 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07002524 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07002525 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07002526 Runnable r = new Runnable() {
2527 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002528 bindItems(items, forceAnimateIcons);
Winson Chungd64d1762013-08-20 14:37:16 -07002529 }
2530 };
2531 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002532 return;
2533 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002534
Sunny Goyal3be633b2016-12-08 09:59:25 -08002535 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07002536 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002537 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07002538 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07002539 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002540 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07002541 int end = items.size();
2542 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002543 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002544
2545 // Short circuit if we are loading dock items for a configuration which has no dock
2546 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2547 mHotseat == null) {
2548 continue;
2549 }
2550
Sunny Goyal639e9062015-08-19 19:17:06 -07002551 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002552 switch (item.itemType) {
2553 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2554 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08002555 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07002556 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07002557 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002558 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002559 }
2560 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07002561 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002562 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002563 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002564 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002565 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07002566 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2567 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07002568 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002569 if (view == null) {
2570 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002571 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08002572 break;
2573 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002574 default:
2575 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002576 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002577
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002578 /*
2579 * Remove colliding items.
2580 */
2581 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
2582 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
2583 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
2584 View v = cl.getChildAt(item.cellX, item.cellY);
2585 Object tag = v.getTag();
2586 String desc = "Collision while binding workspace item: " + item
2587 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08002588 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002589 throw (new RuntimeException(desc));
2590 } else {
2591 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002592 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002593 continue;
2594 }
2595 }
2596 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302597 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07002598 if (animateIcons) {
2599 // Animate all the applications up now
2600 view.setAlpha(0f);
2601 view.setScaleX(0f);
2602 view.setScaleY(0f);
2603 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08002604 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07002605 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002606 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07002607
Winson Chung997a9232013-07-24 15:33:46 -07002608 if (animateIcons) {
2609 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08002610 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07002611 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08002612 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07002613 final Runnable startBounceAnimRunnable = new Runnable() {
2614 public void run() {
2615 anim.playTogether(bounceAnims);
2616 anim.start();
2617 }
2618 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08002619 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07002620 // We post the animation slightly delayed to prevent slowdowns
2621 // when we are loading right after we return to launcher.
2622 mWorkspace.postDelayed(new Runnable() {
2623 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00002624 if (mWorkspace != null) {
2625 mWorkspace.snapToPage(newScreenIndex);
2626 mWorkspace.postDelayed(startBounceAnimRunnable,
2627 NEW_APPS_ANIMATION_DELAY);
2628 }
Winson Chung94d67682013-09-25 16:29:40 -07002629 }
2630 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07002631 } else {
2632 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07002633 }
2634 }
Winson Chung64359a52013-07-08 17:17:08 -07002635 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002636 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002637 }
2638
Joe Onorato9c1289c2009-08-17 11:03:03 -04002639 /**
2640 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002641 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07002642 public void bindAppWidget(LauncherAppWidgetInfo item) {
2643 View view = inflateAppWidget(item);
2644 if (view != null) {
2645 mWorkspace.addInScreen(view, item);
2646 mWorkspace.requestLayout();
2647 }
2648 }
2649
2650 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002651 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302652 PendingAppWidgetHostView view =
2653 new PendingAppWidgetHostView(this, item, mIconCache, true);
2654 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002655 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002656 }
2657
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002658 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002659
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002660 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08002661
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002662 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2663 // If the provider is not ready, bind as a pending widget.
2664 appWidgetInfo = null;
2665 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2666 // The widget id is not valid. Try to find the widget based on the provider info.
2667 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
2668 } else {
2669 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
2670 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002671
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002672 // If the provider is ready, but the width is not yet restored, try to restore it.
2673 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
2674 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07002675 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002676 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
2677 + " belongs to component " + item.providerName
2678 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002679 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002680 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002681 }
Sunny Goyalff572272014-07-23 13:58:07 -07002682
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002683 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002684 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07002685 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2686 // Id has not been allocated yet. Allocate a new id.
2687 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
2688 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07002689
Sunny Goyald478c832016-04-01 12:04:16 -07002690 // Also try to bind the widget. If the bind fails, the user will be shown
2691 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08002692 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07002693 pendingInfo.spanX = item.spanX;
2694 pendingInfo.spanY = item.spanY;
2695 pendingInfo.minSpanX = item.minSpanX;
2696 pendingInfo.minSpanY = item.minSpanY;
2697 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07002698
2699 boolean isDirectConfig =
2700 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2701 if (isDirectConfig && item.bindOptions != null) {
2702 Bundle newOptions = item.bindOptions.getExtras();
2703 if (options != null) {
2704 newOptions.putAll(options);
2705 }
2706 options = newOptions;
2707 }
Sunny Goyald478c832016-04-01 12:04:16 -07002708 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2709 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002710
Sunny Goyal86df1382016-08-10 15:03:22 -07002711 // We tried to bind once. If we were not able to bind, we would need to
2712 // go through the permission dialog, which means we cannot skip the config
2713 // activity.
2714 item.bindOptions = null;
2715 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2716
Sunny Goyald478c832016-04-01 12:04:16 -07002717 // Bind succeeded
2718 if (success) {
2719 // If the widget has a configure activity, it is still needs to set it up,
2720 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002721 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002722 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2723 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002724 }
Sunny Goyald478c832016-04-01 12:04:16 -07002725
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002726 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002727 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002728 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002729 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002730 // The widget was marked as UI not ready, but there is no configure activity to
2731 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002732 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002733 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002734 }
Sunny Goyalff572272014-07-23 13:58:07 -07002735 }
2736
Sunny Goyald5462aa2016-11-22 16:52:39 +05302737 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002738 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002739 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002740 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002741 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07002742 deleteWidgetInfo(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002743 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002744 }
2745
Sunny Goyal233ee962015-08-03 13:05:01 -07002746 item.minSpanX = appWidgetInfo.minSpanX;
2747 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302748 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002749 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302750 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002751 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302752 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002753
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002754 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002755 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002756 }
2757
Sunny Goyalff572272014-07-23 13:58:07 -07002758 /**
2759 * Restores a pending widget.
2760 *
2761 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002762 */
Sunny Goyald478c832016-04-01 12:04:16 -07002763 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002764 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2765 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2766 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002767 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002768 }
2769
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002770 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002771 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002772 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2773 info.pendingItemInfo = null;
2774 }
Sunny Goyalff572272014-07-23 13:58:07 -07002775
Sunny Goyalba47faa2017-10-04 16:50:57 -07002776 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
2777 view.reinflate();
2778 }
2779
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002780 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002781 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002782 }
2783
Adam Cohen1462de32012-07-24 22:34:36 -07002784 public void onPageBoundSynchronously(int page) {
2785 mSynchronouslyBoundPages.add(page);
2786 }
2787
Sunny Goyal527c7d32015-08-28 15:19:36 -07002788 @Override
2789 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2790 if (mPendingExecutor != null) {
2791 mPendingExecutor.markCompleted();
2792 }
2793 mPendingExecutor = executor;
2794 executor.attachTo(this);
2795 }
2796
2797 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2798 if (mPendingExecutor == executor) {
2799 mPendingExecutor = null;
2800 }
2801 }
2802
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002803 @Override
2804 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
2805 Runnable r = new Runnable() {
2806 public void run() {
2807 finishFirstPageBind(executor);
2808 }
2809 };
2810 if (waitUntilResume(r)) {
2811 return;
2812 }
2813
2814 Runnable onComplete = new Runnable() {
2815 @Override
2816 public void run() {
2817 if (executor != null) {
2818 executor.onLoadAnimationCompleted();
2819 }
2820 }
2821 };
2822 if (mDragLayer.getAlpha() < 1) {
2823 mDragLayer.animate().alpha(1).withEndAction(onComplete).start();
2824 } else {
2825 onComplete.run();
2826 }
2827 }
2828
Joe Onorato9c1289c2009-08-17 11:03:03 -04002829 /**
2830 * Callback saying that there aren't any more items to bind.
2831 *
2832 * Implementation of the method from LauncherModel.Callbacks.
2833 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002834 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07002835 Runnable r = new Runnable() {
2836 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002837 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07002838 }
2839 };
2840 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002841 return;
2842 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002843 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002844 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002845
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002846 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002847
Sunny Goyal2100c782016-08-22 16:00:03 -07002848 if (mPendingActivityResult != null) {
2849 handleActivityResult(mPendingActivityResult.requestCode,
2850 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2851 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002852 }
2853
Sunny Goyala474a9b2017-05-04 16:47:11 -07002854 InstallShortcutReceiver.disableAndFlushInstallQueue(
2855 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002856
Tony Wickham010d2552017-01-20 08:15:28 -08002857 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002858 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002859 }
2860
Winson Chunga2413752012-04-03 14:22:34 -07002861 private boolean canRunNewAppsAnimation() {
2862 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002863 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002864 }
2865
Winson Chungc9168342013-06-26 14:54:55 -07002866 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002867 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07002868 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
2869 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002870 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002871 return bounceAnim;
2872 }
2873
Adam Cohen27772732013-11-11 14:00:56 +00002874 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07002875 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00002876 }
2877
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002878 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002879 * Add the icons for all apps.
2880 *
2881 * Implementation of the method from LauncherModel.Callbacks.
2882 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002883 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002884 Runnable r = new RunnableWithId(RUNNABLE_ID_BIND_APPS) {
2885 public void run() {
2886 bindAllApplications(apps);
2887 }
2888 };
2889 if (waitUntilResume(r)) {
Winson Chungbb785c62015-05-05 10:05:08 -07002890 return;
2891 }
2892
Winson Chungb745afb2015-03-02 11:51:23 -08002893 if (mAppsView != null) {
Tony2917a8b2017-07-31 23:29:42 -07002894 Executor pendingExecutor = getPendingExecutor();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002895 if (pendingExecutor != null && !isInState(ALL_APPS)) {
Tony2917a8b2017-07-31 23:29:42 -07002896 // Wait until the fade in animation has finished before setting all apps list.
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002897 pendingExecutor.execute(r);
Tony2917a8b2017-07-31 23:29:42 -07002898 return;
2899 }
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002900
Winson Chungb745afb2015-03-02 11:51:23 -08002901 mAppsView.setApps(apps);
2902 }
Adam Cohen9211d422014-10-07 18:14:53 -07002903 if (mLauncherCallbacks != null) {
2904 mLauncherCallbacks.bindAllApplications(apps);
2905 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002906 }
2907
2908 /**
Tony2917a8b2017-07-31 23:29:42 -07002909 * Returns an Executor that will run after the launcher is first drawn (including after the
2910 * initial fade in animation). Returns null if the first draw has already occurred.
2911 */
2912 public @Nullable Executor getPendingExecutor() {
2913 return mPendingExecutor != null && mPendingExecutor.canQueue() ? mPendingExecutor : null;
2914 }
2915
2916 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002917 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
2918 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2919 */
2920 @Override
2921 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002922 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002923 }
2924
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002925 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002926 * A package was updated.
2927 *
2928 * Implementation of the method from LauncherModel.Callbacks.
2929 */
Sunny Goyalb23980c2017-08-17 07:45:25 -07002930 public void bindAppsAddedOrUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07002931 Runnable r = new Runnable() {
2932 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002933 bindAppsAddedOrUpdated(apps);
Winson Chungd64d1762013-08-20 14:37:16 -07002934 }
2935 };
2936 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002937 return;
2938 }
2939
Winson Chungb745afb2015-03-02 11:51:23 -08002940 if (mAppsView != null) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002941 mAppsView.addOrUpdateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07002942 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002943 }
2944
Sunny Goyal4390ace2014-10-13 11:33:11 -07002945 @Override
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002946 public void bindPromiseAppProgressUpdated(final PromiseAppInfo app) {
2947 Runnable r = new Runnable() {
2948 public void run() {
2949 bindPromiseAppProgressUpdated(app);
2950 }
2951 };
2952 if (waitUntilResume(r)) {
2953 return;
2954 }
2955
2956 if (mAppsView != null) {
2957 mAppsView.updatePromiseAppProgress(app);
2958 }
2959 }
2960
2961 @Override
Sunny Goyal4390ace2014-10-13 11:33:11 -07002962 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
2963 Runnable r = new Runnable() {
2964 public void run() {
2965 bindWidgetsRestored(widgets);
2966 }
2967 };
2968 if (waitUntilResume(r)) {
2969 return;
2970 }
2971 mWorkspace.widgetsRestored(widgets);
2972 }
2973
Joe Onorato9c1289c2009-08-17 11:03:03 -04002974 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002975 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002976 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002977 *
2978 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002979 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002980 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002981 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002982 Runnable r = new Runnable() {
2983 public void run() {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002984 bindShortcutsChanged(updated, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002985 }
2986 };
2987 if (waitUntilResume(r)) {
2988 return;
2989 }
2990
Sunny Goyal4390ace2014-10-13 11:33:11 -07002991 if (!updated.isEmpty()) {
2992 mWorkspace.updateShortcuts(updated);
2993 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002994 }
2995
2996 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002997 * Update the state of a package, typically related to install state.
2998 *
2999 * Implementation of the method from LauncherModel.Callbacks.
3000 */
Sunny Goyale755d462014-07-22 13:48:29 -07003001 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07003002 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
3003 Runnable r = new Runnable() {
3004 public void run() {
3005 bindRestoreItemsChange(updates);
3006 }
3007 };
3008 if (waitUntilResume(r)) {
3009 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003010 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003011
Sunny Goyal756adbc2015-04-16 15:20:51 -07003012 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07003013 }
3014
3015 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003016 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07003017 * in addition to specific applications to remove, the reason being that
3018 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003019 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07003020 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003021 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07003022 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003023 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Winson Chungd64d1762013-08-20 14:37:16 -07003024 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003025 public void run() {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003026 bindWorkspaceComponentsRemoved(matcher);
Winson Chung83892cc2013-05-01 16:53:33 -07003027 }
Winson Chungd64d1762013-08-20 14:37:16 -07003028 };
3029 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003030 return;
3031 }
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003032 mWorkspace.removeItemsByMatcher(matcher);
3033 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003034 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003035
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003036 @Override
3037 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
3038 Runnable r = new Runnable() {
3039 public void run() {
3040 bindAppInfosRemoved(appInfos);
3041 }
3042 };
3043 if (waitUntilResume(r)) {
3044 return;
Joe Onorato36115782010-06-17 13:28:48 -04003045 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003046
Winson Chungdf95eb12013-10-16 14:57:07 -07003047 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08003048 if (mAppsView != null) {
3049 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07003050 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003051 }
Joe Onoratobe386092009-11-17 17:32:16 -08003052
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003053 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07003054 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
3055 mPopupDataProvider.setAllWidgets(allWidgets);
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003056 Runnable r = new RunnableWithId(RUNNABLE_ID_BIND_WIDGETS) {
3057 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07003058 public void run() {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003059 bindAllWidgets(allWidgets);
Winson Chung83892cc2013-05-01 16:53:33 -07003060 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003061 };
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003062 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003063 return;
3064 }
3065
Tony Wickham26b17462017-03-20 17:12:24 -07003066 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
3067 if (topView != null) {
3068 topView.onWidgetsBound();
3069 }
3070 }
3071
Tony Wickham86222d22017-03-29 15:30:43 -07003072 /**
3073 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
3074 * refreshes the widgets and shortcuts associated with the given package/user
3075 */
3076 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07003077 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003078 }
3079
Sunny Goyalc99cb172017-10-19 16:15:09 -07003080 public boolean isRotationEnabled () {
3081 return mRotationEnabled;
Winson Chung400438b2011-01-16 17:53:48 -08003082 }
3083
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003084 private boolean shouldShowDiscoveryBounce() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07003085 return isInState(NORMAL)
Sunny Goyalbe93f262017-10-20 17:05:27 -07003086 && !mSharedPrefs.getBoolean(AllAppsState.APPS_VIEW_SHOWN, false)
Sunny Goyalf9403d92017-10-18 10:55:56 -07003087 && !UserManagerCompat.getInstance(this).isDemoUser();
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003088 }
3089
Winson Chung80baf5a2010-08-09 16:03:15 -07003090 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003091 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08003092 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003093 @Override
3094 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3095 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003096
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003097 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
3098 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07003099 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003100 writer.println(prefix + " Homescreen " + i);
3101
3102 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
3103 for (int j = 0; j < layout.getChildCount(); j++) {
3104 Object tag = layout.getChildAt(j).getTag();
3105 if (tag != null) {
3106 writer.println(prefix + " " + tag.toString());
3107 }
3108 }
3109 }
3110
3111 writer.println(prefix + " Hotseat");
3112 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07003113 for (int j = 0; j < layout.getChildCount(); j++) {
3114 Object tag = layout.getChildAt(j).getTag();
3115 if (tag != null) {
3116 writer.println(prefix + " " + tag.toString());
3117 }
Adam Cohen4caf2982013-08-20 18:54:31 -07003118 }
Sunny Goyala1365452015-10-01 15:46:24 -07003119
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003120 try {
3121 FileLog.flushAll(writer);
3122 } catch (Exception e) {
3123 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07003124 }
3125 }
3126
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003127 writer.println(prefix + "Misc:");
3128 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
3129 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
3130 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
3131
3132 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003133
Adam Cohen9211d422014-10-07 18:14:53 -07003134 if (mLauncherCallbacks != null) {
3135 mLauncherCallbacks.dump(prefix, fd, writer, args);
3136 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003137 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003138
Sunny Goyal66b24572016-09-21 15:57:55 -07003139 @Override
3140 @TargetApi(Build.VERSION_CODES.N)
3141 public void onProvideKeyboardShortcuts(
3142 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
3143
3144 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07003145 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07003146 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
3147 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
3148 }
3149 View currentFocus = getCurrentFocus();
3150 if (new CustomActionsPopup(this, currentFocus).canShow()) {
3151 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
3152 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
3153 }
Tony18c4aa42017-05-10 21:23:57 -05003154 if (currentFocus.getTag() instanceof ItemInfo
3155 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
Sunny Goyal66b24572016-09-21 15:57:55 -07003156 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
3157 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
3158 }
3159 if (!shortcutInfos.isEmpty()) {
3160 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
3161 }
3162
3163 super.onProvideKeyboardShortcuts(data, menu, deviceId);
3164 }
3165
3166 @Override
3167 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
3168 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
3169 switch (keyCode) {
3170 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07003171 if (isInState(NORMAL)) {
3172 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07003173 return true;
3174 }
3175 break;
3176 case KeyEvent.KEYCODE_S: {
3177 View focusedView = getCurrentFocus();
3178 if (focusedView instanceof BubbleTextView
3179 && focusedView.getTag() instanceof ItemInfo
3180 && mAccessibilityDelegate.performAction(focusedView,
3181 (ItemInfo) focusedView.getTag(),
3182 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal10a1bd02017-10-09 14:56:21 -07003183 BaseActionPopup.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07003184 return true;
3185 }
3186 break;
3187 }
3188 case KeyEvent.KEYCODE_O:
3189 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
3190 return true;
3191 }
3192 break;
3193 }
3194 }
3195 return super.onKeyShortcut(keyCode, event);
3196 }
3197
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07003198 public static Launcher getLauncher(Context context) {
3199 if (context instanceof Launcher) {
3200 return (Launcher) context;
3201 }
3202 return ((Launcher) ((ContextWrapper) context).getBaseContext());
3203 }
3204
Sunny Goyal5a81c382017-03-20 15:08:06 -07003205 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener {
Sunny Goyal745bad92016-05-02 10:54:12 -07003206
3207 @Override
3208 public void onSharedPreferenceChanged(
3209 SharedPreferences sharedPreferences, String key) {
3210 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
Sunny Goyale13d4642017-05-22 15:58:42 -07003211 // Recreate the activity so that it initializes the rotation preference again.
3212 recreate();
Sunny Goyal745bad92016-05-02 10:54:12 -07003213 }
3214 }
Sunny Goyal745bad92016-05-02 10:54:12 -07003215 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07003216
3217 /**
3218 * Callback for listening for onResume
3219 */
3220 public interface OnResumeCallback {
3221
3222 void onLauncherResume();
3223 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003224}