blob: 782ddc30fbf92d30d950267ad0690218fd2a1c9a [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;
Mario Bertschler27288382017-05-24 15:35:09 -0700110import com.android.launcher3.dynamicui.WallpaperColorInfo;
Sunny Goyal26119432016-02-18 22:09:23 +0000111import com.android.launcher3.folder.Folder;
112import com.android.launcher3.folder.FolderIcon;
Sunny Goyal66b24572016-09-21 15:57:55 -0700113import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700114import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700115import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700116import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyala535ae42017-02-27 10:07:13 -0800117import com.android.launcher3.model.ModelWriter;
Sunny Goyala535ae42017-02-27 10:07:13 -0800118import com.android.launcher3.notification.NotificationListener;
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700119import com.android.launcher3.pageindicators.PageIndicator;
Sunny Goyal10a1bd02017-10-09 14:56:21 -0700120import com.android.launcher3.popup.BaseActionPopup;
Tony Wickham540913e2017-01-23 11:47:51 -0800121import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -0800122import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700123import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700124import com.android.launcher3.states.AllAppsState;
Sunny Goyalf8088ee2017-11-10 14:52:00 -0800125import com.android.launcher3.states.InternalStateHandler;
Jon Mirandac6cf4932017-02-07 17:12:36 -0800126import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530127import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
128import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
129import com.android.launcher3.userevent.nano.LauncherLogProto.ControlType;
Sunny Goyalaeb16432017-10-16 11:46:41 -0700130import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyal2100c782016-08-22 16:00:03 -0700131import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700132import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700133import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700134import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700135import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800136import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700137import com.android.launcher3.util.PendingRequestArgs;
Sunny Goyal326403e2017-10-02 12:45:10 -0700138import com.android.launcher3.util.RunnableWithId;
Sunny Goyal8392c822017-06-20 10:03:56 -0700139import com.android.launcher3.util.SystemUiController;
Mario Bertschlera6936942017-05-31 14:48:19 -0700140import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700141import com.android.launcher3.util.Thunk;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700142import com.android.launcher3.util.TraceHelper;
Sunny Goyal326403e2017-10-02 12:45:10 -0700143import com.android.launcher3.util.UiThreadHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700144import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800145import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700146import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800147import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700148import com.android.launcher3.widget.WidgetHostViewLoader;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700149import com.android.launcher3.widget.WidgetListRowEntry;
150import com.android.launcher3.widget.WidgetsFullSheet;
Sunny Goyal952e63d2017-08-16 04:59:08 -0700151import com.android.launcher3.widget.custom.CustomWidgetParser;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700152
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700153import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700154import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700155import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700156import java.util.Collection;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700157import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700158import java.util.List;
Tony Wickham010d2552017-01-20 08:15:28 -0800159import java.util.Set;
Tony2917a8b2017-07-31 23:29:42 -0700160import java.util.concurrent.Executor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700161
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162/**
163 * Default launcher application.
164 */
Sunny Goyal27835952017-01-13 12:15:53 -0800165public class Launcher extends BaseActivity
Adam Cohen79d90c52016-04-22 13:29:20 -0700166 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
167 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Mario Bertschler27288382017-05-24 15:35:09 -0700168 WallpaperColorInfo.OnThemeChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700169 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700170 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171
Winson Chunga2413752012-04-03 14:22:34 -0700172 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700173
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700175 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700176
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700177 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700178 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179
Michael Jurka8b805b12012-04-18 14:23:14 -0700180 private static final int REQUEST_BIND_APPWIDGET = 11;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700181 private static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
182 private static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700183
Jon Mirandac476d6e2017-05-04 16:30:01 -0700184 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700185
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700186 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
187
Mathew Inwood876a8462013-06-14 14:12:41 +0100188 /**
189 * IntentStarter uses request codes starting with this. This must be greater than all activity
190 * request codes used internally.
191 */
192 protected static final int REQUEST_LAST = 100;
193
Winson Chung2672ff92012-05-04 16:22:30 -0700194 // The Intent extra that defines whether to ignore the launch animation
195 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400196 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700197
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198 // Type: int
199 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700200 // Type: int
201 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700202 // Type: PendingRequestArgs
203 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
204 // Type: ActivityResultInfo
205 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700206 // Type: SparseArray<Parcelable>
207 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700209 private LauncherStateManager mStateManager;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700210
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700211 private boolean mIsSafeModeEnabled;
212
Adam Cohenad4e15c2013-10-17 16:21:35 -0700213 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800214
Winson Chunga2413752012-04-03 14:22:34 -0700215 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700216 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
217 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
218 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700219
Adam Cohen091440a2015-03-18 14:16:05 -0700220 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700221 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700222 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800223 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700224
Sunny Goyalffe83f12014-08-14 17:39:34 -0700225 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700226 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700227
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700228 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700229
Sunny Goyal316490e2015-06-02 09:38:28 -0700230 @Thunk Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700231
Michael Jurka838a4ca2011-02-07 13:33:06 -0800232 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700233
Sunny Goyal47328fd2016-05-25 18:56:41 -0700234 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700235
236 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700237 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700238 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700239
Winson Chung8ae41982017-11-10 11:42:13 -0800240 // UI and state for the overview panel
241 private ViewGroup mOverviewPanel;
242
Jon Miranda6bed3502017-09-19 14:43:17 -0700243 // We need to store the orientation Launcher was created with, due to a bug (b/64916689)
244 // that results in widgets being inflated in the wrong orientation.
245 private int mOrientation;
246
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800247 private SpannableStringBuilder mDefaultKeySsb = null;
248
Adam Cohen091440a2015-03-18 14:16:05 -0700249 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400250
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800251 private boolean mPaused = true;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700252 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800253
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700254 private final ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<>();
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700255 private OnResumeCallback mOnResumeCallback;
256
Sunny Goyal527c7d32015-08-28 15:19:36 -0700257 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700258
Joe Onorato9c1289c2009-08-17 11:03:03 -0400259 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800260 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800261 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700262 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Sunny Goyal1acc56a2016-09-25 21:23:25 -0700263 private boolean mHasFocus = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400264
Jon Miranda2d89ea82017-05-04 11:47:53 -0700265 private ObjectAnimator mScrimAnimator;
Jon Miranda7fda2852017-07-19 16:07:01 -0700266 private boolean mShouldFadeInScrim;
Jon Miranda2d89ea82017-05-04 11:47:53 -0700267
Tony Wickham010d2552017-01-20 08:15:28 -0800268 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700269
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700270 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<>();
Adam Cohen1462de32012-07-24 22:34:36 -0700271
Winson Chung46353de2012-02-16 14:05:10 -0800272 // We only want to get the SharedPreferences once since it does an FS stat each time we get
273 // it from the context.
274 private SharedPreferences mSharedPrefs;
275
Sunny Goyal2100c782016-08-22 16:00:03 -0700276 // Activity result which needs to be processed after workspace has loaded.
277 private ActivityResultInfo mPendingActivityResult;
278 /**
279 * Holds extra information required to handle a result from an external call, like
280 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
281 */
282 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800283
Jon Miranda379198e2016-09-23 08:54:40 -0700284 private float mLastDispatchTouchEventX = 0.0f;
Jon Miranda379198e2016-09-23 08:54:40 -0700285
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700286 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700287 private boolean mRotationEnabled = false;
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700288 private boolean mAppLaunchSuccess;
289
Sunny Goyal745bad92016-05-02 10:54:12 -0700290 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700291
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800292 @Override
293 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700294 if (DEBUG_STRICT_MODE) {
295 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
296 .detectDiskReads()
297 .detectDiskWrites()
298 .detectNetwork() // or .detectAll() for all detectable problems
299 .penaltyLog()
300 .build());
301 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
302 .detectLeakedSqlLiteObjects()
303 .detectLeakedClosableObjects()
304 .penaltyLog()
305 .penaltyDeath()
306 .build());
307 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700308 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700309
Adam Cohen9211d422014-10-07 18:14:53 -0700310 if (mLauncherCallbacks != null) {
311 mLauncherCallbacks.preOnCreate();
312 }
313
Mario Bertschler27288382017-05-24 15:35:09 -0700314 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
315 wallpaperColorInfo.setOnThemeChangeListener(this);
Mario Bertschlera6936942017-05-31 14:48:19 -0700316 overrideTheme(wallpaperColorInfo.isDark(), wallpaperColorInfo.supportsDarkText());
Mario Bertschler27288382017-05-24 15:35:09 -0700317
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800318 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700319 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400320
Sunny Goyal87f784c2017-01-11 10:48:34 -0800321 LauncherAppState app = LauncherAppState.getInstance(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700322
Adam Cohen2e6da152015-05-06 11:42:25 -0700323 // Load configuration-specific DeviceProfile
Sunny Goyal27835952017-01-13 12:15:53 -0800324 mDeviceProfile = app.getInvariantDeviceProfile().getDeviceProfile(this);
Jon Mirandafe964322017-03-22 10:25:17 -0700325 if (isInMultiWindowModeCompat()) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700326 Display display = getWindowManager().getDefaultDisplay();
327 Point mwSize = new Point();
328 display.getSize(mwSize);
329 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
330 }
Sunny Goyalc6205602015-05-21 20:46:33 -0700331
Jon Miranda6bed3502017-09-19 14:43:17 -0700332 mOrientation = getResources().getConfiguration().orientation;
Sunny Goyalf7258242015-10-19 16:59:07 -0700333 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700334 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800335 mModel = app.setLauncher(this);
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800336 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout());
Joe Onorato0589f0f2010-02-08 13:44:00 -0800337 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700338 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700339
Joe Onorato41a12d22009-10-31 18:30:00 -0400340 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700341 mAllAppsController = new AllAppsTransitionController(this);
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800342 mStateManager = new LauncherStateManager(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700343
Sunny Goyalffe83f12014-08-14 17:39:34 -0700344 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700345
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700346 mAppWidgetHost = new LauncherAppWidgetHost(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700347 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700348
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700349 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
350 // this also ensures that any synchronous binding below doesn't re-trigger another
351 // LauncherModel load.
352 mPaused = false;
353
Sunny Goyal60820d72017-05-09 12:40:11 -0700354 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700355
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800356 setupViews();
Winson1f064272016-07-18 17:18:02 -0700357 mDeviceProfile.layout(this, false /* notifyListeners */);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800358
Tony Wickham010d2552017-01-20 08:15:28 -0800359 mPopupDataProvider = new PopupDataProvider(this);
360
Sunny Goyald3864fa2017-11-14 15:06:36 -0800361 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
362 // In case we are on a device with locked rotation, we should look at preferences to check
363 // if the user has specifically allowed rotation.
364 if (!mRotationEnabled) {
365 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
366 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
367 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
368 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800369
Sunny Goyald3864fa2017-11-14 15:06:36 -0800370 boolean internalStateHandled = InternalStateHandler.handleCreate(this, getIntent());
371 if (internalStateHandled) {
372 // Temporarily enable the rotation
373 mRotationEnabled = true;
374
375 if (savedInstanceState != null) {
376 // InternalStateHandler has already set the appropriate state.
377 // We dont need to do anything.
378 savedInstanceState.remove(RUNTIME_STATE);
379 }
380 }
381 restoreState(savedInstanceState);
Winson Chungb63b44c2017-11-10 17:54:44 -0800382
Sunny Goyal2100c782016-08-22 16:00:03 -0700383 // We only load the page synchronously if the user rotates (or triggers a
384 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700385 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
386 if (savedInstanceState != null) {
387 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
388 }
Sunny Goyald3864fa2017-11-14 15:06:36 -0800389
Sunny Goyalfe770c92016-09-27 14:38:58 -0700390 if (!mModel.startLoader(currentScreen)) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800391 if (!internalStateHandled) {
392 // If we are not binding synchronously, show a fade in animation when
393 // the first page bind completes.
394 mDragLayer.setAlpha(0);
395 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700396 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700397 // Pages bound synchronously.
398 mWorkspace.setCurrentPage(currentScreen);
399
Sunny Goyal2100c782016-08-22 16:00:03 -0700400 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800401 }
402
403 // For handling default keys
404 mDefaultKeySsb = new SpannableStringBuilder();
405 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800406
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700407 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
408 // we want the screen to auto-rotate based on the current orientation
Sunny Goyalc99cb172017-10-19 16:15:09 -0700409 setRequestedOrientation(mRotationEnabled
410 ? SCREEN_ORIENTATION_UNSPECIFIED : SCREEN_ORIENTATION_NOSENSOR);
Winson Chungaf40f202013-09-18 18:26:31 -0700411
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800412 setContentView(mLauncherView);
Mario Bertschlera6936942017-05-31 14:48:19 -0700413
Jon Miranda7fda2852017-07-19 16:07:01 -0700414 // Listen for broadcasts
415 IntentFilter filter = new IntentFilter();
416 filter.addAction(Intent.ACTION_SCREEN_OFF);
417 filter.addAction(Intent.ACTION_USER_PRESENT); // When the device wakes up + keyguard is gone
418 registerReceiver(mReceiver, filter);
419 mShouldFadeInScrim = true;
Sunny Goyala616d2b2017-06-12 13:54:01 -0700420
Sunny Goyal8392c822017-06-20 10:03:56 -0700421 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
422 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700423
424 if (mLauncherCallbacks != null) {
425 mLauncherCallbacks.onCreate(savedInstanceState);
426 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700427
428 TraceHelper.endSection("Launcher-onCreate");
Adam Cohen9211d422014-10-07 18:14:53 -0700429 }
430
Sunny Goyal7779d622015-06-11 16:18:39 -0700431 @Override
Mario Bertschler27288382017-05-24 15:35:09 -0700432 public void onThemeChanged() {
433 recreate();
434 }
435
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700436 public LauncherStateManager getStateManager() {
437 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700438 }
439
Mario Bertschlera6936942017-05-31 14:48:19 -0700440 protected void overrideTheme(boolean isDark, boolean supportsDarkText) {
Mario Bertschler27288382017-05-24 15:35:09 -0700441 if (isDark) {
442 setTheme(R.style.LauncherThemeDark);
Mario Bertschlera6936942017-05-31 14:48:19 -0700443 } else if (supportsDarkText) {
444 setTheme(R.style.LauncherThemeDarkText);
Mario Bertschler27288382017-05-24 15:35:09 -0700445 }
446 }
447
448 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700449 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800450 return mLauncherView.findViewById(id);
451 }
452
453 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700454 public void onAppWidgetHostReset() {
455 if (mAppWidgetHost != null) {
456 mAppWidgetHost.startListening();
457 }
458 }
459
Adam Cohen9211d422014-10-07 18:14:53 -0700460 private LauncherCallbacks mLauncherCallbacks;
461
Winson1f064272016-07-18 17:18:02 -0700462 public void onInsetsChanged(Rect insets) {
463 mDeviceProfile.updateInsets(insets);
464 mDeviceProfile.layout(this, true /* notifyListeners */);
465 }
466
Sunny Goyal32554d12015-12-03 15:31:25 -0800467 /**
468 * Call this after onCreate to set or clear overlay.
469 */
470 public void setLauncherOverlay(LauncherOverlay overlay) {
471 if (overlay != null) {
472 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
473 }
474 mWorkspace.setLauncherOverlay(overlay);
475 }
476
Adam Cohen9211d422014-10-07 18:14:53 -0700477 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
478 mLauncherCallbacks = callbacks;
479 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700480 }
481
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700482 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700483 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700484 if (mLauncherCallbacks != null) {
485 mLauncherCallbacks.onLauncherProviderChange();
486 }
487 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700488
Hyunyoung Song3f471442015-04-08 19:01:34 -0700489 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700490 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
491 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700492 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700493 }
494
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000495 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700496 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
497 // This cast is safe as long as the id < 0x00FFFFFF
498 // Since we jail all the dynamically generated views, there should be no clashes
499 // with any other views.
500 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000501 }
502
Tony Wickham010d2552017-01-20 08:15:28 -0800503 public PopupDataProvider getPopupDataProvider() {
504 return mPopupDataProvider;
505 }
506
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000507 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700508 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
509 * a configuration step, this allows the proper animations to run after other transitions.
510 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700511 private long completeAdd(
512 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
513 long screenId = info.screenId;
514 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700515 // When the screen id represents an actual screen (as opposed to a rank) we make sure
516 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700517 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700518 }
Adam Cohendb364c32014-05-20 14:23:40 -0700519
Sunny Goyal2100c782016-08-22 16:00:03 -0700520 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800521 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700522 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700523 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800524 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700525 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700526 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700527 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700528 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700529 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700530 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700531 int widgetId = appWidgetId;
532 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700533 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700534 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700535 // Since the view was just bound, also launch the configure activity if needed
536 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
537 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800538 if (provider != null) {
539 new WidgetAddFlowHandler(provider)
540 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700541 }
542 }
543 break;
544 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800545 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700546
Adam Cohendb364c32014-05-20 14:23:40 -0700547 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800548 }
549
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800550 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700551 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700552 if (isWorkspaceLoading()) {
553 // process the result once the workspace has loaded.
554 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
555 return;
556 }
557 mPendingActivityResult = null;
558
Winson Chunge341d302013-08-16 14:31:00 -0700559 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700560 final PendingRequestArgs requestArgs = mPendingRequestArgs;
561 setWaitingForResult(null);
562 if (requestArgs == null) {
563 return;
564 }
565
566 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700567
Adam Cohenad4e15c2013-10-17 16:21:35 -0700568 Runnable exitSpringLoaded = new Runnable() {
569 @Override
570 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700571 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700572 }
573 };
574
Michael Jurka8b805b12012-04-18 14:23:14 -0700575 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700576 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700577 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700578 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
579 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700580 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700581 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700582 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700583 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700584 addAppWidgetImpl(
585 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800586 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700587 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700588 }
589 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200590 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700591 if (resultCode == RESULT_OK && isInState(OVERVIEW)) {
Tony Wickhame3054412015-09-09 09:05:25 -0700592 // User could have free-scrolled between pages before picking a wallpaper; make sure
593 // we move to the closest one now.
594 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700595 mStateManager.goToState(NORMAL, false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200596 }
597 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700598 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200599
Adam Cohened66b2b2012-01-23 17:28:51 -0800600 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700601 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700602
Adam Cohened66b2b2012-01-23 17:28:51 -0800603 // We have special handling for widgets
604 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800605 final int appWidgetId;
606 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
607 : -1;
608 if (widgetId < 0) {
609 appWidgetId = pendingAddWidgetId;
610 } else {
611 appWidgetId = widgetId;
612 }
613
Adam Cohenad4e15c2013-10-17 16:21:35 -0700614 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800615 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700616 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
617 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700618 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700619 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700620 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700621 @Override
622 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700623 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700624 }
625 };
Adam Cohendb364c32014-05-20 14:23:40 -0700626
Sunny Goyal2100c782016-08-22 16:00:03 -0700627 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
628 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
629 } else {
630 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
631 // When the screen id represents an actual screen (as opposed to a rank)
632 // we make sure that the drop page actually exists.
633 requestArgs.screenId =
634 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700635 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700636 final CellLayout dropLayout =
637 mWorkspace.getScreenWithId(requestArgs.screenId);
638
639 dropLayout.setDropPending(true);
640 final Runnable onComplete = new Runnable() {
641 @Override
642 public void run() {
643 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
644 dropLayout.setDropPending(false);
645 }
646 };
647 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
648 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700649 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800650 return;
651 }
652
Sunny Goyald478c832016-04-01 12:04:16 -0700653 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
654 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700655 if (resultCode == RESULT_OK) {
656 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700657 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700658 }
659 // Leave the widget in the pending state if the user canceled the configure.
660 return;
661 }
662
Sunny Goyalaad90582015-07-09 14:22:50 -0700663 if (requestCode == REQUEST_CREATE_SHORTCUT) {
664 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700665 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
666 completeAdd(requestCode, data, -1, requestArgs);
667 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
668 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
669
Sunny Goyalaad90582015-07-09 14:22:50 -0700670 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700671 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
672 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800673 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800674 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800675 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800676 }
677
678 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700679 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800680 final int requestCode, final int resultCode, final Intent data) {
681 handleActivityResult(requestCode, resultCode, data);
682 if (mLauncherCallbacks != null) {
683 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
684 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800685 }
686
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700687 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700688 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600689 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700690 PendingRequestArgs pendingArgs = mPendingRequestArgs;
691 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
692 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
693 setWaitingForResult(null);
694
Sunny Goyal28c6b962015-10-12 11:42:05 -0700695 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700696 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700697 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700698 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700699 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700700 Intent intent = pendingArgs.getPendingIntent();
701
Sunny Goyal28c6b962015-10-12 11:42:05 -0700702 if (grantResults.length > 0
703 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700704 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700705 } else {
706 // TODO: Show a snack bar with link to settings
707 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700708 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700709 }
710 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600711 if (mLauncherCallbacks != null) {
712 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
713 grantResults);
714 }
715 }
716
Adam Cohendb364c32014-05-20 14:23:40 -0700717 /**
718 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
719 *
720 * @param screenId the screen id to check
721 * @return the new screen, or screenId if it exists
722 */
723 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800724 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700725 if (dropLayout == null) {
726 // it's possible that the add screen was removed because it was
727 // empty and a re-bind occurred
728 mWorkspace.addExtraEmptyScreen();
729 return mWorkspace.commitExtraEmptyScreen();
730 } else {
731 return screenId;
732 }
733 }
734
Sunny Goyal2100c782016-08-22 16:00:03 -0700735 @Thunk void completeTwoStageWidgetDrop(
736 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
737 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800738 Runnable onCompleteRunnable = null;
739 int animationType = 0;
740
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700741 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800742 if (resultCode == RESULT_OK) {
743 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
744 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800745 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700746 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800747 onCompleteRunnable = new Runnable() {
748 @Override
749 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700750 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700751 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800752 }
753 };
754 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800755 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800756 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800757 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700758 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700759 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700760 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
761 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700762 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700763 // The animated view may be null in the case of a rotation during widget configuration
764 onCompleteRunnable.run();
765 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800766 }
767
768 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700769 protected void onStop() {
770 super.onStop();
771 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700772
773 if (mLauncherCallbacks != null) {
774 mLauncherCallbacks.onStop();
775 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800776 mAppWidgetHost.setListenIfResumed(false);
Tony Wickham010d2552017-01-20 08:15:28 -0800777
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700778 if (!mAppLaunchSuccess) {
779 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
Sunny Goyalea609262017-10-25 15:47:38 -0700780 mStateManager.getState().containerType);
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700781 }
Tony Wickham010d2552017-01-20 08:15:28 -0800782 NotificationListener.removeNotificationsChangedListener();
Michael Jurkacd496d72013-04-11 11:32:45 -0700783 }
784
785 @Override
786 protected void onStart() {
787 super.onStart();
788 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700789
790 if (mLauncherCallbacks != null) {
791 mLauncherCallbacks.onStart();
792 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800793 mAppWidgetHost.setListenIfResumed(true);
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());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001351 boolean internalStateHandled = InternalStateHandler
1352 .handleNewIntent(this, intent, alreadyOnHome);
1353
Winson15f8b172015-08-19 11:02:39 -07001354 if (isActionMain) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001355 if (!internalStateHandled) {
1356 // Note: There should be at most one log per method call. This is enforced
1357 // implicitly by using if-else statements.
1358 UserEventDispatcher ued = getUserEventDispatcher();
1359 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
1360 if (topOpenView != null) {
1361 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
1362 } else if (alreadyOnHome) {
1363 Target target = newContainerTarget(mStateManager.getState().containerType);
1364 target.pageIndex = mWorkspace.getCurrentPage();
1365 ued.logActionCommand(Action.Command.HOME_INTENT, target);
1366 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001367
Sunny Goyald3864fa2017-11-14 15:06:36 -08001368 // In all these cases, only animate if we're already on home
1369 AbstractFloatingView.closeAllOpenViews(this, alreadyOnHome);
Jon Mirandafeba90f2016-10-06 10:53:29 -07001370
Sunny Goyald3864fa2017-11-14 15:06:36 -08001371 mStateManager.goToState(NORMAL, alreadyOnHome /* animated */);
1372
1373 // Reset the apps view
1374 if (!alreadyOnHome && mAppsView != null) {
1375 mAppsView.reset();
1376 }
1377
1378 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()) {
1379 mWorkspace.post(mWorkspace::moveToDefaultScreen);
1380 }
1381 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001382
1383 final View v = getWindow().peekDecorView();
1384 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001385 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001386 }
1387
Adam Cohen9211d422014-10-07 18:14:53 -07001388 if (mLauncherCallbacks != null) {
1389 mLauncherCallbacks.onHomeIntent();
1390 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001391 }
Winson15f8b172015-08-19 11:02:39 -07001392
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001393 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001394 }
1395
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001396 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001397 public void onRestoreInstanceState(Bundle state) {
1398 super.onRestoreInstanceState(state);
1399 for (int page: mSynchronouslyBoundPages) {
1400 mWorkspace.restoreInstanceStateForChild(page);
1401 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001402 }
1403
1404 @Override
1405 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001406 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001407 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001408
Adam Cohen21cd0022013-10-09 18:57:02 -07001409 }
Sunny Goyalea609262017-10-25 15:47:38 -07001410 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001411
1412
1413 AbstractFloatingView widgets = AbstractFloatingView
1414 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1415 if (widgets != null) {
1416 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1417 widgets.saveHierarchyState(widgetsState);
1418 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1419 } else {
1420 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1421 }
1422
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001423 // We close any open folders and shortcut containers since they will not be re-opened,
1424 // and we need to make sure this state is reflected.
1425 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001426
Sunny Goyal2100c782016-08-22 16:00:03 -07001427 if (mPendingRequestArgs != null) {
1428 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1429 }
1430 if (mPendingActivityResult != null) {
1431 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001432 }
1433
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001434 super.onSaveInstanceState(outState);
1435
Adam Cohen9211d422014-10-07 18:14:53 -07001436 if (mLauncherCallbacks != null) {
1437 mLauncherCallbacks.onSaveInstanceState(outState);
1438 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001439 }
1440
1441 @Override
1442 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001443 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001444
Sunny Goyal5cc7bbd2017-06-12 09:50:39 -07001445 unregisterReceiver(mReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001446 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001447
Winson Chungcd2b0142011-06-08 16:02:26 -07001448 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001449 // It's possible to receive onDestroy after a new Launcher activity has
1450 // been created. In this case, don't interfere with the new Launcher.
1451 if (mModel.isCurrentCallbacks(this)) {
1452 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001453 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001454 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001455
Sunny Goyal745bad92016-05-02 10:54:12 -07001456 if (mRotationPrefChangeHandler != null) {
1457 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1458 }
1459
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001460 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001461 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001462 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001463 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001464 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001465 mAppWidgetHost = null;
1466
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001467 TextKeyListener.getInstance().release();
Mario Bertschler27288382017-05-24 15:35:09 -07001468 WallpaperColorInfo.getInstance(this).setOnThemeChangeListener(null);
1469
Michael Jurka2ecf9952012-06-18 12:52:28 -07001470 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001471
Tony2917a8b2017-07-31 23:29:42 -07001472 clearPendingBinds();
1473
Adam Cohen9211d422014-10-07 18:14:53 -07001474 if (mLauncherCallbacks != null) {
1475 mLauncherCallbacks.onDestroy();
1476 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001477 }
1478
Sunny Goyalae502842016-06-17 08:43:56 -07001479 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1480 return mAccessibilityDelegate;
1481 }
1482
Adam Cohena9cf38f2011-05-02 15:36:58 -07001483 public DragController getDragController() {
1484 return mDragController;
1485 }
1486
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001487 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001488 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001489 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001490 }
1491
1492 @Override
1493 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1494 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001495 try {
1496 super.startIntentSenderForResult(intent, requestCode,
1497 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1498 } catch (IntentSender.SendIntentException e) {
1499 throw new ActivityNotFoundException();
1500 }
1501 }
1502
Winson Chung70d72102011-08-12 11:24:35 -07001503 /**
1504 * Indicates that we want global search for this activity by setting the globalSearch
1505 * argument for {@link #startSearch} to true.
1506 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001507 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001508 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001509 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001510
Karl Rosaen138a0412009-04-23 19:00:21 -07001511 if (initialQuery == null) {
1512 // Use any text typed in the launcher as the initial query
1513 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001514 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001515 if (appSearchData == null) {
1516 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001517 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001518 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001519
Sunny Goyal6f28e712016-09-13 14:19:29 -07001520 if (mLauncherCallbacks == null ||
1521 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1522 // Starting search from the callbacks failed. Start the default global search.
1523 startGlobalSearch(initialQuery, selectInitialQuery, appSearchData, null);
Ian Parkinson047036e2014-09-01 15:40:53 +01001524 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001525
1526 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001527 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001528 }
1529
Ian Parkinson047036e2014-09-01 15:40:53 +01001530 /**
Michael Jurkaa33411c2012-06-14 16:18:21 -07001531 * Starts the global search activity. This code is a copied from SearchManager
1532 */
Sunny Goyal6f28e712016-09-13 14:19:29 -07001533 public void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001534 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001535 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001536 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1537 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1538 if (globalSearchActivity == null) {
1539 Log.w(TAG, "No global search activity found.");
1540 return;
1541 }
1542 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1543 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1544 intent.setComponent(globalSearchActivity);
1545 // Make sure that we have a Bundle to put source in
1546 if (appSearchData == null) {
1547 appSearchData = new Bundle();
1548 } else {
1549 appSearchData = new Bundle(appSearchData);
1550 }
Adam Cohen9211d422014-10-07 18:14:53 -07001551 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07001552 if (!appSearchData.containsKey("source")) {
1553 appSearchData.putString("source", getPackageName());
1554 }
1555 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1556 if (!TextUtils.isEmpty(initialQuery)) {
1557 intent.putExtra(SearchManager.QUERY, initialQuery);
1558 }
1559 if (selectInitialQuery) {
1560 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1561 }
1562 intent.setSourceBounds(sourceBounds);
1563 try {
1564 startActivity(intent);
1565 } catch (ActivityNotFoundException ex) {
1566 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1567 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001568 }
1569
Winson Chung70d72102011-08-12 11:24:35 -07001570 @Override
Winson Chung70d72102011-08-12 11:24:35 -07001571 public boolean onPrepareOptionsMenu(Menu menu) {
1572 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07001573 if (mLauncherCallbacks != null) {
1574 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
1575 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02001576 return false;
Winson Chung70d72102011-08-12 11:24:35 -07001577 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001578
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001579 @Override
1580 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001581 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001582 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001583 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001584 }
1585
Joe Onorato9c1289c2009-08-17 11:03:03 -04001586 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001587 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001588 }
1589
Adam Cohen517a7f52014-03-01 12:12:59 -08001590 public boolean isWorkspaceLoading() {
1591 return mWorkspaceLoading;
1592 }
1593
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001594 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001595 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001596 }
1597
Sunny Goyal04a324a2017-01-20 21:08:59 -08001598 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001599 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001600 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001601
Sunny Goyal2100c782016-08-22 16:00:03 -07001602 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001603 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001604 if (LOGD) {
1605 Log.d(TAG, "Adding widget from drop");
1606 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001607 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001608 }
1609
Sunny Goyal2100c782016-08-22 16:00:03 -07001610 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001611 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1612 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1613 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001614
Adam Cohenad4e15c2013-10-17 16:21:35 -07001615 Runnable onComplete = new Runnable() {
1616 @Override
1617 public void run() {
1618 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001619 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001620 }
1621 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001622 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001623 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001624 }
1625 }
Romain Guycbb89e42009-06-08 15:52:54 -07001626
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001627 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1628 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001629 info.container = container;
1630 info.screenId = screenId;
1631 if (cell != null) {
1632 info.cellX = cell[0];
1633 info.cellY = cell[1];
1634 }
1635 info.spanX = spanX;
1636 info.spanY = spanY;
1637
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001638 switch (info.itemType) {
1639 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1640 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001641 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001642 break;
1643 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001644 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001645 break;
1646 default:
1647 throw new IllegalStateException("Unknown item type: " + info.itemType);
1648 }
1649 }
1650
Adam Cohenfbba09b2011-07-18 21:43:05 -07001651 /**
1652 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001653 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001654 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001655 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1656 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001657 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1658 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1659 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001660 }
1661
Adam Cohenfbba09b2011-07-18 21:43:05 -07001662 /**
1663 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001664 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001665 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001666 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001667 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001668 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001669 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001670 // In the case where we've prebound the widget, we remove it from the DragLayer
1671 if (LOGD) {
1672 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1673 }
1674 getDragLayer().removeView(hostView);
1675
Adam Cohened66b2b2012-01-23 17:28:51 -08001676 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001677 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001678
1679 // Clear the boundWidget so that it doesn't get destroyed.
1680 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001681 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001682 // In this case, we either need to start an activity to get permission to bind
1683 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001684 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1685 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1686 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1687 this, info.componentName);
1688 } else {
1689 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1690 }
Adam Cohendd70d662012-10-04 16:53:44 -07001691 Bundle options = info.bindOptions;
1692
Sunny Goyalffe83f12014-08-14 17:39:34 -07001693 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1694 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001695 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001696 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001697 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001698 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001699 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001700 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001701 }
1702
Adam Cohendcd297f2013-06-18 13:13:40 -07001703 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001704 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001705 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001706 folderInfo.title = getText(R.string.folder_name);
1707
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001708 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001709 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001710
1711 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001712 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301713 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001714 // Force measure the new folder icon
1715 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1716 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001717 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001718 }
Romain Guycbb89e42009-06-08 15:52:54 -07001719
Winsonc0b52fe2015-09-09 16:38:15 -07001720 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001721 * Unbinds the view for the specified item, and removes the item and all its children.
1722 *
1723 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001724 * @param itemInfo the {@link ItemInfo} for this view.
1725 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001726 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001727 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001728 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001729 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001730 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1731 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001732 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001733 } else {
1734 mWorkspace.removeWorkspaceItem(v);
1735 }
Winsonc0b52fe2015-09-09 16:38:15 -07001736 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001737 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001738 }
1739 } else if (itemInfo instanceof FolderInfo) {
1740 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001741 if (v instanceof FolderIcon) {
1742 ((FolderIcon) v).removeListeners();
1743 }
Winsonc0b52fe2015-09-09 16:38:15 -07001744 mWorkspace.removeWorkspaceItem(v);
1745 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001746 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001747 }
1748 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1749 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001750 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001751 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07001752 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001753 }
1754 } else {
1755 return false;
1756 }
1757 return true;
1758 }
1759
1760 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07001761 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07001762 */
Sunny Goyal56c73602015-09-25 12:55:01 -07001763 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07001764 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07001765 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07001766 // Deleting an app widget ID is a void call but writes to disk before returning
1767 // to the caller...
1768 new AsyncTask<Void, Void, Void>() {
1769 public Void doInBackground(Void ... args) {
1770 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
1771 return null;
1772 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07001773 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07001774 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001775 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001776 }
1777
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001778 @Override
1779 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001780 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001781 }
1782
Joe Onorato88ec0992009-11-19 13:16:06 -08001783 @Override
1784 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07001785 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1786 return;
1787 }
1788
Sunny Goyal45478022015-06-08 16:52:41 -07001789 if (mDragController.isDragging()) {
1790 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001791 return;
1792 }
1793
Jon Mirandafeba90f2016-10-06 10:53:29 -07001794 // Note: There should be at most one log per method call. This is enforced implicitly
1795 // by using if-else statements.
1796 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001797 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
1798 if (topView != null) {
Sunny Goyal37920962017-09-28 13:43:24 -07001799 topView.onBackPressed();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001800 } else if (!isInState(NORMAL)) {
Sunny Goyalea609262017-10-25 15:47:38 -07001801 ued.logActionCommand(Action.Command.BACK, mStateManager.getState().containerType);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001802 mStateManager.goToState(NORMAL);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001803 } else {
1804 // Back button is a no-op here, but give at least some feedback for the button press
1805 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001806 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001807 }
1808
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001809 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001810 * Launches the intent referred by the clicked shortcut.
1811 *
1812 * @param v The view representing the clicked shortcut.
1813 */
1814 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001815 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1816 // view has detached (it's possible for this to happen if the view is removed mid touch).
1817 if (v.getWindowToken() == null) {
1818 return;
1819 }
1820
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001821 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001822 return;
1823 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001824
Adam Cohen1697b792013-09-17 19:08:21 -07001825 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001826 if (isInState(OVERVIEW)) {
Jon Miranda2e61fba2017-02-24 09:12:59 -08001827 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1828 LauncherLogProto.Action.Direction.NONE,
1829 LauncherLogProto.ContainerType.OVERVIEW, mWorkspace.getCurrentPage());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001830 mStateManager.goToState(NORMAL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001831 }
1832 return;
1833 }
1834
1835 if (v instanceof CellLayout) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001836 if (isInState(OVERVIEW)) {
Jon Mirandac6cf4932017-02-07 17:12:36 -08001837 int page = mWorkspace.indexOfChild(v);
1838 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1839 LauncherLogProto.Action.Direction.NONE,
1840 LauncherLogProto.ContainerType.OVERVIEW, page);
1841 mWorkspace.snapToPageFromOverView(page);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001842 mStateManager.goToState(NORMAL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001843 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07001844 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001845 }
1846
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001847 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001848 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001849 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001850 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001851 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001852 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001853 }
Hyunyoung Songd725f642017-08-17 22:26:35 -07001854 } else if ((v instanceof PageIndicator) ||
Adam Cohenbf78f3c2017-07-14 15:58:48 -07001855 (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001856 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07001857 } else if (tag instanceof AppInfo) {
1858 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07001859 } else if (tag instanceof LauncherAppWidgetInfo) {
1860 if (v instanceof PendingAppWidgetHostView) {
1861 onClickPendingWidget((PendingAppWidgetHostView) v);
1862 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001863 }
1864 }
1865
Sunny Goyal70660032015-05-14 00:07:08 -07001866 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07001867 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07001868 return false;
1869 }
1870
Michael Jurkaaf442092010-06-10 17:01:57 -07001871 /**
Sunny Goyalff572272014-07-23 13:58:07 -07001872 * Event handler for the app widget view which has not fully restored.
1873 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001874 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07001875 if (mIsSafeModeEnabled) {
1876 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
1877 return;
1878 }
1879
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001880 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07001881 if (v.isReadyForClickSetup()) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001882 LauncherAppWidgetProviderInfo appWidgetInfo =
1883 mAppWidgetManager.findProvider(info.providerName, info.user);
1884 if (appWidgetInfo == null) {
1885 return;
1886 }
1887 WidgetAddFlowHandler addFlowHandler = new WidgetAddFlowHandler(appWidgetInfo);
1888
Sunny Goyald478c832016-04-01 12:04:16 -07001889 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
1890 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
1891 // This should not happen, as we make sure that an Id is allocated during bind.
1892 return;
1893 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001894 addFlowHandler.startBindFlow(this, info.appWidgetId, info,
1895 REQUEST_BIND_PENDING_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -07001896 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001897 addFlowHandler.startConfigActivity(this, info, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07001898 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001899 } else {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001900 final String packageName = info.providerName.getPackageName();
Sunny Goyale6e72002017-01-12 16:55:36 -08001901 onClickPendingAppItem(v, packageName, info.installProgress >= 0);
Sunny Goyalff572272014-07-23 13:58:07 -07001902 }
1903 }
1904
1905 /**
Adam Cohenbf78f3c2017-07-14 15:58:48 -07001906 * Event handler for the "grid" button or "caret" that appears on the home screen, which
1907 * enters all apps mode. In verticalBarLayout the caret can be seen when all apps is open, and
1908 * so in that case reverses the action.
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001909 *
1910 * @param v The view that was clicked.
1911 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001912 protected void onClickAllAppsButton(View v) {
1913 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001914 if (!isInState(ALL_APPS)) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301915 getUserEventDispatcher().logActionOnControl(Action.Touch.TAP,
1916 ControlType.ALL_APPS_BUTTON);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001917 mStateManager.goToState(ALL_APPS);
Adam Cohen5441a9d2017-07-14 14:22:05 -07001918 } else {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001919 mStateManager.goToState(NORMAL);
Adam Cohen5441a9d2017-07-14 14:22:05 -07001920 }
1921 }
1922
Sunny Goyale6e72002017-01-12 16:55:36 -08001923 private void onClickPendingAppItem(final View v, final String packageName,
1924 boolean downloadStarted) {
1925 if (downloadStarted) {
1926 // If the download has started, simply direct to the market app.
1927 startMarketIntentForPackage(v, packageName);
1928 return;
1929 }
Sunny Goyale03b8122014-10-08 09:55:24 -07001930 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001931 .setTitle(R.string.abandoned_promises_title)
1932 .setMessage(R.string.abandoned_promise_explanation)
Sunny Goyale6e72002017-01-12 16:55:36 -08001933 .setPositiveButton(R.string.abandoned_search, new DialogInterface.OnClickListener() {
1934 @Override
1935 public void onClick(DialogInterface dialogInterface, int i) {
1936 startMarketIntentForPackage(v, packageName);
1937 }
1938 })
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001939 .setNeutralButton(R.string.abandoned_clean_this,
1940 new DialogInterface.OnClickListener() {
1941 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001942 final UserHandle user = Process.myUserHandle();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001943 mWorkspace.removeAbandonedPromise(packageName, user);
1944 }
1945 })
1946 .create().show();
Sunny Goyale6e72002017-01-12 16:55:36 -08001947 }
1948
1949 private void startMarketIntentForPackage(View v, String packageName) {
1950 ItemInfo item = (ItemInfo) v.getTag();
1951 Intent intent = PackageManagerHelper.getMarketIntent(packageName);
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001952 startActivitySafely(v, intent, item);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001953 }
1954
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001955 /**
1956 * Event handler for an app shortcut click.
1957 *
1958 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
1959 */
Chris Wren40c5ed32014-06-24 18:24:23 -04001960 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001961 if (LOGD) Log.d(TAG, "onClickAppShortcut");
1962 Object tag = v.getTag();
1963 if (!(tag instanceof ShortcutInfo)) {
1964 throw new IllegalArgumentException("Input must be a Shortcut");
1965 }
1966
1967 // Open shortcut
1968 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001969
Sunny Goyal076839c2017-10-30 13:52:20 -07001970 if (shortcut.isDisabled()) {
1971 if ((shortcut.runtimeStatusFlags &
1972 ~FLAG_DISABLED_SUSPENDED &
1973 ~FLAG_DISABLED_QUIET_USER) == 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001974 // If the app is only disabled because of the above flags, launch activity anyway.
1975 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00001976 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07001977 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
1978 // Use a message specific to this shortcut, if it has one.
1979 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
1980 return;
1981 }
1982 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00001983 int error = R.string.activity_not_available;
Sunny Goyal076839c2017-10-30 13:52:20 -07001984 if ((shortcut.runtimeStatusFlags & FLAG_DISABLED_SAFEMODE) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00001985 error = R.string.safemode_shortcut_error;
Sunny Goyal076839c2017-10-30 13:52:20 -07001986 } else if ((shortcut.runtimeStatusFlags & FLAG_DISABLED_BY_PUBLISHER) != 0 ||
1987 (shortcut.runtimeStatusFlags & FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07001988 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00001989 }
1990 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
1991 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001992 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001993 }
1994
Chris Wren40c5ed32014-06-24 18:24:23 -04001995 // Check for abandoned promise
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -07001996 if ((v instanceof BubbleTextView) && shortcut.hasPromiseIconUi()) {
Sunny Goyale6e72002017-01-12 16:55:36 -08001997 String packageName = shortcut.intent.getComponent() != null ?
1998 shortcut.intent.getComponent().getPackageName() : shortcut.intent.getPackage();
1999 if (!TextUtils.isEmpty(packageName)) {
2000 onClickPendingAppItem(v, packageName,
2001 shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE));
2002 return;
2003 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002004 }
2005
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002006 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002007 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002008 }
2009
Sunny Goyala7ce1662016-05-31 15:01:35 -07002010 private void startAppShortcutOrInfoActivity(View v) {
2011 ItemInfo item = (ItemInfo) v.getTag();
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002012 Intent intent;
2013 if (item instanceof PromiseAppInfo) {
2014 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) item;
2015 intent = promiseAppInfo.getMarketIntent();
2016 } else {
2017 intent = item.getIntent();
2018 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002019 if (intent == null) {
2020 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002021 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002022 startActivitySafely(v, intent, item);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002023 }
2024
2025 /**
2026 * Event handler for a folder icon click.
2027 *
2028 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2029 */
2030 protected void onClickFolderIcon(View v) {
2031 if (LOGD) Log.d(TAG, "onClickFolder");
2032 if (!(v instanceof FolderIcon)){
2033 throw new IllegalArgumentException("Input must be a FolderIcon");
2034 }
2035
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002036 Folder folder = ((FolderIcon) v).getFolder();
2037 if (!folder.isOpen() && !folder.isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002038 // Open the requested folder
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002039 folder.animateOpen();
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002040 }
Michael Jurka5130e402011-10-13 04:55:35 -07002041 }
2042
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002043 /**
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002044 * Event handler for the wallpaper picker button that appears after a long press
2045 * on the home screen.
2046 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002047 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07002048 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002049 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2050 return;
2051 }
2052
Tony Wickham785f7a52015-08-31 17:28:32 -07002053 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2054 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal2100c782016-08-22 16:00:03 -07002055 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002056 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002057 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002058
2059 String pickerPackage = getString(R.string.wallpaper_picker_package);
Tony85efb4b2017-06-02 13:06:10 -07002060 boolean hasTargetPackage = !TextUtils.isEmpty(pickerPackage);
2061 if (hasTargetPackage) {
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002062 intent.setPackage(pickerPackage);
2063 }
2064
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002065 intent.setSourceBounds(getViewBounds(v));
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002066 try {
2067 startActivityForResult(intent, REQUEST_PICK_WALLPAPER,
2068 // If there is no target package, use the default intent chooser animation
2069 hasTargetPackage ? getActivityLaunchOptions(v) : null);
2070 } catch (ActivityNotFoundException e) {
2071 setWaitingForResult(null);
2072 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2073 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002074 }
2075
Sunny Goyala7ce1662016-05-31 15:01:35 -07002076 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002077 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002078 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2079 try {
2080 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2081 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2082 // is enabled by default on NYC.
2083 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2084 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002085
2086 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2087 String id = ((ShortcutInfo) info).getDeepShortcutId();
2088 String packageName = intent.getPackage();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05302089 DeepShortcutManager.getInstance(this).startShortcut(
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002090 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002091 } else {
2092 // Could be launching some bookkeeping activity
2093 startActivity(intent, optsBundle);
2094 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002095 } finally {
2096 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002097 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002098 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002099 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2100 // corresponding permission. Show the appropriate permission prompt if that
2101 // is the case.
2102 if (intent.getComponent() == null
2103 && Intent.ACTION_CALL.equals(intent.getAction())
2104 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2105 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002106
2107 setWaitingForResult(PendingRequestArgs
2108 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002109 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2110 REQUEST_PERMISSION_CALL_PHONE);
2111 } else {
2112 // No idea why this was thrown.
2113 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002114 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002115 }
2116 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002117
Sunny Goyalfe770c92016-09-27 14:38:58 -07002118 @TargetApi(Build.VERSION_CODES.M)
Tonye3c59252017-05-02 21:32:27 -07002119 public Bundle getActivityLaunchOptions(View v) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002120 if (Utilities.ATLEAST_MARSHMALLOW) {
2121 int left = 0, top = 0;
2122 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07002123 if (v instanceof BubbleTextView) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002124 // Launch from center of icon, not entire view
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07002125 Drawable icon = ((BubbleTextView) v).getIcon();
Sunny Goyala7ce1662016-05-31 15:01:35 -07002126 if (icon != null) {
2127 Rect bounds = icon.getBounds();
2128 left = (width - bounds.width()) / 2;
2129 top = v.getPaddingTop();
2130 width = bounds.width();
2131 height = bounds.height();
2132 }
2133 }
2134 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2135 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2136 // On L devices, we use the device default slide-up transition.
2137 // On L MR1 devices, we use a custom version of the slide-up transition which
2138 // doesn't have the delay present in the device default.
2139 return ActivityOptions.makeCustomAnimation(
2140 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2141 }
2142 return null;
2143 }
2144
Tonye3c59252017-05-02 21:32:27 -07002145 public Rect getViewBounds(View v) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002146 int[] pos = new int[2];
2147 v.getLocationOnScreen(pos);
2148 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2149 }
2150
Sunny Goyala7ce1662016-05-31 15:01:35 -07002151 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002152 mAppLaunchSuccess = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002153 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2154 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002155 return mAppLaunchSuccess;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002156 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002157 // Only launch using the new animation if the shortcut has not opted out (this is a
2158 // private contract between launcher and may be ignored in the future).
2159 boolean useLaunchAnimation = (v != null) &&
2160 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2161 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2162
Sunny Goyal24bb66a2017-03-21 15:12:01 -07002163 UserHandle user = item == null ? null : item.user;
Sunny Goyala7ce1662016-05-31 15:01:35 -07002164
2165 // Prepare intent
2166 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2167 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002168 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002169 }
2170 try {
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002171 if (Utilities.ATLEAST_MARSHMALLOW
2172 && (item instanceof ShortcutInfo)
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002173 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002174 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyale6e72002017-01-12 16:55:36 -08002175 && !((ShortcutInfo) item).isPromise()) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002176 // Shortcuts need some special checks due to legacy reasons.
2177 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002178 } else if (user == null || user.equals(Process.myUserHandle())) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002179 // Could be launching some bookkeeping activity
2180 startActivity(intent, optsBundle);
2181 } else {
2182 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2183 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2184 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002185
2186 if (v instanceof BubbleTextView) {
2187 // This is set to the view that launched the activity that navigated the user away
2188 // from launcher. Since there is no callback for when the activity has finished
2189 // launching, enable the press state and keep this reference to reset the press
2190 // state when we return to launcher.
2191 BubbleTextView btv = (BubbleTextView) v;
2192 btv.setStayPressed(true);
2193 setOnResumeCallback(btv);
2194 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002195 mAppLaunchSuccess = true;
2196 getUserEventDispatcher().logAppLaunch(v, intent); // TODO for discovered apps b/35802115
Sunny Goyala7ce1662016-05-31 15:01:35 -07002197 } catch (ActivityNotFoundException|SecurityException e) {
2198 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2199 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2200 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002201 return mAppLaunchSuccess;
Michael Jurka86a720e2012-05-09 11:23:23 -07002202 }
2203
Tony Wickhamdadb3042016-02-24 11:07:00 -08002204 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07002205 public boolean dispatchTouchEvent(MotionEvent ev) {
2206 mLastDispatchTouchEventX = ev.getX();
2207 return super.dispatchTouchEvent(ev);
2208 }
2209
2210 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002211 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002212 if (!isDraggingEnabled()) return false;
2213 if (isWorkspaceLocked()) return false;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002214 if (!isInState(NORMAL) && !isInState(OVERVIEW)) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002215
Jon Miranda51f037d2016-11-07 08:37:20 -08002216 boolean ignoreLongPressToOverview =
2217 mDeviceProfile.shouldIgnoreLongPressToOverview(mLastDispatchTouchEventX);
Jon Miranda379198e2016-09-23 08:54:40 -07002218
Adam Cohen1697b792013-09-17 19:08:21 -07002219 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002220 if (!isInState(OVERVIEW)) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002221 if (!mWorkspace.isTouchActive() && !ignoreLongPressToOverview) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302222 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2223 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002224 mWorkspace.getCurrentPage());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002225 getStateManager().goToState(OVERVIEW);
Adam Cohen93c97562013-09-26 13:48:01 -07002226 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2227 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2228 return true;
2229 } else {
2230 return false;
2231 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002232 } else {
2233 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002234 }
Adam Cohen1697b792013-09-17 19:08:21 -07002235 }
2236
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002237 CellLayout.CellInfo longClickCellInfo = null;
2238 View itemUnderLongClick = null;
2239 if (v.getTag() instanceof ItemInfo) {
2240 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002241 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002242 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07002243 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002244 }
2245
Winson Chung3d503fb2011-07-13 17:25:49 -07002246 // The hotseat touch handling does not go through Workspace, and we always allow long press
2247 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002248 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002249 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002250 // User long pressed on empty space
Sunny Goyalc99cb172017-10-19 16:15:09 -07002251 if (mWorkspace.isPageRearrangeEnabled()) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002252 mWorkspace.startReordering(v);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302253 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2254 Action.Direction.NONE, ContainerType.OVERVIEW);
Adam Cohend3ecce92013-09-16 14:58:00 -07002255 } else {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002256 if (ignoreLongPressToOverview) {
Jon Miranda379198e2016-09-23 08:54:40 -07002257 return false;
2258 }
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302259 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2260 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002261 mWorkspace.getCurrentPage());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002262 getStateManager().goToState(OVERVIEW);
Adam Cohendedbd962013-07-11 14:21:49 -07002263 }
Jon Miranda379198e2016-09-23 08:54:40 -07002264 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2265 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002266 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07002267 final boolean isAllAppsButton =
2268 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
2269 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
2270 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05002271 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002272 // User long pressed on an item
Tony Wickham10236d62016-09-28 12:49:25 -07002273 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002274 }
2275 }
2276 }
2277 return true;
2278 }
2279
Winson Chung3d503fb2011-07-13 17:25:49 -07002280 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08002281 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07002282 return mHotseat != null && layout != null &&
2283 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2284 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002285
Winson Chung3d503fb2011-07-13 17:25:49 -07002286 /**
2287 * Returns the CellLayout of the specified container at the specified screen.
2288 */
Sunny Goyal92820592015-03-02 11:31:35 -08002289 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002290 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2291 if (mHotseat != null) {
2292 return mHotseat.getLayout();
2293 } else {
2294 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002295 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002296 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002297 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002298 }
2299 }
2300
Michael Jurkae326f182011-11-21 14:05:46 -08002301 @Override
2302 public void onTrimMemory(int level) {
2303 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07002304 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2305 // The widget preview db can result in holding onto over
2306 // 3MB of memory for caching which isn't necessary.
2307 SQLiteDatabase.releaseMemory();
2308
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08002309 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07002310 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08002311 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01002312 if (mLauncherCallbacks != null) {
2313 mLauncherCallbacks.onTrimMemory(level);
2314 }
Michael Jurkae326f182011-11-21 14:05:46 -08002315 }
2316
Michael Jurkad7c28052012-04-27 15:43:36 -07002317 @Override
2318 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07002319 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07002320 final List<CharSequence> text = event.getText();
2321 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07002322 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002323 // TODO: When can workspace be null?
2324 text.add(mWorkspace == null
2325 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07002326 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07002327 return result;
2328 }
2329
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002330 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07002331 * If the activity is currently paused, signal that we need to run the passed Runnable
2332 * in onResume.
2333 *
2334 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07002335 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
2336 * wrong when we're not running, and if the activity comes back to what the configuration was
2337 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07002338 *
2339 * Implementation of the method from LauncherModel.Callbacks.
2340 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07002341 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07002342 */
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002343 @Thunk boolean waitUntilResume(Runnable run) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002344 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002345 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002346 if (run instanceof RunnableWithId) {
2347 // Remove any runnables which have the same id
2348 while (mBindOnResumeCallbacks.remove(run)) { }
Michael Jurkac402cd92013-05-20 15:49:32 +02002349 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07002350 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07002351 return true;
2352 } else {
2353 return false;
2354 }
2355 }
2356
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002357 public void setOnResumeCallback(OnResumeCallback callback) {
2358 if (mOnResumeCallback != null) {
2359 mOnResumeCallback.onLauncherResume();
2360 }
2361 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07002362 }
2363
Michael Jurka7607c2f2013-04-03 14:33:19 -07002364 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002365 * If the activity is currently paused, signal that we need to re-run the loader
2366 * in onResume.
2367 *
2368 * This needs to be called from incoming places where resources might have been loaded
2369 * while we are paused. That is becaues the Configuration might be wrong
2370 * when we're not running, and if it comes back to what it was when we
2371 * were paused, we are not restarted.
2372 *
2373 * Implementation of the method from LauncherModel.Callbacks.
2374 *
2375 * @return true if we are currently paused. The caller might be able to
2376 * skip some work in that case since we will come back again.
2377 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002378 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002379 public boolean setLoadOnResume() {
2380 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002381 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002382 mOnResumeNeedsLoad = true;
2383 return true;
2384 } else {
2385 return false;
2386 }
2387 }
2388
2389 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002390 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002391 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002392 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002393 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002394 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002395 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002396 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07002397 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07002398 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002399 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002400
Joe Onorato9c1289c2009-08-17 11:03:03 -04002401 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07002402 * Clear any pending bind callbacks. This is called when is loader is planning to
2403 * perform a full rebind from scratch.
2404 */
2405 @Override
2406 public void clearPendingBinds() {
2407 mBindOnResumeCallbacks.clear();
2408 if (mPendingExecutor != null) {
2409 mPendingExecutor.markCompleted();
2410 mPendingExecutor = null;
2411 }
2412 }
2413
2414 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002415 * Refreshes the shortcuts shown on the workspace.
2416 *
2417 * Implementation of the method from LauncherModel.Callbacks.
2418 */
2419 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002420 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002421 // Floating panels (except the full widget sheet) are associated with individual icons. If
2422 // we are starting a fresh bind, close all such panels as all the icons are about
2423 // to go away.
2424 AbstractFloatingView.closeOpenViews(this, true,
2425 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08002426
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002427 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08002428
Winson Chungd64d1762013-08-20 14:37:16 -07002429 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07002430 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07002431 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07002432
Winson Chung3d503fb2011-07-13 17:25:49 -07002433 if (mHotseat != null) {
2434 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002435 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002436 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002437 }
2438
Adam Cohendcd297f2013-06-18 13:13:40 -07002439 @Override
2440 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002441 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002442 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
2443 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002444 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
2445 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002446 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002447 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
2448 // If there are no screens, we need to have an empty screen
2449 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07002450 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002451 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07002452
Winsonc7d2e832016-07-28 12:24:55 -07002453 // After we have added all the screens, if the wallpaper was locked to the default state,
2454 // then notify to indicate that it can be released and a proper wallpaper offset can be
2455 // computed before the next layout
2456 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07002457 }
2458
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002459 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002460 int count = orderedScreenIds.size();
2461 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002462 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002463 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002464 // No need to bind the first screen, as its always bound.
2465 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
2466 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002467 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002468 }
2469
Sunny Goyalb23980c2017-08-17 07:45:25 -07002470 @Override
Winson Chungd64d1762013-08-20 14:37:16 -07002471 public void bindAppsAdded(final ArrayList<Long> newScreens,
2472 final ArrayList<ItemInfo> addNotAnimated,
Sunny Goyalb23980c2017-08-17 07:45:25 -07002473 final ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07002474 Runnable r = new Runnable() {
2475 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002476 bindAppsAdded(newScreens, addNotAnimated, addAnimated);
Winson Chungd64d1762013-08-20 14:37:16 -07002477 }
2478 };
2479 if (waitUntilResume(r)) {
2480 return;
2481 }
2482
Winson Chungd64d1762013-08-20 14:37:16 -07002483 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08002484 if (newScreens != null) {
2485 bindAddScreens(newScreens);
2486 }
Winson Chungd64d1762013-08-20 14:37:16 -07002487
2488 // We add the items without animation on non-visible pages, and with
2489 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08002490 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002491 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002492 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002493 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002494 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07002495 }
Winson Chungc58497e2013-09-03 17:48:37 -07002496
Winson Chung87412982013-10-03 18:34:14 -07002497 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07002498 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002499 }
2500
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002501 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002502 * Bind the items start-end from the list.
2503 *
2504 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002505 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07002506 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07002507 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07002508 Runnable r = new Runnable() {
2509 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002510 bindItems(items, forceAnimateIcons);
Winson Chungd64d1762013-08-20 14:37:16 -07002511 }
2512 };
2513 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002514 return;
2515 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002516
Sunny Goyal3be633b2016-12-08 09:59:25 -08002517 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07002518 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002519 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07002520 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07002521 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002522 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07002523 int end = items.size();
2524 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002525 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002526
2527 // Short circuit if we are loading dock items for a configuration which has no dock
2528 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2529 mHotseat == null) {
2530 continue;
2531 }
2532
Sunny Goyal639e9062015-08-19 19:17:06 -07002533 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002534 switch (item.itemType) {
2535 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2536 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08002537 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07002538 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07002539 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002540 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002541 }
2542 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07002543 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002544 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002545 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002546 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002547 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07002548 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2549 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07002550 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002551 if (view == null) {
2552 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002553 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08002554 break;
2555 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002556 default:
2557 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002558 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002559
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002560 /*
2561 * Remove colliding items.
2562 */
2563 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
2564 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
2565 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
2566 View v = cl.getChildAt(item.cellX, item.cellY);
2567 Object tag = v.getTag();
2568 String desc = "Collision while binding workspace item: " + item
2569 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08002570 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002571 throw (new RuntimeException(desc));
2572 } else {
2573 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002574 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002575 continue;
2576 }
2577 }
2578 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302579 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07002580 if (animateIcons) {
2581 // Animate all the applications up now
2582 view.setAlpha(0f);
2583 view.setScaleX(0f);
2584 view.setScaleY(0f);
2585 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08002586 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07002587 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002588 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07002589
Winson Chung997a9232013-07-24 15:33:46 -07002590 if (animateIcons) {
2591 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08002592 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07002593 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08002594 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07002595 final Runnable startBounceAnimRunnable = new Runnable() {
2596 public void run() {
2597 anim.playTogether(bounceAnims);
2598 anim.start();
2599 }
2600 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08002601 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07002602 // We post the animation slightly delayed to prevent slowdowns
2603 // when we are loading right after we return to launcher.
2604 mWorkspace.postDelayed(new Runnable() {
2605 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00002606 if (mWorkspace != null) {
2607 mWorkspace.snapToPage(newScreenIndex);
2608 mWorkspace.postDelayed(startBounceAnimRunnable,
2609 NEW_APPS_ANIMATION_DELAY);
2610 }
Winson Chung94d67682013-09-25 16:29:40 -07002611 }
2612 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07002613 } else {
2614 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07002615 }
2616 }
Winson Chung64359a52013-07-08 17:17:08 -07002617 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002618 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002619 }
2620
Joe Onorato9c1289c2009-08-17 11:03:03 -04002621 /**
2622 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002623 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07002624 public void bindAppWidget(LauncherAppWidgetInfo item) {
2625 View view = inflateAppWidget(item);
2626 if (view != null) {
2627 mWorkspace.addInScreen(view, item);
2628 mWorkspace.requestLayout();
2629 }
2630 }
2631
2632 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002633 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302634 PendingAppWidgetHostView view =
2635 new PendingAppWidgetHostView(this, item, mIconCache, true);
2636 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002637 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002638 }
2639
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002640 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002641
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002642 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08002643
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002644 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2645 // If the provider is not ready, bind as a pending widget.
2646 appWidgetInfo = null;
2647 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2648 // The widget id is not valid. Try to find the widget based on the provider info.
2649 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
2650 } else {
2651 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
2652 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002653
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002654 // If the provider is ready, but the width is not yet restored, try to restore it.
2655 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
2656 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07002657 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002658 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
2659 + " belongs to component " + item.providerName
2660 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002661 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002662 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002663 }
Sunny Goyalff572272014-07-23 13:58:07 -07002664
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002665 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002666 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07002667 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2668 // Id has not been allocated yet. Allocate a new id.
2669 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
2670 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07002671
Sunny Goyald478c832016-04-01 12:04:16 -07002672 // Also try to bind the widget. If the bind fails, the user will be shown
2673 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08002674 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07002675 pendingInfo.spanX = item.spanX;
2676 pendingInfo.spanY = item.spanY;
2677 pendingInfo.minSpanX = item.minSpanX;
2678 pendingInfo.minSpanY = item.minSpanY;
2679 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07002680
2681 boolean isDirectConfig =
2682 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2683 if (isDirectConfig && item.bindOptions != null) {
2684 Bundle newOptions = item.bindOptions.getExtras();
2685 if (options != null) {
2686 newOptions.putAll(options);
2687 }
2688 options = newOptions;
2689 }
Sunny Goyald478c832016-04-01 12:04:16 -07002690 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2691 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002692
Sunny Goyal86df1382016-08-10 15:03:22 -07002693 // We tried to bind once. If we were not able to bind, we would need to
2694 // go through the permission dialog, which means we cannot skip the config
2695 // activity.
2696 item.bindOptions = null;
2697 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2698
Sunny Goyald478c832016-04-01 12:04:16 -07002699 // Bind succeeded
2700 if (success) {
2701 // If the widget has a configure activity, it is still needs to set it up,
2702 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002703 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002704 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2705 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002706 }
Sunny Goyald478c832016-04-01 12:04:16 -07002707
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002708 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002709 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002710 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002711 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002712 // The widget was marked as UI not ready, but there is no configure activity to
2713 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002714 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002715 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002716 }
Sunny Goyalff572272014-07-23 13:58:07 -07002717 }
2718
Sunny Goyald5462aa2016-11-22 16:52:39 +05302719 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002720 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002721 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002722 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002723 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07002724 deleteWidgetInfo(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002725 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002726 }
2727
Sunny Goyal233ee962015-08-03 13:05:01 -07002728 item.minSpanX = appWidgetInfo.minSpanX;
2729 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302730 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002731 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302732 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002733 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302734 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002735
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002736 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002737 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002738 }
2739
Sunny Goyalff572272014-07-23 13:58:07 -07002740 /**
2741 * Restores a pending widget.
2742 *
2743 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002744 */
Sunny Goyald478c832016-04-01 12:04:16 -07002745 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002746 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2747 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2748 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002749 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002750 }
2751
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002752 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002753 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002754 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2755 info.pendingItemInfo = null;
2756 }
Sunny Goyalff572272014-07-23 13:58:07 -07002757
Sunny Goyalba47faa2017-10-04 16:50:57 -07002758 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
2759 view.reinflate();
2760 }
2761
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002762 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002763 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002764 }
2765
Adam Cohen1462de32012-07-24 22:34:36 -07002766 public void onPageBoundSynchronously(int page) {
2767 mSynchronouslyBoundPages.add(page);
2768 }
2769
Sunny Goyal527c7d32015-08-28 15:19:36 -07002770 @Override
2771 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2772 if (mPendingExecutor != null) {
2773 mPendingExecutor.markCompleted();
2774 }
2775 mPendingExecutor = executor;
2776 executor.attachTo(this);
2777 }
2778
2779 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2780 if (mPendingExecutor == executor) {
2781 mPendingExecutor = null;
2782 }
2783 }
2784
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002785 @Override
2786 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
2787 Runnable r = new Runnable() {
2788 public void run() {
2789 finishFirstPageBind(executor);
2790 }
2791 };
2792 if (waitUntilResume(r)) {
2793 return;
2794 }
2795
2796 Runnable onComplete = new Runnable() {
2797 @Override
2798 public void run() {
2799 if (executor != null) {
2800 executor.onLoadAnimationCompleted();
2801 }
2802 }
2803 };
2804 if (mDragLayer.getAlpha() < 1) {
2805 mDragLayer.animate().alpha(1).withEndAction(onComplete).start();
2806 } else {
2807 onComplete.run();
2808 }
2809 }
2810
Joe Onorato9c1289c2009-08-17 11:03:03 -04002811 /**
2812 * Callback saying that there aren't any more items to bind.
2813 *
2814 * Implementation of the method from LauncherModel.Callbacks.
2815 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002816 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07002817 Runnable r = new Runnable() {
2818 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002819 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07002820 }
2821 };
2822 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002823 return;
2824 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002825 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002826 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002827
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002828 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002829
Sunny Goyal2100c782016-08-22 16:00:03 -07002830 if (mPendingActivityResult != null) {
2831 handleActivityResult(mPendingActivityResult.requestCode,
2832 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2833 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002834 }
2835
Sunny Goyala474a9b2017-05-04 16:47:11 -07002836 InstallShortcutReceiver.disableAndFlushInstallQueue(
2837 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002838
Tony Wickham010d2552017-01-20 08:15:28 -08002839 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002840 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002841 }
2842
Winson Chunga2413752012-04-03 14:22:34 -07002843 private boolean canRunNewAppsAnimation() {
2844 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002845 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002846 }
2847
Winson Chungc9168342013-06-26 14:54:55 -07002848 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002849 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07002850 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
2851 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002852 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002853 return bounceAnim;
2854 }
2855
Adam Cohen27772732013-11-11 14:00:56 +00002856 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07002857 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00002858 }
2859
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002860 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002861 * Add the icons for all apps.
2862 *
2863 * Implementation of the method from LauncherModel.Callbacks.
2864 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002865 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002866 Runnable r = new RunnableWithId(RUNNABLE_ID_BIND_APPS) {
2867 public void run() {
2868 bindAllApplications(apps);
2869 }
2870 };
2871 if (waitUntilResume(r)) {
Winson Chungbb785c62015-05-05 10:05:08 -07002872 return;
2873 }
2874
Winson Chungb745afb2015-03-02 11:51:23 -08002875 if (mAppsView != null) {
Tony2917a8b2017-07-31 23:29:42 -07002876 Executor pendingExecutor = getPendingExecutor();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002877 if (pendingExecutor != null && !isInState(ALL_APPS)) {
Tony2917a8b2017-07-31 23:29:42 -07002878 // Wait until the fade in animation has finished before setting all apps list.
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002879 pendingExecutor.execute(r);
Tony2917a8b2017-07-31 23:29:42 -07002880 return;
2881 }
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002882
Winson Chungb745afb2015-03-02 11:51:23 -08002883 mAppsView.setApps(apps);
2884 }
Adam Cohen9211d422014-10-07 18:14:53 -07002885 if (mLauncherCallbacks != null) {
2886 mLauncherCallbacks.bindAllApplications(apps);
2887 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002888 }
2889
2890 /**
Tony2917a8b2017-07-31 23:29:42 -07002891 * Returns an Executor that will run after the launcher is first drawn (including after the
2892 * initial fade in animation). Returns null if the first draw has already occurred.
2893 */
2894 public @Nullable Executor getPendingExecutor() {
2895 return mPendingExecutor != null && mPendingExecutor.canQueue() ? mPendingExecutor : null;
2896 }
2897
2898 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002899 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
2900 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2901 */
2902 @Override
2903 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002904 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002905 }
2906
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002907 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002908 * A package was updated.
2909 *
2910 * Implementation of the method from LauncherModel.Callbacks.
2911 */
Sunny Goyalb23980c2017-08-17 07:45:25 -07002912 public void bindAppsAddedOrUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07002913 Runnable r = new Runnable() {
2914 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002915 bindAppsAddedOrUpdated(apps);
Winson Chungd64d1762013-08-20 14:37:16 -07002916 }
2917 };
2918 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002919 return;
2920 }
2921
Winson Chungb745afb2015-03-02 11:51:23 -08002922 if (mAppsView != null) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002923 mAppsView.addOrUpdateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07002924 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002925 }
2926
Sunny Goyal4390ace2014-10-13 11:33:11 -07002927 @Override
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002928 public void bindPromiseAppProgressUpdated(final PromiseAppInfo app) {
2929 Runnable r = new Runnable() {
2930 public void run() {
2931 bindPromiseAppProgressUpdated(app);
2932 }
2933 };
2934 if (waitUntilResume(r)) {
2935 return;
2936 }
2937
2938 if (mAppsView != null) {
2939 mAppsView.updatePromiseAppProgress(app);
2940 }
2941 }
2942
2943 @Override
Sunny Goyal4390ace2014-10-13 11:33:11 -07002944 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
2945 Runnable r = new Runnable() {
2946 public void run() {
2947 bindWidgetsRestored(widgets);
2948 }
2949 };
2950 if (waitUntilResume(r)) {
2951 return;
2952 }
2953 mWorkspace.widgetsRestored(widgets);
2954 }
2955
Joe Onorato9c1289c2009-08-17 11:03:03 -04002956 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002957 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002958 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002959 *
2960 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002961 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002962 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002963 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002964 Runnable r = new Runnable() {
2965 public void run() {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002966 bindShortcutsChanged(updated, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002967 }
2968 };
2969 if (waitUntilResume(r)) {
2970 return;
2971 }
2972
Sunny Goyal4390ace2014-10-13 11:33:11 -07002973 if (!updated.isEmpty()) {
2974 mWorkspace.updateShortcuts(updated);
2975 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002976 }
2977
2978 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002979 * Update the state of a package, typically related to install state.
2980 *
2981 * Implementation of the method from LauncherModel.Callbacks.
2982 */
Sunny Goyale755d462014-07-22 13:48:29 -07002983 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07002984 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
2985 Runnable r = new Runnable() {
2986 public void run() {
2987 bindRestoreItemsChange(updates);
2988 }
2989 };
2990 if (waitUntilResume(r)) {
2991 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002992 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002993
Sunny Goyal756adbc2015-04-16 15:20:51 -07002994 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07002995 }
2996
2997 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002998 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07002999 * in addition to specific applications to remove, the reason being that
3000 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003001 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07003002 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003003 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07003004 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003005 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Winson Chungd64d1762013-08-20 14:37:16 -07003006 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003007 public void run() {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003008 bindWorkspaceComponentsRemoved(matcher);
Winson Chung83892cc2013-05-01 16:53:33 -07003009 }
Winson Chungd64d1762013-08-20 14:37:16 -07003010 };
3011 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003012 return;
3013 }
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003014 mWorkspace.removeItemsByMatcher(matcher);
3015 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003016 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003017
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003018 @Override
3019 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
3020 Runnable r = new Runnable() {
3021 public void run() {
3022 bindAppInfosRemoved(appInfos);
3023 }
3024 };
3025 if (waitUntilResume(r)) {
3026 return;
Joe Onorato36115782010-06-17 13:28:48 -04003027 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003028
Winson Chungdf95eb12013-10-16 14:57:07 -07003029 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08003030 if (mAppsView != null) {
3031 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07003032 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003033 }
Joe Onoratobe386092009-11-17 17:32:16 -08003034
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003035 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07003036 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
3037 mPopupDataProvider.setAllWidgets(allWidgets);
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003038 Runnable r = new RunnableWithId(RUNNABLE_ID_BIND_WIDGETS) {
3039 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07003040 public void run() {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003041 bindAllWidgets(allWidgets);
Winson Chung83892cc2013-05-01 16:53:33 -07003042 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003043 };
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003044 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003045 return;
3046 }
3047
Tony Wickham26b17462017-03-20 17:12:24 -07003048 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
3049 if (topView != null) {
3050 topView.onWidgetsBound();
3051 }
3052 }
3053
Tony Wickham86222d22017-03-29 15:30:43 -07003054 /**
3055 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
3056 * refreshes the widgets and shortcuts associated with the given package/user
3057 */
3058 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07003059 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003060 }
3061
Sunny Goyalc99cb172017-10-19 16:15:09 -07003062 public boolean isRotationEnabled () {
3063 return mRotationEnabled;
Winson Chung400438b2011-01-16 17:53:48 -08003064 }
3065
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003066 private boolean shouldShowDiscoveryBounce() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07003067 return isInState(NORMAL)
Sunny Goyalbe93f262017-10-20 17:05:27 -07003068 && !mSharedPrefs.getBoolean(AllAppsState.APPS_VIEW_SHOWN, false)
Sunny Goyalf9403d92017-10-18 10:55:56 -07003069 && !UserManagerCompat.getInstance(this).isDemoUser();
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003070 }
3071
Winson Chung80baf5a2010-08-09 16:03:15 -07003072 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003073 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08003074 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003075 @Override
3076 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3077 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003078
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003079 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
3080 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07003081 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003082 writer.println(prefix + " Homescreen " + i);
3083
3084 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
3085 for (int j = 0; j < layout.getChildCount(); j++) {
3086 Object tag = layout.getChildAt(j).getTag();
3087 if (tag != null) {
3088 writer.println(prefix + " " + tag.toString());
3089 }
3090 }
3091 }
3092
3093 writer.println(prefix + " Hotseat");
3094 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07003095 for (int j = 0; j < layout.getChildCount(); j++) {
3096 Object tag = layout.getChildAt(j).getTag();
3097 if (tag != null) {
3098 writer.println(prefix + " " + tag.toString());
3099 }
Adam Cohen4caf2982013-08-20 18:54:31 -07003100 }
Sunny Goyala1365452015-10-01 15:46:24 -07003101
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003102 try {
3103 FileLog.flushAll(writer);
3104 } catch (Exception e) {
3105 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07003106 }
3107 }
3108
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003109 writer.println(prefix + "Misc:");
3110 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
3111 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
3112 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
3113
3114 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003115
Adam Cohen9211d422014-10-07 18:14:53 -07003116 if (mLauncherCallbacks != null) {
3117 mLauncherCallbacks.dump(prefix, fd, writer, args);
3118 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003119 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003120
Sunny Goyal66b24572016-09-21 15:57:55 -07003121 @Override
3122 @TargetApi(Build.VERSION_CODES.N)
3123 public void onProvideKeyboardShortcuts(
3124 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
3125
3126 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07003127 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07003128 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
3129 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
3130 }
3131 View currentFocus = getCurrentFocus();
3132 if (new CustomActionsPopup(this, currentFocus).canShow()) {
3133 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
3134 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
3135 }
Tony18c4aa42017-05-10 21:23:57 -05003136 if (currentFocus.getTag() instanceof ItemInfo
3137 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
Sunny Goyal66b24572016-09-21 15:57:55 -07003138 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
3139 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
3140 }
3141 if (!shortcutInfos.isEmpty()) {
3142 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
3143 }
3144
3145 super.onProvideKeyboardShortcuts(data, menu, deviceId);
3146 }
3147
3148 @Override
3149 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
3150 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
3151 switch (keyCode) {
3152 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07003153 if (isInState(NORMAL)) {
3154 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07003155 return true;
3156 }
3157 break;
3158 case KeyEvent.KEYCODE_S: {
3159 View focusedView = getCurrentFocus();
3160 if (focusedView instanceof BubbleTextView
3161 && focusedView.getTag() instanceof ItemInfo
3162 && mAccessibilityDelegate.performAction(focusedView,
3163 (ItemInfo) focusedView.getTag(),
3164 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal10a1bd02017-10-09 14:56:21 -07003165 BaseActionPopup.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07003166 return true;
3167 }
3168 break;
3169 }
3170 case KeyEvent.KEYCODE_O:
3171 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
3172 return true;
3173 }
3174 break;
3175 }
3176 }
3177 return super.onKeyShortcut(keyCode, event);
3178 }
3179
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07003180 public static Launcher getLauncher(Context context) {
3181 if (context instanceof Launcher) {
3182 return (Launcher) context;
3183 }
3184 return ((Launcher) ((ContextWrapper) context).getBaseContext());
3185 }
3186
Sunny Goyal5a81c382017-03-20 15:08:06 -07003187 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener {
Sunny Goyal745bad92016-05-02 10:54:12 -07003188
3189 @Override
3190 public void onSharedPreferenceChanged(
3191 SharedPreferences sharedPreferences, String key) {
3192 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
Sunny Goyale13d4642017-05-22 15:58:42 -07003193 // Recreate the activity so that it initializes the rotation preference again.
3194 recreate();
Sunny Goyal745bad92016-05-02 10:54:12 -07003195 }
3196 }
Sunny Goyal745bad92016-05-02 10:54:12 -07003197 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07003198
3199 /**
3200 * Callback for listening for onResume
3201 */
3202 public interface OnResumeCallback {
3203
3204 void onLauncherResume();
3205 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003206}